MediaWiki  1.34.0
ParserFileProcessingHookHandlers.php
Go to the documentation of this file.
1 <?php
2 
3 namespace PageImages\Hooks;
4 
5 use File;
7 use Parser;
8 use Title;
9 
18 
29  public static function onParserMakeImageParams(
30  Title $title,
31  $file,
32  array &$params,
33  Parser $parser
34  ) {
35  $handler = new self();
36  $handler->doParserMakeImageParams( $title, $file, $params, $parser );
37  }
38 
45  public static function onAfterParserFetchFileAndTitle(
46  Parser $parser, ImageGalleryBase $gallery
47  ) {
48  $handler = new self();
49  $handler->doAfterParserFetchFileAndTitle( $parser, $gallery );
50  }
51 
58  public function doParserMakeImageParams(
59  Title $title,
60  $file,
61  array &$params,
62  Parser $parser
63  ) {
64  $this->processFile( $parser, $file, $params );
65  }
66 
71  public function doAfterParserFetchFileAndTitle( Parser $parser, ImageGalleryBase $gallery ) {
72  foreach ( $gallery->getImages() as $image ) {
73  $this->processFile( $parser, $image[0], null );
74  }
75  }
76 
82  private function processFile( Parser $parser, $file, $handlerParams ) {
83  if ( !$file || !$this->processThisTitle( $parser->getTitle() ) ) {
84  return;
85  }
86 
87  if ( !( $file instanceof File ) ) {
88  $file = wfFindFile( $file );
89  // Non-image files (e.g. audio files) from a <gallery> can end here
90  if ( !$file || !$file->canRender() ) {
91  return;
92  }
93  }
94 
95  if ( is_array( $handlerParams ) ) {
96  $myParams = $handlerParams;
97  $this->calcWidth( $myParams, $file );
98  } else {
99  $myParams = [];
100  }
101 
102  $myParams['filename'] = $file->getTitle()->getDBkey();
103  $myParams['fullwidth'] = $file->getWidth();
104  $myParams['fullheight'] = $file->getHeight();
105 
106  $out = $parser->getOutput();
107  $pageImages = $out->getExtensionData( 'pageImages' ) ?: [];
108  $pageImages[] = $myParams;
109  $out->setExtensionData( 'pageImages', $pageImages );
110  }
111 
119  private function processThisTitle( Title $title ) {
120  global $wgPageImagesNamespaces;
121  static $flipped = false;
122 
123  if ( $flipped === false ) {
124  $flipped = array_flip( $wgPageImagesNamespaces );
125  }
126 
127  return isset( $flipped[$title->getNamespace()] );
128  }
129 
138  private function calcWidth( array &$params, File $file ) {
140 
141  if ( isset( $params['handler']['width'] ) ) {
142  return;
143  }
144 
145  if ( isset( $params['handler']['height'] ) && $file->getHeight() > 0 ) {
146  $params['handler']['width'] =
147  $file->getWidth() * ( $params['handler']['height'] / $file->getHeight() );
148  } elseif ( isset( $params['frame']['thumbnail'] )
149  || isset( $params['frame']['thumb'] )
150  || isset( $params['frame']['frameless'] )
151  ) {
152  $params['handler']['width'] = $wgThumbLimits[$wgDefaultUserOptions['thumbsize']]
153  ?? 250;
154  } else {
155  $params['handler']['width'] = $file->getWidth();
156  }
157  }
158 
159 }
$wgThumbLimits
$wgThumbLimits
Adjust thumbnails on image pages according to a user setting.
Definition: DefaultSettings.php:1474
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
$wgDefaultUserOptions
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
Definition: DefaultSettings.php:4832
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Hooks\ParserFileProcessingHookHandlers\doAfterParserFetchFileAndTitle
doAfterParserFetchFileAndTitle(Parser $parser, ImageGalleryBase $gallery)
Definition: ParserFileProcessingHookHandlers.php:71
Hooks\ParserFileProcessingHookHandlers\processFile
processFile(Parser $parser, $file, $handlerParams)
Definition: ParserFileProcessingHookHandlers.php:82
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:61
Hooks\ParserFileProcessingHookHandlers\processThisTitle
processThisTitle(Title $title)
Returns true if data for this title should be saved.
Definition: ParserFileProcessingHookHandlers.php:119
PageImages\Hooks
Definition: LinksUpdateHookHandler.php:3
$title
$title
Definition: testCompression.php:34
ImageGalleryBase\getImages
getImages()
Returns the list of images this gallery contains.
Definition: ImageGalleryBase.php:303
Hooks\ParserFileProcessingHookHandlers\onAfterParserFetchFileAndTitle
static onAfterParserFetchFileAndTitle(Parser $parser, ImageGalleryBase $gallery)
AfterParserFetchFileAndTitle hook handler, saves information about gallery images.
Definition: ParserFileProcessingHookHandlers.php:45
Hooks\ParserFileProcessingHookHandlers\onParserMakeImageParams
static onParserMakeImageParams(Title $title, $file, array &$params, Parser $parser)
ParserMakeImageParams hook handler, saves extended information about images used on page.
Definition: ParserFileProcessingHookHandlers.php:29
wfFindFile
wfFindFile( $title, $options=[])
Find a file.
Definition: GlobalFunctions.php:2604
Hooks\ParserFileProcessingHookHandlers\calcWidth
calcWidth(array &$params, File $file)
Estimates image size as displayed if not explicitly provided.
Definition: ParserFileProcessingHookHandlers.php:138
Title
Represents a title within MediaWiki.
Definition: Title.php:42
Hooks\ParserFileProcessingHookHandlers\doParserMakeImageParams
doParserMakeImageParams(Title $title, $file, array &$params, Parser $parser)
Definition: ParserFileProcessingHookHandlers.php:58
Hooks\ParserFileProcessingHookHandlers
Handler for the "ParserMakeImageParams" and "AfterParserFetchFileAndTitle" hooks.
Definition: ParserFileProcessingHookHandlers.php:17