3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
16 /*-------------------------------------------------------------------------
18 *------------------------------------------------------------------------*/
20 * Add an array of refreshLinks jobs to the queue
21 * @param array $titles Array of title objects.
24 function queueLinksJobs( $titles ) {
25 $fname = 'Job::queueLinksJobs';
26 wfProfileIn( $fname );
27 foreach ( $titles as $title ) {
28 $job = new Job( 'refreshLinks', $title );
31 wfProfileOut( $fname );
35 * Pop a job off the front of the queue
37 * @return Job or false if there's no jobs
41 wfProfileIn( $fname );
43 $dbr =& wfGetDB( DB_SLAVE
);
45 // Get a job from the slave
46 $row = $dbr->selectRow( 'job', '*', '', $fname,
47 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )
50 if ( $row === false ) {
51 wfProfileOut( $fname );
55 // Try to delete it from the master
56 $dbw =& wfGetDB( DB_MASTER
);
57 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), $fname );
58 $affected = $dbw->affectedRows();
59 $dbw->immediateCommit();
62 // Failed, someone else beat us to it
63 // Try getting a random row
64 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
65 'MAX(job_id) as maxjob' ), '', $fname );
66 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
68 wfProfileOut( $fname );
72 $row = $dbw->selectRow( 'job', '*',
73 array( 'job_id' => mt_rand( $row->minjob
, $row->maxjob
) ), $fname );
74 if ( $row === false ) {
75 // Random job gone before we got the chance to select it
77 wfProfileOut( $fname );
80 // Delete the random row
81 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), $fname );
82 $affected = $dbw->affectedRows();
83 $dbw->immediateCommit();
86 // Random job gone before we exclusively deleted it
88 wfProfileOut( $fname );
93 // If execution got to here, there's a row in $row that has been deleted from the database
94 // by this thread. Hence the concurrent pop was successful.
95 $namespace = $row->job_namespace
;
96 $dbkey = $row->job_title
;
97 $title = Title
::makeTitleSafe( $namespace, $dbkey );
98 $job = new Job( $row->job_cmd
, $title, $row->job_params
, $row->job_id
);
99 wfProfileOut( $fname );
103 /*-------------------------------------------------------------------------
104 * Non-static functions
105 *------------------------------------------------------------------------*/
107 function Job( $command, $title, $params = '', $id = 0 ) {
108 $this->command
= $command;
109 $this->title
= $title;
110 $this->params
= $params;
113 // A bit of premature generalisation
114 // Oh well, the whole class is premature generalisation really
115 $this->removeDuplicates
= true;
119 $fname = 'Job::insert';
122 'job_cmd' => $this->command
,
123 'job_namespace' => $this->title
->getNamespace(),
124 'job_title' => $this->title
->getDBkey(),
125 'job_params' => $this->params
128 $dbw =& wfGetDB( DB_MASTER
);
130 if ( $this->removeDuplicates
) {
131 $res = $dbw->select( 'job', array( '1' ), $fields, $fname );
132 if ( $dbw->numRows( $res ) ) {
136 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
137 $dbw->insert( 'job', $fields, $fname );
142 * @return boolean success
146 wfProfileIn( $fname );
147 switch ( $this->command
) {
149 $retval = $this->refreshLinks();
152 $this->error
= "Invalid job type {$this->command}, ignoring";
153 wfDebug( $this->error
. "\n" );
156 wfProfileOut( $fname );
161 * Run a refreshLinks job
162 * @return boolean success
164 function refreshLinks() {
166 $fname = 'Job::refreshLinks';
167 wfProfileIn( $fname );
169 # FIXME: $dbw never used.
170 $dbw =& wfGetDB( DB_MASTER
);
172 $linkCache =& LinkCache
::singleton();
175 if ( is_null( $this->title
) ) {
176 $this->error
= "refreshLinks: Invalid title";
177 wfProfileOut( $fname );
181 $revision = Revision
::newFromTitle( $this->title
);
183 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
184 wfProfileOut( $fname );
188 wfProfileIn( "$fname-parse" );
189 $options = new ParserOptions
;
190 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
191 wfProfileOut( "$fname-parse" );
192 wfProfileIn( "$fname-update" );
193 $update = new LinksUpdate( $this->title
, $parserOutput, false );
195 wfProfileOut( "$fname-update" );
196 wfProfileOut( $fname );
200 function toString() {
201 if ( is_object( $this->title
) ) {
202 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
203 if ( $this->params
!== '' ) {
204 $s .= ', ' . $this->params
;
208 return "{$this->command} {$this->params}";
212 function getLastError() {