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 );
89 $dbw->query( $sql, $fname );
93 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
96 $add = $wgLinkCache->getBadLinks();
101 if ( 0 != count ( $add ) ) {
102 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
104 foreach( $add as $blt ) {
105 $blt = $dbw->strencode( $blt );
106 if ( ! $first ) { $sql .= ","; }
109 $sql .= "({$this->mId},'{$blt}')";
113 $dbw->query( $sql, $fname );
116 #------------------------------------------------------------------------------
118 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
119 $dbw->query( $sql, $fname );
122 $add = $wgLinkCache->getImageLinks();
126 $image = Namespace::getImage();
127 if ( 0 != count ( $add ) ) {
128 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
130 foreach( $add as $iname => $val ) {
131 # FIXME: Change all this to avoid unnecessary duplication
132 $nt = Title
::makeTitle( $image, $iname );
134 $nt->invalidateCache();
136 $iname = $dbw->strencode( $iname );
137 if ( ! $first ) { $sql .= ","; }
140 $sql .= "({$this->mId},'{$iname}')";
144 $dbw->query( $sql, $fname );
147 #------------------------------------------------------------------------------
149 if( $wgUseCategoryMagic ) {
150 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
151 $dbw->query( $sql, $fname );
154 $add = $wgLinkCache->getCategoryLinks();
158 if ( 0 != count ( $add ) ) {
159 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
161 foreach( $add as $cname => $sortkey ) {
162 # FIXME: Change all this to avoid unnecessary duplication
163 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
165 $nt->invalidateCache();
167 if ( ! $first ) { $sql .= ","; }
170 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
171 "','" . $dbw->strencode( $sortkey ) . "')";
175 $dbw->query( $sql, $fname );
179 $this->fixBrokenLinks();
181 wfProfileOut( $fname );
184 function doDumbUpdate()
186 # Old inefficient update function
187 # Used for rebuilding the link table
188 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
189 $fname = "LinksUpdate::doDumbUpdate";
190 wfProfileIn( $fname );
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' );
199 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
200 $dbw->query( $sql, $fname );
202 $a = $wgLinkCache->getGoodLinks();
204 if ( 0 != count( $a ) ) {
205 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
207 foreach( $a as $lt => $lid ) {
208 if ( ! $first ) { $sql .= ","; }
211 $sql .= "({$this->mId},{$lid})";
215 $dbw->query( $sql, $fname );
218 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
219 $dbw->query( $sql, $fname );
221 $a = $wgLinkCache->getBadLinks();
223 if ( 0 != count ( $a ) ) {
224 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
226 foreach( $a as $blt ) {
227 $blt = $dbw->strencode( $blt );
228 if ( ! $first ) { $sql .= ","; }
231 $sql .= "({$this->mId},'{$blt}')";
235 $dbw->query( $sql, $fname );
238 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
239 $dbw->query( $sql, $fname );
241 $a = $wgLinkCache->getImageLinks();
243 if ( 0 != count ( $a ) ) {
244 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
246 foreach( $a as $iname => $val ) {
247 $iname = $dbw->strencode( $iname );
248 if ( ! $first ) { $sql .= ","; }
251 $sql .= "({$this->mId},'{$iname}')";
255 $dbw->query( $sql, $fname );
258 if( $wgUseCategoryMagic ) {
259 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
260 $dbw->query( $sql, $fname );
263 $add = $wgLinkCache->getCategoryLinks();
267 if ( 0 != count ( $add ) ) {
268 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
270 foreach( $add as $cname => $sortkey ) {
271 # FIXME: Change all this to avoid unnecessary duplication
272 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
274 $nt->invalidateCache();
276 if ( ! $first ) { $sql .= ","; }
279 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
280 "','" . $dbw->strencode( $sortkey ) . "')";
284 $dbw->query( $sql, $fname );
287 $this->fixBrokenLinks();
288 wfProfileOut( $fname );
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";
296 $dbw =& wfGetDB( DB_MASTER
);
297 $cur = $dbw->tableName( 'cur' );
298 $links = $dbw->tableName( 'links' );
300 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
301 $fname, 'FOR UPDATE' );
302 if ( 0 == $dbw->numRows( $res ) ) { return; }
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 (";
310 while ( $row = $dbw->fetchObject( $res ) ) {
311 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
314 $sql .= "({$row->bl_from},{$this->mId})";
315 $sql2 .= $row->bl_from
;
318 $dbw->query( $sql, $fname );
319 $dbw->query( $sql2, $fname );
321 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );