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 off the front of the queue
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) {
67 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
68 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
70 if ($row === false ) {
71 wfProfileOut( __METHOD__
);
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();
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
) ) {
90 wfProfileOut( __METHOD__
);
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
99 wfProfileOut( __METHOD__
);
102 // Delete the random row
103 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
104 $affected = $dbw->affectedRows();
105 $dbw->immediateCommit();
108 // Random job gone before we exclusively deleted it
110 wfProfileOut( __METHOD__
);
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__
);
130 * Create an object of a subclass
132 static function factory( $command, $title, $params = false, $id = 0 ) {
133 switch ( $command ) {
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 );
140 return new EmaillingJob($params);
142 throw new MWException( "Invalid job command \"$command\"" );
146 static function makeBlob( $params ) {
147 if ( $params !== false ) {
148 return serialize( $params );
154 static function extractBlob( $blob ) {
155 if ( (string)$blob !== '' ) {
156 return unserialize( $blob );
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
);
175 foreach( $jobs as $job ) {
176 $rows[] = $job->insertFields();
178 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
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;
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.
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 ) ) {
212 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
213 $dbw->insert( 'job', $fields, __METHOD__
);
216 protected function insertFields() {
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() {
227 if ( $this->params
) {
228 foreach ( $this->params
as $key => $value ) {
229 if ( $paramString != '' ) {
232 $paramString .= "$key=$value";
236 if ( is_object( $this->title
) ) {
237 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
238 if ( $paramString !== '' ) {
239 $s .= ' ' . $paramString;
243 return "{$this->command} $paramString";
247 function getLastError() {
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
267 wfProfileIn( __METHOD__
);
269 $linkCache =& LinkCache
::singleton();
272 if ( is_null( $this->title
) ) {
273 $this->error
= "refreshLinks: Invalid title";
274 wfProfileOut( __METHOD__
);
278 $revision = Revision
::newFromTitle( $this->title
);
280 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
281 wfProfileOut( __METHOD__
);
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 );
292 wfProfileOut( __METHOD__
.'-update' );
293 wfProfileOut( __METHOD__
);
298 class EmaillingJob
extends Job
{
299 function __construct($params) {
300 parent
::__construct('sendMail', Title
::newMainPage(), $params);
304 userMailer($this->params
['to'], $this->params
['from'], $this->params
['subj'],
305 $this->params
['body'], $this->params
['replyto']);