| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\PoolCounter\PoolCounterWorkViaCallback; |
| 27 |
use MediaWiki\Shell\Shell; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class DjVuHandler extends ImageHandler { |
| 35 |
private const EXPENSIVE_SIZE_LIMIT = 10_485_760; |
| 36 |
|
| 37 |
|
| 38 |
private const STATE_DJVU_IMAGE = 'djvuImage'; |
| 39 |
private const STATE_TEXT_TREE = 'djvuTextTree'; |
| 40 |
private const STATE_META_TREE = 'djvuMetaTree'; |
| 41 |
private const CACHE_VERSION = 'v2'; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function isEnabled() { |
| 47 |
$djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer ); |
| 48 |
$djvuDump = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuDump ); |
| 49 |
if ( !$djvuRenderer || !$djvuDump ) { |
| 50 |
wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump" ); |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function mustRender( $file ) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function isExpensiveToThumbnail( $file ) { |
| 71 |
return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function isMultiPage( $file ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function getParamMap() { |
| 86 |
return [ |
| 87 |
'img_width' => 'width', |
| 88 |
'img_page' => 'page', |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function validateParam( $name, $value ) { |
| 98 |
if ( $name === 'page' && trim( $value ) !== (string)intval( $value ) ) { |
| 99 |
|
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
return in_array( $name, [ 'width', 'height', 'page', 'physicalWidth', 'physicalHeight' ] ) && $value > 0; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function makeParamString( $params ) { |
| 111 |
$page = $params['page'] ?? 1; |
| 112 |
$width = $params['physicalWidth'] ?? $params['width'] ?? null; |
| 113 |
if ( !$width ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
return "page{$page}-{$width}px"; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function parseParamString( $str ) { |
| 125 |
$m = false; |
| 126 |
if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) { |
| 127 |
return [ 'width' => $m[2], 'page' => $m[1] ]; |
| 128 |
} |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function normaliseParams( $image, &$params ) { |
| 136 |
|
| 137 |
|
| 138 |
if ( !parent::normaliseParams( $image, $params ) ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
$srcWidth = $image->getWidth( $params['page'] ); |
| 142 |
$srcHeight = $image->getHeight( $params['page'] ); |
| 143 |
$params['physicalWidth'] = $this->getSteppedThumbWidth( |
| 144 |
$image, $params['physicalWidth'], $srcWidth, $srcHeight |
| 145 |
); |
| 146 |
$params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, $params['physicalWidth'] ); |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
protected function getScriptParams( $params ) { |
| 155 |
return [ |
| 156 |
'width' => $params['width'], |
| 157 |
'page' => $params['page'], |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
| 170 |
$djvuRenderer = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DjvuRenderer ); |
| 171 |
$djvuPostProcessor = MediaWikiServices::getInstance()->getMainConfig() |
| 172 |
->get( MainConfigNames::DjvuPostProcessor ); |
| 173 |
if ( !$this->normaliseParams( $image, $params ) ) { |
| 174 |
return new TransformParameterError( $params ); |
| 175 |
} |
| 176 |
$width = $params['width']; |
| 177 |
$height = $params['height']; |
| 178 |
$page = $params['page']; |
| 179 |
|
| 180 |
if ( $flags & self::TRANSFORM_LATER ) { |
| 181 |
$params = [ |
| 182 |
'width' => $width, |
| 183 |
'height' => $height, |
| 184 |
'page' => $page |
| 185 |
]; |
| 186 |
|
| 187 |
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
| 188 |
} |
| 189 |
|
| 190 |
if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { |
| 191 |
return new MediaTransformError( |
| 192 |
'thumbnail_error', |
| 193 |
$width, |
| 194 |
$height, |
| 195 |
wfMessage( 'thumbnail_dest_directory' ) |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if ( $image->getSize() >= 1e7 ) { |
| 203 |
$work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ), |
| 204 |
[ |
| 205 |
'doWork' => static function () use ( $image ) { |
| 206 |
return $image->getLocalRefPath(); |
| 207 |
} |
| 208 |
] |
| 209 |
); |
| 210 |
$srcPath = $work->execute(); |
| 211 |
} else { |
| 212 |
$srcPath = $image->getLocalRefPath(); |
| 213 |
} |
| 214 |
|
| 215 |
if ( $srcPath === false ) { |
| 216 |
wfDebugLog( 'thumbnail', |
| 217 |
sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"', |
| 218 |
wfHostname(), $image->getName() ) ); |
| 219 |
|
| 220 |
return new MediaTransformError( 'thumbnail_error', |
| 221 |
$params['width'], $params['height'], |
| 222 |
wfMessage( 'filemissing' ) |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
$cmd = '(' . Shell::escape( |
| 229 |
$djvuRenderer, |
| 230 |
"-format=ppm", |
| 231 |
"-page={$page}", |
| 232 |
"-size={$params['physicalWidth']}x{$params['physicalHeight']}", |
| 233 |
$srcPath ); |
| 234 |
if ( $djvuPostProcessor ) { |
| 235 |
$cmd .= " | {$djvuPostProcessor}"; |
| 236 |
} |
| 237 |
$cmd .= ' > ' . Shell::escape( $dstPath ) . ') 2>&1'; |
| 238 |
wfDebug( __METHOD__ . ": $cmd" ); |
| 239 |
$retval = 0; |
| 240 |
$err = wfShellExec( $cmd, $retval ); |
| 241 |
|
| 242 |
$removed = $this->removeBadFile( $dstPath, $retval ); |
| 243 |
if ( $retval !== 0 || $removed ) { |
| 244 |
$this->logErrorForExternalProcess( $retval, $err, $cmd ); |
| 245 |
return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); |
| 246 |
} |
| 247 |
$params = [ |
| 248 |
'width' => $width, |
| 249 |
'height' => $height, |
| 250 |
'page' => $page |
| 251 |
]; |
| 252 |
|
| 253 |
return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
private function getDjVuImage( $state, $path ) { |
| 265 |
$deja = $state->getHandlerState( self::STATE_DJVU_IMAGE ); |
| 266 |
if ( !$deja ) { |
| 267 |
$deja = new DjVuImage( $path ); |
| 268 |
$state->setHandlerState( self::STATE_DJVU_IMAGE, $deja ); |
| 269 |
} |
| 270 |
return $deja; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private function getMetadataInternal( File $file, $gettext ) { |
| 281 |
$itemNames = [ 'error', '_error', 'data' ]; |
| 282 |
if ( $gettext ) { |
| 283 |
$itemNames[] = 'text'; |
| 284 |
} |
| 285 |
$unser = $file->getMetadataItems( $itemNames ); |
| 286 |
|
| 287 |
if ( isset( $unser['error'] ) ) { |
| 288 |
return false; |
| 289 |
} |
| 290 |
if ( isset( $unser['_error'] ) ) { |
| 291 |
return false; |
| 292 |
} |
| 293 |
return $unser; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function getMetaTree( $image, $gettext = false ) { |
| 303 |
if ( $gettext && $image->getHandlerState( self::STATE_TEXT_TREE ) ) { |
| 304 |
return $image->getHandlerState( self::STATE_TEXT_TREE ); |
| 305 |
} |
| 306 |
if ( !$gettext && $image->getHandlerState( self::STATE_META_TREE ) ) { |
| 307 |
return $image->getHandlerState( self::STATE_META_TREE ); |
| 308 |
} |
| 309 |
|
| 310 |
$metadata = $this->getMetadataInternal( $image, $gettext ); |
| 311 |
if ( !$metadata ) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
if ( !$gettext ) { |
| 316 |
unset( $metadata['text'] ); |
| 317 |
} |
| 318 |
return $metadata; |
| 319 |
} |
| 320 |
|
| 321 |
public function getThumbType( $ext, $mime, $params = null ) { |
| 322 |
$djvuOutputExtension = MediaWikiServices::getInstance()->getMainConfig() |
| 323 |
->get( MainConfigNames::DjvuOutputExtension ); |
| 324 |
static $mime; |
| 325 |
if ( !isset( $mime ) ) { |
| 326 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 327 |
$mime = $magic->getMimeTypeFromExtensionOrNull( $djvuOutputExtension ); |
| 328 |
} |
| 329 |
|
| 330 |
return [ $djvuOutputExtension, $mime ]; |
| 331 |
} |
| 332 |
|
| 333 |
public function getSizeAndMetadata( $state, $path ) { |
| 334 |
wfDebug( "Getting DjVu metadata for $path" ); |
| 335 |
|
| 336 |
$djvuImage = $this->getDjVuImage( $state, $path ); |
| 337 |
$metadata = $djvuImage->retrieveMetaData(); |
| 338 |
if ( $metadata === false ) { |
| 339 |
|
| 340 |
$metadata = [ 'error' => 'Error extracting metadata' ]; |
| 341 |
} |
| 342 |
return [ 'metadata' => $metadata ] + $djvuImage->getImageSize(); |
| 343 |
} |
| 344 |
|
| 345 |
public function getMetadataType( $image ) { |
| 346 |
|
| 347 |
return 'djvuxml'; |
| 348 |
} |
| 349 |
|
| 350 |
public function isFileMetadataValid( $image ) { |
| 351 |
return $image->getMetadataArray() ? self::METADATA_GOOD : self::METADATA_BAD; |
| 352 |
} |
| 353 |
|
| 354 |
public function pageCount( File $image ) { |
| 355 |
$info = $this->getDimensionInfo( $image ); |
| 356 |
|
| 357 |
return $info ? $info['pageCount'] : false; |
| 358 |
} |
| 359 |
|
| 360 |
public function getPageDimensions( File $image, $page ) { |
| 361 |
$index = $page - 1; |
| 362 |
|
| 363 |
$info = $this->getDimensionInfo( $image ); |
| 364 |
if ( $info && isset( $info['dimensionsByPage'][$index] ) ) { |
| 365 |
return $info['dimensionsByPage'][$index]; |
| 366 |
} |
| 367 |
|
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
protected function getDimensionInfo( File $file ) { |
| 372 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 373 |
return $cache->getWithSetCallback( |
| 374 |
$cache->makeKey( 'file-djvu', 'dimensions', self::CACHE_VERSION, $file->getSha1() ), |
| 375 |
$cache::TTL_INDEFINITE, |
| 376 |
function () use ( $file ) { |
| 377 |
$tree = $this->getMetaTree( $file ); |
| 378 |
return $this->getDimensionInfoFromMetaTree( $tree ); |
| 379 |
}, |
| 380 |
[ 'pcTTL' => $cache::TTL_INDEFINITE ] |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
protected function getDimensionInfoFromMetaTree( $metatree ) { |
| 390 |
if ( !$metatree ) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
$dimsByPage = []; |
| 394 |
|
| 395 |
if ( !isset( $metatree['data'] ) || !$metatree['data'] ) { |
| 396 |
return false; |
| 397 |
} |
| 398 |
foreach ( $metatree['data']['pages'] as $page ) { |
| 399 |
if ( !$page ) { |
| 400 |
$dimsByPage[] = false; |
| 401 |
} else { |
| 402 |
$dimsByPage[] = [ |
| 403 |
'width' => (int)$page['width'], |
| 404 |
'height' => (int)$page['height'], |
| 405 |
]; |
| 406 |
} |
| 407 |
} |
| 408 |
return [ |
| 409 |
'pageCount' => count( $metatree['data']['pages'] ), |
| 410 |
'dimensionsByPage' => $dimsByPage |
| 411 |
]; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public function getPageText( File $image, $page ) { |
| 420 |
$tree = $this->getMetaTree( $image, true ); |
| 421 |
if ( !$tree ) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
if ( isset( $tree['text'] ) && isset( $tree['text'][$page - 1] ) ) { |
| 425 |
return $tree['text'][$page - 1]; |
| 426 |
} |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
public function useSplitMetadata() { |
| 431 |
return true; |
| 432 |
} |
| 433 |
} |