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 && $offset != 0 ) {
52 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
53 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
55 if ($row === false ) {
56 wfProfileOut( __METHOD__
);
61 /* Still avoid scanning purged rows */
62 $offset = $row->job_id
;
64 // Try to delete it from the master
65 $dbw = wfGetDB( DB_MASTER
);
66 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
67 $affected = $dbw->affectedRows();
68 $dbw->immediateCommit();
71 // Failed, someone else beat us to it
72 // Try getting a random row
73 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
74 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__
);
75 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
77 wfProfileOut( __METHOD__
);
81 $row = $dbw->selectRow( 'job', '*',
82 'job_id >= ' . mt_rand( $row->minjob
, $row->maxjob
), __METHOD__
);
83 if ( $row === false ) {
84 // Random job gone before we got the chance to select it
86 wfProfileOut( __METHOD__
);
89 // Delete the random row
90 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
91 $affected = $dbw->affectedRows();
92 $dbw->immediateCommit();
95 // Random job gone before we exclusively deleted it
97 wfProfileOut( __METHOD__
);
102 // If execution got to here, there's a row in $row that has been deleted from the database
103 // by this thread. Hence the concurrent pop was successful.
104 $namespace = $row->job_namespace
;
105 $dbkey = $row->job_title
;
106 $title = Title
::makeTitleSafe( $namespace, $dbkey );
107 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
109 // Remove any duplicates it may have later in the queue
110 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
112 wfProfileOut( __METHOD__
);
117 * Create an object of a subclass
119 static function factory( $command, $title, $params = false, $id = 0 ) {
120 switch ( $command ) {
122 return new RefreshLinksJob( $title, $params, $id );
123 case 'htmlCacheUpdate':
124 case 'html_cache_update': # BC
125 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
127 throw new MWException( "Invalid job command \"$command\"" );
131 static function makeBlob( $params ) {
132 if ( $params !== false ) {
133 return serialize( $params );
139 static function extractBlob( $blob ) {
140 if ( (string)$blob !== '' ) {
141 return unserialize( $blob );
147 /*-------------------------------------------------------------------------
148 * Non-static functions
149 *------------------------------------------------------------------------*/
151 function __construct( $command, $title, $params = false, $id = 0 ) {
152 $this->command
= $command;
153 $this->title
= $title;
154 $this->params
= $params;
157 // A bit of premature generalisation
158 // Oh well, the whole class is premature generalisation really
159 $this->removeDuplicates
= true;
163 * Insert a single job into the queue.
166 $fields = $this->insertFields();
168 $dbw = wfGetDB( DB_MASTER
);
170 if ( $this->removeDuplicates
) {
171 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
172 if ( $dbw->numRows( $res ) ) {
176 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
177 $dbw->insert( 'job', $fields, __METHOD__
);
180 protected function insertFields() {
182 'job_cmd' => $this->command
,
183 'job_namespace' => $this->title
->getNamespace(),
184 'job_title' => $this->title
->getDBkey(),
185 'job_params' => Job
::makeBlob( $this->params
)
190 * Batch-insert a group of jobs into the queue.
191 * This will be wrapped in a transaction with a forced commit.
193 * This may add duplicate at insert time, but they will be
194 * removed later on, when the first one is popped.
196 * @param $jobs array of Job objects
198 static function batchInsert( $jobs ) {
199 if( count( $jobs ) ) {
200 $dbw = wfGetDB( DB_MASTER
);
202 foreach( $jobs as $job ) {
203 $rows[] = $job->insertFields();
205 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
212 * @return boolean success
214 abstract function run();
216 function toString() {
218 if ( $this->params
) {
219 foreach ( $this->params
as $key => $value ) {
220 if ( $paramString != '' ) {
223 $paramString .= "$key=$value";
227 if ( is_object( $this->title
) ) {
228 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
229 if ( $paramString !== '' ) {
230 $s .= ' ' . $paramString;
234 return "{$this->command} $paramString";
238 function getLastError() {
243 class RefreshLinksJob
extends Job
{
244 function __construct( $title, $params = '', $id = 0 ) {
245 parent
::__construct( 'refreshLinks', $title, $params, $id );
249 * Run a refreshLinks job
250 * @return boolean success
254 wfProfileIn( __METHOD__
);
256 $linkCache =& LinkCache
::singleton();
259 if ( is_null( $this->title
) ) {
260 $this->error
= "refreshLinks: Invalid title";
261 wfProfileOut( __METHOD__
);
265 $revision = Revision
::newFromTitle( $this->title
);
267 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
268 wfProfileOut( __METHOD__
);
272 wfProfileIn( __METHOD__
.'-parse' );
273 $options = new ParserOptions
;
274 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
275 wfProfileOut( __METHOD__
.'-parse' );
276 wfProfileIn( __METHOD__
.'-update' );
277 $update = new LinksUpdate( $this->title
, $parserOutput, false );
279 wfProfileOut( __METHOD__
.'-update' );
280 wfProfileOut( __METHOD__
);