MediaWiki REL1_34
ParserFileProcessingHookHandlers.php
Go to the documentation of this file.
1<?php
2
3namespace PageImages\Hooks;
4
5use File;
7use Parser;
8use Title;
9
18
29 public static function onParserMakeImageParams(
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(
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 ) ) {
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
Adjust thumbnails on image pages according to a user setting.
$wgDefaultUserOptions
Settings added to this array will override the default globals for the user preferences used by anony...
wfFindFile( $title, $options=[])
Find a file.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:61
getImages()
Returns the list of images this gallery contains.
Handler for the "ParserMakeImageParams" and "AfterParserFetchFileAndTitle" hooks.
static onParserMakeImageParams(Title $title, $file, array &$params, Parser $parser)
ParserMakeImageParams hook handler, saves extended information about images used on page.
processThisTitle(Title $title)
Returns true if data for this title should be saved.
doAfterParserFetchFileAndTitle(Parser $parser, ImageGalleryBase $gallery)
doParserMakeImageParams(Title $title, $file, array &$params, Parser $parser)
static onAfterParserFetchFileAndTitle(Parser $parser, ImageGalleryBase $gallery)
AfterParserFetchFileAndTitle hook handler, saves information about gallery images.
calcWidth(array &$params, File $file)
Estimates image size as displayed if not explicitly provided.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:74
getTitle()
Accessor for the Title object.
Definition Parser.php:935
getOutput()
Get the ParserOutput object.
Definition Parser.php:983
Represents a title within MediaWiki.
Definition Title.php:42
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42