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