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