3 * Job to update links for a given title.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Background job to update links for a given title.
29 class RefreshLinksJob
extends Job
{
30 function __construct( $title, $params = '', $id = 0 ) {
31 parent
::__construct( 'refreshLinks', $title, $params, $id );
32 $this->removeDuplicates
= true; // job is expensive
36 * Run a refreshLinks job
37 * @return boolean success
40 wfProfileIn( __METHOD__
);
42 $linkCache = LinkCache
::singleton();
45 if ( is_null( $this->title
) ) {
46 $this->error
= "refreshLinks: Invalid title";
47 wfProfileOut( __METHOD__
);
51 # Wait for the DB of the current/next slave DB handle to catch up to the master.
52 # This way, we get the correct page_latest for templates or files that just changed
53 # milliseconds ago, having triggered this job to begin with.
54 if ( isset( $this->params
['masterPos'] ) ) {
55 wfGetLB()->waitFor( $this->params
['masterPos'] );
58 $revision = Revision
::newFromTitle( $this->title
, false, Revision
::READ_NORMAL
);
60 $this->error
= 'refreshLinks: Article not found "' .
61 $this->title
->getPrefixedDBkey() . '"';
62 wfProfileOut( __METHOD__
);
63 return false; // XXX: what if it was just deleted?
66 self
::runForTitleInternal( $this->title
, $revision, __METHOD__
);
68 wfProfileOut( __METHOD__
);
75 public function getDeduplicationInfo() {
76 $info = parent
::getDeduplicationInfo();
77 // Don't let highly unique "masterPos" values ruin duplicate detection
78 if ( is_array( $info['params'] ) ) {
79 unset( $info['params']['masterPos'] );
86 * @param $revision Revision
87 * @param $fname string
90 public static function runForTitleInternal( Title
$title, Revision
$revision, $fname ) {
91 wfProfileIn( $fname );
92 $content = $revision->getContent( Revision
::RAW
);
95 // if there is no content, pretend the content is empty
96 $content = $revision->getContentHandler()->makeEmptyContent();
99 // Revision ID must be passed to the parser output to get revision variables correct
100 $parserOutput = $content->getParserOutput( $title, $revision->getId(), null, false );
102 $updates = $content->getSecondaryDataUpdates( $title, null, false, $parserOutput );
103 DataUpdate
::runUpdates( $updates );
104 wfProfileOut( $fname );
109 * Background job to update links for a given title.
110 * Newer version for high use templates.
114 class RefreshLinksJob2
extends Job
{
115 function __construct( $title, $params, $id = 0 ) {
116 parent
::__construct( 'refreshLinks2', $title, $params, $id );
120 * Run a refreshLinks2 job
121 * @return boolean success
124 global $wgUpdateRowsPerJob;
126 wfProfileIn( __METHOD__
);
128 $linkCache = LinkCache
::singleton();
131 if ( is_null( $this->title
) ) {
132 $this->error
= "refreshLinks2: Invalid title";
133 wfProfileOut( __METHOD__
);
137 // Back compat for pre-r94435 jobs
138 $table = isset( $this->params
['table'] ) ?
$this->params
['table'] : 'templatelinks';
140 // Avoid slave lag when fetching templates.
141 // When the outermost job is run, we know that the caller that enqueued it must have
142 // committed the relevant changes to the DB by now. At that point, record the master
143 // position and pass it along as the job recursively breaks into smaller range jobs.
144 // Hopefully, when leaf jobs are popped, the slaves will have reached that position.
145 if ( isset( $this->params
['masterPos'] ) ) {
146 $masterPos = $this->params
['masterPos'];
147 } elseif ( wfGetLB()->getServerCount() > 1 ) {
148 $masterPos = wfGetLB()->getMasterPos();
153 $tbc = $this->title
->getBacklinkCache();
155 $jobs = array(); // jobs to insert
156 if ( isset( $this->params
['start'] ) && isset( $this->params
['end'] ) ) {
157 # This is a partition job to trigger the insertion of leaf jobs...
158 $jobs = array_merge( $jobs, $this->getSingleTitleJobs( $table, $masterPos ) );
160 # This is a base job to trigger the insertion of partitioned jobs...
161 if ( $tbc->getNumLinks( $table ) <= $wgUpdateRowsPerJob ) {
162 # Just directly insert the single per-title jobs
163 $jobs = array_merge( $jobs, $this->getSingleTitleJobs( $table, $masterPos ) );
165 # Insert the partition jobs to make per-title jobs
166 foreach ( $tbc->partition( $table, $wgUpdateRowsPerJob ) as $batch ) {
167 list( $start, $end ) = $batch;
168 $jobs[] = new RefreshLinksJob2( $this->title
,
173 'masterPos' => $masterPos,
174 ) +
$this->getRootJobParams() // carry over information for de-duplication
180 if ( count( $jobs ) ) {
181 JobQueueGroup
::singleton()->push( $jobs );
184 wfProfileOut( __METHOD__
);
189 * @param $table string
190 * @param $masterPos mixed
193 protected function getSingleTitleJobs( $table, $masterPos ) {
194 # The "start"/"end" fields are not set for the base jobs
195 $start = isset( $this->params
['start'] ) ?
$this->params
['start'] : false;
196 $end = isset( $this->params
['end'] ) ?
$this->params
['end'] : false;
197 $titles = $this->title
->getBacklinkCache()->getLinks( $table, $start, $end );
198 # Convert into single page refresh links jobs.
199 # This handles well when in sapi mode and is useful in any case for job
200 # de-duplication. If many pages use template A, and that template itself
201 # uses template B, then an edit to both will create many duplicate jobs.
202 # Roughly speaking, for each page, one of the "RefreshLinksJob" jobs will
203 # get run first, and when it does, it will remove the duplicates. Of course,
204 # one page could have its job popped when the other page's job is still
205 # buried within the logic of a refreshLinks2 job.
207 foreach ( $titles as $title ) {
208 $jobs[] = new RefreshLinksJob( $title,
209 array( 'masterPos' => $masterPos ) +
$this->getRootJobParams()
210 ); // carry over information for de-duplication
218 public function getDeduplicationInfo() {
219 $info = parent
::getDeduplicationInfo();
220 // Don't let highly unique "masterPos" values ruin duplicate detection
221 if ( is_array( $info['params'] ) ) {
222 unset( $info['params']['masterPos'] );