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