Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RebuildMessageGroupStatsJob
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 newJob
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 newRefreshGroupsJob
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Statistics;
5
6use GenericParameterJob;
7use InvalidArgumentException;
8use MediaWiki\Extension\Translate\Jobs\GenericTranslateJob;
9use MediaWiki\Extension\Translate\MessageGroupProcessing\MessageGroups;
10use MediaWiki\MediaWikiServices;
11use MessageGroupStats;
12
13/**
14 * Job for rebuilding message group stats.
15 *
16 * @author Niklas Laxström
17 * @license GPL-2.0-or-later
18 * @ingroup JobQueue
19 */
20class RebuildMessageGroupStatsJob extends GenericTranslateJob implements GenericParameterJob {
21    public const GROUP_ID = 'groupid';
22    public const LANGUAGE_CODE = 'languagecode';
23    public const CLEAR_GROUPS = 'cleargroups';
24    public const REFRESH = 'purge';
25    /** @inheritdoc */
26    protected $removeDuplicates = true;
27
28    public static function newJob( array $params ): self {
29        return new self( $params );
30    }
31
32    /**
33     * Force updating of message group stats for given groups.
34     *
35     * This uses cache for groups not given. If given groups have dependencies such
36     * as an aggregate group and it's subgroup, this attempts to take care of it so
37     * that no duplicate work is done.
38     *
39     * @param string[] $messageGroupIds
40     */
41    public static function newRefreshGroupsJob( array $messageGroupIds ): self {
42        return new self( [ self::CLEAR_GROUPS => $messageGroupIds ] );
43    }
44
45    public function __construct( array $params = [] ) {
46        parent::__construct( 'RebuildMessageGroupStatsJob', $params );
47    }
48
49    public function run(): bool {
50        $lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
51        if ( !$lb->waitForReplication() ) {
52            $this->logWarning( 'Continuing despite replication lag' );
53        }
54
55        $params = $this->params;
56        $flags = 0;
57
58        // Sanity check that this is run via JobQueue. Immediate writes are only safe when they
59        // are run in isolation, e.g. as a separate job in the JobQueue.
60        if ( defined( 'MEDIAWIKI_JOB_RUNNER' ) ) {
61            $flags |= MessageGroupStats::FLAG_IMMEDIATE_WRITES;
62        }
63
64        // This is to make sure the priority value is not read from the process cache.
65        // There is still a possibility that, due to replication lag, an old value is read.
66        MessageGroups::singleton()->clearProcessCache();
67
68        if ( isset( $params[self::REFRESH] ) && $params[self::REFRESH] ) {
69            $flags |= MessageGroupStats::FLAG_NO_CACHE;
70        }
71
72        if ( isset( $params[self::GROUP_ID] ) && isset( $params[self::LANGUAGE_CODE] ) ) {
73            MessageGroupStats::forItem( $params[self::GROUP_ID], $params[self::LANGUAGE_CODE], $flags );
74        } elseif ( isset( $params[self::GROUP_ID] ) ) {
75            MessageGroupStats::forGroup( $params[self::GROUP_ID], $flags );
76        } elseif ( isset( $params[self::CLEAR_GROUPS] ) ) {
77            MessageGroupStats::clearGroup( $params[self::CLEAR_GROUPS], $flags );
78        } elseif ( isset( $params[self::LANGUAGE_CODE] ) ) {
79            MessageGroupStats::forLanguage( $params[self::LANGUAGE_CODE], $flags );
80        } else {
81            throw new InvalidArgumentException( 'No groupid or languagecode or cleargroup provided' );
82        }
83
84        return true;
85    }
86}