Move addTrackingCategory from Parser to ParserOutput
authorGergő Tisza <tgr.huwiki@gmail.com>
Tue, 23 Sep 2014 14:36:40 +0000 (14:36 +0000)
committerGergő Tisza <tgr.huwiki@gmail.com>
Sun, 28 Sep 2014 23:35:52 +0000 (23:35 +0000)
addTrackingCategory is more in line with ParserOutput's functionality
(addLink, addCategory etc), and tracking categories are useful even for
content types which do not use the parser at all. There is no reason to
require the caller to obtain a Parser object just to be able to add
tracking categories.

Change-Id: I89d9ea1db3a4e6486e77eee940bd438f7753b776

includes/parser/Parser.php
includes/parser/ParserOutput.php

index 2d72deb..ddd1f9a 100644 (file)
@@ -4359,40 +4359,12 @@ class Parser {
        }
 
        /**
-        * Add a tracking category, getting the title from a system message,
-        * or print a debug message if the title is invalid.
-        *
-        * Please add any message that you use with this function to
-        * $wgTrackingCategories. That way they will be listed on
-        * Special:TrackingCategories.
-        *
+        * @see ParserOutput::addTrackingCategory()
         * @param string $msg Message key
         * @return bool Whether the addition was successful
         */
        public function addTrackingCategory( $msg ) {
-               if ( $this->mTitle->getNamespace() === NS_SPECIAL ) {
-                       wfDebug( __METHOD__ . ": Not adding tracking category $msg to special page!\n" );
-                       return false;
-               }
-               // Important to parse with correct title (bug 31469)
-               $cat = wfMessage( $msg )
-                       ->title( $this->getTitle() )
-                       ->inContentLanguage()
-                       ->text();
-
-               # Allow tracking categories to be disabled by setting them to "-"
-               if ( $cat === '-' ) {
-                       return false;
-               }
-
-               $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
-               if ( $containerCategory ) {
-                       $this->mOutput->addCategory( $containerCategory->getDBkey(), $this->getDefaultSort() );
-                       return true;
-               } else {
-                       wfDebug( __METHOD__ . ": [[MediaWiki:$msg]] is not a valid title!\n" );
-                       return false;
-               }
+               return $this->mOutput->addTrackingCategory( $msg, $this->mTitle );
        }
 
        /**
index 5037ce1..43e8d0b 100644 (file)
@@ -471,6 +471,46 @@ class ParserOutput extends CacheTime {
                $this->mPreventClickjacking = $this->mPreventClickjacking || $out->getPreventClickjacking();
        }
 
+       /**
+        * Add a tracking category, getting the title from a system message,
+        * or print a debug message if the title is invalid.
+        *
+        * Please add any message that you use with this function to
+        * $wgTrackingCategories. That way they will be listed on
+        * Special:TrackingCategories.
+        *
+        * @param string $msg Message key
+        * @param Title $title title of the page which is being tracked
+        * @return bool Whether the addition was successful
+        * @since 1.25
+        */
+       public function addTrackingCategory( $msg, $title ) {
+               if ( $title->getNamespace() === NS_SPECIAL ) {
+                       wfDebug( __METHOD__ . ": Not adding tracking category $msg to special page!\n" );
+                       return false;
+               }
+
+               // Important to parse with correct title (bug 31469)
+               $cat = wfMessage( $msg )
+                       ->title( $title )
+                       ->inContentLanguage()
+                       ->text();
+
+               # Allow tracking categories to be disabled by setting them to "-"
+               if ( $cat === '-' ) {
+                       return false;
+               }
+
+               $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
+               if ( $containerCategory ) {
+                       $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?: '' );
+                       return true;
+               } else {
+                       wfDebug( __METHOD__ . ": [[MediaWiki:$msg]] is not a valid title!\n" );
+                       return false;
+               }
+       }
+
        /**
         * Override the title to be used for display
         * -- this is assumed to have been validated