* @ingroup Media
*/
class DjVuHandler extends ImageHandler {
+ const EXPENSIVE_SIZE_LIMIT = 10485760; // 10MiB
+
/**
* @return bool
*/
return true;
}
+ /**
+ * True if creating thumbnails from the file is large or otherwise resource-intensive.
+ * @param File $file
+ * @return bool
+ */
+ public function isExpensiveToThumbnail( $file ) {
+ return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
+ }
+
/**
* @param File $file
* @return bool
return $metadata;
}
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
$unser = unserialize( $metadata );
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( is_array( $unser ) ) {
if ( isset( $unser['error'] ) ) {
return false;
* @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;
}
return false;
}
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
try {
// Set to false rather than null to avoid further attempts
$image->dejaMetaTree = false;
} catch ( Exception $e ) {
wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
}
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( $gettext ) {
return $image->djvuTextTree;
} else {
return !empty( $metadata ) && $metadata != serialize( array() );
}
- function pageCount( $image ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
+ function pageCount( File $image ) {
+ $info = $this->getDimensionInfo( $image );
- return count( $tree->xpath( '//OBJECT' ) );
+ return $info ? $info['pageCount'] : false;
}
- function getPageDimensions( $image, $page ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
+ function getPageDimensions( File $image, $page ) {
+ $index = $page - 1; // MW starts pages at 1
- $o = $tree->BODY[0]->OBJECT[$page - 1];
- if ( $o ) {
- return array(
- 'width' => intval( $o['width'] ),
- 'height' => intval( $o['height'] )
- );
- } else {
- return false;
+ $info = $this->getDimensionInfo( $image );
+ if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
+ return $info['dimensionsByPage'][$index];
}
+
+ return false;
+ }
+
+ protected function getDimensionInfo( File $file ) {
+ $that = $this;
+
+ 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;
+ }
+
+ $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 array( 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage );
+ }
+ );
}
/**
* @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;