* @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 !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;
* @since 1.23
*/
public function fetchExtendedMetadata( File $file ) {
- global $wgMemc;
+ $cache = ObjectCache::getMainWANInstance();
// If revision deleted, exit immediately
if ( $file->isDeleted( File::DELETED_FILE ) ) {
$file->getSha1()
);
- $cachedValue = $wgMemc->get( $cacheKey );
+ $cachedValue = $cache->get( $cacheKey );
if (
$cachedValue
&& Hooks::run( 'ValidateExtendedMetadataCache', array( $cachedValue['timestamp'], $file ) )
// computation on a cache hit.
$this->sanitizeArrayForAPI( $extendedMetadata );
$valueToCache = array( 'data' => $extendedMetadata, 'timestamp' => wfTimestampNow() );
- $wgMemc->set( $cacheKey, $valueToCache, $maxCacheTime );
+ $cache->set( $cacheKey, $valueToCache, $maxCacheTime );
}
return $extendedMetadata;