X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Fmedia%2FDjVu.php;h=0d2ed29b002d17bcca6caac035440c92b725b55d;hb=58cb1f824ac75c3b58ba19d1e88c1b38f9dc1fab;hp=749ef23c7b02894b4714ed92fa958794ffad5162;hpb=fed9e3e163c5e4debffdcb03bfb0ceec7fec8f0d;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/media/DjVu.php b/includes/media/DjVu.php index 749ef23c7b..0d2ed29b00 100644 --- a/includes/media/DjVu.php +++ b/includes/media/DjVu.php @@ -72,10 +72,10 @@ class DjVuHandler extends ImageHandler { * @return array */ function getParamMap() { - return array( + return [ 'img_width' => 'width', 'img_page' => 'page', - ); + ]; } /** @@ -89,7 +89,7 @@ class DjVuHandler extends ImageHandler { // 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 { @@ -120,7 +120,7 @@ class DjVuHandler extends ImageHandler { 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; + function pageCount( File $image ) { + $info = $this->getDimensionInfo( $image ); - $key = wfMemcKey( 'file-djvu', 'pageCount', $image->getSha1() ); + return $info ? $info['pageCount'] : false; + } - $count = $wgMemc->get( $key ); - if ( $count === false ) { - $tree = $this->getMetaTree( $image ); - if ( !$tree ) { - return false; - } - $count = count( $tree->xpath( '//OBJECT' ) ); - $wgMemc->set( $key, $count ); + function getPageDimensions( File $image, $page ) { + $index = $page - 1; // MW starts pages at 1 + + $info = $this->getDimensionInfo( $image ); + if ( $info && isset( $info['dimensionsByPage'][$index] ) ) { + return $info['dimensionsByPage'][$index]; } - return $count; + return false; } - function getPageDimensions( $image, $page ) { - global $wgMemc; + protected function getDimensionInfo( File $file ) { + $that = $this; - $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() ); - - $dimsByPage = $wgMemc->get( $key ); - if ( !is_array( $dimsByPage ) ) { - $tree = $this->getMetaTree( $image ); - if ( !$tree ) { - 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; + return ObjectCache::getMainWANInstance()->getWithSetCallback( + wfMemcKey( 'file-djvu', 'dimensions', $file->getSha1() ), + WANObjectCache::TTL_INDEFINITE, + function () use ( $that, $file ) { + $tree = $that->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 ]; + } + ); } /** @@ -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;