6 /* private */ var $mId, $mTitle;
8 function LinksUpdate( $id, $title )
11 $this->mTitle
= $title;
12 $this->mTitleEnc
= wfStrencode( $title );
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 );
30 if( $wgDBtransactions ) {
32 wfQuery( $sql, DB_WRITE
, $fname );
35 #------------------------------------------------------------------------------
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 );
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();
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 ";
62 foreach( $add as $lt => $lid ) {
64 if ( ! $first ) { $sql .= ","; }
67 $sql .= "({$this->mId},{$lid})";
71 wfQuery( $sql, DB_WRITE
, $fname );
74 #------------------------------------------------------------------------------
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 );
86 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
87 wfQuery( $sql, DB_WRITE
, $fname );
90 $add = $wgLinkCache->getBadLinks();
95 if ( 0 != count ( $add ) ) {
96 $sql = "INSERT IGNORE INTO brokenlinks (bl_from,bl_to) VALUES ";
98 foreach( $add as $blt ) {
99 $blt = wfStrencode( $blt );
100 if ( ! $first ) { $sql .= ","; }
103 $sql .= "({$this->mId},'{$blt}')";
107 wfQuery( $sql, DB_WRITE
, $fname );
110 #------------------------------------------------------------------------------
112 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
113 wfQuery( $sql, DB_WRITE
, $fname );
116 $add = $wgLinkCache->getImageLinks();
120 $image = Namespace::getImage();
121 if ( 0 != count ( $add ) ) {
122 $sql = "INSERT IGNORE INTO imagelinks (il_from,il_to) VALUES ";
124 foreach( $add as $iname => $val ) {
125 # FIXME: Change all this to avoid unnecessary duplication
126 $nt = Title
::makeTitle( $image, $iname );
128 $nt->invalidateCache();
130 $iname = wfStrencode( $iname );
131 if ( ! $first ) { $sql .= ","; }
134 $sql .= "({$this->mId},'{$iname}')";
137 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
139 #------------------------------------------------------------------------------
141 if( $wgUseCategoryMagic ) {
142 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
143 wfQuery( $sql, DB_WRITE
, $fname );
146 $add = $wgLinkCache->getCategoryLinks();
150 if ( 0 != count ( $add ) ) {
151 $sql = "INSERT IGNORE INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
153 foreach( $add as $cname => $sortkey ) {
154 # FIXME: Change all this to avoid unnecessary duplication
155 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
157 $nt->invalidateCache();
159 if ( ! $first ) { $sql .= ","; }
162 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
163 "','" . wfStrencode( $sortkey ) . "')";
166 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
169 $this->fixBrokenLinks();
171 if( $wgDBtransactions ) {
173 wfQuery( $sql, DB_WRITE
, $fname );
175 wfProfileOut( $fname );
178 function doDumbUpdate()
180 # Old inefficient update function
181 # Used for rebuilding the link table
183 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
184 $fname = "LinksUpdate::doDumbUpdate";
185 wfProfileIn( $fname );
187 if( $wgDBtransactions ) {
189 wfQuery( $sql, DB_WRITE
, $fname );
192 $sql = "DELETE FROM links WHERE l_from={$this->mId}";
193 wfQuery( $sql, DB_WRITE
, $fname );
195 $a = $wgLinkCache->getGoodLinks();
197 if ( 0 != count( $a ) ) {
198 $sql = "INSERT IGNORE INTO links (l_from,l_to) VALUES ";
200 foreach( $a as $lt => $lid ) {
201 if ( ! $first ) { $sql .= ","; }
204 $sql .= "({$this->mId},{$lid})";
207 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
209 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
210 wfQuery( $sql, DB_WRITE
, $fname );
212 $a = $wgLinkCache->getBadLinks();
214 if ( 0 != count ( $a ) ) {
215 $sql = "INSERT IGNORE INTO brokenlinks (bl_from,bl_to) VALUES ";
217 foreach( $a as $blt ) {
218 $blt = wfStrencode( $blt );
219 if ( ! $first ) { $sql .= ","; }
222 $sql .= "({$this->mId},'{$blt}')";
225 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
227 $sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
228 wfQuery( $sql, DB_WRITE
, $fname );
230 $a = $wgLinkCache->getImageLinks();
232 if ( 0 != count ( $a ) ) {
233 $sql = "INSERT IGNORE INTO imagelinks (il_from,il_to) VALUES ";
235 foreach( $a as $iname => $val ) {
236 $iname = wfStrencode( $iname );
237 if ( ! $first ) { $sql .= ","; }
240 $sql .= "({$this->mId},'{$iname}')";
243 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
245 if( $wgUseCategoryMagic ) {
246 $sql = "DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
247 wfQuery( $sql, DB_WRITE
, $fname );
250 $add = $wgLinkCache->getCategoryLinks();
254 if ( 0 != count ( $add ) ) {
255 $sql = "INSERT IGNORE INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
257 foreach( $add as $cname => $sortkey ) {
258 # FIXME: Change all this to avoid unnecessary duplication
259 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
261 $nt->invalidateCache();
263 if ( ! $first ) { $sql .= ","; }
266 $sql .= "({$this->mId},'" . wfStrencode( $cname ) .
267 "','" . wfStrencode( $sortkey ) . "')";
270 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE
, $fname ); }
272 $this->fixBrokenLinks();
274 if( $wgDBtransactions ) {
276 wfQuery( $sql, DB_WRITE
, $fname );
278 wfProfileOut( $fname );
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";
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; }
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 (";
296 while ( $row = wfFetchObject( $res ) ) {
297 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
300 $sql .= "({$row->bl_from},{$this->mId})";
301 $sql2 .= $row->bl_from
;
304 wfQuery( $sql, DB_WRITE
, $fname );
305 wfQuery( $sql2, DB_WRITE
, $fname );
307 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
308 wfQuery( $sql, DB_WRITE
, $fname );