Remove assignment in conditon in phpunit.php
[mediawiki.git] / includes / job / Job.php
blob5fc1e0644ccc1077dce8e4fdf00c19f4b73639d4
1 <?php
2 /**
3 * Job queue task base code.
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.
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.
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
20 * @file
21 * @defgroup JobQueue JobQueue
24 /**
25 * Class to both describe a background job and handle jobs.
26 * The queue aspects of this class are now deprecated.
27 * Using the class to push jobs onto queues is deprecated (use JobSpecification).
29 * @ingroup JobQueue
31 abstract class Job implements IJobSpecification {
32 /** @var string */
33 public $command;
35 /** @var array|bool Array of job parameters or false if none */
36 public $params;
38 /** @var array Additional queue metadata */
39 public $metadata = array();
41 /** @var Title */
42 protected $title;
44 /** @var bool Expensive jobs may set this to true */
45 protected $removeDuplicates;
47 /** @var string Text for error that occurred last */
48 protected $error;
50 /*-------------------------------------------------------------------------
51 * Abstract functions
52 *------------------------------------------------------------------------*/
54 /**
55 * Run the job
56 * @return bool Success
58 abstract public function run();
60 /*-------------------------------------------------------------------------
61 * Static functions
62 *------------------------------------------------------------------------*/
64 /**
65 * Create the appropriate object to handle a specific job
67 * @param string $command Job command
68 * @param Title $title Associated title
69 * @param array|bool $params Job parameters
70 * @throws MWException
71 * @return Job
73 public static function factory( $command, Title $title, $params = false ) {
74 global $wgJobClasses;
75 if ( isset( $wgJobClasses[$command] ) ) {
76 $class = $wgJobClasses[$command];
78 return new $class( $title, $params );
80 throw new MWException( "Invalid job command `{$command}`" );
83 /**
84 * Batch-insert a group of jobs into the queue.
85 * This will be wrapped in a transaction with a forced commit.
87 * This may add duplicate at insert time, but they will be
88 * removed later on, when the first one is popped.
90 * @param array $jobs of Job objects
91 * @return bool
92 * @deprecated since 1.21
94 public static function batchInsert( $jobs ) {
95 return JobQueueGroup::singleton()->push( $jobs );
98 /**
99 * Insert a group of jobs into the queue.
101 * Same as batchInsert() but does not commit and can thus
102 * be rolled-back as part of a larger transaction. However,
103 * large batches of jobs can cause slave lag.
105 * @param array $jobs of Job objects
106 * @return bool
107 * @deprecated since 1.21
109 public static function safeBatchInsert( $jobs ) {
110 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC );
114 * Pop a job of a certain type. This tries less hard than pop() to
115 * actually find a job; it may be adversely affected by concurrent job
116 * runners.
118 * @param $type string
119 * @return Job|bool Returns false if there are no jobs
120 * @deprecated since 1.21
122 public static function pop_type( $type ) {
123 return JobQueueGroup::singleton()->get( $type )->pop();
127 * Pop a job off the front of the queue.
128 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
130 * @return Job|bool False if there are no jobs
131 * @deprecated since 1.21
133 public static function pop() {
134 return JobQueueGroup::singleton()->pop();
137 /*-------------------------------------------------------------------------
138 * Non-static functions
139 *------------------------------------------------------------------------*/
142 * @param $command
143 * @param $title
144 * @param $params array|bool
146 public function __construct( $command, $title, $params = false ) {
147 $this->command = $command;
148 $this->title = $title;
149 $this->params = $params;
151 // expensive jobs may set this to true
152 $this->removeDuplicates = false;
156 * @return string
158 public function getType() {
159 return $this->command;
163 * @return Title
165 public function getTitle() {
166 return $this->title;
170 * @return array
172 public function getParams() {
173 return $this->params;
177 * @return int|null UNIX timestamp to delay running this job until, otherwise null
178 * @since 1.22
180 public function getReleaseTimestamp() {
181 return isset( $this->params['jobReleaseTimestamp'] )
182 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
183 : null;
187 * @return bool Whether only one of each identical set of jobs should be run
189 public function ignoreDuplicates() {
190 return $this->removeDuplicates;
194 * @return bool Whether this job can be retried on failure by job runners
195 * @since 1.21
197 public function allowRetries() {
198 return true;
202 * @return integer Number of actually "work items" handled in this job
203 * @see $wgJobBackoffThrottling
204 * @since 1.23
206 public function workItemCount() {
207 return 1;
211 * Subclasses may need to override this to make duplication detection work.
212 * The resulting map conveys everything that makes the job unique. This is
213 * only checked if ignoreDuplicates() returns true, meaning that duplicate
214 * jobs are supposed to be ignored.
216 * @return array Map of key/values
217 * @since 1.21
219 public function getDeduplicationInfo() {
220 $info = array(
221 'type' => $this->getType(),
222 'namespace' => $this->getTitle()->getNamespace(),
223 'title' => $this->getTitle()->getDBkey(),
224 'params' => $this->getParams()
226 if ( is_array( $info['params'] ) ) {
227 // Identical jobs with different "root" jobs should count as duplicates
228 unset( $info['params']['rootJobSignature'] );
229 unset( $info['params']['rootJobTimestamp'] );
230 // Likewise for jobs with different delay times
231 unset( $info['params']['jobReleaseTimestamp'] );
234 return $info;
238 * @see JobQueue::deduplicateRootJob()
239 * @param string $key A key that identifies the task
240 * @return array Map of:
241 * - rootJobSignature : hash (e.g. SHA1) that identifies the task
242 * - rootJobTimestamp : TS_MW timestamp of this instance of the task
243 * @since 1.21
245 public static function newRootJobParams( $key ) {
246 return array(
247 'rootJobSignature' => sha1( $key ),
248 'rootJobTimestamp' => wfTimestampNow()
253 * @see JobQueue::deduplicateRootJob()
254 * @return array
255 * @since 1.21
257 public function getRootJobParams() {
258 return array(
259 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
260 ? $this->params['rootJobSignature']
261 : null,
262 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
263 ? $this->params['rootJobTimestamp']
264 : null
269 * @see JobQueue::deduplicateRootJob()
270 * @return bool
271 * @since 1.22
273 public function hasRootJobParams() {
274 return isset( $this->params['rootJobSignature'] )
275 && isset( $this->params['rootJobTimestamp'] );
279 * Insert a single job into the queue.
280 * @return bool true on success
281 * @deprecated since 1.21
283 public function insert() {
284 return JobQueueGroup::singleton()->push( $this );
288 * @return string
290 public function toString() {
291 $paramString = '';
292 if ( $this->params ) {
293 foreach ( $this->params as $key => $value ) {
294 if ( $paramString != '' ) {
295 $paramString .= ' ';
297 if ( is_array( $value ) ) {
298 $value = "array(" . count( $value ) . ")";
299 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
300 $value = "object(" . get_class( $value ) . ")";
302 $value = (string)$value;
303 if ( mb_strlen( $value ) > 1024 ) {
304 $value = "string(" . mb_strlen( $value ) . ")";
307 $paramString .= "$key=$value";
311 if ( is_object( $this->title ) ) {
312 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
313 if ( $paramString !== '' ) {
314 $s .= ' ' . $paramString;
317 return $s;
318 } else {
319 return "{$this->command} $paramString";
323 protected function setLastError( $error ) {
324 $this->error = $error;
327 public function getLastError() {
328 return $this->error;