wiki.techinc.nl/includes/LinksUpdate.php
Brion Vibber 59c6e92429 Some changes to the link tables. They now all use a key on cur_id for the *_from column instead of strings, and have a unique index to force prevent any duplicate entries. There's not yet a clean step in the update script, so just clear out your links tables (patch-linktables.sql) and rebuild them with refreshLinks.php.
This saves trouble in a number of places where we can now do joins with the link tables to get other info (such as cur_is_redirect!) as well as the name, and fewer bits need to be juggled on page renaming, as outgoing links no longer have to be changed (cur_id remains the same when a page is renamed).

rebuildLinks.inc and some of the tools in the 'maintenance page' still need to be updated to work with the new setup. (Special:Maintenance needs a *lot* of cleanup in general. It's kind of a catch-all of vaguely defined features which suck performance like a hydroelectric dam.)

Also I've slipped in some extra debug code. And, I think 'indexes.sql' is a big waste of time and should all be moved into tables.sql. Building indexes separately doesn't help on InnoDB and won't do anything on MyISAM either if you're just going to replace the table after it's built with an imported one from a dump which creates it with indexes.
2004-03-11 09:06:13 +00:00

251 lines
6.2 KiB
PHP

<?php
# See deferred.doc
class LinksUpdate {
/* private */ var $mId, $mTitle;
function LinksUpdate( $id, $title )
{
$this->mId = $id;
$this->mTitle = $title;
$this->mTitleEnc = wfStrencode( $title );
}
function doUpdate()
{
global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
global $wgEnablePersistentLC;
/* Update link tables with outgoing links from an updated article */
/* Relies on the 'link cache' to be filled out */
$fname = "LinksUpdate::doUpdate";
wfProfileIn( $fname );
$del = array();
$add = array();
if( $wgDBtransactions ) {
$sql = "BEGIN";
wfQuery( $sql, DB_WRITE, $fname );
}
#------------------------------------------------------------------------------
# Good links
if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
# Delete where necessary
if ( count( $del ) ) {
$sql = "DELETE FROM links WHERE l_from={$this->mId} AND l_to IN(".
implode( ",", $del ) . ")";
wfQuery( $sql, DB_WRITE, $fname );
}
} else {
# Delete everything
$sql = "DELETE FROM links WHERE l_from={$this->mId}";
wfQuery( $sql, DB_WRITE, $fname );
# Get the addition list
$add = $wgLinkCache->getGoodLinks();
}
# Do the insertion
$sql = "";
if ( 0 != count( $add ) ) {
$sql = "INSERT INTO links (l_from,l_to) VALUES ";
$first = true;
foreach( $add as $lt => $lid ) {
if ( ! $first ) { $sql .= ","; }
$first = false;
$sql .= "({$this->mId},{$lid})";
}
}
if ( "" != $sql ) {
wfQuery( $sql, DB_WRITE, $fname );
}
#------------------------------------------------------------------------------
# Bad links
if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
# Delete where necessary
if ( count( $del ) ) {
$sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
implode( "','", array_map( "wfStrencode", $del ) ) . "')";
wfQuery( $sql, DB_WRITE, $fname );
}
} else {
# Delete all
$sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
wfQuery( $sql, DB_WRITE, $fname );
# Get addition list
$add = $wgLinkCache->getBadLinks();
}
# Do additions
$sql = "";
if ( 0 != count ( $add ) ) {
$sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
$first = true;
foreach( $add as $blt ) {
$blt = wfStrencode( $blt );
if ( ! $first ) { $sql .= ","; }
$first = false;
$sql .= "({$this->mId},'{$blt}')";
}
}
if ( "" != $sql ) {
wfQuery( $sql, DB_WRITE, $fname );
}
#------------------------------------------------------------------------------
# Image links
$sql = "DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
wfQuery( $sql, DB_WRITE, $fname );
# Get addition list
$add = $wgLinkCache->getImageLinks();
# Do the insertion
$sql = "";
$image = Namespace::getImage();
if ( 0 != count ( $add ) ) {
$sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
$first = true;
foreach( $add as $iname => $val ) {
# FIXME: Change all this to avoid unnecessary duplication
$nt = Title::makeTitle( $image, $iname );
if( !$nt ) continue;
$nt->invalidateCache();
$iname = wfStrencode( $iname );
if ( ! $first ) { $sql .= ","; }
$first = false;
$sql .= "({$this->mId},'{$iname}')";
}
}
if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
$this->fixBrokenLinks();
if( $wgDBtransactions ) {
$sql = "COMMIT";
wfQuery( $sql, DB_WRITE, $fname );
}
wfProfileOut( $fname );
}
function doDumbUpdate()
{
# Old inefficient update function
# Used for rebuilding the link table
global $wgLinkCache, $wgDBtransactions;
$fname = "LinksUpdate::doDumbUpdate";
wfProfileIn( $fname );
if( $wgDBtransactions ) {
$sql = "BEGIN";
wfQuery( $sql, DB_WRITE, $fname );
}
$sql = "DELETE FROM links WHERE l_from={$this->mId}";
wfQuery( $sql, DB_WRITE, $fname );
$a = $wgLinkCache->getGoodLinks();
$sql = "";
if ( 0 != count( $a ) ) {
$sql = "INSERT INTO links (l_from,l_to) VALUES ";
$first = true;
foreach( $a as $lt => $lid ) {
if ( ! $first ) { $sql .= ","; }
$first = false;
$sql .= "({$this->mId},{$lid})";
}
}
if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
$sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
wfQuery( $sql, DB_WRITE, $fname );
$a = $wgLinkCache->getBadLinks();
$sql = "";
if ( 0 != count ( $a ) ) {
$sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
$first = true;
foreach( $a as $blt ) {
$blt = wfStrencode( $blt );
if ( ! $first ) { $sql .= ","; }
$first = false;
$sql .= "({$this->mId},'{$blt}')";
}
}
if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
$sql = "DELETE FROM imagelinks WHERE il_from={$this->mId}";
wfQuery( $sql, DB_WRITE, $fname );
$a = $wgLinkCache->getImageLinks();
$sql = "";
if ( 0 != count ( $a ) ) {
$sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
$first = true;
foreach( $a as $iname => $val ) {
$iname = wfStrencode( $iname );
if ( ! $first ) { $sql .= ","; }
$first = false;
$sql .= "({$this->mId},'{$iname}')";
}
}
if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
$this->fixBrokenLinks();
if( $wgDBtransactions ) {
$sql = "COMMIT";
wfQuery( $sql, DB_WRITE, $fname );
}
wfProfileOut( $fname );
}
function fixBrokenLinks() {
/* Update any brokenlinks *to* this page */
/* Call for a newly created page, or just to make sure state is consistent */
$fname = "LinksUpdate::fixBrokenLinks";
$sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
$res = wfQuery( $sql, DB_READ, $fname );
if ( 0 == wfNumRows( $res ) ) { return; }
$sql = "INSERT INTO links (l_from,l_to) VALUES ";
$now = wfTimestampNow();
$sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
$first = true;
while ( $row = wfFetchObject( $res ) ) {
if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
$first = false;
$sql .= "({$row->bl_from},{$this->mId})";
$sql2 .= $row->bl_from;
}
$sql2 .= ")";
wfQuery( $sql, DB_WRITE, $fname );
wfQuery( $sql2, DB_WRITE, $fname );
$sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
wfQuery( $sql, DB_WRITE, $fname );
}
}
?>