Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CirrusTitleJob
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getSearchConfig
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 buildRootEventParams
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace CirrusSearch\Job;
4
5use CirrusSearch\CirrusSearch;
6use CirrusSearch\SearchConfig;
7use MediaWiki\JobQueue\Job;
8use MediaWiki\MediaWikiServices;
9use MediaWiki\Revision\RevisionRecord;
10use MediaWiki\Title\Title;
11use MediaWiki\Utils\MWTimestamp;
12
13/**
14 * CirrusSearch Job that is bound to a Title
15 *
16 * @license GPL-2.0-or-later
17 */
18abstract class CirrusTitleJob extends Job {
19    use JobTraits;
20
21    // TODO: move these constants to JobTraits once we support php 8.2
22    public const UPDATE_KIND = 'update_kind';
23    public const ROOT_EVENT_TIME = 'root_event_time';
24    /** a change made to the page, new revision/delete/restore */
25    public const PAGE_CHANGE = 'page_change';
26    /** a change that could possibly change the rendered output of the page */
27    public const PAGE_REFRESH = 'page_refresh';
28    /** a change emitted when detecting a visibility change on a past revision
29     * theoretically not needed but is being triggered out of caution as generally visibility changes
30     * occur to hide harmful content.
31     */
32    public const VISIBILITY_CHANGE = "visibility_change";
33    /**
34     * Change issued from the saneitizer, either a fixup or a forced update
35     */
36    public const SANEITIZER = "saneitizer";
37
38    /** param key to store the target elastic cluster */
39    public const CLUSTER = 'cluster';
40
41    /**
42     * @var SearchConfig|null (lazy loaded by getSearchConfig())
43     */
44    private $searchConfig;
45
46    /**
47     * @param Title $title
48     * @param array $params
49     */
50    public function __construct( $title, $params ) {
51        $params += [
52            self::CLUSTER => null,
53            self::UPDATE_KIND => 'unknown',
54            self::ROOT_EVENT_TIME => null
55        ];
56        // eg: DeletePages -> cirrusSearchDeletePages
57        $jobName = self::buildJobName( static::class );
58
59        parent::__construct( $jobName, $title, $params );
60
61        // All CirrusSearch jobs are reasonably expensive.  Most involve parsing and it
62        // is ok to remove duplicate _unclaimed_ cirrus jobs.  Once a cirrus job is claimed
63        // it can't be deduplicated or else the search index will end up with out of date
64        // data.  Luckily, this is how the JobQueue implementations work.
65        $this->removeDuplicates = true;
66    }
67
68    public function getSearchConfig(): SearchConfig {
69        if ( $this->searchConfig === null ) {
70            // @phan-suppress-next-line PhanTypeMismatchProperty
71            $this->searchConfig = MediaWikiServices::getInstance()
72                ->getConfigFactory()
73                ->makeConfig( CirrusSearch::NAME );
74        }
75        return $this->searchConfig;
76    }
77
78    /**
79     * Assemble the UPDATE_KIND + ROOT_EVENT_TIME params shared by the page-update
80     * factory methods of the concrete jobs (e.g. LinksUpdate, UpdateRedirectDocument).
81     *
82     * ROOT_EVENT_TIME is seeded from the driving revision's timestamp when one is
83     * available, falling back to the current time (e.g. for deletes or for refreshes
84     * not tied to a specific revision).
85     *
86     * @param string $updateKind one of the *_CHANGE / *_REFRESH / SANEITIZER constants
87     * @param RevisionRecord|null $revisionRecord revision driving the update, if any
88     * @return array params to merge into the job's param array
89     */
90    protected static function buildRootEventParams( string $updateKind, ?RevisionRecord $revisionRecord = null ): array {
91        if ( $revisionRecord !== null && $revisionRecord->getTimestamp() !== null ) {
92            $ts = (int)MWTimestamp::convert( TS_UNIX, $revisionRecord->getTimestamp() );
93        } else {
94            $ts = MWTimestamp::time();
95        }
96        return [
97            self::UPDATE_KIND => $updateKind,
98            self::ROOT_EVENT_TIME => $ts,
99        ];
100    }
101}