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__
);
72 public static function runForTitleInternal( Title
$title, Revision
$revision, $fname ) {
75 wfProfileIn( $fname . '-parse' );
76 $options = ParserOptions
::newFromUserAndLang( new User
, $wgContLang );
77 $content = $revision->getContent();
78 $parserOutput = $content->getParserOutput( $title, $revision->getId(), $options, false );
79 wfProfileOut( $fname . '-parse' );
81 wfProfileIn( $fname . '-update' );
82 $updates = $content->getSecondaryDataUpdates( $title, null, false, $parserOutput );
83 DataUpdate
::runUpdates( $updates );
84 wfProfileOut( $fname . '-update' );
89 * Background job to update links for a given title.
90 * Newer version for high use templates.
94 class RefreshLinksJob2
extends Job
{
95 const MAX_TITLES_RUN
= 10;
97 function __construct( $title, $params, $id = 0 ) {
98 parent
::__construct( 'refreshLinks2', $title, $params, $id );
102 * Run a refreshLinks2 job
103 * @return boolean success
106 wfProfileIn( __METHOD__
);
108 $linkCache = LinkCache
::singleton();
111 if ( is_null( $this->title
) ) {
112 $this->error
= "refreshLinks2: Invalid title";
113 wfProfileOut( __METHOD__
);
115 } elseif ( !isset( $this->params
['start'] ) ||
!isset( $this->params
['end'] ) ) {
116 $this->error
= "refreshLinks2: Invalid params";
117 wfProfileOut( __METHOD__
);
121 // Back compat for pre-r94435 jobs
122 $table = isset( $this->params
['table'] ) ?
$this->params
['table'] : 'templatelinks';
124 // Avoid slave lag when fetching templates
125 if ( isset( $this->params
['masterPos'] ) ) {
126 $masterPos = $this->params
['masterPos'];
127 } elseif ( wfGetLB()->getServerCount() > 1 ) {
128 $masterPos = wfGetLB()->getMasterPos();
133 $titles = $this->title
->getBacklinkCache()->getLinks(
134 $table, $this->params
['start'], $this->params
['end'] );
136 if ( $titles->count() > self
::MAX_TITLES_RUN
) {
137 # We don't want to parse too many pages per job as it can starve other jobs.
138 # If there are too many pages to parse, break this up into smaller jobs. By passing
139 # in the master position here we can cut down on the time spent waiting for slaves to
140 # catch up by the runners handling these jobs since time will have passed between now
141 # and when they pop these jobs off the queue.
142 $start = 0; // batch start
143 $end = 0; // batch end
144 $bsize = 0; // batch size
145 $first = true; // first of batch
147 foreach ( $titles as $title ) {
148 $start = $first ?
$title->getArticleId() : $start;
149 $end = $title->getArticleId();
151 if ( ++
$bsize >= self
::MAX_TITLES_RUN
) {
152 $jobs[] = new RefreshLinksJob2( $this->title
, array(
156 'masterPos' => $masterPos
159 $start = $end = $bsize = 0;
162 if ( $bsize > 0 ) { // group remaining pages into a job
163 $jobs[] = new RefreshLinksJob2( $this->title
, array(
167 'masterPos' => $masterPos
170 Job
::batchInsert( $jobs );
171 } elseif ( php_sapi_name() != 'cli' ) {
172 # Not suitable for page load triggered job running!
173 # Gracefully switch to refreshLinks jobs if this happens.
175 foreach ( $titles as $title ) {
176 $jobs[] = new RefreshLinksJob( $title, array( 'masterPos' => $masterPos ) );
178 Job
::batchInsert( $jobs );
180 # Wait for the DB of the current/next slave DB handle to catch up to the master.
181 # This way, we get the correct page_latest for templates or files that just changed
182 # milliseconds ago, having triggered this job to begin with.
184 wfGetLB()->waitFor( $masterPos );
186 # Re-parse each page that transcludes this page and update their tracking links...
187 foreach ( $titles as $title ) {
188 $revision = Revision
::newFromTitle( $title, false, Revision
::READ_NORMAL
);
190 $this->error
= 'refreshLinks: Article not found "' .
191 $title->getPrefixedDBkey() . '"';
192 continue; // skip this page
194 RefreshLinksJob
::runForTitleInternal( $title, $revision, __METHOD__
);
199 wfProfileOut( __METHOD__
);