c5f87c57acdb0d8d0ac589be2476ae933e960efe
[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 boolean 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 return false;
46 }
47
48 # Wait for the DB of the current/next slave DB handle to catch up to the master.
49 # This way, we get the correct page_latest for templates or files that just changed
50 # milliseconds ago, having triggered this job to begin with.
51 if ( isset( $this->params['masterPos'] ) ) {
52 wfGetLB()->waitFor( $this->params['masterPos'] );
53 }
54
55 $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL );
56 if ( !$revision ) {
57 $this->error = 'refreshLinks: Article not found "' .
58 $this->title->getPrefixedDBkey() . '"';
59 return false; // XXX: what if it was just deleted?
60 }
61
62 self::runForTitleInternal( $this->title, $revision, __METHOD__ );
63 InfoAction::invalidateCache( $this->title );
64
65 return true;
66 }
67
68 /**
69 * @return Array
70 */
71 public function getDeduplicationInfo() {
72 $info = parent::getDeduplicationInfo();
73 // Don't let highly unique "masterPos" values ruin duplicate detection
74 if ( is_array( $info['params'] ) ) {
75 unset( $info['params']['masterPos'] );
76 }
77 return $info;
78 }
79
80 /**
81 * @param $title Title
82 * @param $revision Revision
83 * @param $fname string
84 * @return void
85 */
86 public static function runForTitleInternal( Title $title, Revision $revision, $fname ) {
87 wfProfileIn( $fname );
88 $content = $revision->getContent( Revision::RAW );
89
90 if ( !$content ) {
91 // if there is no content, pretend the content is empty
92 $content = $revision->getContentHandler()->makeEmptyContent();
93 }
94
95 // Revision ID must be passed to the parser output to get revision variables correct
96 $parserOutput = $content->getParserOutput( $title, $revision->getId(), null, false );
97
98 $updates = $content->getSecondaryDataUpdates( $title, null, false, $parserOutput );
99 DataUpdate::runUpdates( $updates );
100 wfProfileOut( $fname );
101 }
102 }
103
104 /**
105 * Background job to update links for a given title.
106 * Newer version for high use templates.
107 *
108 * @ingroup JobQueue
109 */
110 class RefreshLinksJob2 extends Job {
111 function __construct( $title, $params, $id = 0 ) {
112 parent::__construct( 'refreshLinks2', $title, $params, $id );
113 }
114
115 /**
116 * Run a refreshLinks2 job
117 * @return boolean success
118 */
119 function run() {
120 global $wgUpdateRowsPerJob;
121
122 $linkCache = LinkCache::singleton();
123 $linkCache->clear();
124
125 if ( is_null( $this->title ) ) {
126 $this->error = "refreshLinks2: Invalid title";
127 return false;
128 }
129
130 // Back compat for pre-r94435 jobs
131 $table = isset( $this->params['table'] ) ? $this->params['table'] : 'templatelinks';
132
133 // Avoid slave lag when fetching templates.
134 // When the outermost job is run, we know that the caller that enqueued it must have
135 // committed the relevant changes to the DB by now. At that point, record the master
136 // position and pass it along as the job recursively breaks into smaller range jobs.
137 // Hopefully, when leaf jobs are popped, the slaves will have reached that position.
138 if ( isset( $this->params['masterPos'] ) ) {
139 $masterPos = $this->params['masterPos'];
140 } elseif ( wfGetLB()->getServerCount() > 1 ) {
141 $masterPos = wfGetLB()->getMasterPos();
142 } else {
143 $masterPos = false;
144 }
145
146 $tbc = $this->title->getBacklinkCache();
147
148 $jobs = array(); // jobs to insert
149 if ( isset( $this->params['start'] ) && isset( $this->params['end'] ) ) {
150 # This is a partition job to trigger the insertion of leaf jobs...
151 $jobs = array_merge( $jobs, $this->getSingleTitleJobs( $table, $masterPos ) );
152 } else {
153 # This is a base job to trigger the insertion of partitioned jobs...
154 if ( $tbc->getNumLinks( $table, $wgUpdateRowsPerJob + 1 ) <= $wgUpdateRowsPerJob ) {
155 # Just directly insert the single per-title jobs
156 $jobs = array_merge( $jobs, $this->getSingleTitleJobs( $table, $masterPos ) );
157 } else {
158 # Insert the partition jobs to make per-title jobs
159 foreach ( $tbc->partition( $table, $wgUpdateRowsPerJob ) as $batch ) {
160 list( $start, $end ) = $batch;
161 $jobs[] = new RefreshLinksJob2( $this->title,
162 array(
163 'table' => $table,
164 'start' => $start,
165 'end' => $end,
166 'masterPos' => $masterPos,
167 ) + $this->getRootJobParams() // carry over information for de-duplication
168 );
169 }
170 }
171 }
172
173 if ( count( $jobs ) ) {
174 JobQueueGroup::singleton()->push( $jobs );
175 }
176
177 return true;
178 }
179
180 /**
181 * @param $table string
182 * @param $masterPos mixed
183 * @return Array
184 */
185 protected function getSingleTitleJobs( $table, $masterPos ) {
186 # The "start"/"end" fields are not set for the base jobs
187 $start = isset( $this->params['start'] ) ? $this->params['start'] : false;
188 $end = isset( $this->params['end'] ) ? $this->params['end'] : false;
189 $titles = $this->title->getBacklinkCache()->getLinks( $table, $start, $end );
190 # Convert into single page refresh links jobs.
191 # This handles well when in sapi mode and is useful in any case for job
192 # de-duplication. If many pages use template A, and that template itself
193 # uses template B, then an edit to both will create many duplicate jobs.
194 # Roughly speaking, for each page, one of the "RefreshLinksJob" jobs will
195 # get run first, and when it does, it will remove the duplicates. Of course,
196 # one page could have its job popped when the other page's job is still
197 # buried within the logic of a refreshLinks2 job.
198 $jobs = array();
199 foreach ( $titles as $title ) {
200 $jobs[] = new RefreshLinksJob( $title,
201 array( 'masterPos' => $masterPos ) + $this->getRootJobParams()
202 ); // carry over information for de-duplication
203 }
204 return $jobs;
205 }
206
207 /**
208 * @return Array
209 */
210 public function getDeduplicationInfo() {
211 $info = parent::getDeduplicationInfo();
212 // Don't let highly unique "masterPos" values ruin duplicate detection
213 if ( is_array( $info['params'] ) ) {
214 unset( $info['params']['masterPos'] );
215 }
216 return $info;
217 }
218 }