Wrap changes lists in <div class="mw-changeslist" />
[lhc/web/wiklou.git] / includes / job / jobs / RefreshLinksJob.php
1 <?php
2 /**
3 * Job to update links for a given title.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Background job to update links for a given title.
26 *
27 * @ingroup JobQueue
28 */
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
33 }
34
35 /**
36 * Run a refreshLinks job
37 * @return bool success
38 */
39 function run() {
40 $linkCache = LinkCache::singleton();
41 $linkCache->clear();
42
43 if ( is_null( $this->title ) ) {
44 $this->error = "refreshLinks: Invalid title";
45
46 return false;
47 }
48
49 # Wait for the DB of the current/next slave DB handle to catch up to the master.
50 # This way, we get the correct page_latest for templates or files that just changed
51 # milliseconds ago, having triggered this job to begin with.
52 if ( isset( $this->params['masterPos'] ) && $this->params['masterPos'] !== false ) {
53 wfGetLB()->waitFor( $this->params['masterPos'] );
54 }
55
56 $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL );
57 if ( !$revision ) {
58 $this->error = 'refreshLinks: Article not found "' .
59 $this->title->getPrefixedDBkey() . '"';
60
61 return false; // XXX: what if it was just deleted?
62 }
63
64 self::runForTitleInternal( $this->title, $revision, __METHOD__ );
65
66 return true;
67 }
68
69 /**
70 * @return array
71 */
72 public function getDeduplicationInfo() {
73 $info = parent::getDeduplicationInfo();
74 // Don't let highly unique "masterPos" values ruin duplicate detection
75 if ( is_array( $info['params'] ) ) {
76 unset( $info['params']['masterPos'] );
77 }
78
79 return $info;
80 }
81
82 /**
83 * @param Title $title
84 * @param Revision $revision
85 * @param string $fname
86 * @return void
87 */
88 public static function runForTitleInternal( Title $title, Revision $revision, $fname ) {
89 wfProfileIn( $fname );
90 $content = $revision->getContent( Revision::RAW );
91
92 if ( !$content ) {
93 // if there is no content, pretend the content is empty
94 $content = $revision->getContentHandler()->makeEmptyContent();
95 }
96
97 // Revision ID must be passed to the parser output to get revision variables correct
98 $parserOutput = $content->getParserOutput( $title, $revision->getId(), null, false );
99
100 $updates = $content->getSecondaryDataUpdates( $title, null, false, $parserOutput );
101 DataUpdate::runUpdates( $updates );
102
103 InfoAction::invalidateCache( $title );
104
105 wfProfileOut( $fname );
106 }
107 }
108
109 /**
110 * Background job to update links for a given title.
111 * Newer version for high use templates.
112 *
113 * @ingroup JobQueue
114 */
115 class RefreshLinksJob2 extends Job {
116 function __construct( $title, $params, $id = 0 ) {
117 parent::__construct( 'refreshLinks2', $title, $params, $id );
118 // Base jobs for large templates can easily be de-duplicated
119 $this->removeDuplicates = !isset( $params['start'] ) && !isset( $params['end'] );
120 }
121
122 /**
123 * Run a refreshLinks2 job
124 * @return bool success
125 */
126 function run() {
127 global $wgUpdateRowsPerJob;
128
129 $linkCache = LinkCache::singleton();
130 $linkCache->clear();
131
132 if ( is_null( $this->title ) ) {
133 $this->error = "refreshLinks2: Invalid title";
134
135 return false;
136 }
137
138 // Back compat for pre-r94435 jobs
139 $table = isset( $this->params['table'] ) ? $this->params['table'] : 'templatelinks';
140
141 // Avoid slave lag when fetching templates.
142 // When the outermost job is run, we know that the caller that enqueued it must have
143 // committed the relevant changes to the DB by now. At that point, record the master
144 // position and pass it along as the job recursively breaks into smaller range jobs.
145 // Hopefully, when leaf jobs are popped, the slaves will have reached that position.
146 if ( isset( $this->params['masterPos'] ) ) {
147 $masterPos = $this->params['masterPos'];
148 } elseif ( wfGetLB()->getServerCount() > 1 ) {
149 $masterPos = wfGetLB()->getMasterPos();
150 } else {
151 $masterPos = false;
152 }
153
154 $tbc = $this->title->getBacklinkCache();
155
156 $jobs = array(); // jobs to insert
157 if ( isset( $this->params['start'] ) && isset( $this->params['end'] ) ) {
158 # This is a partition job to trigger the insertion of leaf jobs...
159 $jobs = array_merge( $jobs, $this->getSingleTitleJobs( $table, $masterPos ) );
160 } else {
161 # This is a base job to trigger the insertion of partitioned jobs...
162 if ( $tbc->getNumLinks( $table, $wgUpdateRowsPerJob + 1 ) <= $wgUpdateRowsPerJob ) {
163 # Just directly insert the single per-title jobs
164 $jobs = array_merge( $jobs, $this->getSingleTitleJobs( $table, $masterPos ) );
165 } else {
166 # Insert the partition jobs to make per-title jobs
167 foreach ( $tbc->partition( $table, $wgUpdateRowsPerJob ) as $batch ) {
168 list( $start, $end ) = $batch;
169 $jobs[] = new RefreshLinksJob2( $this->title,
170 array(
171 'table' => $table,
172 'start' => $start,
173 'end' => $end,
174 'masterPos' => $masterPos,
175 ) + $this->getRootJobParams() // carry over information for de-duplication
176 );
177 }
178 }
179 }
180
181 if ( count( $jobs ) ) {
182 JobQueueGroup::singleton()->push( $jobs );
183 }
184
185 return true;
186 }
187
188 /**
189 * @param string $table
190 * @param mixed $masterPos
191 * @return array
192 */
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.
206 $jobs = array();
207 foreach ( $titles as $title ) {
208 $jobs[] = new RefreshLinksJob( $title,
209 array( 'masterPos' => $masterPos ) + $this->getRootJobParams()
210 ); // carry over information for de-duplication
211 }
212
213 return $jobs;
214 }
215
216 /**
217 * @return array
218 */
219 public function getDeduplicationInfo() {
220 $info = parent::getDeduplicationInfo();
221 // Don't let highly unique "masterPos" values ruin duplicate detection
222 if ( is_array( $info['params'] ) ) {
223 unset( $info['params']['masterPos'] );
224 }
225
226 return $info;
227 }
228 }