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();
54 if ( 0 != count( $add ) ) {
56 foreach($add as $lt=>$lid)
57 array_push($arr,array(
60 # The link cache was constructed without FOR UPDATE, so there may be collisions
61 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
62 # sure it's better than without IGNORE
63 $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
66 #------------------------------------------------------------------------------
69 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD
, $del, $add ) ) {
70 # Delete where necessary
71 if ( count( $del ) ) {
72 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
74 foreach( $del as $badTitle ) {
80 $sql .= $dbw->addQuotes( $badTitle );
83 $dbw->query( $sql, $fname );
87 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
90 $add = $wgLinkCache->getBadLinks();
95 if ( 0 != count ( $add ) ) {
97 foreach( $add as $blt ) {
98 $blt = $dbw->strencode( $blt );
99 array_push($arr,array(
100 'bl_from'=>$this->mId
,
103 $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
106 #------------------------------------------------------------------------------
108 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
109 $dbw->query( $sql, $fname );
112 $add = $wgLinkCache->getImageLinks();
116 $image = Namespace::getImage();
117 if ( 0 != count ( $add ) ) {
119 foreach ($add as $iname => $val ) {
120 $nt = Title
::makeTitle( $image, $iname );
122 $nt->invalidateCache();
123 $iname = $dbw->strencode( $iname );
124 array_push($arr,array(
125 'il_from'=>$this->mId
,
128 $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
131 #------------------------------------------------------------------------------
133 if( $wgUseCategoryMagic ) {
134 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
135 $dbw->query( $sql, $fname );
138 $add = $wgLinkCache->getCategoryLinks();
142 if ( 0 != count ( $add ) ) {
144 foreach( $add as $cname => $sortkey ) {
145 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
147 $nt->invalidateCache();
148 array_push($arr,array(
149 'cl_from'=>$this->mId
,
150 'cl_to'=>$dbw->strencode( $cname ),
151 'cl_sortkey'=>$dbw->strencode( $sortkey )));
153 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
157 $this->fixBrokenLinks();
159 wfProfileOut( $fname );
162 function doDumbUpdate()
164 # Old inefficient update function
165 # Used for rebuilding the link table
166 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
167 $fname = 'LinksUpdate::doDumbUpdate';
168 wfProfileIn( $fname );
171 $dbw =& wfGetDB( DB_MASTER
);
172 $links = $dbw->tableName( 'links' );
173 $brokenlinks = $dbw->tableName( 'brokenlinks' );
174 $imagelinks = $dbw->tableName( 'imagelinks' );
175 $categorylinks = $dbw->tableName( 'categorylinks' );
177 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
178 $dbw->query( $sql, $fname );
180 $a = $wgLinkCache->getGoodLinks();
181 if ( 0 != count( $a ) ) {
183 foreach( $a as $lt => $lid ) {
184 array_push($arr,array(
185 'l_from'=>$this->mId
,
188 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
191 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
192 $dbw->query( $sql, $fname );
194 $a = $wgLinkCache->getBadLinks();
195 if ( 0 != count ( $a ) ) {
197 foreach( $a as $blt ) {
198 $blt = $dbw->strencode( $blt );
199 array_push($arr,array(
200 'bl_from'=>$this->mId
,
203 $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
206 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
207 $dbw->query( $sql, $fname );
209 $a = $wgLinkCache->getImageLinks();
211 if ( 0 != count ( $a ) ) {
213 foreach( $a as $iname => $val )
214 array_push($arr,array(
215 'il_from'=>$this->mId
,
216 'il_to'=>$dbw->strencode( $iname )));
217 $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
220 if( $wgUseCategoryMagic ) {
221 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
222 $dbw->query( $sql, $fname );
225 $add = $wgLinkCache->getCategoryLinks();
229 if ( 0 != count ( $add ) ) {
231 foreach( $add as $cname => $sortkey ) {
232 # FIXME: Change all this to avoid unnecessary duplication
233 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
235 $nt->invalidateCache();
236 array_push($arr,array(
237 'cl_from'=>$this->mId
,
238 'cl_to'=>$dbw->strencode( $cname ),
239 'cl_sortkey'=>$dbw->strencode( $sortkey )));
241 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
244 $this->fixBrokenLinks();
245 wfProfileOut( $fname );
248 function fixBrokenLinks() {
249 /* Update any brokenlinks *to* this page */
250 /* Call for a newly created page, or just to make sure state is consistent */
251 $fname = 'LinksUpdate::fixBrokenLinks';
253 $dbw =& wfGetDB( DB_MASTER
);
254 $cur = $dbw->tableName( 'cur' );
255 $links = $dbw->tableName( 'links' );
257 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
258 $fname, 'FOR UPDATE' );
259 if ( 0 == $dbw->numRows( $res ) ) { return; }
261 # Ignore errors. If a link existed in both the brokenlinks table and the links
262 # table, that's an error which can be fixed at this stage by simply ignoring collisions
264 $now = wfTimestampNow();
265 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
267 while ( $row = $dbw->fetchObject( $res ) ) {
268 if ( ! $first ) { $sql2 .= ","; }
270 array_push($arr,array('l_from'=>$row->bl_from
,'l_to'=>$this->mId
));
271 $sql2 .= $row->bl_from
;
274 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
275 $dbw->query( $sql2, $fname );
276 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );