jenkins-bot has submitted this change and it was merged.

Change subject: Extract ParserMakeImageParams/AfterParserFetchFileAndTitle hook 
handlers
......................................................................


Extract ParserMakeImageParams/AfterParserFetchFileAndTitle hook handlers

This patch only moves existing code around, but does not change any
implementation detail.

I found it very suprising that all code called by these two hook handlers
is 100% exclusive to these hook handlers. There is zero interaction
between these hook handlers code and all other code. Why is it in the
same file then? And why is it all static? It doesn't have to be. I
had to change literally nothing, except cutting and pasting, removing
all "static" and replacing all "self::..." with "$this->...". That's
all.

Change-Id: I7fdc582db425d3b95f7d02934b439eb9c102e712
---
M PageImages.php
M includes/PageImages.php
A includes/ParserFileProcessingHookHandler.php
3 files changed, 174 insertions(+), 110 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/PageImages.php b/PageImages.php
index 7c9df3e..dcd757f 100644
--- a/PageImages.php
+++ b/PageImages.php
@@ -18,16 +18,20 @@
 $wgAutoloadClasses['PageImages'] = __DIR__ . '/includes/PageImages.php';
 $wgAutoloadClasses['PageImages\Hooks\LinksUpdateHookHandler']
        = __DIR__ . '/includes/LinksUpdateHookHandler.php';
+$wgAutoloadClasses['PageImages\Hooks\ParserFileProcessingHookHandler']
+       = __DIR__ . '/includes/ParserFileProcessingHookHandler.php';
 
 $wgMessagesDirs['PageImages'] = __DIR__ . '/i18n';
 $wgExtensionMessagesFiles['PageImages'] = __DIR__ . "/PageImages.i18n.php";
 
-$wgHooks['ParserMakeImageParams'][] = 'PageImages::onParserMakeImageParams';
+$wgHooks['ParserMakeImageParams'][]
+       = 
'PageImages\Hooks\ParserFileProcessingHookHandler::onParserMakeImageParams';
 $wgHooks['LinksUpdate'][] = 
'PageImages\Hooks\LinksUpdateHookHandler::onLinksUpdate';
 $wgHooks['OpenSearchXml'][] = 'PageImages::onApiOpenSearchSuggest';
 $wgHooks['ApiOpenSearchSuggest'][] = 'PageImages::onApiOpenSearchSuggest';
 $wgHooks['InfoAction'][] = 'PageImages::onInfoAction';
-$wgHooks['AfterParserFetchFileAndTitle'][] = 
'PageImages::onAfterParserFetchFileAndTitle';
+$wgHooks['AfterParserFetchFileAndTitle'][]
+       = 
'PageImages\Hooks\ParserFileProcessingHookHandler::onAfterParserFetchFileAndTitle';
 $wgHooks['SpecialMobileEditWatchlist::images'][] = 
'PageImages::onSpecialMobileEditWatchlist_images';
 
 $wgHooks['UnitTestsList'][] = function( array &$paths ) {
diff --git a/includes/PageImages.php b/includes/PageImages.php
index 0f76303..ba33055 100644
--- a/includes/PageImages.php
+++ b/includes/PageImages.php
@@ -38,114 +38,6 @@
        }
 
        /**
-        * Returns true if data for this title should be saved
-        *
-        * @param Title $title
-        * @return bool
-        */
-       private static function processThisTitle( Title $title ) {
-               static $flipped = false;
-               if ( $flipped === false ) {
-                       global $wgPageImagesNamespaces;
-                       $flipped = array_flip( $wgPageImagesNamespaces );
-               }
-               return isset( $flipped[$title->getNamespace()] );
-       }
-
-       /**
-        * ParserMakeImageParams hook handler, saves extended information about 
images used on page
-        *
-        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/ParserMakeImageParams
-        *
-        * @param Title $title
-        * @param File|bool $file
-        * @param array &$params
-        * @param Parser $parser
-        * @return bool
-        */
-       public static function onParserMakeImageParams( Title $title, $file, 
array &$params, Parser $parser ) {
-               self::processFile( $parser, $file, $params );
-               return true;
-       }
-
-       /**
-        * AfterParserFetchFileAndTitle hook handler, saves information about 
gallery images
-        *
-        * @param Parser $parser
-        * @param ImageGalleryBase $ig
-        * @return bool
-        */
-       public static function onAfterParserFetchFileAndTitle( Parser $parser, 
ImageGalleryBase $ig ) {
-               foreach ( $ig->getImages() as $image ) {
-                       self::processFile( $parser, $image[0], null );
-               }
-               return true;
-       }
-
-       /**
-        * @param Parser $parser
-        * @param File|Title|null $file
-        * @param array|null $handlerParams
-        */
-       private static function processFile( Parser $parser, $file, 
$handlerParams ) {
-               if ( !$file || !self::processThisTitle( $parser->getTitle() ) ) 
{
-                       return;
-               }
-
-               if ( !$file instanceof File ) {
-                       $file = wfFindFile( $file );
-                       if ( !$file ) {
-                               return;
-                       }
-               }
-
-               if ( is_array( $handlerParams ) ) {
-                       $myParams = $handlerParams;
-                       self::calcWidth( $myParams, $file );
-               } else {
-                       $myParams = array();
-               }
-
-               $myParams['filename'] = $file->getTitle()->getDBkey();
-               $myParams['fullwidth'] = $file->getWidth();
-               $myParams['fullheight'] = $file->getHeight();
-
-               $out = $parser->getOutput();
-               $pageImages = $out->getExtensionData( 'pageImages' ) ?: array();
-               $pageImages[] = $myParams;
-               $out->setExtensionData( 'pageImages', $pageImages );
-       }
-
-       /**
-        * Estimates image size as displayed if not explicitly provided.
-        * We don't follow the core size calculation algorithm precisely 
because it's not required and editor's
-        * intentions are more important than the precise number.
-        *
-        * @param array &$params
-        * @param File $file
-        */
-       private static function calcWidth( array &$params, File $file ) {
-               global $wgThumbLimits, $wgDefaultUserOptions;
-
-               if ( isset( $params['handler']['width'] ) ) {
-                       return;
-               }
-               if ( isset( $params['handler']['height'] ) && 
$file->getHeight() > 0 ) {
-                       $params['handler']['width'] =
-                               $file->getWidth() * ( 
$params['handler']['height'] / $file->getHeight() );
-               } elseif ( isset( $params['frame']['thumbnail'] )
-                       || isset( $params['frame']['thumb'] )
-                       || isset( $params['frame']['frameless'] ) )
-               {
-                       $params['handler']['width'] = isset( 
$wgThumbLimits[$wgDefaultUserOptions['thumbsize']] )
-                               ? 
$wgThumbLimits[$wgDefaultUserOptions['thumbsize']]
-                               : 250;
-               } else {
-                       $params['handler']['width'] = $file->getWidth();
-               }
-       }
-
-       /**
         * InfoAction hook handler, adds the page image to the info=action page
         *
         * @see https://www.mediawiki.org/wiki/Manual:Hooks/InfoAction
diff --git a/includes/ParserFileProcessingHookHandler.php 
b/includes/ParserFileProcessingHookHandler.php
new file mode 100644
index 0000000..34ec1e8
--- /dev/null
+++ b/includes/ParserFileProcessingHookHandler.php
@@ -0,0 +1,168 @@
+<?php
+
+namespace PageImages\Hooks;
+
+use File;
+use ImageGalleryBase;
+use Parser;
+use Title;
+
+/**
+ * Handler for the "ParserMakeImageParams" and "AfterParserFetchFileAndTitle" 
hooks.
+ *
+ * @license WTFPL 2.0
+ * @author Max Semenik
+ * @author Thiemo Mättig
+ */
+class ParserFileProcessingHookHandler {
+
+       /**
+        * ParserMakeImageParams hook handler, saves extended information about 
images used on page
+        *
+        * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/ParserMakeImageParams
+        *
+        * @param Title $title
+        * @param File|bool $file
+        * @param array &$params
+        * @param Parser $parser
+        *
+        * @return bool
+        */
+       public static function onParserMakeImageParams(
+               Title $title,
+               $file,
+               array &$params,
+               Parser $parser
+       ) {
+               $handler = new self();
+               return $handler->doParserMakeImageParams( $title, $file, 
$params, $parser );
+       }
+
+       /**
+        * AfterParserFetchFileAndTitle hook handler, saves information about 
gallery images
+        *
+        * @param Parser $parser
+        * @param ImageGalleryBase $gallery
+        *
+        * @return bool
+        */
+       public static function onAfterParserFetchFileAndTitle( Parser $parser, 
ImageGalleryBase $gallery ) {
+               $handler = new self();
+               return $handler->doAfterParserFetchFileAndTitle( $parser, 
$gallery );
+       }
+
+       /**
+        * @param Title $title
+        * @param File|bool $file
+        * @param array &$params
+        * @param Parser $parser
+        *
+        * @return bool Always true.
+        */
+       public function doParserMakeImageParams(
+               Title $title,
+               $file,
+               array &$params,
+               Parser $parser
+       ) {
+               $this->processFile( $parser, $file, $params );
+               return true;
+       }
+
+       /**
+        * @param Parser $parser
+        * @param ImageGalleryBase $gallery
+        *
+        * @return bool Always true.
+        */
+       public function doAfterParserFetchFileAndTitle( Parser $parser, 
ImageGalleryBase $gallery ) {
+               foreach ( $gallery->getImages() as $image ) {
+                       $this->processFile( $parser, $image[0], null );
+               }
+
+               return true;
+       }
+
+       /**
+        * @param Parser $parser
+        * @param File|Title|null $file
+        * @param array|null $handlerParams
+        */
+       private function processFile( Parser $parser, $file, $handlerParams ) {
+               if ( !$file || !$this->processThisTitle( $parser->getTitle() ) 
) {
+                       return;
+               }
+
+               if ( !( $file instanceof File ) ) {
+                       $file = wfFindFile( $file );
+                       if ( !$file ) {
+                               return;
+                       }
+               }
+
+               if ( is_array( $handlerParams ) ) {
+                       $myParams = $handlerParams;
+                       $this->calcWidth( $myParams, $file );
+               } else {
+                       $myParams = array();
+               }
+
+               $myParams['filename'] = $file->getTitle()->getDBkey();
+               $myParams['fullwidth'] = $file->getWidth();
+               $myParams['fullheight'] = $file->getHeight();
+
+               $out = $parser->getOutput();
+               $pageImages = $out->getExtensionData( 'pageImages' ) ?: array();
+               $pageImages[] = $myParams;
+               $out->setExtensionData( 'pageImages', $pageImages );
+       }
+
+       /**
+        * Returns true if data for this title should be saved
+        *
+        * @param Title $title
+        *
+        * @return bool
+        */
+       private function processThisTitle( Title $title ) {
+               global $wgPageImagesNamespaces;
+               static $flipped = false;
+
+               if ( $flipped === false ) {
+                       $flipped = array_flip( $wgPageImagesNamespaces );
+               }
+
+               return isset( $flipped[$title->getNamespace()] );
+       }
+
+       /**
+        * Estimates image size as displayed if not explicitly provided. We 
don't follow the core size
+        * calculation algorithm precisely because it's not required and 
editor's intentions are more
+        * important than the precise number.
+        *
+        * @param array &$params
+        * @param File $file
+        */
+       private function calcWidth( array &$params, File $file ) {
+               global $wgThumbLimits, $wgDefaultUserOptions;
+
+               if ( isset( $params['handler']['width'] ) ) {
+                       return;
+               }
+
+               if ( isset( $params['handler']['height'] ) && 
$file->getHeight() > 0 ) {
+                       $params['handler']['width'] =
+                               $file->getWidth() * ( 
$params['handler']['height'] / $file->getHeight() );
+               } elseif ( isset( $params['frame']['thumbnail'] )
+                       || isset( $params['frame']['thumb'] )
+                       || isset( $params['frame']['frameless'] ) )
+               {
+                       $params['handler']['width'] = isset( 
$wgThumbLimits[$wgDefaultUserOptions['thumbsize']] )
+                               ? 
$wgThumbLimits[$wgDefaultUserOptions['thumbsize']]
+                               : 250;
+               } else {
+                       $params['handler']['width'] = $file->getWidth();
+               }
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/261627
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7fdc582db425d3b95f7d02934b439eb9c102e712
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/PageImages
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Hoo man <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to