make links to MediaWiki pages blue for messages with default values
[mediawiki.git] / includes / JobQueue.php
blob3780e4fea146c3b64584e6ccdf69d32748837c63
1 <?php
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
7 require_once('UserMailer.php');
9 /**
10 * Class to both describe a background job and handle jobs.
12 abstract class Job {
13 var $command,
14 $title,
15 $params,
16 $id,
17 $removeDuplicates,
18 $error;
20 /*-------------------------------------------------------------------------
21 * Abstract functions
22 *------------------------------------------------------------------------*/
24 /**
25 * Run the job
26 * @return boolean success
28 abstract function run();
30 /*-------------------------------------------------------------------------
31 * Static functions
32 *------------------------------------------------------------------------*/
34 /**
35 * @deprecated use LinksUpdate::queueRecursiveJobs()
37 /**
38 * static function queueLinksJobs( $titles ) {}
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
47 static function pop($offset=0) {
48 wfProfileIn( __METHOD__ );
50 $dbr = wfGetDB( DB_SLAVE );
52 /* Get a job from the slave, start with an offset,
53 scan full set afterwards, avoid hitting purged rows
55 NB: If random fetch previously was used, offset
56 will always be ahead of few entries
59 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
60 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
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 :)
65 if ( $row === false) {
66 if ($offset!=0)
67 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
68 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
70 if ($row === false ) {
71 wfProfileOut( __METHOD__ );
72 return false;
75 $offset = $row->job_id;
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();
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;
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;
102 // Delete the random row
103 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
104 $affected = $dbw->affectedRows();
105 $dbw->immediateCommit();
107 if ( !$affected ) {
108 // Random job gone before we exclusively deleted it
109 // Give up
110 wfProfileOut( __METHOD__ );
111 return false;
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 );
122 // Remove any duplicates it may have later in the queue
123 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
125 wfProfileOut( __METHOD__ );
126 return $job;
130 * Create an object of a subclass
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\"" );
146 static function makeBlob( $params ) {
147 if ( $params !== false ) {
148 return serialize( $params );
149 } else {
150 return '';
154 static function extractBlob( $blob ) {
155 if ( (string)$blob !== '' ) {
156 return unserialize( $blob );
157 } else {
158 return false;
163 * Batch-insert a group of jobs into the queue.
164 * This will be wrapped in a transaction with a forced commit.
166 * This may add duplicate at insert time, but they will be
167 * removed later on, when the first one is popped.
169 * @param $jobs array of Job objects
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();
178 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
179 $dbw->commit();
183 /*-------------------------------------------------------------------------
184 * Non-static functions
185 *------------------------------------------------------------------------*/
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;
193 // A bit of premature generalisation
194 // Oh well, the whole class is premature generalisation really
195 $this->removeDuplicates = true;
199 * Insert a single job into the queue.
201 function insert() {
202 $fields = $this->insertFields();
204 $dbw = wfGetDB( DB_MASTER );
206 if ( $this->removeDuplicates ) {
207 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
208 if ( $dbw->numRows( $res ) ) {
209 return;
212 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
213 $dbw->insert( 'job', $fields, __METHOD__ );
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 )
225 function toString() {
226 $paramString = '';
227 if ( $this->params ) {
228 foreach ( $this->params as $key => $value ) {
229 if ( $paramString != '' ) {
230 $paramString .= ' ';
232 $paramString .= "$key=$value";
236 if ( is_object( $this->title ) ) {
237 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
238 if ( $paramString !== '' ) {
239 $s .= ' ' . $paramString;
241 return $s;
242 } else {
243 return "{$this->command} $paramString";
247 function getLastError() {
248 return $this->error;
254 * Background job to update links for a given title.
256 class RefreshLinksJob extends Job {
257 function __construct( $title, $params = '', $id = 0 ) {
258 parent::__construct( 'refreshLinks', $title, $params, $id );
262 * Run a refreshLinks job
263 * @return boolean success
265 function run() {
266 global $wgParser;
267 wfProfileIn( __METHOD__ );
269 $linkCache =& LinkCache::singleton();
270 $linkCache->clear();
272 if ( is_null( $this->title ) ) {
273 $this->error = "refreshLinks: Invalid title";
274 wfProfileOut( __METHOD__ );
275 return false;
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;
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;
298 class EmaillingJob extends Job {
299 function __construct($params) {
300 parent::__construct('sendMail', Title::newMainPage(), $params);
303 function run() {
304 userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
305 $this->params['body'], $this->params['replyto']);