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