Merge "[search] Fix method call on null value"
[lhc/web/wiklou.git] / includes / deferred / LinksUpdate.php
index d996870..9a24b96 100644 (file)
@@ -61,6 +61,12 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
        /** @var bool Whether to queue jobs for recursive updates */
        public $mRecursive;
 
+       /** @var bool Whether this job was triggered by a recursive update job */
+       private $mTriggeredRecursive;
+
+       /** @var Revision Revision for which this update has been triggered */
+       private $mRevision;
+
        /**
         * @var null|array Added links if calculated.
         */
@@ -71,6 +77,11 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
         */
        private $linkDeletions = null;
 
+       /**
+        * @var User|null
+        */
+       private $user;
+
        /**
         * Constructor
         *
@@ -79,25 +90,16 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
         * @param bool $recursive Queue jobs for recursive updates?
         * @throws MWException
         */
-       function __construct( $title, $parserOutput, $recursive = true ) {
+       function __construct( Title $title, ParserOutput $parserOutput, $recursive = true ) {
                parent::__construct( false ); // no implicit transaction
 
-               if ( !( $title instanceof Title ) ) {
-                       throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
-                               "Please see Article::editUpdates() for an invocation example.\n" );
-               }
-
-               if ( !( $parserOutput instanceof ParserOutput ) ) {
-                       throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
-                               "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
-               }
-
                $this->mTitle = $title;
                $this->mId = $title->getArticleID();
 
                if ( !$this->mId ) {
-                       throw new MWException( "The Title object did not provide an article " .
-                               "ID. Perhaps the page doesn't exist?" );
+                       throw new InvalidArgumentException(
+                               "The Title object yields no ID. Perhaps the page doesn't exist?"
+                       );
                }
 
                $this->mParserOutput = $parserOutput;
@@ -147,6 +149,7 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
        }
 
        protected function doIncrementalUpdate() {
+               global $wgRCWatchCategoryMembership;
 
                # Page links
                $existing = $this->getExistingLinks();
@@ -199,6 +202,14 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
                $this->invalidateCategories( $categoryUpdates );
                $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
 
+               # Category membership changes
+               if (
+                       $wgRCWatchCategoryMembership &&
+                       !$this->mTriggeredRecursive && ( $categoryInserts || $categoryDeletes )
+               ) {
+                       $this->triggerCategoryChanges( $categoryInserts, $categoryDeletes );
+               }
+
                # Page properties
                $existing = $this->getExistingProperties();
 
@@ -222,6 +233,24 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
 
        }
 
+       private function triggerCategoryChanges( $categoryInserts, $categoryDeletes ) {
+               $catMembChange = new CategoryMembershipChange( $this->mTitle, $this->mRevision );
+
+               if ( $this->mRecursive ) {
+                       $catMembChange->checkTemplateLinks();
+               }
+
+               foreach ( $categoryInserts as $categoryName => $value ) {
+                       $categoryTitle = Title::newFromText( $categoryName, NS_CATEGORY );
+                       $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
+               }
+
+               foreach ( $categoryDeletes as $categoryName => $value ) {
+                       $categoryTitle = Title::newFromText( $categoryName, NS_CATEGORY );
+                       $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
+               }
+       }
+
        /**
         * Queue recursive jobs for this page
         *
@@ -243,7 +272,7 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
                // Which ever runs first generally no-ops the other one.
                $jobs = array();
                foreach ( $bc->getCascadeProtectedLinks() as $title ) {
-                       $jobs[] = new RefreshLinksJob( $title, array( 'prioritize' => true ) );
+                       $jobs[] = RefreshLinksJob::newPrioritized( $title, array() );
                }
                JobQueueGroup::singleton()->push( $jobs );
        }
@@ -863,6 +892,44 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
                return $this->mImages;
        }
 
+       /**
+        * Set this object as being triggered by a recursive LinksUpdate
+        *
+        * @since 1.27
+        */
+       public function setTriggeredRecursive() {
+               $this->mTriggeredRecursive = true;
+       }
+
+       /**
+        * Set the revision corresponding to this LinksUpdate
+        *
+        * @since 1.27
+        *
+        * @param Revision $revision
+        */
+       public function setRevision( Revision $revision ) {
+               $this->mRevision = $revision;
+       }
+
+       /**
+        * Set the User who triggered this LinksUpdate
+        *
+        * @since 1.27
+        * @param User $user
+        */
+       public function setTriggeringUser( User $user ) {
+               $this->user = $user;
+       }
+
+       /**
+        * @since 1.27
+        * @return null|User
+        */
+       public function getTriggeringUser() {
+               return $this->user;
+       }
+
        /**
         * Invalidate any necessary link lists related to page property changes
         * @param array $changed
@@ -936,11 +1003,24 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
        }
 
        public function getAsJobSpecification() {
+               if ( $this->user ) {
+                       $userInfo = array(
+                               'userId' => $this->user->getId(),
+                               'userName' => $this->user->getName(),
+                       );
+               } else {
+                       $userInfo = false;
+               }
                return array(
                        'wiki' => $this->mDb->getWikiID(),
                        'job'  => new JobSpecification(
-                               'refreshLinks',
-                               array( 'prioritize' => true ),
+                               'refreshLinksPrioritized',
+                               array(
+                                       // Reuse the parser cache if it was saved
+                                       'rootJobTimestamp' => $this->mParserOutput->getCacheTime(),
+                                       'useRecursiveLinksUpdate' => $this->mRecursive,
+                                       'triggeringUser' => $userInfo,
+                               ),
                                array( 'removeDuplicates' => true ),
                                $this->getTitle()
                        )