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 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}' AND cl_to IN('";
174 $sql .= implode("','", $del) . "')";
175 $dbw->query( $sql, $fname );
176 foreach($del as $cname){
177 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
178 $nt->invalidateCache();
179 // update the timestamp which indicates when the last article
180 // was added or removed to/from this article
181 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
182 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
183 #wfDebug( "Linksupdate:Cats:del: ".serialize($nt)." $key \n" );
186 // add any new category memberships
187 if (count($add) > 0) {
189 foreach( $add as $cname => $sortkey ) {
190 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
192 $nt->invalidateCache();
193 // update the timestamp which indicates when the last article
194 // was added or removed to/from this article
195 $key = $wgDBname.':Category:'.$nt->getDBkey().':adddeltimestamp';
196 $messageMemc->set( $key , wfTimestamp( TS_MW
), 24*3600 );
197 #wfDebug( "Linksupdate:Cats:add: ".serialize($nt)." $key\n" );
198 #wfDebug( "LU-get: ".$messageMemc->get( $key)."\n");
199 array_push( $arr, array(
200 'cl_from' => $this->mId
,
202 'cl_sortkey' => $sortkey ) );
204 // do the actual sql insertion
205 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
209 $this->fixBrokenLinks();
211 wfProfileOut( $fname );
215 * Link update which clears the previous entries and inserts new ones
216 * May be slower or faster depending on level of lock contention and write speed of DB
217 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
219 function doDumbUpdate() {
220 global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
221 $fname = 'LinksUpdate::doDumbUpdate';
222 wfProfileIn( $fname );
225 $dbw =& wfGetDB( DB_MASTER
);
226 $links = $dbw->tableName( 'links' );
227 $brokenlinks = $dbw->tableName( 'brokenlinks' );
228 $imagelinks = $dbw->tableName( 'imagelinks' );
229 $categorylinks = $dbw->tableName( 'categorylinks' );
231 $sql = "DELETE FROM $links WHERE l_from={$this->mId}";
232 $dbw->query( $sql, $fname );
234 $a = $wgLinkCache->getGoodLinks();
235 if ( 0 != count( $a ) ) {
237 foreach( $a as $lt => $lid ) {
238 array_push( $arr, array(
239 'l_from' => $this->mId
,
242 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
245 $sql = "DELETE FROM $brokenlinks WHERE bl_from={$this->mId}";
246 $dbw->query( $sql, $fname );
248 $a = $wgLinkCache->getBadLinks();
249 if ( 0 != count ( $a ) ) {
251 foreach( $a as $blt ) {
252 array_push($arr,array(
253 'bl_from' => $this->mId
,
256 $dbw->insert( 'brokenlinks', $arr, $fname, array( 'IGNORE' ) );
259 $sql = "DELETE FROM $imagelinks WHERE il_from={$this->mId}";
260 $dbw->query( $sql, $fname );
262 $a = $wgLinkCache->getImageLinks();
264 if ( 0 != count ( $a ) ) {
266 foreach( $a as $iname => $val )
267 array_push( $arr, array(
268 'il_from' => $this->mId
,
269 'il_to' => $iname ) );
270 $dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
273 if( $wgUseCategoryMagic ) {
274 $sql = "DELETE FROM $categorylinks WHERE cl_from='{$this->mId}'";
275 $dbw->query( $sql, $fname );
278 $add = $wgLinkCache->getCategoryLinks();
282 if ( 0 != count ( $add ) ) {
284 foreach( $add as $cname => $sortkey ) {
285 # FIXME: Change all this to avoid unnecessary duplication
286 $nt = Title
::makeTitle( NS_CATEGORY
, $cname );
288 $nt->invalidateCache();
289 array_push( $arr, array(
290 'cl_from' => $this->mId
,
292 'cl_sortkey' => $sortkey ) );
294 $dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
297 $this->fixBrokenLinks();
298 wfProfileOut( $fname );
302 * Update any brokenlinks *to* this page
303 * Call for a newly created page, or just to make sure state is consistent
305 function fixBrokenLinks() {
306 $fname = 'LinksUpdate::fixBrokenLinks';
308 $dbw =& wfGetDB( DB_MASTER
);
309 $page = $dbw->tableName( 'page' );
310 $links = $dbw->tableName( 'links' );
312 $res = $dbw->select( 'brokenlinks', array( 'bl_from' ), array( 'bl_to' => $this->mTitle
),
313 $fname, 'FOR UPDATE' );
314 if ( 0 == $dbw->numRows( $res ) ) { return; }
317 $now = $dbw->timestamp();
318 $sql2 = "UPDATE $page SET page_touched='{$now}' WHERE page_id IN (";
320 while ( $row = $dbw->fetchObject( $res ) ) {
321 if ( ! $first ) { $sql2 .= ","; }
323 array_push( $arr, array(
324 'l_from' => $row->bl_from
,
325 'l_to' => $this->mId
) );
326 $sql2 .= $row->bl_from
;
330 # Ignore errors. If a link existed in both the brokenlinks table and the links
331 # table, that's an error which can be fixed at this stage by simply ignoring collisions
332 $dbw->insert( 'links', $arr, $fname, array( 'IGNORE' ) );
333 $dbw->query( $sql2, $fname );
334 $dbw->delete( 'brokenlinks', array( 'bl_to' => $this->mTitle
), $fname );