* (bug 12938) Fix template expansion and 404 returns for action=raw with section
[lhc/web/wiklou.git] / includes / JobQueue.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
5 }
6
7 /**
8 * Class to both describe a background job and handle jobs.
9 */
10 abstract class Job {
11 var $command,
12 $title,
13 $params,
14 $id,
15 $removeDuplicates,
16 $error;
17
18 /*-------------------------------------------------------------------------
19 * Abstract functions
20 *------------------------------------------------------------------------*/
21
22 /**
23 * Run the job
24 * @return boolean success
25 */
26 abstract function run();
27
28 /*-------------------------------------------------------------------------
29 * Static functions
30 *------------------------------------------------------------------------*/
31
32 /**
33 * @deprecated use LinksUpdate::queueRecursiveJobs()
34 */
35 /**
36 * static function queueLinksJobs( $titles ) {}
37 */
38
39 /**
40 * Pop a job of a certain type. This tries less hard than pop() to
41 * actually find a job; it may be adversely affected by concurrent job
42 * runners.
43 */
44 static function pop_type($type) {
45 wfProfilein( __METHOD__ );
46
47 $dbw = wfGetDB( DB_MASTER );
48
49
50 $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__,
51 array( 'LIMIT' => 1 ));
52
53 if ($row === false) {
54 wfProfileOut( __METHOD__ );
55 return false;
56 }
57
58 /* Ensure we "own" this row */
59 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
60 $affected = $dbw->affectedRows();
61
62 if ($affected == 0) {
63 wfProfileOut( __METHOD__ );
64 return false;
65 }
66
67 $namespace = $row->job_namespace;
68 $dbkey = $row->job_title;
69 $title = Title::makeTitleSafe( $namespace, $dbkey );
70 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
71
72 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
73 $dbw->immediateCommit();
74
75 wfProfileOut( __METHOD__ );
76 return $job;
77 }
78
79 /**
80 * Pop a job off the front of the queue
81 * @static
82 * @param $offset Number of jobs to skip
83 * @return Job or false if there's no jobs
84 */
85 static function pop($offset=0) {
86 wfProfileIn( __METHOD__ );
87
88 $dbr = wfGetDB( DB_SLAVE );
89
90 /* Get a job from the slave, start with an offset,
91 scan full set afterwards, avoid hitting purged rows
92
93 NB: If random fetch previously was used, offset
94 will always be ahead of few entries
95 */
96
97 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
98 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
99
100 // Refetching without offset is needed as some of job IDs could have had delayed commits
101 // and have lower IDs than jobs already executed, blame concurrency :)
102 //
103 if ( $row === false) {
104 if ($offset!=0)
105 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
106 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
107
108 if ($row === false ) {
109 wfProfileOut( __METHOD__ );
110 return false;
111 }
112 }
113 $offset = $row->job_id;
114
115 // Try to delete it from the master
116 $dbw = wfGetDB( DB_MASTER );
117 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
118 $affected = $dbw->affectedRows();
119 $dbw->immediateCommit();
120
121 if ( !$affected ) {
122 // Failed, someone else beat us to it
123 // Try getting a random row
124 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
125 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__ );
126 if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
127 // No jobs to get
128 wfProfileOut( __METHOD__ );
129 return false;
130 }
131 // Get the random row
132 $row = $dbw->selectRow( 'job', '*',
133 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
134 if ( $row === false ) {
135 // Random job gone before we got the chance to select it
136 // Give up
137 wfProfileOut( __METHOD__ );
138 return false;
139 }
140 // Delete the random row
141 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
142 $affected = $dbw->affectedRows();
143 $dbw->immediateCommit();
144
145 if ( !$affected ) {
146 // Random job gone before we exclusively deleted it
147 // Give up
148 wfProfileOut( __METHOD__ );
149 return false;
150 }
151 }
152
153 // If execution got to here, there's a row in $row that has been deleted from the database
154 // by this thread. Hence the concurrent pop was successful.
155 $namespace = $row->job_namespace;
156 $dbkey = $row->job_title;
157 $title = Title::makeTitleSafe( $namespace, $dbkey );
158 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
159
160 // Remove any duplicates it may have later in the queue
161 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
162
163 wfProfileOut( __METHOD__ );
164 return $job;
165 }
166
167 /**
168 * Create the appropriate object to handle a specific job
169 *
170 * @param string $command Job command
171 * @param Title $title Associated title
172 * @param array $params Job parameters
173 * @param int $id Job identifier
174 * @return Job
175 */
176 static function factory( $command, $title, $params = false, $id = 0 ) {
177 global $wgJobClasses;
178 if( isset( $wgJobClasses[$command] ) ) {
179 $class = $wgJobClasses[$command];
180 return new $class( $title, $params, $id );
181 }
182 throw new MWException( "Invalid job command `{$command}`" );
183 }
184
185 static function makeBlob( $params ) {
186 if ( $params !== false ) {
187 return serialize( $params );
188 } else {
189 return '';
190 }
191 }
192
193 static function extractBlob( $blob ) {
194 if ( (string)$blob !== '' ) {
195 return unserialize( $blob );
196 } else {
197 return false;
198 }
199 }
200
201 /**
202 * Batch-insert a group of jobs into the queue.
203 * This will be wrapped in a transaction with a forced commit.
204 *
205 * This may add duplicate at insert time, but they will be
206 * removed later on, when the first one is popped.
207 *
208 * @param $jobs array of Job objects
209 */
210 static function batchInsert( $jobs ) {
211 if( count( $jobs ) ) {
212 $dbw = wfGetDB( DB_MASTER );
213 $dbw->begin();
214 foreach( $jobs as $job ) {
215 $rows[] = $job->insertFields();
216 }
217 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
218 $dbw->commit();
219 }
220 }
221
222 /*-------------------------------------------------------------------------
223 * Non-static functions
224 *------------------------------------------------------------------------*/
225
226 function __construct( $command, $title, $params = false, $id = 0 ) {
227 $this->command = $command;
228 $this->title = $title;
229 $this->params = $params;
230 $this->id = $id;
231
232 // A bit of premature generalisation
233 // Oh well, the whole class is premature generalisation really
234 $this->removeDuplicates = true;
235 }
236
237 /**
238 * Insert a single job into the queue.
239 */
240 function insert() {
241 $fields = $this->insertFields();
242
243 $dbw = wfGetDB( DB_MASTER );
244
245 if ( $this->removeDuplicates ) {
246 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
247 if ( $dbw->numRows( $res ) ) {
248 return;
249 }
250 }
251 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
252 $dbw->insert( 'job', $fields, __METHOD__ );
253 }
254
255 protected function insertFields() {
256 return array(
257 'job_cmd' => $this->command,
258 'job_namespace' => $this->title->getNamespace(),
259 'job_title' => $this->title->getDBkey(),
260 'job_params' => Job::makeBlob( $this->params )
261 );
262 }
263
264 function toString() {
265 $paramString = '';
266 if ( $this->params ) {
267 foreach ( $this->params as $key => $value ) {
268 if ( $paramString != '' ) {
269 $paramString .= ' ';
270 }
271 $paramString .= "$key=$value";
272 }
273 }
274
275 if ( is_object( $this->title ) ) {
276 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
277 if ( $paramString !== '' ) {
278 $s .= ' ' . $paramString;
279 }
280 return $s;
281 } else {
282 return "{$this->command} $paramString";
283 }
284 }
285
286 function getLastError() {
287 return $this->error;
288 }
289 }
290
291