Improve WikiMap::getWikiReferenceFromWgConf()
[lhc/web/wiklou.git] / includes / media / DjVu.php
index 749ef23..9add138 100644 (file)
@@ -47,7 +47,7 @@ class DjVuHandler extends ImageHandler {
         * @param File $file
         * @return bool
         */
-       function mustRender( $file ) {
+       public function mustRender( $file ) {
                return true;
        }
 
@@ -64,18 +64,18 @@ class DjVuHandler extends ImageHandler {
         * @param File $file
         * @return bool
         */
-       function isMultiPage( $file ) {
+       public function isMultiPage( $file ) {
                return true;
        }
 
        /**
         * @return array
         */
-       function getParamMap() {
-               return array(
+       public function getParamMap() {
+               return [
                        'img_width' => 'width',
                        'img_page' => 'page',
-               );
+               ];
        }
 
        /**
@@ -83,13 +83,13 @@ class DjVuHandler extends ImageHandler {
         * @param mixed $value
         * @return bool
         */
-       function validateParam( $name, $value ) {
+       public function validateParam( $name, $value ) {
                if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) {
                        // Extra junk on the end of page, probably actually a caption
                        // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
                        return false;
                }
-               if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
+               if ( in_array( $name, [ 'width', 'height', 'page' ] ) ) {
                        if ( $value <= 0 ) {
                                return false;
                        } else {
@@ -104,7 +104,7 @@ class DjVuHandler extends ImageHandler {
         * @param array $params
         * @return bool|string
         */
-       function makeParamString( $params ) {
+       public function makeParamString( $params ) {
                $page = isset( $params['page'] ) ? $params['page'] : 1;
                if ( !isset( $params['width'] ) ) {
                        return false;
@@ -117,10 +117,10 @@ class DjVuHandler extends ImageHandler {
         * @param string $str
         * @return array|bool
         */
-       function parseParamString( $str ) {
+       public function parseParamString( $str ) {
                $m = false;
                if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
-                       return array( 'width' => $m[2], 'page' => $m[1] );
+                       return [ 'width' => $m[2], 'page' => $m[1] ];
                } else {
                        return false;
                }
@@ -131,10 +131,10 @@ class DjVuHandler extends ImageHandler {
         * @return array
         */
        function getScriptParams( $params ) {
-               return array(
+               return [
                        'width' => $params['width'],
                        'page' => $params['page'],
-               );
+               ];
        }
 
        /**
@@ -156,11 +156,11 @@ class DjVuHandler extends ImageHandler {
                $page = $params['page'];
 
                if ( $flags & self::TRANSFORM_LATER ) {
-                       $params = array(
+                       $params = [
                                'width' => $width,
                                'height' => $height,
                                'page' => $page
-                       );
+                       ];
 
                        return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
                }
@@ -179,11 +179,11 @@ class DjVuHandler extends ImageHandler {
                // Provide a way to pool count limit the number of downloaders.
                if ( $image->getSize() >= 1e7 ) { // 10MB
                        $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
-                               array(
+                               [
                                        'doWork' => function () use ( $image ) {
                                                return $image->getLocalRefPath();
                                        }
-                               )
+                               ]
                        );
                        $srcPath = $work->execute();
                } else {
@@ -222,11 +222,11 @@ class DjVuHandler extends ImageHandler {
                        $this->logErrorForExternalProcess( $retval, $err, $cmd );
                        return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
                } else {
-                       $params = array(
+                       $params = [
                                'width' => $width,
                                'height' => $height,
                                'page' => $page
-                       );
+                       ];
 
                        return new ThumbnailImage( $image, $dstUrl, $dstPath, $params );
                }
@@ -265,9 +265,9 @@ class DjVuHandler extends ImageHandler {
                        return $metadata;
                }
 
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $unser = unserialize( $metadata );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
                if ( is_array( $unser ) ) {
                        if ( isset( $unser['error'] ) ) {
                                return false;
@@ -289,7 +289,7 @@ class DjVuHandler extends ImageHandler {
         * @param bool $gettext DOCUMENT (Default: false)
         * @return bool|SimpleXMLElement
         */
-       function getMetaTree( $image, $gettext = false ) {
+       public function getMetaTree( $image, $gettext = false ) {
                if ( $gettext && isset( $image->djvuTextTree ) ) {
                        return $image->djvuTextTree;
                }
@@ -304,12 +304,12 @@ class DjVuHandler extends ImageHandler {
                        return false;
                }
 
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                try {
                        // Set to false rather than null to avoid further attempts
                        $image->dejaMetaTree = false;
                        $image->djvuTextTree = false;
-                       $tree = new SimpleXMLElement( $metadata );
+                       $tree = new SimpleXMLElement( $metadata, LIBXML_PARSEHUGE );
                        if ( $tree->getName() == 'mw-djvu' ) {
                                /** @var SimpleXMLElement $b */
                                foreach ( $tree->children() as $b ) {
@@ -327,7 +327,7 @@ class DjVuHandler extends ImageHandler {
                } catch ( Exception $e ) {
                        wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
                }
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
                if ( $gettext ) {
                        return $image->djvuTextTree;
                } else {
@@ -352,7 +352,7 @@ class DjVuHandler extends ImageHandler {
                        $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
                }
 
-               return array( $wgDjvuOutputExtension, $mime );
+               return [ $wgDjvuOutputExtension, $mime ];
        }
 
        function getMetadata( $image, $path ) {
@@ -361,9 +361,9 @@ class DjVuHandler extends ImageHandler {
                $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
                if ( $xml === false ) {
                        // Special value so that we don't repetitively try and decode a broken file.
-                       return serialize( array( 'error' => 'Error extracting metadata' ) );
+                       return serialize( [ 'error' => 'Error extracting metadata' ] );
                } else {
-                       return serialize( array( 'xml' => $xml ) );
+                       return serialize( [ 'xml' => $xml ] );
                }
        }
 
@@ -372,59 +372,55 @@ class DjVuHandler extends ImageHandler {
        }
 
        function isMetadataValid( $image, $metadata ) {
-               return !empty( $metadata ) && $metadata != serialize( array() );
+               return !empty( $metadata ) && $metadata != serialize( [] );
        }
 
-       function pageCount( $image ) {
-               global $wgMemc;
-
-               $key = wfMemcKey( 'file-djvu', 'pageCount', $image->getSha1() );
+       function pageCount( File $image ) {
+               $info = $this->getDimensionInfo( $image );
 
-               $count = $wgMemc->get( $key );
-               if ( $count === false ) {
-                       $tree = $this->getMetaTree( $image );
-                       if ( !$tree ) {
-                               return false;
-                       }
-                       $count = count( $tree->xpath( '//OBJECT' ) );
-                       $wgMemc->set( $key, $count );
-               }
-
-               return $count;
+               return $info ? $info['pageCount'] : false;
        }
 
-       function getPageDimensions( $image, $page ) {
-               global $wgMemc;
+       function getPageDimensions( File $image, $page ) {
+               $index = $page - 1; // MW starts pages at 1
 
-               $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() );
+               $info = $this->getDimensionInfo( $image );
+               if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
+                       return $info['dimensionsByPage'][$index];
+               }
 
-               $dimsByPage = $wgMemc->get( $key );
-               if ( !is_array( $dimsByPage ) ) {
-                       $tree = $this->getMetaTree( $image );
-                       if ( !$tree ) {
-                               return false;
-                       }
+               return false;
+       }
 
-                       $dimsByPage = array();
-                       $count = count( $tree->xpath( '//OBJECT' ) );
-                       for ( $i = 0; $i < $count; ++$i ) {
-                               $o = $tree->BODY[0]->OBJECT[$i];
-                               if ( $o ) {
-                                       $dimsByPage[$i] = array(
-                                               'width' => (int)$o['width'],
-                                               'height' => (int)$o['height']
-                                       );
-                               } else {
-                                       $dimsByPage[$i] = false;
+       protected function getDimensionInfo( File $file ) {
+               $cache = ObjectCache::getMainWANInstance();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'file-djvu', 'dimensions', $file->getSha1() ),
+                       $cache::TTL_INDEFINITE,
+                       function () use ( $file ) {
+                               $tree = $this->getMetaTree( $file );
+                               if ( !$tree ) {
+                                       return false;
                                }
-                       }
-
-                       $wgMemc->set( $key, $dimsByPage );
-               }
 
-               $index = $page - 1; // MW starts pages at 1
+                               $dimsByPage = [];
+                               $count = count( $tree->xpath( '//OBJECT' ) );
+                               for ( $i = 0; $i < $count; $i++ ) {
+                                       $o = $tree->BODY[0]->OBJECT[$i];
+                                       if ( $o ) {
+                                               $dimsByPage[$i] = [
+                                                       'width' => (int)$o['width'],
+                                                       'height' => (int)$o['height'],
+                                               ];
+                                       } else {
+                                               $dimsByPage[$i] = false;
+                                       }
+                               }
 
-               return isset( $dimsByPage[$index] ) ? $dimsByPage[$index] : false;
+                               return [ 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage ];
+                       },
+                       [ 'pcTTL' => $cache::TTL_INDEFINITE ]
+               );
        }
 
        /**
@@ -432,7 +428,7 @@ class DjVuHandler extends ImageHandler {
         * @param int $page Page number to get information for
         * @return bool|string Page text or false when no text found.
         */
-       function getPageText( $image, $page ) {
+       function getPageText( File $image, $page ) {
                $tree = $this->getMetaTree( $image, true );
                if ( !$tree ) {
                        return false;