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(
74 'l_from' => $this->mId
,
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->insert( '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 array_push( $arr, array(
115 'bl_from' => $this->mId
,
118 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
121 #------------------------------------------------------------------------------
123 $sql = "DELETE FROM $imagelinks WHERE il_from='{$this->mId}'";
124 $dbw->query( $sql, $fname );
127 $add = $wgLinkCache->getImageLinks();
132 if ( 0 != count ( $add ) ) {
134 foreach ($add as $iname => $val ) {
135 $nt = Title
::makeTitle( $image, $iname );
137 $nt->invalidateCache();
138 array_push( $arr, array(
139 'il_from' => $this->mId
,
140 'il_to' => $iname ) );
142 $dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
145 #------------------------------------------------------------------------------
147 if( $wgUseCategoryMagic ) {
148 global $messageMemc, $wgDBname;
151 $add = $wgLinkCache->getCategoryLinks();
153 # select existing catlinks for this page
154 $res = $dbw->select( $categorylinks, array( 'cl_to' ), array( 'cl_from' => $this->mId
),
155 $fname, 'FOR UPDATE' );
158 if(0 != $dbw->numRows( $res )) {
159 while ( $row = $dbw->fetchObject( $res ) ) {
160 if(!isset($add[$row->cl_to
])) {
161 // in the db, but no longer in the page -> delete
162 $del[] = $row->cl_to
;
164 // remove already existing category memberships
165 // from the add array
166 unset($add[$row->cl_to
]);
170 // delete any removed categorylinks
171 if(count($del) > 0) {
173 $dbw->delete('categorylinks', array('cl_from'=>$this->mId
, 'cl_to'=>$del));
174 foreach($del as $cname){
175 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
176 $nt->invalidateCache();
177 // update the timestamp which indicates when the last article
178 // was added or removed to/from this article
179 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
180 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
181 #wfDebug( "Linksupdate:Cats:del: ".serialize($nt)." $key \n" );
184 // add any new category memberships
185 if (count($add) > 0) {
187 foreach( $add as $cname => $sortkey ) {
188 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
190 $nt->invalidateCache();
191 // update the timestamp which indicates when the last article
192 // was added or removed to/from this article
193 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
194 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
195 #wfDebug( "Linksupdate:Cats:add: ".serialize($nt)." $key\n" );
196 #wfDebug( "LU-get: ".$messageMemc->get( $key)."\n");
197 array_push( $arr, array(
198 'cl_from' => $this->mId
,
200 'cl_sortkey' => $sortkey ) );
202 // do the actual sql insertion
203 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
207 $this->fixBrokenLinks();
209 wfProfileOut( $fname );
213 * Link update which clears the previous entries and inserts new ones
214 * May be slower or faster depending on level of lock contention and write speed of DB
215 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
217 function doDumbUpdate() {
218 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
219 $fname = 'LinksUpdate::doDumbUpdate';
220 wfProfileIn( $fname );
223 $dbw =& wfGetDB( DB_MASTER
);
224 $links = $dbw->tableName( 'links' );
225 $brokenlinks = $dbw->tableName( 'brokenlinks' );
226 $imagelinks = $dbw->tableName( 'imagelinks' );
227 $categorylinks = $dbw->tableName( 'categorylinks' );
229 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
230 $dbw->query( $sql, $fname );
232 $a = $wgLinkCache->getGoodLinks();
233 if ( 0 != count( $a ) ) {
235 foreach( $a as $lt => $lid ) {
236 array_push( $arr, array(
237 'l_from' => $this->mId
,
240 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
243 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
244 $dbw->query( $sql, $fname );
246 $a = $wgLinkCache->getBadLinks();
247 if ( 0 != count ( $a ) ) {
249 foreach( $a as $blt ) {
250 array_push($arr,array(
251 'bl_from' => $this->mId
,
254 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
257 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
258 $dbw->query( $sql, $fname );
260 $a = $wgLinkCache->getImageLinks();
262 if ( 0 != count ( $a ) ) {
264 foreach( $a as $iname => $val )
265 array_push( $arr, array(
266 'il_from' => $this->mId
,
267 'il_to' => $iname ) );
268 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
271 if( $wgUseCategoryMagic ) {
272 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
273 $dbw->query( $sql, $fname );
276 $add = $wgLinkCache->getCategoryLinks();
280 if ( 0 != count ( $add ) ) {
282 foreach( $add as $cname => $sortkey ) {
283 # FIXME: Change all this to avoid unnecessary duplication
284 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
286 $nt->invalidateCache();
287 array_push( $arr, array(
288 'cl_from' => $this->mId
,
290 'cl_sortkey' => $sortkey ) );
292 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
295 $this->fixBrokenLinks();
296 wfProfileOut( $fname );
300 * Update any brokenlinks *to* this page
301 * Call for a newly created page, or just to make sure state is consistent
303 function fixBrokenLinks() {
304 $fname = 'LinksUpdate::fixBrokenLinks';
306 $dbw =& wfGetDB( DB_MASTER
);
307 $page = $dbw->tableName( 'page' );
308 $links = $dbw->tableName( 'links' );
310 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
311 $fname, 'FOR UPDATE' );
312 if ( 0 == $dbw->numRows( $res ) ) { return; }
315 $now = $dbw->timestamp();
316 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
318 while ( $row = $dbw->fetchObject( $res ) ) {
319 if ( ! $first ) { $sql2 .= ","; }
321 array_push( $arr, array(
322 'l_from' => $row->bl_from
,
323 'l_to' => $this->mId
) );
324 $sql2 .= $row->bl_from
;
328 # Ignore errors. If a link existed in both the brokenlinks table and the links
329 # table, that's an error which can be fixed at this stage by simply ignoring collisions
330 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
331 $dbw->query( $sql2, $fname );
332 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );