Typo/logic error in move page watchlist update.
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 # See deferred.doc
3
4 class LinksUpdate {
5
6 /* private */ var $mId, $mTitle;
7
8 function LinksUpdate( $id, $title )
9 {
10 $this->mId = $id;
11 $this->mTitle = $title;
12 }
13
14
15 function doUpdate()
16 {
17 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
18 global $wgEnablePersistentLC, $wgUseCategoryMagic;
19
20 /* Update link tables with outgoing links from an updated article */
21 /* Relies on the 'link cache' to be filled out */
22
23 $fname = "LinksUpdate::doUpdate";
24 wfProfileIn( $fname );
25
26 $del = array();
27 $add = array();
28
29 $dbw =& wfGetDB( DB_MASTER );
30 $links = $dbw->tableName( 'links' );
31 $brokenlinks = $dbw->tableName( 'brokenlinks' );
32 $imagelinks = $dbw->tableName( 'imagelinks' );
33 $categorylinks = $dbw->tableName( 'categorylinks' );
34
35 #------------------------------------------------------------------------------
36 # Good links
37
38 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
39 # Delete where necessary
40 if ( count( $del ) ) {
41 $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
42 implode( ",", $del ) . ")";
43 $dbw->query( $sql, $fname );
44 }
45 } else {
46 # Delete everything
47 $dbw->delete( 'links', array( 'l_from' => $this->mId ), $fname );
48
49 # Get the addition list
50 $add = $wgLinkCache->getGoodLinks();
51 }
52
53 # Do the insertion
54 $sql = "";
55 if ( 0 != count( $add ) ) {
56 # The link cache was constructed without FOR UPDATE, so there may be collisions
57 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
58 # sure it's better than without IGNORE
59 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
60 $first = true;
61 foreach( $add as $lt => $lid ) {
62 if ( ! $first ) { $sql .= ","; }
63 $first = false;
64
65 $sql .= "({$this->mId},{$lid})";
66 }
67 }
68 if ( "" != $sql ) {
69 $dbw->query( $sql, $fname );
70 }
71
72 #------------------------------------------------------------------------------
73 # Bad links
74
75 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
76 # Delete where necessary
77 if ( count( $del ) ) {
78 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
79 $first = true;
80 foreach( $del as $badTitle ) {
81 if ( $first ) {
82 $first = false;
83 } else {
84 $sql .= ",";
85 }
86 $sql .= $dbw->addQuotes( $badTitle );
87 }
88 $sql .= ")";
89 $dbw->query( $sql, $fname );
90 }
91 } else {
92 # Delete all
93 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId ) );
94
95 # Get addition list
96 $add = $wgLinkCache->getBadLinks();
97 }
98
99 # Do additions
100 $sql = "";
101 if ( 0 != count ( $add ) ) {
102 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
103 $first = true;
104 foreach( $add as $blt ) {
105 $blt = $dbw->strencode( $blt );
106 if ( ! $first ) { $sql .= ","; }
107 $first = false;
108
109 $sql .= "({$this->mId},'{$blt}')";
110 }
111 }
112 if ( "" != $sql ) {
113 $dbw->query( $sql, $fname );
114 }
115
116 #------------------------------------------------------------------------------
117 # Image links
118 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
119 $dbw->query( $sql, $fname );
120
121 # Get addition list
122 $add = $wgLinkCache->getImageLinks();
123
124 # Do the insertion
125 $sql = "";
126 $image = Namespace::getImage();
127 if ( 0 != count ( $add ) ) {
128 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
129 $first = true;
130 foreach( $add as $iname => $val ) {
131 # FIXME: Change all this to avoid unnecessary duplication
132 $nt = Title::makeTitle( $image, $iname );
133 if( !$nt ) continue;
134 $nt->invalidateCache();
135
136 $iname = $dbw->strencode( $iname );
137 if ( ! $first ) { $sql .= ","; }
138 $first = false;
139
140 $sql .= "({$this->mId},'{$iname}')";
141 }
142 }
143 if ( "" != $sql ) {
144 $dbw->query( $sql, $fname );
145 }
146
147 #------------------------------------------------------------------------------
148 # Category links
149 if( $wgUseCategoryMagic ) {
150 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
151 $dbw->query( $sql, $fname );
152
153 # Get addition list
154 $add = $wgLinkCache->getCategoryLinks();
155
156 # Do the insertion
157 $sql = "";
158 if ( 0 != count ( $add ) ) {
159 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
160 $first = true;
161 foreach( $add as $cname => $sortkey ) {
162 # FIXME: Change all this to avoid unnecessary duplication
163 $nt = Title::makeTitle( NS_CATEGORY, $cname );
164 if( !$nt ) continue;
165 $nt->invalidateCache();
166
167 if ( ! $first ) { $sql .= ","; }
168 $first = false;
169
170 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
171 "','" . $dbw->strencode( $sortkey ) . "')";
172 }
173 }
174 if ( "" != $sql ) {
175 $dbw->query( $sql, $fname );
176 }
177 }
178
179 $this->fixBrokenLinks();
180
181 wfProfileOut( $fname );
182 }
183
184 function doDumbUpdate()
185 {
186 # Old inefficient update function
187 # Used for rebuilding the link table
188 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
189 $fname = "LinksUpdate::doDumbUpdate";
190 wfProfileIn( $fname );
191
192
193 $dbw =& wfGetDB( DB_MASTER );
194 $links = $dbw->tableName( 'links' );
195 $brokenlinks = $dbw->tableName( 'brokenlinks' );
196 $imagelinks = $dbw->tableName( 'imagelinks' );
197 $categorylinks = $dbw->tableName( 'categorylinks' );
198
199 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
200 $dbw->query( $sql, $fname );
201
202 $a = $wgLinkCache->getGoodLinks();
203 $sql = "";
204 if ( 0 != count( $a ) ) {
205 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
206 $first = true;
207 foreach( $a as $lt => $lid ) {
208 if ( ! $first ) { $sql .= ","; }
209 $first = false;
210
211 $sql .= "({$this->mId},{$lid})";
212 }
213 }
214 if ( "" != $sql ) {
215 $dbw->query( $sql, $fname );
216 }
217
218 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
219 $dbw->query( $sql, $fname );
220
221 $a = $wgLinkCache->getBadLinks();
222 $sql = "";
223 if ( 0 != count ( $a ) ) {
224 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
225 $first = true;
226 foreach( $a as $blt ) {
227 $blt = $dbw->strencode( $blt );
228 if ( ! $first ) { $sql .= ","; }
229 $first = false;
230
231 $sql .= "({$this->mId},'{$blt}')";
232 }
233 }
234 if ( "" != $sql ) {
235 $dbw->query( $sql, $fname );
236 }
237
238 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
239 $dbw->query( $sql, $fname );
240
241 $a = $wgLinkCache->getImageLinks();
242 $sql = "";
243 if ( 0 != count ( $a ) ) {
244 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
245 $first = true;
246 foreach( $a as $iname => $val ) {
247 $iname = $dbw->strencode( $iname );
248 if ( ! $first ) { $sql .= ","; }
249 $first = false;
250
251 $sql .= "({$this->mId},'{$iname}')";
252 }
253 }
254 if ( "" != $sql ) {
255 $dbw->query( $sql, $fname );
256 }
257
258 if( $wgUseCategoryMagic ) {
259 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
260 $dbw->query( $sql, $fname );
261
262 # Get addition list
263 $add = $wgLinkCache->getCategoryLinks();
264
265 # Do the insertion
266 $sql = "";
267 if ( 0 != count ( $add ) ) {
268 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
269 $first = true;
270 foreach( $add as $cname => $sortkey ) {
271 # FIXME: Change all this to avoid unnecessary duplication
272 $nt = Title::makeTitle( NS_CATEGORY, $cname );
273 if( !$nt ) continue;
274 $nt->invalidateCache();
275
276 if ( ! $first ) { $sql .= ","; }
277 $first = false;
278
279 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
280 "','" . $dbw->strencode( $sortkey ) . "')";
281 }
282 }
283 if ( "" != $sql ) {
284 $dbw->query( $sql, $fname );
285 }
286 }
287 $this->fixBrokenLinks();
288 wfProfileOut( $fname );
289 }
290
291 function fixBrokenLinks() {
292 /* Update any brokenlinks *to* this page */
293 /* Call for a newly created page, or just to make sure state is consistent */
294 $fname = "LinksUpdate::fixBrokenLinks";
295
296 $dbw =& wfGetDB( DB_MASTER );
297 $cur = $dbw->tableName( 'cur' );
298 $links = $dbw->tableName( 'links' );
299
300 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle ),
301 $fname, 'FOR UPDATE' );
302 if ( 0 == $dbw->numRows( $res ) ) { return; }
303
304 # Ignore errors. If a link existed in both the brokenlinks table and the links
305 # table, that's an error which can be fixed at this stage by simply ignoring collisions
306 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
307 $now = wfTimestampNow();
308 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
309 $first = true;
310 while ( $row = $dbw->fetchObject( $res ) ) {
311 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
312 $first = false;
313
314 $sql .= "({$row->bl_from},{$this->mId})";
315 $sql2 .= $row->bl_from;
316 }
317 $sql2 .= ")";
318 $dbw->query( $sql, $fname );
319 $dbw->query( $sql2, $fname );
320
321 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle ), $fname );
322 }
323 }
324
325 ?>