3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
8 * @todo document (e.g. one-sentence top-level class description).
18 /*-------------------------------------------------------------------------
20 *------------------------------------------------------------------------*/
23 * @deprecated use LinksUpdate::queueRecursiveJobs()
26 * static function queueLinksJobs( $titles ) {}
30 * Pop a job off the front of the queue
32 * @param $offset Number of jobs to skip
33 * @return Job or false if there's no jobs
35 static function pop($offset=0) {
36 wfProfileIn( __METHOD__
);
38 $dbr = wfGetDB( DB_SLAVE
);
40 /* Get a job from the slave, start with an offset,
41 scan full set afterwards, avoid hitting purged rows
43 NB: If random fetch previously was used, offset
44 will always be ahead of few entries
47 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
48 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
50 // Refetching without offset is needed as some of job IDs could have had delayed commits
51 // and have lower IDs than jobs already executed, blame concurrency :)
53 if ( $row === false) {
55 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
56 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
58 if ($row === false ) {
59 wfProfileOut( __METHOD__
);
63 $offset = $row->job_id
;
65 // Try to delete it from the master
66 $dbw = wfGetDB( DB_MASTER
);
67 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
68 $affected = $dbw->affectedRows();
69 $dbw->immediateCommit();
72 // Failed, someone else beat us to it
73 // Try getting a random row
74 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
75 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
76 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
78 wfProfileOut( __METHOD__
);
82 $row = $dbw->selectRow( 'job', '*',
83 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
84 if ( $row === false ) {
85 // Random job gone before we got the chance to select it
87 wfProfileOut( __METHOD__
);
90 // Delete the random row
91 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
92 $affected = $dbw->affectedRows();
93 $dbw->immediateCommit();
96 // Random job gone before we exclusively deleted it
98 wfProfileOut( __METHOD__
);
103 // If execution got to here, there's a row in $row that has been deleted from the database
104 // by this thread. Hence the concurrent pop was successful.
105 $namespace = $row->job_namespace
;
106 $dbkey = $row->job_title
;
107 $title = Title
::makeTitleSafe( $namespace, $dbkey );
108 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
110 // Remove any duplicates it may have later in the queue
111 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
113 wfProfileOut( __METHOD__
);
118 * Create an object of a subclass
120 static function factory( $command, $title, $params = false, $id = 0 ) {
121 switch ( $command ) {
123 return new RefreshLinksJob( $title, $params, $id );
124 case 'htmlCacheUpdate':
125 case 'html_cache_update': # BC
126 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
128 throw new MWException( "Invalid job command \"$command\"" );
132 static function makeBlob( $params ) {
133 if ( $params !== false ) {
134 return serialize( $params );
140 static function extractBlob( $blob ) {
141 if ( (string)$blob !== '' ) {
142 return unserialize( $blob );
148 /*-------------------------------------------------------------------------
149 * Non-static functions
150 *------------------------------------------------------------------------*/
152 function __construct( $command, $title, $params = false, $id = 0 ) {
153 $this->command
= $command;
154 $this->title
= $title;
155 $this->params
= $params;
158 // A bit of premature generalisation
159 // Oh well, the whole class is premature generalisation really
160 $this->removeDuplicates
= true;
164 * Insert a single job into the queue.
167 $fields = $this->insertFields();
169 $dbw = wfGetDB( DB_MASTER
);
171 if ( $this->removeDuplicates
) {
172 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
173 if ( $dbw->numRows( $res ) ) {
177 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
178 $dbw->insert( 'job', $fields, __METHOD__
);
181 protected function insertFields() {
183 'job_cmd' => $this->command
,
184 'job_namespace' => $this->title
->getNamespace(),
185 'job_title' => $this->title
->getDBkey(),
186 'job_params' => Job
::makeBlob( $this->params
)
191 * Batch-insert a group of jobs into the queue.
192 * This will be wrapped in a transaction with a forced commit.
194 * This may add duplicate at insert time, but they will be
195 * removed later on, when the first one is popped.
197 * @param $jobs array of Job objects
199 static function batchInsert( $jobs ) {
200 if( count( $jobs ) ) {
201 $dbw = wfGetDB( DB_MASTER
);
203 foreach( $jobs as $job ) {
204 $rows[] = $job->insertFields();
206 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
213 * @return boolean success
215 abstract function run();
217 function toString() {
219 if ( $this->params
) {
220 foreach ( $this->params
as $key => $value ) {
221 if ( $paramString != '' ) {
224 $paramString .= "$key=$value";
228 if ( is_object( $this->title
) ) {
229 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
230 if ( $paramString !== '' ) {
231 $s .= ' ' . $paramString;
235 return "{$this->command} $paramString";
239 function getLastError() {
246 * @todo document (e.g. one-sentence top-level class description).
248 class RefreshLinksJob
extends Job
{
249 function __construct( $title, $params = '', $id = 0 ) {
250 parent
::__construct( 'refreshLinks', $title, $params, $id );
254 * Run a refreshLinks job
255 * @return boolean success
259 wfProfileIn( __METHOD__
);
261 $linkCache =& LinkCache
::singleton();
264 if ( is_null( $this->title
) ) {
265 $this->error
= "refreshLinks: Invalid title";
266 wfProfileOut( __METHOD__
);
270 $revision = Revision
::newFromTitle( $this->title
);
272 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
273 wfProfileOut( __METHOD__
);
277 wfProfileIn( __METHOD__
.'-parse' );
278 $options = new ParserOptions
;
279 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
280 wfProfileOut( __METHOD__
.'-parse' );
281 wfProfileIn( __METHOD__
.'-update' );
282 $update = new LinksUpdate( $this->title
, $parserOutput, false );
284 wfProfileOut( __METHOD__
.'-update' );
285 wfProfileOut( __METHOD__
);