PmWiki recipe that adds versions (modification timestamps) to attachment URLs (for proper browser cache invalidation).
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

versioned-assets.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /** \versioned_assets.php
  3. * \Copyright (c) 2017 Said Achmiz
  4. * \Licensed under the MIT License
  5. * \brief Adds versions (modification timestamps) to attachment URLs, so that browser
  6. * caches invalidate properly when attachments are updated.
  7. */
  8. $RecipeInfo['VersionedAssets']['Version'] = '2017-12-13';
  9. global $LinkFunctions;
  10. $LinkFunctions['Attach:'] = 'LinkUploadVersioned';
  11. SDV($VersionedAssetsReattachFileExtension, true);
  12. function LinkUploadVersioned($pagename, $imap, $path, $alt, $txt, $fmt=NULL) {
  13. global $FmtV, $UploadFileFmt, $LinkUploadCreateFmt,
  14. $UploadUrlFmt, $UploadPrefixFmt, $EnableDirectDownload;
  15. if (preg_match('!^(.*)/([^/]+)$!', $path, $match)) {
  16. $pagename = MakePageName($pagename, $match[1]);
  17. $path = $match[2];
  18. }
  19. $upname = MakeUploadName($pagename, $path);
  20. $encname = rawurlencode($upname);
  21. $filepath = FmtPageName("$UploadFileFmt/$upname", $pagename);
  22. $FmtV['$LinkUpload'] =
  23. FmtPageName("\$PageUrl?action=upload&amp;upname=$encname", $pagename);
  24. $FmtV['$LinkText'] = $txt;
  25. if (!file_exists($filepath))
  26. return FmtPageName($LinkUploadCreateFmt, $pagename);
  27. $path = PUE(FmtPageName(IsEnabled($EnableDirectDownload, 1)
  28. ? "$UploadUrlFmt$UploadPrefixFmt/$encname"
  29. : "{\$PageUrl}?action=download&amp;upname=$encname",
  30. $pagename));
  31. ## Append the modification time to the URL as a GET parameter; this should be ignored
  32. ## by the web server, but is seen as part of the unique URL of the remote resource by
  33. ## the browser; when it changes (because the attachment has been modified), the
  34. ## browser will see that it doesn’t have a cached version of the resource under the
  35. ## new URL, and will retrieve the updated version.
  36. $versioned_path = $path . "?v=" . filemtime($filepath);
  37. global $VersionedAssetsReattachFileExtension;
  38. if ($VersionedAssetsReattachFileExtension == true) {
  39. ## Re-attach the file extension, so that LinkIcons and such things work properly.
  40. preg_match("/\\.[^\\.]+$/", $path, $matches);
  41. $versioned_path .= $matches[0];
  42. }
  43. return LinkIMap($pagename, $imap, $versioned_path, $alt, $txt, $fmt);
  44. }