6 /* private */ var $mId, $mTitle;
8 function LinksUpdate( $id, $title )
11 $this->mTitle
= $title;
17 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
18 global $wgEnablePersistentLC, $wgUseCategoryMagic;
20 /* Update link tables with outgoing links from an updated article */
21 /* Relies on the 'link cache' to be filled out */
23 $fname = "LinksUpdate::doUpdate";
24 wfProfileIn( $fname );
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' );
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 $dbw->query( $sql, $fname );
47 $dbw->delete( 'links', array( 'l_from' => $this->mId
), $fname );
49 # Get the addition list
50 $add = $wgLinkCache->getGoodLinks();
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 ";
61 foreach( $add as $lt => $lid ) {
62 if ( ! $first ) { $sql .= ","; }
65 $sql .= "({$this->mId},{$lid})";
69 $dbw->query( $sql, $fname );
72 #------------------------------------------------------------------------------
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(";
80 foreach( $del as $badTitle ) {
86 $sql .= $dbw->addQuotes( $badTitle );
88 $dbw->query( $sql, $fname );
92 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
95 $add = $wgLinkCache->getBadLinks();
100 if ( 0 != count ( $add ) ) {
101 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
103 foreach( $add as $blt ) {
104 $blt = $dbw->strencode( $blt );
105 if ( ! $first ) { $sql .= ","; }
108 $sql .= "({$this->mId},'{$blt}')";
112 $dbw->query( $sql, $fname );
115 #------------------------------------------------------------------------------
117 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
118 $dbw->query( $sql, $fname );
121 $add = $wgLinkCache->getImageLinks();
125 $image = Namespace::getImage();
126 if ( 0 != count ( $add ) ) {
127 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
129 foreach( $add as $iname => $val ) {
130 # FIXME: Change all this to avoid unnecessary duplication
131 $nt = Title
::makeTitle( $image, $iname );
133 $nt->invalidateCache();
135 $iname = $dbw->strencode( $iname );
136 if ( ! $first ) { $sql .= ","; }
139 $sql .= "({$this->mId},'{$iname}')";
143 $dbw->query( $sql, $fname );
146 #------------------------------------------------------------------------------
148 if( $wgUseCategoryMagic ) {
149 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
150 $dbw->query( $sql, $fname );
153 $add = $wgLinkCache->getCategoryLinks();
157 if ( 0 != count ( $add ) ) {
158 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
160 foreach( $add as $cname => $sortkey ) {
161 # FIXME: Change all this to avoid unnecessary duplication
162 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
164 $nt->invalidateCache();
166 if ( ! $first ) { $sql .= ","; }
169 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
170 "','" . $dbw->strencode( $sortkey ) . "')";
174 $dbw->query( $sql, $fname );
178 $this->fixBrokenLinks();
180 wfProfileOut( $fname );
183 function doDumbUpdate()
185 # Old inefficient update function
186 # Used for rebuilding the link table
187 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
188 $fname = "LinksUpdate::doDumbUpdate";
189 wfProfileIn( $fname );
192 $dbw =& wfGetDB( DB_MASTER
);
193 $links = $dbw->tableName( 'links' );
194 $brokenlinks = $dbw->tableName( 'brokenlinks' );
195 $imagelinks = $dbw->tableName( 'imagelinks' );
196 $categorylinks = $dbw->tableName( 'categorylinks' );
198 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
199 $dbw->query( $sql, $fname );
201 $a = $wgLinkCache->getGoodLinks();
203 if ( 0 != count( $a ) ) {
204 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
206 foreach( $a as $lt => $lid ) {
207 if ( ! $first ) { $sql .= ","; }
210 $sql .= "({$this->mId},{$lid})";
214 $dbw->query( $sql, $fname );
217 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
218 $dbw->query( $sql, $fname );
220 $a = $wgLinkCache->getBadLinks();
222 if ( 0 != count ( $a ) ) {
223 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
225 foreach( $a as $blt ) {
226 $blt = $dbw->strencode( $blt );
227 if ( ! $first ) { $sql .= ","; }
230 $sql .= "({$this->mId},'{$blt}')";
234 $dbw->query( $sql, $fname );
237 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
238 $dbw->query( $sql, $fname );
240 $a = $wgLinkCache->getImageLinks();
242 if ( 0 != count ( $a ) ) {
243 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
245 foreach( $a as $iname => $val ) {
246 $iname = $dbw->strencode( $iname );
247 if ( ! $first ) { $sql .= ","; }
250 $sql .= "({$this->mId},'{$iname}')";
254 $dbw->query( $sql, $fname );
257 if( $wgUseCategoryMagic ) {
258 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
259 $dbw->query( $sql, $fname );
262 $add = $wgLinkCache->getCategoryLinks();
266 if ( 0 != count ( $add ) ) {
267 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
269 foreach( $add as $cname => $sortkey ) {
270 # FIXME: Change all this to avoid unnecessary duplication
271 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
273 $nt->invalidateCache();
275 if ( ! $first ) { $sql .= ","; }
278 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
279 "','" . $dbw->strencode( $sortkey ) . "')";
283 $dbw->query( $sql, $fname );
286 $this->fixBrokenLinks();
287 wfProfileOut( $fname );
290 function fixBrokenLinks() {
291 /* Update any brokenlinks *to* this page */
292 /* Call for a newly created page, or just to make sure state is consistent */
293 $fname = "LinksUpdate::fixBrokenLinks";
295 $dbw =& wfGetDB( DB_MASTER
);
296 $cur = $dbw->tableName( 'cur' );
297 $links = $dbw->tableName( 'links' );
299 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
300 $fname, 'FOR UPDATE' );
301 if ( 0 == $dbw->numRows( $res ) ) { return; }
303 # Ignore errors. If a link existed in both the brokenlinks table and the links
304 # table, that's an error which can be fixed at this stage by simply ignoring collisions
305 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
306 $now = wfTimestampNow();
307 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
309 while ( $row = $dbw->fetchObject( $res ) ) {
310 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
313 $sql .= "({$row->bl_from},{$this->mId})";
314 $sql2 .= $row->bl_from
;
317 $dbw->query( $sql, $fname );
318 $dbw->query( $sql2, $fname );
320 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );