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 ) ) {
57 foreach($add as $lt=>$lid)
58 array_push($arr,array(
61 # The link cache was constructed without FOR UPDATE, so there may be collisions
62 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
63 # sure it's better than without IGNORE
64 $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
67 #------------------------------------------------------------------------------
70 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD
, $del, $add ) ) {
71 # Delete where necessary
72 if ( count( $del ) ) {
73 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
75 foreach( $del as $badTitle ) {
81 $sql .= $dbw->addQuotes( $badTitle );
84 $dbw->query( $sql, $fname );
88 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
91 $add = $wgLinkCache->getBadLinks();
96 if ( 0 != count ( $add ) ) {
98 foreach( $add as $blt ) {
99 $blt = $dbw->strencode( $blt );
100 array_push($arr,array(
101 'bl_from'=>$this->mId
,
104 $dbw->insertArray( 'brokenlinks',$arr,array('IGNORE'));
105 $dbw->query( $sql, $fname );
108 #------------------------------------------------------------------------------
110 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
111 $dbw->query( $sql, $fname );
114 $add = $wgLinkCache->getImageLinks();
118 $image = Namespace::getImage();
119 if ( 0 != count ( $add ) ) {
120 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
122 foreach( $add as $iname => $val ) {
123 # FIXME: Change all this to avoid unnecessary duplication
124 $nt = Title
::makeTitle( $image, $iname );
126 $nt->invalidateCache();
128 $iname = $dbw->strencode( $iname );
129 if ( ! $first ) { $sql .= ','; }
132 $sql .= "({$this->mId},'{$iname}')";
136 $dbw->query( $sql, $fname );
139 #------------------------------------------------------------------------------
141 if( $wgUseCategoryMagic ) {
142 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
143 $dbw->query( $sql, $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},'" . $dbw->strencode( $cname ) .
163 "','" . $dbw->strencode( $sortkey ) . "')";
167 $dbw->query( $sql, $fname );
171 $this->fixBrokenLinks();
173 wfProfileOut( $fname );
176 function doDumbUpdate()
178 # Old inefficient update function
179 # Used for rebuilding the link table
180 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
181 $fname = 'LinksUpdate::doDumbUpdate';
182 wfProfileIn( $fname );
185 $dbw =& wfGetDB( DB_MASTER
);
186 $links = $dbw->tableName( 'links' );
187 $brokenlinks = $dbw->tableName( 'brokenlinks' );
188 $imagelinks = $dbw->tableName( 'imagelinks' );
189 $categorylinks = $dbw->tableName( 'categorylinks' );
191 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
192 $dbw->query( $sql, $fname );
194 $a = $wgLinkCache->getGoodLinks();
196 if ( 0 != count( $a ) ) {
197 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
199 foreach( $a as $lt => $lid ) {
200 if ( ! $first ) { $sql .= ","; }
203 $sql .= "({$this->mId},{$lid})";
207 $dbw->query( $sql, $fname );
210 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
211 $dbw->query( $sql, $fname );
213 $a = $wgLinkCache->getBadLinks();
215 if ( 0 != count ( $a ) ) {
216 $sql = "INSERT IGNORE INTO $brokenlinks (bl_from,bl_to) VALUES ";
218 foreach( $a as $blt ) {
219 $blt = $dbw->strencode( $blt );
220 if ( ! $first ) { $sql .= ","; }
223 $sql .= "({$this->mId},'{$blt}')";
227 $dbw->query( $sql, $fname );
230 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
231 $dbw->query( $sql, $fname );
233 $a = $wgLinkCache->getImageLinks();
235 if ( 0 != count ( $a ) ) {
236 $sql = "INSERT IGNORE INTO $imagelinks (il_from,il_to) VALUES ";
238 foreach( $a as $iname => $val ) {
239 $iname = $dbw->strencode( $iname );
240 if ( ! $first ) { $sql .= ","; }
243 $sql .= "({$this->mId},'{$iname}')";
247 $dbw->query( $sql, $fname );
250 if( $wgUseCategoryMagic ) {
251 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
252 $dbw->query( $sql, $fname );
255 $add = $wgLinkCache->getCategoryLinks();
259 if ( 0 != count ( $add ) ) {
260 $sql = "INSERT IGNORE INTO $categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
262 foreach( $add as $cname => $sortkey ) {
263 # FIXME: Change all this to avoid unnecessary duplication
264 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
266 $nt->invalidateCache();
268 if ( ! $first ) { $sql .= ","; }
271 $sql .= "({$this->mId},'" . $dbw->strencode( $cname ) .
272 "','" . $dbw->strencode( $sortkey ) . "')";
276 $dbw->query( $sql, $fname );
279 $this->fixBrokenLinks();
280 wfProfileOut( $fname );
283 function fixBrokenLinks() {
284 /* Update any brokenlinks *to* this page */
285 /* Call for a newly created page, or just to make sure state is consistent */
286 $fname = 'LinksUpdate::fixBrokenLinks';
288 $dbw =& wfGetDB( DB_MASTER
);
289 $cur = $dbw->tableName( 'cur' );
290 $links = $dbw->tableName( 'links' );
292 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
293 $fname, 'FOR UPDATE' );
294 if ( 0 == $dbw->numRows( $res ) ) { return; }
296 # Ignore errors. If a link existed in both the brokenlinks table and the links
297 # table, that's an error which can be fixed at this stage by simply ignoring collisions
298 $sql = "INSERT IGNORE INTO $links (l_from,l_to) VALUES ";
299 $now = wfTimestampNow();
300 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
302 while ( $row = $dbw->fetchObject( $res ) ) {
303 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
306 $sql .= "({$row->bl_from},{$this->mId})";
307 $sql2 .= $row->bl_from
;
310 $dbw->query( $sql, $fname );
311 $dbw->query( $sql2, $fname );
313 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );