PmWiki recipe that adds versions (modification timestamps) to attachment URLs (for proper browser cache invalidation).
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

versioned-assets.php 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /** \versioned_assets.php
  3. * \Copyright 2017-2021 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'] = '2021-12-12';
  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. }