Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/126210

Change subject: Added support for CDN-only thumbnail storage
......................................................................

Added support for CDN-only thumbnail storage

Change-Id: I10f772c678fa2c4b2fb99e9136e2a0cfd3d249a9
---
M includes/DefaultSettings.php
M includes/filerepo/FileRepo.php
M includes/filerepo/file/File.php
M includes/filerepo/file/LocalFile.php
4 files changed, 63 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/10/126210/1

diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 663098f..79fbf7d 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -417,6 +417,10 @@
  *   - thumbScriptUrl   The URL for thumb.php (optional, not recommended)
  *   - transformVia404  Whether to skip media file transformation on parse and 
rely on a 404
  *                      handler instead.
+ *   - cdnThumbnails    Do not store thumbnails in the backend. This assumes 
that any CDN
+ *                      cache collates all thumbnails for a file under the 
directory for
+ *                      thumbnails for that file. PURGE requests are sent to 
that directory.
+ *                      This only has an effect if "transformVia404" is also 
enabled.
  *   - initialCapital   Equivalent to $wgCapitalLinks (or 
$wgCapitalLinkOverrides[NS_FILE],
  *                      determines whether filenames implicitly start with a 
capital letter.
  *                      The current implementation may give incorrect 
description page links
diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php
index 888af37..e702567 100644
--- a/includes/filerepo/FileRepo.php
+++ b/includes/filerepo/FileRepo.php
@@ -111,6 +111,9 @@
         */
        protected $abbrvThreshold;
 
+       /** @var bool Whether thumbnails are stored CDN only and collated by 
source file name */
+       protected $cdnThumbnails = false;
+
        /** @var string The URL of the repo's favicon, if any */
        protected $favicon;
 
@@ -150,7 +153,7 @@
                $optionalSettings = array(
                        'descBaseUrl', 'scriptDirUrl', 'articleUrl', 
'fetchDescription',
                        'thumbScriptUrl', 'pathDisclosureProtection', 
'descriptionCacheExpiry',
-                       'scriptExtension', 'favicon'
+                       'scriptExtension', 'favicon', 'cdnThumbnails'
                );
                foreach ( $optionalSettings as $var ) {
                        if ( isset( $info[$var] ) ) {
@@ -628,6 +631,18 @@
        }
 
        /**
+        * Returns true if the repository relies only on CDN for thumbnail 
storage
+        *
+        * CDN-only storage requires that a cache PURGE request to the parent
+        * directory of thumbnails for a file results in purging all such 
thumbnails
+        *
+        * @return bool
+        */
+       public function cdnThumbnails() {
+               return $this->cdnThumbnails;
+       }
+
+       /**
         * Get the name of a file from its title object
         *
         * @param Title $title
diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php
index 21ff65c..741a35d 100644
--- a/includes/filerepo/file/File.php
+++ b/includes/filerepo/file/File.php
@@ -993,7 +993,9 @@
                                $this->migrateThumbFile( $thumbName );
                                // Check if an up-to-date thumbnail already 
exists...
                                wfDebug( __METHOD__ . ": Doing stat for 
$thumbPath\n" );
-                               if ( !( $flags & self::RENDER_FORCE ) && 
$this->repo->fileExists( $thumbPath ) ) {
+                               if ( !( $flags & self::RENDER_FORCE ) && 
!$this->repo->cdnThumbnails()
+                                       && $this->repo->fileExists( $thumbPath )
+                               ) {
                                        $timestamp = 
$this->repo->getFileTimestamp( $thumbPath );
                                        if ( $timestamp !== false && $timestamp 
>= $wgThumbnailEpoch ) {
                                                // XXX: Pass in the storage 
path even though we are not rendering anything
@@ -1010,7 +1012,7 @@
 
                        // If the backend is ready-only, don't keep generating 
thumbnails
                        // only to return transformation errors, just return 
the error now.
-                       if ( $this->repo->getReadOnlyReason() !== false ) {
+                       if ( !$this->repo->cdnThumbnails() && 
$this->repo->getReadOnlyReason() !== false ) {
                                $thumb = $this->transformErrorOutput( 
$thumbPath, $thumbUrl, $params, $flags );
                                break;
                        }
@@ -1039,13 +1041,16 @@
                                        $thumb = $handler->getTransform( $this, 
$tmpThumbPath, $thumbUrl, $params );
                                }
                        } elseif ( $this->repo && $thumb->hasFile() && 
!$thumb->fileIsSource() ) {
-                               // Copy the thumbnail from the file system into 
storage...
-                               $disposition = $this->getThumbDisposition( 
$thumbName );
-                               $status = $this->repo->quickImport( 
$tmpThumbPath, $thumbPath, $disposition );
-                               if ( $status->isOK() ) {
-                                       $thumb->setStoragePath( $thumbPath );
-                               } else {
-                                       $thumb = $this->transformErrorOutput( 
$thumbPath, $thumbUrl, $params, $flags );
+                               if ( !$this->repo->cdnThumbnails() ) {
+                                       // Copy the thumbnail from the file 
system into storage...
+                                       $disposition = 
$this->getThumbDisposition( $thumbName );
+                                       $status = $this->repo->quickImport( 
$tmpThumbPath, $thumbPath, $disposition );
+                                       if ( $status->isOK() ) {
+                                               $thumb->setStoragePath( 
$thumbPath );
+                                       } else {
+                                               $thumb = 
$this->transformErrorOutput(
+                                                       $thumbPath, $thumbUrl, 
$params, $flags );
+                                       }
                                }
                                // Give extensions a chance to do something 
with this thumbnail...
                                wfRunHooks( 'FileTransformed', array( $this, 
$thumb, $tmpThumbPath, $thumbPath ) );
diff --git a/includes/filerepo/file/LocalFile.php 
b/includes/filerepo/file/LocalFile.php
index eb55e82..a699824 100644
--- a/includes/filerepo/file/LocalFile.php
+++ b/includes/filerepo/file/LocalFile.php
@@ -901,22 +901,32 @@
                global $wgUseSquid;
                wfProfileIn( __METHOD__ );
 
-               // Get a list of old thumbnails and URLs
-               $files = $this->getThumbnails( $archiveName );
+               // Get a list of old thumbnails
+               if ( $this->repo->cdnThumbnails() ) {
+                       $files = array( $this->getArchiveThumbPath( 
$archiveName ) );
+               } else {
+                       $files = $this->getThumbnails( $archiveName );
+               }
 
                // Purge any custom thumbnail caches
                wfRunHooks( 'LocalFilePurgeThumbnails', array( $this, 
$archiveName ) );
 
+               // Purge from permanent storage
                $dir = array_shift( $files );
                $this->purgeThumbList( $dir, $files );
 
                // Purge the squid
                if ( $wgUseSquid ) {
-                       $urls = array();
-                       foreach ( $files as $file ) {
-                               $urls[] = $this->getArchiveThumbUrl( 
$archiveName, $file );
+                       if ( $this->repo->cdnThumbnails() ) {
+                               // Purge base URL to clear all thumbnails
+                               SquidUpdate::purge( array( 
$this->getArchiveThumbUrl( $archiveName ) ) );
+                       } else {
+                               $urls = array();
+                               foreach ( $files as $file ) {
+                                       $urls[] = $this->getArchiveThumbUrl( 
$archiveName, $file );
+                               }
+                               SquidUpdate::purge( $urls );
                        }
-                       SquidUpdate::purge( $urls );
                }
 
                wfProfileOut( __METHOD__ );
@@ -929,8 +939,13 @@
                global $wgUseSquid;
                wfProfileIn( __METHOD__ );
 
-               // Delete thumbnails
-               $files = $this->getThumbnails();
+               // Get a list of old thumbnails
+               if ( $this->repo->cdnThumbnails() ) {
+                       $files = array( $this->getThumbPath() );
+               } else {
+                       $files = $this->getThumbnails();
+               }
+
                // Always purge all files from squid regardless of handler 
filters
                $urls = array();
                if ( $wgUseSquid ) {
@@ -956,7 +971,12 @@
 
                // Purge the squid
                if ( $wgUseSquid ) {
-                       SquidUpdate::purge( $urls );
+                       if ( $this->repo->cdnThumbnails() ) {
+                               // Purge base URL to clear all thumbnails
+                               SquidUpdate::purge( array( $this->getThumbUrl() 
) );
+                       } else {
+                               SquidUpdate::purge( $urls );
+                       }
                }
 
                wfProfileOut( __METHOD__ );

-- 
To view, visit https://gerrit.wikimedia.org/r/126210
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10f772c678fa2c4b2fb99e9136e2a0cfd3d249a9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to