Merge "Show a warning in edit preview when a template loop is detected"
[lhc/web/wiklou.git] / includes / jobqueue / aggregator / JobQueueAggregator.php
1 <?php
2 /**
3 * Job queue aggregator code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Class to handle tracking information about all queues
25 *
26 * @ingroup JobQueue
27 * @since 1.21
28 */
29 abstract class JobQueueAggregator {
30 /** @var JobQueueAggregator */
31 protected static $instance = null;
32
33 /**
34 * @param array $params
35 */
36 public function __construct( array $params ) {
37 }
38
39 /**
40 * @throws MWException
41 * @return JobQueueAggregator
42 */
43 final public static function singleton() {
44 global $wgJobQueueAggregator;
45
46 if ( !isset( self::$instance ) ) {
47 $class = $wgJobQueueAggregator['class'];
48 $obj = new $class( $wgJobQueueAggregator );
49 if ( !( $obj instanceof JobQueueAggregator ) ) {
50 throw new MWException( "Class '$class' is not a JobQueueAggregator class." );
51 }
52 self::$instance = $obj;
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * Destroy the singleton instance
60 *
61 * @return void
62 */
63 final public static function destroySingleton() {
64 self::$instance = null;
65 }
66
67 /**
68 * Mark a queue as being empty
69 *
70 * @param string $wiki
71 * @param string $type
72 * @return bool Success
73 */
74 final public function notifyQueueEmpty( $wiki, $type ) {
75 $ok = $this->doNotifyQueueEmpty( $wiki, $type );
76
77 return $ok;
78 }
79
80 /**
81 * @see JobQueueAggregator::notifyQueueEmpty()
82 * @param string $wiki
83 * @param string $type
84 * @return bool
85 */
86 abstract protected function doNotifyQueueEmpty( $wiki, $type );
87
88 /**
89 * Mark a queue as being non-empty
90 *
91 * @param string $wiki
92 * @param string $type
93 * @return bool Success
94 */
95 final public function notifyQueueNonEmpty( $wiki, $type ) {
96 $ok = $this->doNotifyQueueNonEmpty( $wiki, $type );
97
98 return $ok;
99 }
100
101 /**
102 * @see JobQueueAggregator::notifyQueueNonEmpty()
103 * @param string $wiki
104 * @param string $type
105 * @return bool
106 */
107 abstract protected function doNotifyQueueNonEmpty( $wiki, $type );
108
109 /**
110 * Get the list of all of the queues with jobs
111 *
112 * @return array (job type => (list of wiki IDs))
113 */
114 final public function getAllReadyWikiQueues() {
115 $res = $this->doGetAllReadyWikiQueues();
116
117 return $res;
118 }
119
120 /**
121 * @see JobQueueAggregator::getAllReadyWikiQueues()
122 */
123 abstract protected function doGetAllReadyWikiQueues();
124
125 /**
126 * Purge all of the aggregator information
127 *
128 * @return bool Success
129 */
130 final public function purge() {
131 $res = $this->doPurge();
132
133 return $res;
134 }
135
136 /**
137 * @see JobQueueAggregator::purge()
138 */
139 abstract protected function doPurge();
140
141 /**
142 * Get all databases that have a pending job.
143 * This poll all the queues and is this expensive.
144 *
145 * @return array (job type => (list of wiki IDs))
146 */
147 protected function findPendingWikiQueues() {
148 global $wgLocalDatabases;
149
150 $pendingDBs = []; // (job type => (db list))
151 foreach ( $wgLocalDatabases as $db ) {
152 foreach ( JobQueueGroup::singleton( $db )->getQueuesWithJobs() as $type ) {
153 $pendingDBs[$type][] = $db;
154 }
155 }
156
157 return $pendingDBs;
158 }
159 }
160
161 class JobQueueAggregatorNull extends JobQueueAggregator {
162 protected function doNotifyQueueEmpty( $wiki, $type ) {
163 return true;
164 }
165
166 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
167 return true;
168 }
169
170 protected function doGetAllReadyWikiQueues() {
171 return [];
172 }
173
174 protected function doPurge() {
175 return true;
176 }
177 }