3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
8 * Class to both describe a background job and handle jobs.
18 /*-------------------------------------------------------------------------
20 *------------------------------------------------------------------------*/
24 * @return boolean success
26 abstract function run();
28 /*-------------------------------------------------------------------------
30 *------------------------------------------------------------------------*/
33 * @deprecated use LinksUpdate::queueRecursiveJobs()
36 * static function queueLinksJobs( $titles ) {}
40 * Pop a job off the front of the queue
42 * @param $offset Number of jobs to skip
43 * @return Job or false if there's no jobs
45 static function pop($offset=0) {
46 wfProfileIn( __METHOD__
);
48 $dbr = wfGetDB( DB_SLAVE
);
50 /* Get a job from the slave, start with an offset,
51 scan full set afterwards, avoid hitting purged rows
53 NB: If random fetch previously was used, offset
54 will always be ahead of few entries
57 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
58 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
60 // Refetching without offset is needed as some of job IDs could have had delayed commits
61 // and have lower IDs than jobs already executed, blame concurrency :)
63 if ( $row === false) {
65 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
66 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
68 if ($row === false ) {
69 wfProfileOut( __METHOD__
);
73 $offset = $row->job_id
;
75 // Try to delete it from the master
76 $dbw = wfGetDB( DB_MASTER
);
77 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
78 $affected = $dbw->affectedRows();
79 $dbw->immediateCommit();
82 // Failed, someone else beat us to it
83 // Try getting a random row
84 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
85 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
86 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
88 wfProfileOut( __METHOD__
);
92 $row = $dbw->selectRow( 'job', '*',
93 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
94 if ( $row === false ) {
95 // Random job gone before we got the chance to select it
97 wfProfileOut( __METHOD__
);
100 // Delete the random row
101 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
102 $affected = $dbw->affectedRows();
103 $dbw->immediateCommit();
106 // Random job gone before we exclusively deleted it
108 wfProfileOut( __METHOD__
);
113 // If execution got to here, there's a row in $row that has been deleted from the database
114 // by this thread. Hence the concurrent pop was successful.
115 $namespace = $row->job_namespace
;
116 $dbkey = $row->job_title
;
117 $title = Title
::makeTitleSafe( $namespace, $dbkey );
118 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
120 // Remove any duplicates it may have later in the queue
121 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
123 wfProfileOut( __METHOD__
);
128 * Create an object of a subclass
130 static function factory( $command, $title, $params = false, $id = 0 ) {
131 switch ( $command ) {
133 return new RefreshLinksJob( $title, $params, $id );
134 case 'htmlCacheUpdate':
135 case 'html_cache_update': # BC
136 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
138 throw new MWException( "Invalid job command \"$command\"" );
142 static function makeBlob( $params ) {
143 if ( $params !== false ) {
144 return serialize( $params );
150 static function extractBlob( $blob ) {
151 if ( (string)$blob !== '' ) {
152 return unserialize( $blob );
159 * Batch-insert a group of jobs into the queue.
160 * This will be wrapped in a transaction with a forced commit.
162 * This may add duplicate at insert time, but they will be
163 * removed later on, when the first one is popped.
165 * @param $jobs array of Job objects
167 static function batchInsert( $jobs ) {
168 if( count( $jobs ) ) {
169 $dbw = wfGetDB( DB_MASTER
);
171 foreach( $jobs as $job ) {
172 $rows[] = $job->insertFields();
174 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
179 /*-------------------------------------------------------------------------
180 * Non-static functions
181 *------------------------------------------------------------------------*/
183 function __construct( $command, $title, $params = false, $id = 0 ) {
184 $this->command
= $command;
185 $this->title
= $title;
186 $this->params
= $params;
189 // A bit of premature generalisation
190 // Oh well, the whole class is premature generalisation really
191 $this->removeDuplicates
= true;
195 * Insert a single job into the queue.
198 $fields = $this->insertFields();
200 $dbw = wfGetDB( DB_MASTER
);
202 if ( $this->removeDuplicates
) {
203 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
204 if ( $dbw->numRows( $res ) ) {
208 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
209 $dbw->insert( 'job', $fields, __METHOD__
);
212 protected function insertFields() {
214 'job_cmd' => $this->command
,
215 'job_namespace' => $this->title
->getNamespace(),
216 'job_title' => $this->title
->getDBkey(),
217 'job_params' => Job
::makeBlob( $this->params
)
221 function toString() {
223 if ( $this->params
) {
224 foreach ( $this->params
as $key => $value ) {
225 if ( $paramString != '' ) {
228 $paramString .= "$key=$value";
232 if ( is_object( $this->title
) ) {
233 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
234 if ( $paramString !== '' ) {
235 $s .= ' ' . $paramString;
239 return "{$this->command} $paramString";
243 function getLastError() {
250 * Background job to update links for a given title.
252 class RefreshLinksJob
extends Job
{
253 function __construct( $title, $params = '', $id = 0 ) {
254 parent
::__construct( 'refreshLinks', $title, $params, $id );
258 * Run a refreshLinks job
259 * @return boolean success
263 wfProfileIn( __METHOD__
);
265 $linkCache =& LinkCache
::singleton();
268 if ( is_null( $this->title
) ) {
269 $this->error
= "refreshLinks: Invalid title";
270 wfProfileOut( __METHOD__
);
274 $revision = Revision
::newFromTitle( $this->title
);
276 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
277 wfProfileOut( __METHOD__
);
281 wfProfileIn( __METHOD__
.'-parse' );
282 $options = new ParserOptions
;
283 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
284 wfProfileOut( __METHOD__
.'-parse' );
285 wfProfileIn( __METHOD__
.'-update' );
286 $update = new LinksUpdate( $this->title
, $parserOutput, false );
288 wfProfileOut( __METHOD__
.'-update' );
289 wfProfileOut( __METHOD__
);