Merge "mw.language.convertPlural: Check if matching form exists"
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index 1195d5f..47efa70 100644 (file)
@@ -42,24 +42,81 @@ class FileRepo {
 
        /** @var FileBackend */
        protected $backend;
+
        /** @var Array Map of zones to config */
        protected $zones = array();
 
-       var $thumbScriptUrl, $transformVia404;
-       var $descBaseUrl, $scriptDirUrl, $scriptExtension, $articleUrl;
-       var $fetchDescription, $initialCapital;
-       var $pathDisclosureProtection = 'simple'; // 'paranoid'
-       var $descriptionCacheExpiry, $url, $thumbUrl;
-       var $hashLevels, $deletedHashLevels;
+       /** @var string URL of thumb.php  */
+       protected $thumbScriptUrl;
+
+       /** @var bool Whether to skip media file transformation on parse and rely
+        *    on a 404 handler instead. */
+       protected $transformVia404;
+
+       /** @var string URL of image description pages, e.g.
+        *    http://en.wikipedia.org/wiki/File:
+        */
+       protected $descBaseUrl;
+
+       /** @var string URL of the MediaWiki installation, equivalent to
+        *    $wgScriptPath, e.g. https://en.wikipedia.org/w
+        */
+       protected $scriptDirUrl;
+
+       /** @var string Script extension of the MediaWiki installation, equivalent
+        *    to $wgScriptExtension, e.g. .php5 defaults to .php */
+       protected $scriptExtension;
+
+       /** @var string Equivalent to $wgArticlePath, e.g. http://en.wikipedia.org/wiki/$1 */
+       protected $articleUrl;
+
+       /** @var bool Whether to fetch commons image description pages and display
+        *    them on the local wiki */
+       public $fetchDescription;
+
+       /** @var bool 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
+        *    when the local $wgCapitalLinks and initialCapital are mismatched.
+        */
+       protected $initialCapital;
+
+       /** @var string May be 'paranoid' to remove all parameters from error
+        *    messages, 'none' to leave the paths in unchanged, or 'simple' to
+        *    replace paths with placeholders. Default for LocalRepo is
+        *    'simple'.
+        */
+       protected $pathDisclosureProtection = 'simple';
+
+       /** @var int */
+       public $descriptionCacheExpiry;
+
+       /** @var bool Public zone URL. */
+       protected $url;
+
+       /** @var string The base thumbnail URL. Defaults to "<url>/thumb". */
+       protected $thumbUrl;
+
+       /** @var int The number of directory levels for hash-based division of files */
+       protected $hashLevels;
+
+       /** @var int The number of directory levels for hash-based division of deleted files */
+       protected $deletedHashLevels;
+
+       /** @var int File names over this size will use the short form of thumbnail
+        *    names. Short thumbnail names only have the width, parameters, and the
+        *    extension.
+        */
        protected $abbrvThreshold;
 
        /**
         * Factory functions for creating new files
         * Override these in the base class
         */
-       var $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
-       var $oldFileFactory = false;
-       var $fileFactoryKey = false, $oldFileFactoryKey = false;
+       protected $fileFactory = array( 'UnregisteredLocalFile', 'newFromTitle' );
+       protected $oldFileFactory = false;
+       protected $fileFactoryKey = false;
+       protected $oldFileFactoryKey = false;
 
        /**
         * @param $info array|null
@@ -72,7 +129,8 @@ class FileRepo {
                        || !array_key_exists( 'name', $info )
                        || !array_key_exists( 'backend', $info )
                ) {
-                       throw new MWException( __CLASS__ . " requires an array of options having both 'name' and 'backend' keys.\n" );
+                       throw new MWException( __CLASS__ .
+                               " requires an array of options having both 'name' and 'backend' keys.\n" );
                }
 
                // Required settings
@@ -167,6 +225,7 @@ class FileRepo {
                                throw new MWException( "No '$zone' zone defined in the {$this->name} repo." );
                        }
                }
+
                return $status;
        }
 
@@ -193,6 +252,7 @@ class FileRepo {
                if ( $suffix !== false ) {
                        $path .= '/' . rawurlencode( $suffix );
                }
+
                return $path;
        }
 
@@ -201,14 +261,17 @@ class FileRepo {
         *
         * @param string $zone One of: public, deleted, temp, thumb
         * @param string|null $ext Optional file extension
-        * @return String or false
+        * @return string|bool
         */
        public function getZoneUrl( $zone, $ext = null ) {
-               if ( in_array( $zone, array( 'public', 'temp', 'thumb', 'transcoded' ) ) ) { // standard public zones
+               if ( in_array( $zone, array( 'public', 'temp', 'thumb', 'transcoded' ) ) ) {
+                       // standard public zones
                        if ( $ext !== null && isset( $this->zones[$zone]['urlsByExt'][$ext] ) ) {
-                               return $this->zones[$zone]['urlsByExt'][$ext]; // custom URL for extension/zone
+                               // custom URL for extension/zone
+                               return $this->zones[$zone]['urlsByExt'][$ext];
                        } elseif ( isset( $this->zones[$zone]['url'] ) ) {
-                               return $this->zones[$zone]['url']; // custom URL for zone
+                               // custom URL for zone
+                               return $this->zones[$zone]['url'];
                        }
                }
                switch ( $zone ) {
@@ -242,10 +305,11 @@ class FileRepo {
         */
        public function getZoneHandlerUrl( $zone ) {
                if ( isset( $this->zones[$zone]['handlerUrl'] )
-                       && in_array( $zone, array( 'public', 'temp', 'thumb', 'transcoded' ) ) )
-               {
+                       && in_array( $zone, array( 'public', 'temp', 'thumb', 'transcoded' ) )
+               {
                        return $this->zones[$zone]['handlerUrl'];
                }
+
                return false;
        }
 
@@ -273,6 +337,7 @@ class FileRepo {
                if ( !$base ) {
                        throw new MWException( __METHOD__ . ": invalid zone: $zone" );
                }
+
                return $base . '/' . rawurldecode( $rel );
        }
 
@@ -286,6 +351,7 @@ class FileRepo {
                if ( !isset( $this->zones[$zone] ) ) {
                        return array( null, null ); // bogus
                }
+
                return array( $this->zones[$zone]['container'], $this->zones[$zone]['directory'] );
        }
 
@@ -304,6 +370,7 @@ class FileRepo {
                if ( $base != '' ) { // may not be set
                        $base = "/{$base}";
                }
+
                return "mwstore://$backendName/{$container}{$base}";
        }
 
@@ -390,9 +457,11 @@ class FileRepo {
                        }
                        if ( $img->exists() ) {
                                $img->redirectedFrom( $title->getDBkey() );
+
                                return $img;
                        }
                }
+
                return false;
        }
 
@@ -423,6 +492,7 @@ class FileRepo {
                                $result[$file->getTitle()->getDBkey()] = $file;
                        }
                }
+
                return $result;
        }
 
@@ -457,6 +527,7 @@ class FileRepo {
                                }
                        }
                }
+
                return false;
        }
 
@@ -487,6 +558,7 @@ class FileRepo {
                                $result[$hash] = $files;
                        }
                }
+
                return $result;
        }
 
@@ -531,7 +603,7 @@ class FileRepo {
        }
 
        /**
-        * Get the name of an image from its title object
+        * Get the name of a file from its title object
         *
         * @param $title Title
         * @return String
@@ -546,6 +618,7 @@ class FileRepo {
                } else {
                        $name = $title->getDBkey();
                }
+
                return $name;
        }
 
@@ -596,6 +669,7 @@ class FileRepo {
                        for ( $i = 1; $i <= $levels; $i++ ) {
                                $path .= substr( $hash, 0, $i ) . '/';
                        }
+
                        return $path;
                }
        }
@@ -628,8 +702,10 @@ class FileRepo {
        public function makeUrl( $query = '', $entry = 'index' ) {
                if ( isset( $this->scriptDirUrl ) ) {
                        $ext = isset( $this->scriptExtension ) ? $this->scriptExtension : '.php';
+
                        return wfAppendQuery( "{$this->scriptDirUrl}/{$entry}{$ext}", $query );
                }
+
                return false;
        }
 
@@ -648,7 +724,7 @@ class FileRepo {
        public function getDescriptionUrl( $name ) {
                $encName = wfUrlencode( $name );
                if ( !is_null( $this->descBaseUrl ) ) {
-                       # "http://example.com/wiki/Image:"
+                       # "http://example.com/wiki/File:"
                        return $this->descBaseUrl . $encName;
                }
                if ( !is_null( $this->articleUrl ) ) {
@@ -667,6 +743,7 @@ class FileRepo {
                        # and just sort of hope index.php is right. ;)
                        return $this->makeUrl( "title=Image:$encName" );
                }
+
                return false;
        }
 
@@ -710,6 +787,7 @@ class FileRepo {
                        return $this->makeUrl( 'title=MediaWiki:Filepage.css&' .
                                wfArrayToCgi( Skin::getDynamicStylesheetQuery() ) );
                }
+
                return false;
        }
 
@@ -988,6 +1066,7 @@ class FileRepo {
                $temp = $this->getVirtualUrl( 'temp' );
                if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
                        wfDebug( __METHOD__ . ": Invalid temp virtual URL\n" );
+
                        return false;
                }
 
@@ -1238,6 +1317,7 @@ class FileRepo {
         */
        public function fileExists( $file ) {
                $result = $this->fileExistsBatch( array( $file ) );
+
                return $result[0];
        }
 
@@ -1253,6 +1333,7 @@ class FileRepo {
                        $file = $this->resolveToStoragePath( $file );
                        $result[$key] = $this->backend->fileExists( array( 'src' => $file ) );
                }
+
                return $result;
        }
 
@@ -1367,6 +1448,7 @@ class FileRepo {
                for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
                        $path .= $key[$i] . '/';
                }
+
                return $path;
        }
 
@@ -1382,6 +1464,7 @@ class FileRepo {
                if ( $this->isVirtualUrl( $path ) ) {
                        return $this->resolveVirtualUrl( $path );
                }
+
                return $path;
        }
 
@@ -1394,6 +1477,7 @@ class FileRepo {
         */
        public function getLocalCopy( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
+
                return $this->backend->getLocalCopy( array( 'src' => $path ) );
        }
 
@@ -1407,6 +1491,7 @@ class FileRepo {
         */
        public function getLocalReference( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
+
                return $this->backend->getLocalReference( array( 'src' => $path ) );
        }
 
@@ -1419,6 +1504,7 @@ class FileRepo {
         */
        public function getFileProps( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
+
                return $this->backend->getFileProps( array( 'src' => $path ) );
        }
 
@@ -1430,6 +1516,7 @@ class FileRepo {
         */
        public function getFileTimestamp( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
+
                return $this->backend->getFileTimestamp( array( 'src' => $path ) );
        }
 
@@ -1441,6 +1528,7 @@ class FileRepo {
         */
        public function getFileSize( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
+
                return $this->backend->getFileSize( array( 'src' => $path ) );
        }
 
@@ -1452,6 +1540,7 @@ class FileRepo {
         */
        public function getFileSha1( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
+
                return $this->backend->getFileSha1Base36( array( 'src' => $path ) );
        }
 
@@ -1465,6 +1554,7 @@ class FileRepo {
        public function streamFile( $virtualUrl, $headers = array() ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
                $params = array( 'src' => $path, 'headers' => $headers );
+
                return $this->backend->streamFile( $params )->isOK();
        }
 
@@ -1516,6 +1606,7 @@ class FileRepo {
                if ( strval( $filename ) == '' ) {
                        return false;
                }
+
                return FileBackend::isPathTraversalFree( $filename );
        }
 
@@ -1564,6 +1655,7 @@ class FileRepo {
        public function newFatal( $message /*, parameters...*/ ) {
                $params = func_get_args();
                array_unshift( $params, $this );
+
                return call_user_func_array( array( 'FileRepoStatus', 'newFatal' ), $params );
        }
 
@@ -1596,7 +1688,8 @@ class FileRepo {
         * STUB
         * @param $title Title of image
         */
-       public function invalidateImageRedirect( Title $title ) {}
+       public function invalidateImageRedirect( Title $title ) {
+       }
 
        /**
         * Get the human-readable name of the repo
@@ -1608,6 +1701,7 @@ class FileRepo {
                if ( $this->isLocal() ) {
                        return null;
                }
+
                // 'shared-repo-name-wikimediacommons' is used when $wgUseInstantCommons = true
                return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
        }
@@ -1624,6 +1718,7 @@ class FileRepo {
                        $ext = FileBackend::extensionFromPath( $name );
                        $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
                }
+
                return $name;
        }
 
@@ -1658,6 +1753,7 @@ class FileRepo {
        public function getLocalCacheKey( /*...*/ ) {
                $args = func_get_args();
                array_unshift( $args, 'filerepo', $this->getName() );
+
                return call_user_func_array( 'wfMemcKey', $args );
        }
 
@@ -1680,13 +1776,13 @@ class FileRepo {
                                ),
                                'thumb' => array(
                                        'container' => $this->zones['thumb']['container'],
-                                       'directory' => ( $this->zones['thumb']['directory'] == '' )
+                                       'directory' => $this->zones['thumb']['directory'] == ''
                                                ? 'temp'
                                                : $this->zones['thumb']['directory'] . '/temp'
                                ),
                                'transcoded' => array(
                                        'container' => $this->zones['transcoded']['container'],
-                                       'directory' => ( $this->zones['transcoded']['directory'] == '' )
+                                       'directory' => $this->zones['transcoded']['directory'] == ''
                                                ? 'temp'
                                                : $this->zones['transcoded']['directory'] . '/temp'
                                )
@@ -1715,8 +1811,8 @@ class FileRepo {
         * @return void
         * @throws MWException
         */
-       protected function assertWritableRepo() {}
-
+       protected function assertWritableRepo() {
+       }
 
        /**
         * Return information about the repository.
@@ -1725,12 +1821,24 @@ class FileRepo {
         * @since 1.22
         */
        public function getInfo() {
-               return array(
+               $ret = array(
                        'name' => $this->getName(),
                        'displayname' => $this->getDisplayName(),
-                       'rootUrl' => $this->getRootUrl(),
+                       'rootUrl' => $this->getZoneUrl( 'public' ),
                        'local' => $this->isLocal(),
                );
+
+               $optionalSettings = array(
+                       'url', 'thumbUrl', 'initialCapital', 'descBaseUrl', 'scriptDirUrl', 'articleUrl',
+                       'fetchDescription', 'descriptionCacheExpiry', 'scriptExtension'
+               );
+               foreach ( $optionalSettings as $k ) {
+                       if ( isset( $this->$k ) ) {
+                               $ret[$k] = $this->$k;
+                       }
+               }
+
+               return $ret;
        }
 }