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