(bug 47070) check content model namespace on import.
[lhc/web/wiklou.git] / includes / job / Job.php
1 <?php
2 /**
3 * Job queue base 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 * @defgroup JobQueue JobQueue
22 */
23
24 /**
25 * Class to both describe a background job and handle jobs.
26 * The queue aspects of this class are now deprecated.
27 *
28 * @ingroup JobQueue
29 */
30 abstract class Job {
31 /** @var int Job identifier */
32 public $id;
33
34 /** @var string */
35 public $command;
36
37 /** @var array|bool Array of job parameters or false if none */
38 public $params;
39
40 /** @var array Additional queue metadata */
41 public $metadata = array();
42
43 /** @var Title */
44 protected $title;
45
46 /** @var bool Expensive jobs may set this to true */
47 protected $removeDuplicates;
48
49 /** @var string Text for error that occurred last */
50 protected $error;
51
52 /*-------------------------------------------------------------------------
53 * Abstract functions
54 *------------------------------------------------------------------------*/
55
56 /**
57 * Run the job
58 * @return bool Success
59 */
60 abstract public function run();
61
62 /*-------------------------------------------------------------------------
63 * Static functions
64 *------------------------------------------------------------------------*/
65
66 /**
67 * Create the appropriate object to handle a specific job
68 *
69 * @param string $command Job command
70 * @param Title $title Associated title
71 * @param array|bool $params Job parameters
72 * @param int $id Job identifier
73 * @throws MWException
74 * @return Job
75 */
76 public static function factory( $command, Title $title, $params = false, $id = 0 ) {
77 global $wgJobClasses;
78 if ( isset( $wgJobClasses[$command] ) ) {
79 $class = $wgJobClasses[$command];
80
81 return new $class( $title, $params, $id );
82 }
83 throw new MWException( "Invalid job command `{$command}`" );
84 }
85
86 /**
87 * Batch-insert a group of jobs into the queue.
88 * This will be wrapped in a transaction with a forced commit.
89 *
90 * This may add duplicate at insert time, but they will be
91 * removed later on, when the first one is popped.
92 *
93 * @param array $jobs of Job objects
94 * @return bool
95 * @deprecated since 1.21
96 */
97 public static function batchInsert( $jobs ) {
98 return JobQueueGroup::singleton()->push( $jobs );
99 }
100
101 /**
102 * Insert a group of jobs into the queue.
103 *
104 * Same as batchInsert() but does not commit and can thus
105 * be rolled-back as part of a larger transaction. However,
106 * large batches of jobs can cause slave lag.
107 *
108 * @param array $jobs of Job objects
109 * @return bool
110 * @deprecated since 1.21
111 */
112 public static function safeBatchInsert( $jobs ) {
113 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC );
114 }
115
116 /**
117 * Pop a job of a certain type. This tries less hard than pop() to
118 * actually find a job; it may be adversely affected by concurrent job
119 * runners.
120 *
121 * @param $type string
122 * @return Job|bool Returns false if there are no jobs
123 * @deprecated since 1.21
124 */
125 public static function pop_type( $type ) {
126 return JobQueueGroup::singleton()->get( $type )->pop();
127 }
128
129 /**
130 * Pop a job off the front of the queue.
131 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
132 *
133 * @return Job|bool False if there are no jobs
134 * @deprecated since 1.21
135 */
136 public static function pop() {
137 return JobQueueGroup::singleton()->pop();
138 }
139
140 /*-------------------------------------------------------------------------
141 * Non-static functions
142 *------------------------------------------------------------------------*/
143
144 /**
145 * @param $command
146 * @param $title
147 * @param $params array|bool
148 * @param $id int
149 */
150 public function __construct( $command, $title, $params = false, $id = 0 ) {
151 $this->command = $command;
152 $this->title = $title;
153 $this->params = $params;
154 $this->id = $id;
155
156 // expensive jobs may set this to true
157 $this->removeDuplicates = false;
158 }
159
160 /**
161 * @return int May be 0 for jobs stored outside the DB
162 * @deprecated since 1.22
163 */
164 public function getId() {
165 return $this->id;
166 }
167
168 /**
169 * @return string
170 */
171 public function getType() {
172 return $this->command;
173 }
174
175 /**
176 * @return Title
177 */
178 public function getTitle() {
179 return $this->title;
180 }
181
182 /**
183 * @return array
184 */
185 public function getParams() {
186 return $this->params;
187 }
188
189 /**
190 * @return int|null UNIX timestamp to delay running this job until, otherwise null
191 * @since 1.22
192 */
193 public function getReleaseTimestamp() {
194 return isset( $this->params['jobReleaseTimestamp'] )
195 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
196 : null;
197 }
198
199 /**
200 * @return bool Whether only one of each identical set of jobs should be run
201 */
202 public function ignoreDuplicates() {
203 return $this->removeDuplicates;
204 }
205
206 /**
207 * @return bool Whether this job can be retried on failure by job runners
208 * @since 1.21
209 */
210 public function allowRetries() {
211 return true;
212 }
213
214 /**
215 * Subclasses may need to override this to make duplication detection work.
216 * The resulting map conveys everything that makes the job unique. This is
217 * only checked if ignoreDuplicates() returns true, meaning that duplicate
218 * jobs are supposed to be ignored.
219 *
220 * @return array Map of key/values
221 * @since 1.21
222 */
223 public function getDeduplicationInfo() {
224 $info = array(
225 'type' => $this->getType(),
226 'namespace' => $this->getTitle()->getNamespace(),
227 'title' => $this->getTitle()->getDBkey(),
228 'params' => $this->getParams()
229 );
230 if ( is_array( $info['params'] ) ) {
231 // Identical jobs with different "root" jobs should count as duplicates
232 unset( $info['params']['rootJobSignature'] );
233 unset( $info['params']['rootJobTimestamp'] );
234 // Likewise for jobs with different delay times
235 unset( $info['params']['jobReleaseTimestamp'] );
236 }
237
238 return $info;
239 }
240
241 /**
242 * @see JobQueue::deduplicateRootJob()
243 * @param string $key A key that identifies the task
244 * @return array Map of:
245 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
246 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
247 * @since 1.21
248 */
249 public static function newRootJobParams( $key ) {
250 return array(
251 'rootJobSignature' => sha1( $key ),
252 'rootJobTimestamp' => wfTimestampNow()
253 );
254 }
255
256 /**
257 * @see JobQueue::deduplicateRootJob()
258 * @return array
259 * @since 1.21
260 */
261 public function getRootJobParams() {
262 return array(
263 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
264 ? $this->params['rootJobSignature']
265 : null,
266 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
267 ? $this->params['rootJobTimestamp']
268 : null
269 );
270 }
271
272 /**
273 * @see JobQueue::deduplicateRootJob()
274 * @return bool
275 * @since 1.22
276 */
277 public function hasRootJobParams() {
278 return isset( $this->params['rootJobSignature'] )
279 && isset( $this->params['rootJobTimestamp'] );
280 }
281
282 /**
283 * Insert a single job into the queue.
284 * @return bool true on success
285 * @deprecated since 1.21
286 */
287 public function insert() {
288 return JobQueueGroup::singleton()->push( $this );
289 }
290
291 /**
292 * @return string
293 */
294 public function toString() {
295 $paramString = '';
296 if ( $this->params ) {
297 foreach ( $this->params as $key => $value ) {
298 if ( $paramString != '' ) {
299 $paramString .= ' ';
300 }
301 if ( is_array( $value ) ) {
302 $value = "array(" . count( $value ) . ")";
303 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
304 $value = "object(" . get_class( $value ) . ")";
305 }
306 $value = (string)$value;
307 if ( mb_strlen( $value ) > 1024 ) {
308 $value = "string(" . mb_strlen( $value ) . ")";
309 }
310
311 $paramString .= "$key=$value";
312 }
313 }
314
315 if ( is_object( $this->title ) ) {
316 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
317 if ( $paramString !== '' ) {
318 $s .= ' ' . $paramString;
319 }
320
321 return $s;
322 } else {
323 return "{$this->command} $paramString";
324 }
325 }
326
327 protected function setLastError( $error ) {
328 $this->error = $error;
329 }
330
331 public function getLastError() {
332 return $this->error;
333 }
334 }