3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
15 /*-------------------------------------------------------------------------
17 *------------------------------------------------------------------------*/
20 * @deprecated use LinksUpdate::queueRecursiveJobs()
23 * static function queueLinksJobs( $titles ) {}
27 * Pop a job off the front of the queue
29 * @param $offset Number of jobs to skip
30 * @return Job or false if there's no jobs
32 static function pop($offset=0) {
33 wfProfileIn( __METHOD__
);
35 $dbr = wfGetDB( DB_SLAVE
);
37 /* Get a job from the slave, start with an offset,
38 scan full set afterwards, avoid hitting purged rows
40 NB: If random fetch previously was used, offset
41 will always be ahead of few entries
44 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__
,
45 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
47 // Refetching without offset is needed as some of job IDs could have had delayed commits
48 // and have lower IDs than jobs already executed, blame concurrency :)
50 if ( $row === false) {
52 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
53 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
55 if ($row === false ) {
56 wfProfileOut( __METHOD__
);
60 $offset = $row->job_id
;
62 // Try to delete it from the master
63 $dbw = wfGetDB( DB_MASTER
);
64 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
65 $affected = $dbw->affectedRows();
66 $dbw->immediateCommit();
69 // Failed, someone else beat us to it
70 // Try getting a random row
71 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
72 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
73 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
75 wfProfileOut( __METHOD__
);
79 $row = $dbw->selectRow( 'job', '*',
80 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
81 if ( $row === false ) {
82 // Random job gone before we got the chance to select it
84 wfProfileOut( __METHOD__
);
87 // Delete the random row
88 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
89 $affected = $dbw->affectedRows();
90 $dbw->immediateCommit();
93 // Random job gone before we exclusively deleted it
95 wfProfileOut( __METHOD__
);
100 // If execution got to here, there's a row in $row that has been deleted from the database
101 // by this thread. Hence the concurrent pop was successful.
102 $namespace = $row->job_namespace
;
103 $dbkey = $row->job_title
;
104 $title = Title
::makeTitleSafe( $namespace, $dbkey );
105 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
107 // Remove any duplicates it may have later in the queue
108 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
110 wfProfileOut( __METHOD__
);
115 * Create an object of a subclass
117 static function factory( $command, $title, $params = false, $id = 0 ) {
118 switch ( $command ) {
120 return new RefreshLinksJob( $title, $params, $id );
121 case 'htmlCacheUpdate':
122 case 'html_cache_update': # BC
123 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
125 throw new MWException( "Invalid job command \"$command\"" );
129 static function makeBlob( $params ) {
130 if ( $params !== false ) {
131 return serialize( $params );
137 static function extractBlob( $blob ) {
138 if ( (string)$blob !== '' ) {
139 return unserialize( $blob );
145 /*-------------------------------------------------------------------------
146 * Non-static functions
147 *------------------------------------------------------------------------*/
149 function __construct( $command, $title, $params = false, $id = 0 ) {
150 $this->command
= $command;
151 $this->title
= $title;
152 $this->params
= $params;
155 // A bit of premature generalisation
156 // Oh well, the whole class is premature generalisation really
157 $this->removeDuplicates
= true;
161 * Insert a single job into the queue.
164 $fields = $this->insertFields();
166 $dbw = wfGetDB( DB_MASTER
);
168 if ( $this->removeDuplicates
) {
169 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
170 if ( $dbw->numRows( $res ) ) {
174 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
175 $dbw->insert( 'job', $fields, __METHOD__
);
178 protected function insertFields() {
180 'job_cmd' => $this->command
,
181 'job_namespace' => $this->title
->getNamespace(),
182 'job_title' => $this->title
->getDBkey(),
183 'job_params' => Job
::makeBlob( $this->params
)
188 * Batch-insert a group of jobs into the queue.
189 * This will be wrapped in a transaction with a forced commit.
191 * This may add duplicate at insert time, but they will be
192 * removed later on, when the first one is popped.
194 * @param $jobs array of Job objects
196 static function batchInsert( $jobs ) {
197 if( count( $jobs ) ) {
198 $dbw = wfGetDB( DB_MASTER
);
200 foreach( $jobs as $job ) {
201 $rows[] = $job->insertFields();
203 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
210 * @return boolean success
212 abstract function run();
214 function toString() {
216 if ( $this->params
) {
217 foreach ( $this->params
as $key => $value ) {
218 if ( $paramString != '' ) {
221 $paramString .= "$key=$value";
225 if ( is_object( $this->title
) ) {
226 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
227 if ( $paramString !== '' ) {
228 $s .= ' ' . $paramString;
232 return "{$this->command} $paramString";
236 function getLastError() {
241 class RefreshLinksJob
extends Job
{
242 function __construct( $title, $params = '', $id = 0 ) {
243 parent
::__construct( 'refreshLinks', $title, $params, $id );
247 * Run a refreshLinks job
248 * @return boolean success
252 wfProfileIn( __METHOD__
);
254 $linkCache =& LinkCache
::singleton();
257 if ( is_null( $this->title
) ) {
258 $this->error
= "refreshLinks: Invalid title";
259 wfProfileOut( __METHOD__
);
263 $revision = Revision
::newFromTitle( $this->title
);
265 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
266 wfProfileOut( __METHOD__
);
270 wfProfileIn( __METHOD__
.'-parse' );
271 $options = new ParserOptions
;
272 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
273 wfProfileOut( __METHOD__
.'-parse' );
274 wfProfileIn( __METHOD__
.'-update' );
275 $update = new LinksUpdate( $this->title
, $parserOutput, false );
277 wfProfileOut( __METHOD__
.'-update' );
278 wfProfileOut( __METHOD__
);