Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.35% covered (danger)
4.35%
1 / 23
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MassIndex
4.35% covered (danger)
4.35%
1 / 23
33.33% covered (danger)
33.33%
1 / 3
37.51
0.00% covered (danger)
0.00%
0 / 1
 build
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 doJob
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 workItemCount
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Job;
4
5use CirrusSearch\Updater;
6use MediaWiki\Logger\LoggerFactory;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Page\WikiPage;
9use MediaWiki\Title\Title;
10
11/**
12 * Job wrapper around Updater::updatePages.  Used by forceSearchIndex.php
13 * when in job queueing mode.
14 *
15 * @license GPL-2.0-or-later
16 */
17class MassIndex extends CirrusGenericJob {
18    /**
19     * @param WikiPage[] $pages
20     * @param int $updateFlags
21     * @param string|null $cluster
22     * @return self
23     */
24    public static function build( array $pages, $updateFlags, $cluster = null ) {
25        // Strip $pages down to PrefixedDBKeys so we don't put a ton of stuff in the job queue.
26        $pageDBKeys = [];
27        foreach ( $pages as $page ) {
28            $pageDBKeys[] = $page->getTitle()->getPrefixedDBkey();
29        }
30
31        // We don't have a "title" for this job so we use the Main Page because it exists.
32        return new self( [
33            'pageDBKeys' => $pageDBKeys,
34            'updateFlags' => $updateFlags,
35            'cluster' => $cluster,
36        ] );
37    }
38
39    /**
40     * @return bool
41     */
42    protected function doJob() {
43        // Reload pages from pageIds to throw into the updater
44        $pageData = [];
45        $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
46        foreach ( $this->params[ 'pageDBKeys' ] as $pageDBKey ) {
47            $title = Title::newFromDBkey( $pageDBKey );
48            // Skip any titles with broken keys.  We can't do anything with them.
49            if ( !$title ) {
50                LoggerFactory::getInstance( 'CirrusSearch' )->warning(
51                    "Skipping invalid DBKey: {pageDBKey}",
52                    [ 'pageDBKey' => $pageDBKey ]
53                );
54                continue;
55            }
56            $pageData[] = $wikiPageFactory->newFromTitle( $title );
57        }
58        // Now invoke the updater!
59        $updater = Updater::build( $this->searchConfig, $this->params['cluster'] ?? null );
60        $updater->updatePages( $pageData, $this->params[ 'updateFlags' ] );
61        // retries are handled in a separate queue
62        return true;
63    }
64
65    /**
66     * @return int
67     */
68    public function workItemCount() {
69        return count( $this->params[ 'pageDBKeys' ] );
70    }
71}