Revert "Update WikiData extensions to correct points for 1.21wmf11"
[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 /**
32 * @var Title
33 */
34 var $title;
35
36 var $command,
37 $params,
38 $id,
39 $removeDuplicates,
40 $error;
41
42 /** @var Array Additional queue metadata */
43 public $metadata = array();
44
45 /*-------------------------------------------------------------------------
46 * Abstract functions
47 *------------------------------------------------------------------------*/
48
49 /**
50 * Run the job
51 * @return boolean success
52 */
53 abstract public function run();
54
55 /*-------------------------------------------------------------------------
56 * Static functions
57 *------------------------------------------------------------------------*/
58
59 /**
60 * Create the appropriate object to handle a specific job
61 *
62 * @param $command String: Job command
63 * @param $title Title: Associated title
64 * @param $params Array|bool: Job parameters
65 * @param $id Int: Job identifier
66 * @throws MWException
67 * @return Job
68 */
69 public static function factory( $command, Title $title, $params = false, $id = 0 ) {
70 global $wgJobClasses;
71 if( isset( $wgJobClasses[$command] ) ) {
72 $class = $wgJobClasses[$command];
73 return new $class( $title, $params, $id );
74 }
75 throw new MWException( "Invalid job command `{$command}`" );
76 }
77
78 /**
79 * Batch-insert a group of jobs into the queue.
80 * This will be wrapped in a transaction with a forced commit.
81 *
82 * This may add duplicate at insert time, but they will be
83 * removed later on, when the first one is popped.
84 *
85 * @param $jobs array of Job objects
86 * @return bool
87 * @deprecated 1.21
88 */
89 public static function batchInsert( $jobs ) {
90 return JobQueueGroup::singleton()->push( $jobs );
91 }
92
93 /**
94 * Insert a group of jobs into the queue.
95 *
96 * Same as batchInsert() but does not commit and can thus
97 * be rolled-back as part of a larger transaction. However,
98 * large batches of jobs can cause slave lag.
99 *
100 * @param $jobs array of Job objects
101 * @return bool
102 * @deprecated 1.21
103 */
104 public static function safeBatchInsert( $jobs ) {
105 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QoS_Atomic );
106 }
107
108 /**
109 * Pop a job of a certain type. This tries less hard than pop() to
110 * actually find a job; it may be adversely affected by concurrent job
111 * runners.
112 *
113 * @param $type string
114 * @return Job
115 * @deprecated 1.21
116 */
117 public static function pop_type( $type ) {
118 return JobQueueGroup::singleton()->get( $type )->pop();
119 }
120
121 /**
122 * Pop a job off the front of the queue.
123 * This is subject to $wgJobTypesExcludedFromDefaultQueue.
124 *
125 * @return Job or false if there's no jobs
126 * @deprecated 1.21
127 */
128 public static function pop() {
129 return JobQueueGroup::singleton()->pop();
130 }
131
132 /*-------------------------------------------------------------------------
133 * Non-static functions
134 *------------------------------------------------------------------------*/
135
136 /**
137 * @param $command
138 * @param $title
139 * @param $params array|bool
140 * @param $id int
141 */
142 public function __construct( $command, $title, $params = false, $id = 0 ) {
143 $this->command = $command;
144 $this->title = $title;
145 $this->params = $params;
146 $this->id = $id;
147
148 $this->removeDuplicates = false; // expensive jobs may set this to true
149 }
150
151 /**
152 * @return integer May be 0 for jobs stored outside the DB
153 */
154 public function getId() {
155 return $this->id;
156 }
157
158 /**
159 * @return string
160 */
161 public function getType() {
162 return $this->command;
163 }
164
165 /**
166 * @return Title
167 */
168 public function getTitle() {
169 return $this->title;
170 }
171
172 /**
173 * @return array
174 */
175 public function getParams() {
176 return $this->params;
177 }
178
179 /**
180 * @return bool Whether only one of each identical set of jobs should be run
181 */
182 public function ignoreDuplicates() {
183 return $this->removeDuplicates;
184 }
185
186 /**
187 * @return bool Whether this job can be retried on failure by job runners
188 */
189 public function allowRetries() {
190 return true;
191 }
192
193 /**
194 * Subclasses may need to override this to make duplication detection work
195 *
196 * @return Array Map of key/values
197 */
198 public function getDeduplicationInfo() {
199 $info = array(
200 'type' => $this->getType(),
201 'namespace' => $this->getTitle()->getNamespace(),
202 'title' => $this->getTitle()->getDBkey(),
203 'params' => $this->getParams()
204 );
205 // Identical jobs with different "root" jobs should count as duplicates
206 if ( is_array( $info['params'] ) ) {
207 unset( $info['params']['rootJobSignature'] );
208 unset( $info['params']['rootJobTimestamp'] );
209 }
210 return $info;
211 }
212
213 /**
214 * @param $key string A key that identifies the task
215 * @return Array
216 */
217 public static function newRootJobParams( $key ) {
218 return array(
219 'rootJobSignature' => sha1( $key ),
220 'rootJobTimestamp' => wfTimestampNow()
221 );
222 }
223
224 /**
225 * @return Array
226 */
227 public function getRootJobParams() {
228 return array(
229 'rootJobSignature' => isset( $this->params['rootJobSignature'] )
230 ? $this->params['rootJobSignature']
231 : null,
232 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
233 ? $this->params['rootJobTimestamp']
234 : null
235 );
236 }
237
238 /**
239 * Insert a single job into the queue.
240 * @return bool true on success
241 * @deprecated 1.21
242 */
243 public function insert() {
244 return JobQueueGroup::singleton()->push( $this );
245 }
246
247 /**
248 * @return string
249 */
250 public function toString() {
251 $paramString = '';
252 if ( $this->params ) {
253 foreach ( $this->params as $key => $value ) {
254 if ( $paramString != '' ) {
255 $paramString .= ' ';
256 }
257 if ( is_array( $value ) ) {
258 $value = "array(" . count( $value ) . ")";
259 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
260 $value = "object(" . get_class( $value ) . ")";
261 }
262 $value = (string)$value;
263 if ( mb_strlen( $value ) > 1024 ) {
264 $value = "string(" . mb_strlen( $value ) . ")";
265 }
266
267 $paramString .= "$key=$value";
268 }
269 }
270
271 if ( is_object( $this->title ) ) {
272 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
273 if ( $paramString !== '' ) {
274 $s .= ' ' . $paramString;
275 }
276 return $s;
277 } else {
278 return "{$this->command} $paramString";
279 }
280 }
281
282 protected function setLastError( $error ) {
283 $this->error = $error;
284 }
285
286 public function getLastError() {
287 return $this->error;
288 }
289 }