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