Create DifferenceEngine before calling GetDifferenceEngine
authorGergő Tisza <tgr.huwiki@gmail.com>
Mon, 13 Aug 2018 16:53:59 +0000 (18:53 +0200)
committerGergő Tisza <gtisza@wikimedia.org>
Mon, 20 Aug 2018 13:32:49 +0000 (13:32 +0000)
This way the hook is able to consider what kind of DifferenceEngine
was created by default, or even wrap/decorate it.

Change-Id: Ibb6b1b087a2dda445be2b5e8c7518c7d169b5192

docs/hooks.txt
includes/content/ContentHandler.php

index 219c51f..9a634ad 100644 (file)
@@ -1670,15 +1670,13 @@ $title: Title object that we need to get a sortkey for
 &$sortkey: Sortkey to use.
 
 'GetDifferenceEngine': Called when getting a new difference engine interface
-object Return false for valid object in $differenceEngine or true for the
-default difference engine.
+object.  Can be used to decorate or replace the default difference engine.
 $context: IContextSource context to be used for diff
 $old: Revision ID to show and diff with
 $new: Either a revision ID or one of the strings 'cur', 'prev' or 'next'
 $refreshCache: If set, refreshes the diff cache
 $unhide: If set, allow viewing deleted revs
-&$differenceEngine: output parameter, difference engine object to be used for
-  diff
+&$differenceEngine: The difference engine object to be used for the diff
 
 'GetDoubleUnderscoreIDs': Modify the list of behavior switch (double
 underscore) magic words. Called by MagicWord.
index 004e38a..0a451b2 100644 (file)
@@ -614,6 +614,14 @@ abstract class ContentHandler {
        /**
         * Factory for creating an appropriate DifferenceEngine for this content model.
         *
+        * The DifferenceEngine subclass to use is selected in getDiffEngineClass(). The
+        * GetDifferenceEngine hook will receive the DifferenceEngine object and can replace or
+        * wrap it.
+        * (Note that in older versions of MediaWiki the hook documentation instructed extensions
+        * to return false from the hook; you should not rely on always being able to decorate
+        * the DifferenceEngine instance from the hook. If the owner of the content type wants to
+        * decorare the instance, overriding this method is a safer approach.)
+        *
         * @since 1.21
         *
         * @param IContextSource $context Context to use, anything else will be ignored.
@@ -629,15 +637,11 @@ abstract class ContentHandler {
                $rcid = 0, // FIXME: Deprecated, no longer used
                $refreshCache = false, $unhide = false
        ) {
-               // hook: get difference engine
-               $differenceEngine = null;
-               if ( !Hooks::run( 'GetDifferenceEngine',
-                       [ $context, $old, $new, $refreshCache, $unhide, &$differenceEngine ]
-               ) ) {
-                       return $differenceEngine;
-               }
                $diffEngineClass = $this->getDiffEngineClass();
-               return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
+               $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
+               Hooks::run( 'GetDifferenceEngine', [ $context, $old, $new, $refreshCache, $unhide,
+                       &$differenceEngine ] );
+               return $differenceEngine;
        }
 
        /**