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