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');
10 * Class to both describe a background job and handle jobs.
20 /*-------------------------------------------------------------------------
22 *------------------------------------------------------------------------*/
26 * @return boolean success
28 abstract function run();
30 /*-------------------------------------------------------------------------
32 *------------------------------------------------------------------------*/
35 * @deprecated use LinksUpdate::queueRecursiveJobs()
38 * static function queueLinksJobs( $titles ) {}
42 * Pop a job of a certain type. This tries less hard than pop() to
43 * actually find a job; it may be adversely affected by concurrent job
46 static function pop_type($type) {
47 wfProfilein( __METHOD__
);
49 $dbw = wfGetDB( DB_MASTER
);
52 $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__
,
53 array( 'LIMIT' => 1 ));
56 wfProfileOut( __METHOD__
);
60 /* Ensure we "own" this row */
61 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
62 $affected = $dbw->affectedRows();
65 wfProfileOut( __METHOD__
);
69 $namespace = $row->job_namespace
;
70 $dbkey = $row->job_title
;
71 $title = Title
::makeTitleSafe( $namespace, $dbkey );
72 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
74 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
75 $dbw->immediateCommit();
77 wfProfileOut( __METHOD__
);
82 * Pop a job off the front of the queue
84 * @param $offset Number of jobs to skip
85 * @return Job or false if there's no jobs
87 static function pop($offset=0) {
88 wfProfileIn( __METHOD__
);
90 $dbr = wfGetDB( DB_SLAVE
);
92 /* Get a job from the slave, start with an offset,
93 scan full set afterwards, avoid hitting purged rows
95 NB: If random fetch previously was used, offset
96 will always be ahead of few entries
99 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
100 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
102 // Refetching without offset is needed as some of job IDs could have had delayed commits
103 // and have lower IDs than jobs already executed, blame concurrency :)
105 if ( $row === false) {
107 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
108 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
110 if ($row === false ) {
111 wfProfileOut( __METHOD__
);
115 $offset = $row->job_id
;
117 // Try to delete it from the master
118 $dbw = wfGetDB( DB_MASTER
);
119 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
120 $affected = $dbw->affectedRows();
121 $dbw->immediateCommit();
124 // Failed, someone else beat us to it
125 // Try getting a random row
126 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
127 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
128 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
130 wfProfileOut( __METHOD__
);
133 // Get the random row
134 $row = $dbw->selectRow( 'job', '*',
135 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
136 if ( $row === false ) {
137 // Random job gone before we got the chance to select it
139 wfProfileOut( __METHOD__
);
142 // Delete the random row
143 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
144 $affected = $dbw->affectedRows();
145 $dbw->immediateCommit();
148 // Random job gone before we exclusively deleted it
150 wfProfileOut( __METHOD__
);
155 // If execution got to here, there's a row in $row that has been deleted from the database
156 // by this thread. Hence the concurrent pop was successful.
157 $namespace = $row->job_namespace
;
158 $dbkey = $row->job_title
;
159 $title = Title
::makeTitleSafe( $namespace, $dbkey );
160 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
162 // Remove any duplicates it may have later in the queue
163 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
165 wfProfileOut( __METHOD__
);
170 * Create an object of a subclass
172 static function factory( $command, $title, $params = false, $id = 0 ) {
173 switch ( $command ) {
175 return new RefreshLinksJob( $title, $params, $id );
176 case 'htmlCacheUpdate':
177 case 'html_cache_update': # BC
178 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
180 return new EmaillingJob($params);
182 return new EnotifNotifyJob($title, $params);
184 throw new MWException( "Invalid job command \"$command\"" );
188 static function makeBlob( $params ) {
189 if ( $params !== false ) {
190 return serialize( $params );
196 static function extractBlob( $blob ) {
197 if ( (string)$blob !== '' ) {
198 return unserialize( $blob );
205 * Batch-insert a group of jobs into the queue.
206 * This will be wrapped in a transaction with a forced commit.
208 * This may add duplicate at insert time, but they will be
209 * removed later on, when the first one is popped.
211 * @param $jobs array of Job objects
213 static function batchInsert( $jobs ) {
214 if( count( $jobs ) ) {
215 $dbw = wfGetDB( DB_MASTER
);
217 foreach( $jobs as $job ) {
218 $rows[] = $job->insertFields();
220 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
225 /*-------------------------------------------------------------------------
226 * Non-static functions
227 *------------------------------------------------------------------------*/
229 function __construct( $command, $title, $params = false, $id = 0 ) {
230 $this->command
= $command;
231 $this->title
= $title;
232 $this->params
= $params;
235 // A bit of premature generalisation
236 // Oh well, the whole class is premature generalisation really
237 $this->removeDuplicates
= true;
241 * Insert a single job into the queue.
244 $fields = $this->insertFields();
246 $dbw = wfGetDB( DB_MASTER
);
248 if ( $this->removeDuplicates
) {
249 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
250 if ( $dbw->numRows( $res ) ) {
254 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
255 $dbw->insert( 'job', $fields, __METHOD__
);
258 protected function insertFields() {
260 'job_cmd' => $this->command
,
261 'job_namespace' => $this->title
->getNamespace(),
262 'job_title' => $this->title
->getDBkey(),
263 'job_params' => Job
::makeBlob( $this->params
)
267 function toString() {
269 if ( $this->params
) {
270 foreach ( $this->params
as $key => $value ) {
271 if ( $paramString != '' ) {
274 $paramString .= "$key=$value";
278 if ( is_object( $this->title
) ) {
279 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
280 if ( $paramString !== '' ) {
281 $s .= ' ' . $paramString;
285 return "{$this->command} $paramString";
289 function getLastError() {
296 * Background job to update links for a given title.
298 class RefreshLinksJob
extends Job
{
299 function __construct( $title, $params = '', $id = 0 ) {
300 parent
::__construct( 'refreshLinks', $title, $params, $id );
304 * Run a refreshLinks job
305 * @return boolean success
309 wfProfileIn( __METHOD__
);
311 $linkCache =& LinkCache
::singleton();
314 if ( is_null( $this->title
) ) {
315 $this->error
= "refreshLinks: Invalid title";
316 wfProfileOut( __METHOD__
);
320 $revision = Revision
::newFromTitle( $this->title
);
322 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
323 wfProfileOut( __METHOD__
);
327 wfProfileIn( __METHOD__
.'-parse' );
328 $options = new ParserOptions
;
329 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
330 wfProfileOut( __METHOD__
.'-parse' );
331 wfProfileIn( __METHOD__
.'-update' );
332 $update = new LinksUpdate( $this->title
, $parserOutput, false );
334 wfProfileOut( __METHOD__
.'-update' );
335 wfProfileOut( __METHOD__
);
340 class EmaillingJob
extends Job
{
341 function __construct($params) {
342 parent
::__construct('sendMail', Title
::newMainPage(), $params);
346 userMailer($this->params
['to'], $this->params
['from'], $this->params
['subj'],
347 $this->params
['body'], $this->params
['replyto']);
351 class EnotifNotifyJob
extends Job
{
352 function __construct($title, $params) {
353 parent
::__construct('enotifNotify', $title, $params);
357 $enotif = new EmailNotification();
358 $enotif->actuallyNotifyOnPageChange( User
::newFromName($this->params
['editor'], false),
359 $this->title
, $this->params
['timestamp'],
360 $this->params
['summary'], $this->params
['minorEdit'],
361 $this->params
['oldid']);