From 436d389650887ab8b746465f150038a2e484172c Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Sat, 20 Sep 2003 02:30:00 +0000 Subject: [PATCH] changing wfQuery to allow replication --- includes/Article.php | 64 ++++++++++++++++----------------- includes/Block.php | 10 +++--- includes/DatabaseFunctions.php | 13 +++++-- includes/DifferenceEngine.php | 8 ++--- includes/GlobalFunctions.php | 20 +++++------ includes/SpecialLonelypages.php | 2 +- includes/SpecialWatchlist.php | 10 +++--- update.php | 6 ++-- 8 files changed, 70 insertions(+), 63 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 5df7f163f5..236091ab10 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -98,7 +98,7 @@ class Article { $sql = "SELECT " . "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " . "FROM cur WHERE cur_id={$id}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 == wfNumRows( $res ) ) { return; } $s = wfFetchObject( $res ); @@ -128,7 +128,7 @@ class Article { if ( 0 != $rid ) { $sql = "SELECT cur_text,cur_timestamp,cur_user," . "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 != wfNumRows( $res ) ) { $this->mRedirectedFrom = $this->mTitle->getPrefixedText(); @@ -149,7 +149,7 @@ class Article { } else { # oldid set, retrieve historical version $sql = "SELECT old_text,old_timestamp,old_user FROM old " . "WHERE old_id={$oldid}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 == wfNumRows( $res ) ) { return; } $s = wfFetchObject( $res ); @@ -198,7 +198,7 @@ class Article { $sql = "SELECT cur_user,cur_user_text,cur_timestamp," . "cur_comment,cur_minor_edit FROM cur WHERE " . "cur_id=" . $this->getID(); - $res = wfQuery( $sql, "Article::loadLastEdit" ); + $res = wfQuery( $sql, DB_READ, "Article::loadLastEdit" ); if ( wfNumRows( $res ) > 0 ) { $s = wfFetchObject( $res ); @@ -319,7 +319,7 @@ class Article { $wgUser->getID() . "', '{$now}', " . ( $isminor ? 1 : 0 ) . ", 0, '', '" . wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_WRITE, $fname ); $newid = wfInsertId(); $this->mTitle->resetArticleID( $newid ); @@ -332,7 +332,7 @@ class Article { wfStrencode( $wgUser->getName() ) . "','" . wfStrencode( $summary ) . "',0,0," . ( $wgUser->isBot() ? 1 : 0 ) . ")"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); if ($watchthis) { if(!$this->mTitle->userIsWatching()) $this->watch(); } else { @@ -381,7 +381,7 @@ class Article { if( $wgDBtransactions ) { $sql = "BEGIN"; - wfQuery( $sql ); + wfQuery( $sql, DB_WRITE ); } $oldtext = $this->getContent( true ); @@ -399,7 +399,7 @@ class Article { "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " . "WHERE cur_id=" . $this->getID() . " AND cur_timestamp='" . $this->getTimestamp() . "'"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_WRITE, $fname ); if( wfAffectedRows() == 0 ) { /* Belated edit conflict! Run away!! */ @@ -417,7 +417,7 @@ class Article { wfStrencode( $this->getUserText() ) . "', '" . $this->getTimestamp() . "', " . $me1 . ", '" . wfInvertTimestamp( $this->getTimestamp() ) . "')"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_WRITE, $fname ); $oldid = wfInsertID( $res ); $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," . @@ -429,21 +429,21 @@ class Article { $this->getID() . "," . $wgUser->getID() . ",'" . wfStrencode( $wgUser->getName() ) . "','" . wfStrencode( $summary ) . "',0,{$oldid})"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " . "WHERE rc_namespace=" . $this->mTitle->getNamespace() . " AND " . "rc_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' AND " . "rc_timestamp='" . $this->getTimestamp() . "'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " . "WHERE rc_cur_id=" . $this->getID(); - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); } if( $wgDBtransactions ) { $sql = "COMMIT"; - wfQuery( $sql ); + wfQuery( $sql, DB_WRITE ); } if ($watchthis) { @@ -562,7 +562,7 @@ class Article { "WHERE old_namespace={$namespace} AND " . "old_title='" . wfStrencode( $this->mTitle->getDBkey() ) . "' " . "ORDER BY inverse_timestamp LIMIT $offset, $limit"; - $res = wfQuery( $sql, "Article::history" ); + $res = wfQuery( $sql, DB_READ, "Article::history" ); $revs = wfNumRows( $res ); if( $this->mTitle->getArticleID() == 0 ) { @@ -617,7 +617,7 @@ class Article { } $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," . "cur_restrictions='{$limit}' WHERE cur_id={$id}"; - wfQuery( $sql, "Article::protect" ); + wfQuery( $sql, DB_WRITE, "Article::protect" ); $wgOut->redirect( wfLocalUrl( $this->mTitle->getPrefixedURL() ) ); } @@ -656,7 +656,7 @@ class Article { # and insert a warning if it does # we select the text because it might be useful below $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1"; - $res=wfQuery($sql,$fname); + $res=wfQuery($sql, DB_READ, $fname); if( ($old=wfFetchObject($res)) && !$wpConfirm ) { $skin=$wgUser->getSkin(); $wgOut->addHTML("".wfMsg("historywarning")); @@ -664,7 +664,7 @@ class Article { } $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($this->mTitle->getPrefixedDBkey())."'"; - $res=wfQuery($sql,$fname); + $res=wfQuery($sql, DB_READ, $fname); if( ($s=wfFetchObject($res))) { # if this is a mini-text, we can paste part of it into the deletion reason @@ -789,35 +789,35 @@ class Article { "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," . "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " . "WHERE cur_namespace={$ns} AND cur_title='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," . "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," . "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," . "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " . "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); # Now that it's safely backed up, delete it $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " . "cur_title='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " . "old_title='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " . "rc_title='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); # Finally, clean up the link tables if ( 0 != $id ) { $t = wfStrencode( $title->getPrefixedDBkey() ); $sql = "SELECT l_from FROM links WHERE l_to={$id}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_WRITE, $fname ); $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES "; $now = wfTimestampNow(); @@ -835,22 +835,22 @@ class Article { } $sql2 .= ")"; if ( ! $first ) { - wfQuery( $sql, $fname ); - wfQuery( $sql2, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); + wfQuery( $sql2, DB_WRITE, $fname ); } wfFreeResult( $res ); $sql = "DELETE FROM links WHERE l_to={$id}"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "DELETE FROM links WHERE l_from='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); } $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) ); @@ -878,7 +878,7 @@ class Article { # Get the last editor $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}"; - $res = wfQuery( $sql ); + $res = wfQuery( $sql, DB_READ ); if( ($x = wfNumRows( $res )) != 1 ) { # Something wrong $wgOut->addHTML( wfMsg( "notanarticle" ) ); @@ -910,7 +910,7 @@ class Article { WHERE old_namespace={$n} AND old_title='{$tt}' AND (old_user <> {$uid} OR old_user_text <> '{$ut}') ORDER BY inverse_timestamp LIMIT 1"; - $res = wfQuery( $sql ); + $res = wfQuery( $sql, DB_READ ); if( wfNumRows( $res ) != 1 ) { # Something wrong $wgOut->setPageTitle(wfMsg("rollbackfailed")); @@ -959,7 +959,7 @@ class Article { if ( 0 == mt_rand( 0, 999 ) ) { $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) ); $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'"; - wfQuery( $sql ); + wfQuery( $sql, DB_WRITE ); } $id = $this->getID(); $title = $this->mTitle->getPrefixedDBkey(); diff --git a/includes/Block.php b/includes/Block.php index 2aeff388f3..dc8d96157f 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -47,7 +47,7 @@ class Block "' OR ipb_user={$user})"; } - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 == wfNumRows( $res ) ) { # User is not blocked $this->clear(); @@ -96,7 +96,7 @@ class Block # Callback with a Block object for every block /*static*/ function enumBlocks( $callback, $tag, $killExpired = true ) { $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp"; - $res = wfQuery( $sql, "Block::enumBans" ); + $res = wfQuery( $sql, DB_READ, "Block::enumBans" ); $block = new Block(); while ( $row = wfFetchObject( $res ) ) { @@ -120,7 +120,7 @@ class Block $sql = "DELETE FROM ipblocks WHERE ipb_address='" . wfStrencode( $this->mAddress ) . "'"; } - wfQuery( $sql, "Block::delete" ); + wfQuery( $sql, DB_WRITE, "Block::delete" ); } function insert() { @@ -128,7 +128,7 @@ class Block (ipb_address, ipb_user, ipb_by, ipb_reason, ipb_timestamp, ipb_auto ) VALUES ('" . wfStrencode( $this->mAddress ) . "', {$this->mUser}, {$this->mBy}, '" . wfStrencode( $this->mReason ) . "','{$this->mTimestamp}', {$this->mAuto})"; - wfQuery( $sql, "Block::insert" ); + wfQuery( $sql, DB_WRITE, "Block::insert" ); } function deleteIfExpired() { @@ -164,7 +164,7 @@ class Block function updateTimestamp() { wfQuery( "UPDATE ipblocks SET ipb_timestamp='" . wfTimestampNow() . - "' WHERE ipb_address='" . wfStrencode( $this->mAddress ) . "'", "Block::updateTimestamp" ); + "' WHERE ipb_address='" . wfStrencode( $this->mAddress ) . "'", DB_WRITE, "Block::updateTimestamp" ); } } diff --git a/includes/DatabaseFunctions.php b/includes/DatabaseFunctions.php index a1c438818e..9e485c0a8a 100644 --- a/includes/DatabaseFunctions.php +++ b/includes/DatabaseFunctions.php @@ -3,6 +3,9 @@ global $IP; include_once( "$IP/FulltextStoplist.php" ); include_once( "$IP/CacheManager.php" ); +define( "DB_READ", -1 ); +define( "DB_WRITE", -2 ); + $wgLastDatabaseQuery = ""; function wfGetDB( $altuser = "", $altpassword = "", $altserver = "", $altdb = "" ) @@ -85,7 +88,11 @@ function wfEmergencyAbort( $msg = "" ) { exit; } -function wfQuery( $sql, $fname = "" ) +# $db: DB_READ = -1 read from slave (or only server) +# DB_WRITE = -2 write to master (or only server) +# 0,1,2,... query a database with a specific index +# Replication is not actually implemented just yet +function wfQuery( $sql, $db, $fname = "" ) { global $wgLastDatabaseQuery, $wgOut; ## wfProfileIn( "wfQuery" ); @@ -128,13 +135,13 @@ function wfSetSQL( $table, $var, $value, $cond ) { $sql = "UPDATE $table SET $var = '" . wfStrencode( $value ) . "' WHERE ($cond)"; - wfQuery( $sql, "wfSetSQL" ); + wfQuery( $sql, DB_WRITE, "wfSetSQL" ); } function wfGetSQL( $table, $var, $cond ) { $sql = "SELECT $var FROM $table WHERE ($cond)"; - $result = wfQuery( $sql, "wfGetSQL" ); + $result = wfQuery( $sql, DB_WRITE, "wfGetSQL" ); $ret = ""; if ( mysql_num_rows( $result ) > 0 ) { diff --git a/includes/DifferenceEngine.php b/includes/DifferenceEngine.php index 835ac74f03..26c09ca1f9 100644 --- a/includes/DifferenceEngine.php +++ b/includes/DifferenceEngine.php @@ -75,7 +75,7 @@ cellpadding=0 cellspacing='4px'> $id = $wgTitle->getArticleID(); $sql = "SELECT cur_text FROM cur WHERE cur_id={$id}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 == wfNumRows( $res ) ) { return false; } $s = wfFetchObject( $res ); @@ -84,7 +84,7 @@ cellpadding=0 cellspacing='4px'> $sql = "SELECT old_timestamp,old_text FROM old WHERE " . "old_id={$this->mNewid}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 == wfNumRows( $res ) ) { return false; } $s = wfFetchObject( $res ); @@ -99,11 +99,11 @@ cellpadding=0 cellspacing='4px'> "old_namespace=" . $wgTitle->getNamespace() . " AND " . "old_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' ORDER BY inverse_timestamp LIMIT 1"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); } else { $sql = "SELECT old_timestamp,old_text FROM old WHERE " . "old_id={$this->mOldid}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); } if ( 0 == wfNumRows( $res ) ) { return false; } diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 3991eec58f..195e4b780a 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -185,7 +185,7 @@ function wfMsg( $key ) $dbKey = $title->getDBkey(); $ns = $title->getNamespace(); $sql = "SELECT cur_text FROM cur WHERE cur_namespace=$ns AND cur_title='$dbKey'"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if( ( $s = wfFetchObject( $res ) ) and ( $s->cur_text != "" ) ) { $message = $s->cur_text; # filter out a comment at the top if there is one @@ -336,7 +336,7 @@ function wfNumberOfArticles() $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " . "FROM site_stats WHERE ss_row_id=1"; - $res = wfQuery( $sql, "wfLoadSiteStats" ); + $res = wfQuery( $sql, DB_READ, "wfLoadSiteStats" ); if ( 0 == wfNumRows( $res ) ) { return; } else { @@ -412,7 +412,7 @@ function wfRecordUpload( $name, $oldver, $size, $desc ) $sql = "SELECT img_name,img_size,img_timestamp,img_description,img_user," . "img_user_text FROM image WHERE img_name='" . wfStrencode( $name ) . "'"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); $now = wfTimestampNow(); $won = wfInvertTimestamp( $now ); @@ -431,12 +431,12 @@ function wfRecordUpload( $name, $oldver, $size, $desc ) wfStrencode( $name ) . "',{$size},'{$now}','" . wfStrencode( $desc ) . "', '" . $wgUser->getID() . "', '" . wfStrencode( $wgUser->getName() ) . "')"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "SELECT cur_id,cur_text FROM cur WHERE cur_namespace=" . Namespace::getImage() . " AND cur_title='" . wfStrencode( $name ) . "'"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); if ( 0 == wfNumRows( $res ) ) { $common = Namespace::getImage() . ",'" . @@ -449,12 +449,12 @@ function wfRecordUpload( $name, $oldver, $size, $desc ) "cur_text,inverse_timestamp,cur_touched) VALUES (" . $common . ",'" . wfStrencode( $textdesc ) . "','{$won}','{$now}')"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $id = wfInsertId() or 0; # We should throw an error instead $sql = "INSERT INTO recentchanges (rc_namespace,rc_title, rc_comment,rc_user,rc_user_text,rc_timestamp,rc_new, rc_cur_id,rc_cur_time) VALUES ({$common},{$id},'{$now}')"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $u = new SearchUpdate( $id, $name, $desc ); $u->doUpdate(); } @@ -469,7 +469,7 @@ function wfRecordUpload( $name, $oldver, $size, $desc ) wfStrencode( $s->img_description ) . "','" . wfStrencode( $s->img_user ) . "','" . wfStrencode( $s->img_user_text) . "')"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "UPDATE image SET img_size={$size}," . "img_timestamp='" . wfTimestampNow() . "',img_user='" . @@ -477,12 +477,12 @@ function wfRecordUpload( $name, $oldver, $size, $desc ) wfStrencode( $wgUser->getName() ) . "', img_description='" . wfStrencode( $desc ) . "' WHERE img_name='" . wfStrencode( $name ) . "'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); $sql = "UPDATE cur SET cur_touched='{$now}' WHERE cur_namespace=" . Namespace::getImage() . " AND cur_title='" . wfStrencode( $name ) . "'"; - wfQuery( $sql, $fname ); + wfQuery( $sql, DB_WRITE, $fname ); } $log = new LogPage( wfMsg( "uploadlogpage" ), wfMsg( "uploadlogpagetext" ) ); diff --git a/includes/SpecialLonelypages.php b/includes/SpecialLonelypages.php index 35db75360a..2983b32847 100644 --- a/includes/SpecialLonelypages.php +++ b/includes/SpecialLonelypages.php @@ -21,7 +21,7 @@ function wfSpecialLonelypages() $sql = "SELECT cur_title FROM cur LEFT JOIN links ON " . "cur_id=l_to WHERE l_to IS NULL AND cur_namespace=0 AND " . "cur_is_redirect=0 ORDER BY cur_title LIMIT {$offset}, {$limit}"; - $res = wfQuery( $sql, $fname ); + $res = wfQuery( $sql, DB_READ, $fname ); $sk = $wgUser->getSkin(); diff --git a/includes/SpecialWatchlist.php b/includes/SpecialWatchlist.php index cadf1f96b8..4321c0db54 100644 --- a/includes/SpecialWatchlist.php +++ b/includes/SpecialWatchlist.php @@ -28,7 +28,7 @@ function wfSpecialWatchlist() $sql = "DELETE FROM watchlist WHERE wl_user=$uid AND " . "wl_namespace=" . $t->getNamespace() . " AND " . "wl_title='" . wfStrencode( $t->getDBkey() ) . "'"; - $res = wfQuery( $sql ); + $res = wfQuery( $sql, DB_WRITE ); if($res === FALSE) { $wgOut->addHTML( "
\n" . wfMsg( "couldntremove", htmlspecialchars($one) ) ); } else { @@ -42,7 +42,7 @@ function wfSpecialWatchlist() } $sql = "SELECT COUNT(*) AS n FROM watchlist WHERE wl_user=$uid"; - $res = wfQuery( $sql ); + $res = wfQuery( $sql, DB_READ ); $s = wfFetchObject( $res ); $nitems = $s->n; @@ -72,7 +72,7 @@ function wfSpecialWatchlist() ( $cutoff = wfUnix2Timestamp( time() - intval( $days * 86400 ) ) ) . "'"; $sql = "SELECT COUNT(*) AS n FROM cur WHERE cur_timestamp>'$cutoff'"; - $res = wfQuery( $sql ); + $res = wfQuery( $sql, DB_READ ); $s = wfFetchObject( $res ); $npages = $s->n; } @@ -86,7 +86,7 @@ function wfSpecialWatchlist() "' method='post'>\n" . "