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