summaryrefslogtreecommitdiff
blob: e813d614561ec1b1b2fc317b9acbf7d85d12a80a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
 * Contains class with job for rebuilding message group stats.
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Job for rebuilding message group stats.
 *
 * @ingroup JobQueue
 */
class MessageGroupStatsRebuildJob extends Job {
	/**
	 * @param array $params
	 * @return self
	 */
	public static function newJob( $params ) {
		$job = new self( Title::newMainPage(), $params );
		return $job;
	}

	/**
	 * Force updating of message group stats for given groups.
	 *
	 * This uses cache for groups not given. If given groups have dependencies such
	 * as an aggregate group and it's subgroup, this attempts to take care of it so
	 * that no duplicate work is done.
	 *
	 * @param string[] $messageGroupIds
	 * @return self
	 */
	public static function newRefreshGroupsJob( array $messageGroupIds ) {
		return new self( Title::newMainPage(), [ 'cleargroups' => $messageGroupIds ] );
	}

	/**
	 * @param Title $title
	 * @param array $params
	 */
	public function __construct( $title, $params = [] ) {
		parent::__construct( __CLASS__, $title, $params );
	}

	public function run() {
		$params = $this->params;
		$flags = 0;

		// Sanity check that this is run via JobQueue. Immediate writes are only safe when they
		// are run in isolation, e.g. as a separate job in the JobQueue.
		if ( defined( 'MEDIAWIKI_JOB_RUNNER' ) ) {
			$flags |= MessageGroupStats::FLAG_IMMEDIATE_WRITES;
		}

		// This is to make sure the priority value is not read from the process cache.
		// There is still a possibility that, due to replication lag, an old value is read.
		MessageGroups::singleton()->clearProcessCache();

		if ( isset( $params[ 'purge' ] ) && $params[ 'purge' ] ) {
			$flags |= MessageGroupStats::FLAG_NO_CACHE;
		}

		if ( isset( $params[ 'groupid' ] ) ) {
			MessageGroupStats::forGroup( $params[ 'groupid' ], $flags );
		} elseif ( isset( $params[ 'cleargroups' ] ) ) {
			// clearGroup takes an array of group ids, but no flags
			MessageGroupStats::clearGroup( $params[ 'cleargroups' ] );
		} elseif ( isset( $params[ 'languagecode' ] ) ) {
			MessageGroupStats::forLanguage( $params[ 'languagecode' ], $flags );
		} else {
			throw new InvalidArgumentException( 'No groupid or languagecode or cleargroup provided' );
		}

		return true;
	}
}