13 /* private */ var $mId, $mTitle;
15 function LinksUpdate( $id, $title )
18 $this->mTitle
= $title;
24 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
25 global $wgEnablePersistentLC, $wgUseCategoryMagic;
27 /* Update link tables with outgoing links from an updated article */
28 /* Relies on the 'link cache' to be filled out */
30 $fname = 'LinksUpdate::doUpdate';
31 wfProfileIn( $fname );
36 $dbw =& wfGetDB( DB_MASTER
);
37 $links = $dbw->tableName( 'links' );
38 $brokenlinks = $dbw->tableName( 'brokenlinks' );
39 $imagelinks = $dbw->tableName( 'imagelinks' );
40 $categorylinks = $dbw->tableName( 'categorylinks' );
42 #------------------------------------------------------------------------------
45 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD
, $del, $add ) ) {
46 # Delete where necessary
47 if ( count( $del ) ) {
48 $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
49 implode( ',', $del ) . ')';
50 $dbw->query( $sql, $fname );
54 $dbw->delete( 'links', array( 'l_from' => $this->mId
), $fname );
56 # Get the addition list
57 $add = $wgLinkCache->getGoodLinks();
61 if ( 0 != count( $add ) ) {
63 foreach($add as $lt=>$lid)
64 array_push($arr,array(
67 # The link cache was constructed without FOR UPDATE, so there may be collisions
68 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
69 # sure it's better than without IGNORE
70 $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
73 #------------------------------------------------------------------------------
76 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD
, $del, $add ) ) {
77 # Delete where necessary
78 if ( count( $del ) ) {
79 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
81 foreach( $del as $badTitle ) {
87 $sql .= $dbw->addQuotes( $badTitle );
90 $dbw->query( $sql, $fname );
94 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
97 $add = $wgLinkCache->getBadLinks();
102 if ( 0 != count ( $add ) ) {
104 foreach( $add as $blt ) {
105 $blt = $dbw->strencode( $blt );
106 array_push($arr,array(
107 'bl_from'=>$this->mId
,
110 $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
113 #------------------------------------------------------------------------------
115 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
116 $dbw->query( $sql, $fname );
119 $add = $wgLinkCache->getImageLinks();
123 $image = Namespace::getImage();
124 if ( 0 != count ( $add ) ) {
126 foreach ($add as $iname => $val ) {
127 $nt = Title
::makeTitle( $image, $iname );
129 $nt->invalidateCache();
130 $iname = $dbw->strencode( $iname );
131 array_push($arr,array(
132 'il_from'=>$this->mId
,
135 $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
138 #------------------------------------------------------------------------------
140 if( $wgUseCategoryMagic ) {
141 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
142 $dbw->query( $sql, $fname );
145 $add = $wgLinkCache->getCategoryLinks();
149 if ( 0 != count ( $add ) ) {
151 foreach( $add as $cname => $sortkey ) {
152 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
154 $nt->invalidateCache();
155 array_push($arr,array(
156 'cl_from'=>$this->mId
,
157 'cl_to'=>$dbw->strencode( $cname ),
158 'cl_sortkey'=>$dbw->strencode( $sortkey )));
160 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
164 $this->fixBrokenLinks();
166 wfProfileOut( $fname );
169 function doDumbUpdate()
171 # Old inefficient update function
172 # Used for rebuilding the link table
173 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
174 $fname = 'LinksUpdate::doDumbUpdate';
175 wfProfileIn( $fname );
178 $dbw =& wfGetDB( DB_MASTER
);
179 $links = $dbw->tableName( 'links' );
180 $brokenlinks = $dbw->tableName( 'brokenlinks' );
181 $imagelinks = $dbw->tableName( 'imagelinks' );
182 $categorylinks = $dbw->tableName( 'categorylinks' );
184 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
185 $dbw->query( $sql, $fname );
187 $a = $wgLinkCache->getGoodLinks();
188 if ( 0 != count( $a ) ) {
190 foreach( $a as $lt => $lid ) {
191 array_push($arr,array(
192 'l_from'=>$this->mId
,
195 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
198 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
199 $dbw->query( $sql, $fname );
201 $a = $wgLinkCache->getBadLinks();
202 if ( 0 != count ( $a ) ) {
204 foreach( $a as $blt ) {
205 $blt = $dbw->strencode( $blt );
206 array_push($arr,array(
207 'bl_from'=>$this->mId
,
210 $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
213 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
214 $dbw->query( $sql, $fname );
216 $a = $wgLinkCache->getImageLinks();
218 if ( 0 != count ( $a ) ) {
220 foreach( $a as $iname => $val )
221 array_push($arr,array(
222 'il_from'=>$this->mId
,
223 'il_to'=>$dbw->strencode( $iname )));
224 $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
227 if( $wgUseCategoryMagic ) {
228 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
229 $dbw->query( $sql, $fname );
232 $add = $wgLinkCache->getCategoryLinks();
236 if ( 0 != count ( $add ) ) {
238 foreach( $add as $cname => $sortkey ) {
239 # FIXME: Change all this to avoid unnecessary duplication
240 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
242 $nt->invalidateCache();
243 array_push($arr,array(
244 'cl_from'=>$this->mId
,
245 'cl_to'=>$dbw->strencode( $cname ),
246 'cl_sortkey'=>$dbw->strencode( $sortkey )));
248 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
251 $this->fixBrokenLinks();
252 wfProfileOut( $fname );
255 function fixBrokenLinks() {
256 /* Update any brokenlinks *to* this page */
257 /* Call for a newly created page, or just to make sure state is consistent */
258 $fname = 'LinksUpdate::fixBrokenLinks';
260 $dbw =& wfGetDB( DB_MASTER
);
261 $cur = $dbw->tableName( 'cur' );
262 $links = $dbw->tableName( 'links' );
264 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
265 $fname, 'FOR UPDATE' );
266 if ( 0 == $dbw->numRows( $res ) ) { return; }
268 # Ignore errors. If a link existed in both the brokenlinks table and the links
269 # table, that's an error which can be fixed at this stage by simply ignoring collisions
271 $now = $dbw->timestamp();
272 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
274 while ( $row = $dbw->fetchObject( $res ) ) {
275 if ( ! $first ) { $sql2 .= ","; }
277 array_push($arr,array('l_from'=>$row->bl_from
,'l_to'=>$this->mId
));
278 $sql2 .= $row->bl_from
;
281 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
282 $dbw->query( $sql2, $fname );
283 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );