21 * Initialize private variables
23 * @param string $title
25 function LinksUpdate( $id, $title ) {
27 $this->mTitle
= $title;
31 * Update link tables with outgoing links from an updated article
32 * Relies on the 'link cache' to be filled out.
36 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
37 global $wgEnablePersistentLC, $wgUseCategoryMagic;
39 $fname = 'LinksUpdate::doUpdate';
40 wfProfileIn( $fname );
45 $dbw =& wfGetDB( DB_MASTER
);
46 $links = $dbw->tableName( 'links' );
47 $brokenlinks = $dbw->tableName( 'brokenlinks' );
48 $imagelinks = $dbw->tableName( 'imagelinks' );
49 $categorylinks = $dbw->tableName( 'categorylinks' );
51 #------------------------------------------------------------------------------
54 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD
, $del, $add ) ) {
55 # Delete where necessary
56 if ( count( $del ) ) {
57 $sql = "DELETE FROM $links WHERE l_from={$this->mId} AND l_to IN(".
58 implode( ',', $del ) . ')';
59 $dbw->query( $sql, $fname );
63 $dbw->delete( 'links', array( 'l_from' => $this->mId
), $fname );
65 # Get the addition list
66 $add = $wgLinkCache->getGoodLinks();
70 if ( 0 != count( $add ) ) {
72 foreach($add as $lt=>$lid)
73 array_push($arr,array(
76 # The link cache was constructed without FOR UPDATE, so there may be collisions
77 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
78 # sure it's better than without IGNORE
79 $dbw->insertArray('links', $arr, $fname, array('IGNORE'));
82 #------------------------------------------------------------------------------
85 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD
, $del, $add ) ) {
86 # Delete where necessary
87 if ( count( $del ) ) {
88 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId} AND bl_to IN(";
90 foreach( $del as $badTitle ) {
96 $sql .= $dbw->addQuotes( $badTitle );
99 $dbw->query( $sql, $fname );
103 $dbw->delete( 'brokenlinks', array( 'bl_from' => $this->mId
) );
106 $add = $wgLinkCache->getBadLinks();
111 if ( 0 != count ( $add ) ) {
113 foreach( $add as $blt ) {
114 $blt = $dbw->strencode( $blt );
115 array_push($arr,array(
116 'bl_from'=>$this->mId
,
119 $dbw->insertArray( 'brokenlinks',$arr,$fname,array('IGNORE'));
122 #------------------------------------------------------------------------------
124 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
125 $dbw->query( $sql, $fname );
128 $add = $wgLinkCache->getImageLinks();
132 $image = Namespace::getImage();
133 if ( 0 != count ( $add ) ) {
135 foreach ($add as $iname => $val ) {
136 $nt = Title
::makeTitle( $image, $iname );
138 $nt->invalidateCache();
139 $iname = $dbw->strencode( $iname );
140 array_push($arr,array(
141 'il_from'=>$this->mId
,
144 $dbw->insertArray($imagelinks, $arr, $fname, array('IGNORE'));
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 ) ) {
160 foreach( $add as $cname => $sortkey ) {
161 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
163 $nt->invalidateCache();
164 array_push($arr,array(
165 'cl_from'=>$this->mId
,
166 'cl_to'=>$dbw->strencode( $cname ),
167 'cl_sortkey'=>$dbw->strencode( $sortkey )));
169 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
173 $this->fixBrokenLinks();
175 wfProfileOut( $fname );
179 * Old inefficient update function
180 * Used for rebuilding the link table
181 * @todo Only used by ./maintenance/refreshLinks.inc probably need to be replaced
184 function doDumbUpdate() {
185 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
186 $fname = 'LinksUpdate::doDumbUpdate';
187 wfProfileIn( $fname );
190 $dbw =& wfGetDB( DB_MASTER
);
191 $links = $dbw->tableName( 'links' );
192 $brokenlinks = $dbw->tableName( 'brokenlinks' );
193 $imagelinks = $dbw->tableName( 'imagelinks' );
194 $categorylinks = $dbw->tableName( 'categorylinks' );
196 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
197 $dbw->query( $sql, $fname );
199 $a = $wgLinkCache->getGoodLinks();
200 if ( 0 != count( $a ) ) {
202 foreach( $a as $lt => $lid ) {
203 array_push($arr,array(
204 'l_from'=>$this->mId
,
207 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
210 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
211 $dbw->query( $sql, $fname );
213 $a = $wgLinkCache->getBadLinks();
214 if ( 0 != count ( $a ) ) {
216 foreach( $a as $blt ) {
217 $blt = $dbw->strencode( $blt );
218 array_push($arr,array(
219 'bl_from'=>$this->mId
,
222 $dbw->insertArray($brokenlinks,$arr,$fname,array('IGNORE'));
225 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
226 $dbw->query( $sql, $fname );
228 $a = $wgLinkCache->getImageLinks();
230 if ( 0 != count ( $a ) ) {
232 foreach( $a as $iname => $val )
233 array_push($arr,array(
234 'il_from'=>$this->mId
,
235 'il_to'=>$dbw->strencode( $iname )));
236 $dbw->insertArray($imagelinks,$arr,$fname,array('IGNORE'));
239 if( $wgUseCategoryMagic ) {
240 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
241 $dbw->query( $sql, $fname );
244 $add = $wgLinkCache->getCategoryLinks();
248 if ( 0 != count ( $add ) ) {
250 foreach( $add as $cname => $sortkey ) {
251 # FIXME: Change all this to avoid unnecessary duplication
252 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
254 $nt->invalidateCache();
255 array_push($arr,array(
256 'cl_from'=>$this->mId
,
257 'cl_to'=>$dbw->strencode( $cname ),
258 'cl_sortkey'=>$dbw->strencode( $sortkey )));
260 $dbw->insertArray($categorylinks,$arr,$fname,array('IGNORE'));
263 $this->fixBrokenLinks();
264 wfProfileOut( $fname );
268 * Update any brokenlinks *to* this page
269 * Call for a newly created page, or just to make sure state is consistent
271 function fixBrokenLinks() {
272 $fname = 'LinksUpdate::fixBrokenLinks';
274 $dbw =& wfGetDB( DB_MASTER
);
275 $cur = $dbw->tableName( 'cur' );
276 $links = $dbw->tableName( 'links' );
278 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
279 $fname, 'FOR UPDATE' );
280 if ( 0 == $dbw->numRows( $res ) ) { return; }
282 # Ignore errors. If a link existed in both the brokenlinks table and the links
283 # table, that's an error which can be fixed at this stage by simply ignoring collisions
285 $now = $dbw->timestamp();
286 $sql2 = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_id IN (";
288 while ( $row = $dbw->fetchObject( $res ) ) {
289 if ( ! $first ) { $sql2 .= ","; }
291 array_push($arr,array('l_from'=>$row->bl_from
,'l_to'=>$this->mId
));
292 $sql2 .= $row->bl_from
;
295 $dbw->insertArray($links,$arr,$fname,array('IGNORE'));
296 $dbw->query( $sql2, $fname );
297 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );