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