* @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;
}
// 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 ) {
return !empty( $metadata ) && $metadata != serialize( array() );
}
- 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;
-
- $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() );
-
- $dimsByPage = $wgMemc->get( $key );
- if ( !is_array( $dimsByPage ) ) {
- $tree = $this->getMetaTree( $image );
- if ( !$tree ) {
- return false;
- }
+ protected function getDimensionInfo( File $file ) {
+ $that = $this;
- $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 = 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 isset( $dimsByPage[$index] ) ? $dimsByPage[$index] : 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;