Manybubbles has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92643


Change subject: Use a job to handle link updates.
......................................................................

Use a job to handle link updates.

This job will immediately execute itself if it is built in the CLI.
In a web process it'll be queued for later execution.

Bug: 55848
Change-Id: I7bad34fef368a3177c7658d066092c86efec24ad
---
M CirrusSearch.php
A includes/CirrusSearchLinksUpdateJob.php
M includes/CirrusSearchUpdater.php
3 files changed, 72 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/43/92643/1

diff --git a/CirrusSearch.php b/CirrusSearch.php
index 69d4aa0..b393ef3 100644
--- a/CirrusSearch.php
+++ b/CirrusSearch.php
@@ -130,6 +130,7 @@
 $wgAutoloadClasses['CirrusSearch'] = $includes . 'CirrusSearch.body.php';
 $wgAutoloadClasses['CirrusSearchAnalysisConfigBuilder'] = $includes . 
'CirrusSearchAnalysisConfigBuilder.php';
 $wgAutoloadClasses['CirrusSearchConnection'] = $includes . 
'CirrusSearchConnection.php';
+$wgAutoloadClasses['CirrusSearchLinksUpdateJob'] = $includes . 
'CirrusSearchLinksUpdateJob.php';
 $wgAutoloadClasses['CirrusSearchFullTextResultsType'] = $includes . 
'CirrusSearchResultsType.php';
 $wgAutoloadClasses['CirrusSearchMappingConfigBuilder'] = $includes . 
'CirrusSearchMappingConfigBuilder.php';
 $wgAutoloadClasses['CirrusSearchReindexForkController'] = $includes . 
'CirrusSearchReindexForkController.php';
@@ -144,7 +145,7 @@
 /**
  * Hooks
  */
-$wgHooks['LinksUpdateComplete'][] = 
'CirrusSearchUpdater::linksUpdateCompletedHook';
+$wgHooks['LinksUpdateComplete'][] = 
'CirrusSearchLinksUpdateJob::linksUpdateCompletedHook';
 // Install our prefix search hook only if we're enabled.
 $wgExtensionFunctions[] = function() {
        global $wgSearchType, $wgHooks;
@@ -157,3 +158,8 @@
  * i18n
  */
 $wgExtensionMessagesFiles['CirrusSearch'] = __DIR__ . '/CirrusSearch.i18n.php';
+
+/**
+ * Jobs
+ */
+$wgJobClasses[ 'cirrusSearchLinksUpdate' ] = 'CirrusSearchLinksUpdateJob';
\ No newline at end of file
diff --git a/includes/CirrusSearchLinksUpdateJob.php 
b/includes/CirrusSearchLinksUpdateJob.php
new file mode 100644
index 0000000..8e898f6
--- /dev/null
+++ b/includes/CirrusSearchLinksUpdateJob.php
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Hooks linksUpdateCompleted to update pages either in process or by queueing
+ * this job.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+class CirrusSearchLinksUpdateJob extends Job {
+       /**
+        * Hooked to update the search index for pages when templates that they 
include are changed
+        * and to kick off updating linked articles.
+        * @param $linksUpdate LinksUpdate
+        */
+       public static function linksUpdateCompletedHook( $linksUpdate ) {
+               $inCli = PHP_SAPI == 'cli';
+               // In the web process the title update will be taken care of by 
the SearchEngine infrastructure
+               // so no need to update it in the job.
+               $titleNeedsUpdate = $inCli;
+               $addedLinks = $linksUpdate->getAddedLinks();
+               $removedLinks = $linksUpdate->getRemovedLinks();
+               if ( !$titleNeedsUpdate && count( $addedLinks ) === 0 && count( 
$removedLinks ) === 0 ) {
+                       // Nothing to do
+                       return;
+               }
+               $job = new CirrusSearchLinksUpdateJob( 
$linksUpdate->getTitle(), array(
+                       'addedLinks' => $addedLinks,
+                       'removedLinks' => $removedLinks,
+                       'titleNeedsUpdate' => $titleNeedsUpdate,
+               ) );
+               if ( $inCli ) {
+                       // We are already on the cli (probably already running 
jobs) then there is no reason
+                       // to fork this job from the hook - just run them right 
now.
+                       $job->run();
+               } else {
+                       // We're in a web process so we should push this job on 
the queue and get to it later
+                       JobQueueGroup::singleton()->push( $job );
+               }
+       }
+
+       public function __construct( $title, $params, $id = 0 ) {
+               parent::__construct( 'cirrusSearchLinksUpdate', $title, 
$params, $id );
+       }
+
+       public function run() {
+               if ( $this->params[ 'titleNeedsUpdate' ] ) {
+                       CirrusSearchUpdater::updateFromTitle( $this->title );
+               }
+               CirrusSearchUpdater::updateLinkedArticles( $this->params[ 
'addedLinks' ],
+                       $this->params[ 'removedLinks' ] );
+       }
+}
diff --git a/includes/CirrusSearchUpdater.php b/includes/CirrusSearchUpdater.php
index cca0db8..e522761 100644
--- a/includes/CirrusSearchUpdater.php
+++ b/includes/CirrusSearchUpdater.php
@@ -88,16 +88,6 @@
        }
 
        /**
-        * Hooked to update the search index for pages when templates that they 
include are changed
-        * and to kick off updating linked articles.
-        * @param $linksUpdate LinksUpdate
-        */
-       public static function linksUpdateCompletedHook( $linksUpdate ) {
-               self::updateFromTitle( $linksUpdate->getTitle() );
-               self::updateLinkedArticles( $linksUpdate );
-       }
-
-       /**
         * This updates pages in elasticsearch.
         *
         * @param array $pageData An array of pages. The format is as follows:
@@ -324,7 +314,7 @@
         * Update the search index for articles linked from this article.  Just 
updates link counts.
         * @param $linksUpdate LinksUpdate
         */
-       private static function updateLinkedArticles( $linksUpdate ) {
+       public static function updateLinkedArticles( $linksUpdate ) {
                global $wgCirrusSearchLinkedArticlesToUpdate, 
$wgCirrusSearchUnlinkedArticlesToUpdate;
 
                $titles = array_merge(

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7bad34fef368a3177c7658d066092c86efec24ad
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <[email protected]>

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

Reply via email to