3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
15 /*-------------------------------------------------------------------------
17 *------------------------------------------------------------------------*/
19 * Add an array of refreshLinks jobs to the queue
20 * @param array $titles Array of title objects.
23 function queueLinksJobs( $titles ) {
24 $fname = 'Job::queueLinksJobs';
25 wfProfileIn( $fname );
27 for( $i = 0; $i < count( $titles ); $i +
= $batchSize ) {
28 $batch = array_slice( $titles, $i, $batchSize, true );
30 foreach( $batch as $title ) {
31 $jobs[] = new Job( 'refreshLinks', $title );
33 Job
::batchInsert( $jobs );
35 wfProfileOut( $fname );
39 * Pop a job off the front of the queue
41 * @return Job or false if there's no jobs
45 wfProfileIn( $fname );
47 $dbr =& wfGetDB( DB_SLAVE
);
49 // Get a job from the slave
50 $row = $dbr->selectRow( 'job', '*', '', $fname,
51 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )
54 if ( $row === false ) {
55 wfProfileOut( $fname );
59 // Try to delete it from the master
60 $dbw =& wfGetDB( DB_MASTER
);
61 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), $fname );
62 $affected = $dbw->affectedRows();
63 $dbw->immediateCommit();
66 // Failed, someone else beat us to it
67 // Try getting a random row
68 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
69 'MAX(job_id) as maxjob' ), '', $fname );
70 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
72 wfProfileOut( $fname );
76 $row = $dbw->selectRow( 'job', '*',
77 array( 'job_id' => mt_rand( $row->minjob
, $row->maxjob
) ), $fname );
78 if ( $row === false ) {
79 // Random job gone before we got the chance to select it
81 wfProfileOut( $fname );
84 // Delete the random row
85 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), $fname );
86 $affected = $dbw->affectedRows();
87 $dbw->immediateCommit();
90 // Random job gone before we exclusively deleted it
92 wfProfileOut( $fname );
97 // If execution got to here, there's a row in $row that has been deleted from the database
98 // by this thread. Hence the concurrent pop was successful.
99 $namespace = $row->job_namespace
;
100 $dbkey = $row->job_title
;
101 $title = Title
::makeTitleSafe( $namespace, $dbkey );
102 $job = new Job( $row->job_cmd
, $title, $row->job_params
, $row->job_id
);
104 // Remove any duplicates it may have later in the queue
105 $dbw->delete( 'job', $job->insertFields(), $fname );
107 wfProfileOut( $fname );
111 /*-------------------------------------------------------------------------
112 * Non-static functions
113 *------------------------------------------------------------------------*/
115 function Job( $command, $title, $params = '', $id = 0 ) {
116 $this->command
= $command;
117 $this->title
= $title;
118 $this->params
= $params;
121 // A bit of premature generalisation
122 // Oh well, the whole class is premature generalisation really
123 $this->removeDuplicates
= true;
127 * Insert a single job into the queue.
130 $fname = 'Job::insert';
132 $fields = $this->insertFields();
134 $dbw =& wfGetDB( DB_MASTER
);
136 if ( $this->removeDuplicates
) {
137 $res = $dbw->select( 'job', array( '1' ), $fields, $fname );
138 if ( $dbw->numRows( $res ) ) {
142 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
143 $dbw->insert( 'job', $fields, $fname );
146 protected function insertFields() {
148 'job_cmd' => $this->command
,
149 'job_namespace' => $this->title
->getNamespace(),
150 'job_title' => $this->title
->getDBkey(),
151 'job_params' => $this->params
156 * Batch-insert a group of jobs into the queue.
157 * This will be wrapped in a transaction with a forced commit.
159 * This may add duplicate at insert time, but they will be
160 * removed later on, when the first one is popped.
162 * @param $jobs array of Job objects
164 static function batchInsert( $jobs ) {
165 $fname = __CLASS__
. '::' . __FUNCTION__
;
167 if( count( $jobs ) ) {
168 $dbw = wfGetDB( DB_MASTER
);
170 foreach( $jobs as $job ) {
171 $rows[] = $job->insertFields();
173 $dbw->insert( 'job', $rows, $fname, 'IGNORE' );
174 $dbw->immediateCommit();
180 * @return boolean success
184 wfProfileIn( $fname );
185 switch ( $this->command
) {
187 $retval = $this->refreshLinks();
190 $this->error
= "Invalid job type {$this->command}, ignoring";
191 wfDebug( $this->error
. "\n" );
194 wfProfileOut( $fname );
199 * Run a refreshLinks job
200 * @return boolean success
202 function refreshLinks() {
204 $fname = 'Job::refreshLinks';
205 wfProfileIn( $fname );
207 # FIXME: $dbw never used.
208 $dbw =& wfGetDB( DB_MASTER
);
210 $linkCache =& LinkCache
::singleton();
213 if ( is_null( $this->title
) ) {
214 $this->error
= "refreshLinks: Invalid title";
215 wfProfileOut( $fname );
219 $revision = Revision
::newFromTitle( $this->title
);
221 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
222 wfProfileOut( $fname );
226 wfProfileIn( "$fname-parse" );
227 $options = new ParserOptions
;
228 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
229 wfProfileOut( "$fname-parse" );
230 wfProfileIn( "$fname-update" );
231 $update = new LinksUpdate( $this->title
, $parserOutput, false );
233 wfProfileOut( "$fname-update" );
234 wfProfileOut( $fname );
238 function toString() {
239 if ( is_object( $this->title
) ) {
240 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
241 if ( $this->params
!== '' ) {
242 $s .= ', ' . $this->params
;
246 return "{$this->command} {$this->params}";
250 function getLastError() {