A few language files from some of the larger Wikipedias, extracted from the MediaWiki...
[mediawiki.git] / includes / LinksUpdate.php
blob104e44247152def574bf7a403c85cf02dd2d50a3
1 <?php
2 # See deferred.doc
4 class LinksUpdate {
6 /* private */ var $mId, $mTitle;
8 function LinksUpdate( $id, $title )
10 $this->mId = $id;
11 $this->mTitle = $title;
12 $this->mTitleEnc = wfStrencode( $title );
16 function doUpdate()
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC, $wgUseCategoryMagic;
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
24 $fname = "LinksUpdate::doUpdate";
25 wfProfileIn( $fname );
27 $del = array();
28 $add = array();
30 if( $wgDBtransactions ) {
31 $sql = "BEGIN";
32 wfQuery( $sql, DB_WRITE, $fname );
35 #------------------------------------------------------------------------------
36 # Good links
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 );
45 } else {
46 # Delete everything
47 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
48 wfQuery( $sql, DB_WRITE, $fname );
50 # Get the addition list
51 $add = $wgLinkCache->getGoodLinks();
54 # Do the insertion
55 $sql = "";
56 if ( 0 != count( $add ) ) {
57 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
58 $first = true;
59 foreach( $add as $lt => $lid ) {
61 if ( ! $first ) { $sql .= ","; }
62 $first = false;
64 $sql .= "({$this->mId},{$lid})";
67 if ( "" != $sql ) {
68 wfQuery( $sql, DB_WRITE, $fname );
71 #------------------------------------------------------------------------------
72 # Bad links
74 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
75 # Delete where necessary
76 if ( count( $del ) ) {
77 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
78 implode( "','", array_map( "wfStrencode", $del ) ) . "')";
79 wfQuery( $sql, DB_WRITE, $fname );
81 } else {
82 # Delete all
83 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
84 wfQuery( $sql, DB_WRITE, $fname );
86 # Get addition list
87 $add = $wgLinkCache->getBadLinks();
90 # Do additions
91 $sql = "";
92 if ( 0 != count ( $add ) ) {
93 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
94 $first = true;
95 foreach( $add as $blt ) {
96 $blt = wfStrencode( $blt );
97 if ( ! $first ) { $sql .= ","; }
98 $first = false;
100 $sql .= "({$this->mId},'{$blt}')";
103 if ( "" != $sql ) {
104 wfQuery( $sql, DB_WRITE, $fname );
107 #------------------------------------------------------------------------------
108 # Image links
109 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
110 wfQuery( $sql, DB_WRITE, $fname );
112 # Get addition list
113 $add = $wgLinkCache->getImageLinks();
115 # Do the insertion
116 $sql = "";
117 $image = Namespace::getImage();
118 if ( 0 != count ( $add ) ) {
119 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
120 $first = true;
121 foreach( $add as $iname => $val ) {
122 # FIXME: Change all this to avoid unnecessary duplication
123 $nt = Title::makeTitle( $image, $iname );
124 if( !$nt ) continue;
125 $nt->invalidateCache();
127 $iname = wfStrencode( $iname );
128 if ( ! $first ) { $sql .= ","; }
129 $first = false;
131 $sql .= "({$this->mId},'{$iname}')";
134 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
136 #------------------------------------------------------------------------------
137 # Category links
138 if( $wgUseCategoryMagic ) {
139 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
140 wfQuery( $sql, DB_WRITE, $fname );
142 # Get addition list
143 $add = $wgLinkCache->getCategoryLinks();
145 # Do the insertion
146 $sql = "";
147 if ( 0 != count ( $add ) ) {
148 $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
149 $first = true;
150 foreach( $add as $cname => $sortkey ) {
151 # FIXME: Change all this to avoid unnecessary duplication
152 $nt = Title::makeTitle( NS_CATEGORY, $cname );
153 if( !$nt ) continue;
154 $nt->invalidateCache();
156 if ( ! $first ) { $sql .= ","; }
157 $first = false;
159 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
160 "','" . wfStrencode( $sortkey ) . "')";
163 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
166 $this->fixBrokenLinks();
168 if( $wgDBtransactions ) {
169 $sql = "COMMIT";
170 wfQuery( $sql, DB_WRITE, $fname );
172 wfProfileOut( $fname );
175 function doDumbUpdate()
177 # Old inefficient update function
178 # Used for rebuilding the link table
180 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
181 $fname = "LinksUpdate::doDumbUpdate";
182 wfProfileIn( $fname );
184 if( $wgDBtransactions ) {
185 $sql = "BEGIN";
186 wfQuery( $sql, DB_WRITE, $fname );
189 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
190 wfQuery( $sql, DB_WRITE, $fname );
192 $a = $wgLinkCache->getGoodLinks();
193 $sql = "";
194 if ( 0 != count( $a ) ) {
195 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
196 $first = true;
197 foreach( $a as $lt => $lid ) {
198 if ( ! $first ) { $sql .= ","; }
199 $first = false;
201 $sql .= "({$this->mId},{$lid})";
204 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
206 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
207 wfQuery( $sql, DB_WRITE, $fname );
209 $a = $wgLinkCache->getBadLinks();
210 $sql = "";
211 if ( 0 != count ( $a ) ) {
212 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
213 $first = true;
214 foreach( $a as $blt ) {
215 $blt = wfStrencode( $blt );
216 if ( ! $first ) { $sql .= ","; }
217 $first = false;
219 $sql .= "({$this->mId},'{$blt}')";
222 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
224 $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
225 wfQuery( $sql, DB_WRITE, $fname );
227 $a = $wgLinkCache->getImageLinks();
228 $sql = "";
229 if ( 0 != count ( $a ) ) {
230 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
231 $first = true;
232 foreach( $a as $iname => $val ) {
233 $iname = wfStrencode( $iname );
234 if ( ! $first ) { $sql .= ","; }
235 $first = false;
237 $sql .= "({$this->mId},'{$iname}')";
240 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
242 if( $wgUseCategoryMagic ) {
243 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
244 wfQuery( $sql, DB_WRITE, $fname );
246 # Get addition list
247 $add = $wgLinkCache->getCategoryLinks();
249 # Do the insertion
250 $sql = "";
251 if ( 0 != count ( $add ) ) {
252 $sql = "INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
253 $first = true;
254 foreach( $add as $cname => $sortkey ) {
255 # FIXME: Change all this to avoid unnecessary duplication
256 $nt = Title::makeTitle( NS_CATEGORY, $cname );
257 if( !$nt ) continue;
258 $nt->invalidateCache();
260 if ( ! $first ) { $sql .= ","; }
261 $first = false;
263 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
264 "','" . wfStrencode( $sortkey ) . "')";
267 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
269 $this->fixBrokenLinks();
271 if( $wgDBtransactions ) {
272 $sql = "COMMIT";
273 wfQuery( $sql, DB_WRITE, $fname );
275 wfProfileOut( $fname );
278 function fixBrokenLinks() {
279 /* Update any brokenlinks *to* this page */
280 /* Call for a newly created page, or just to make sure state is consistent */
281 $fname = "LinksUpdate::fixBrokenLinks";
283 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
284 $res = wfQuery( $sql, DB_READ, $fname );
285 if ( 0 == wfNumRows( $res ) ) { return; }
287 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
288 $now = wfTimestampNow();
289 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
290 $first = true;
291 while ( $row = wfFetchObject( $res ) ) {
292 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
293 $first = false;
295 $sql .= "({$row->bl_from},{$this->mId})";
296 $sql2 .= $row->bl_from;
298 $sql2 .= ")";
299 wfQuery( $sql, DB_WRITE, $fname );
300 wfQuery( $sql2, DB_WRITE, $fname );
302 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
303 wfQuery( $sql, DB_WRITE, $fname );