From 904042c07ca9d1bd246b8ded0d6904cef3ae153a Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Sun, 19 Jun 2005 00:21:49 +0000 Subject: [PATCH] ported site_stats change from 1.4 --- includes/Article.php | 15 ++-- includes/Database.php | 4 +- includes/SiteStatsUpdate.php | 88 +++++++++++++------ includes/SpecialMovepage.php | 20 ----- includes/SpecialStatistics.php | 56 +++++++----- includes/SpecialUserlogin.php | 4 + includes/Title.php | 21 +++++ .../archives/patch-ss_total_articles.sql | 6 ++ maintenance/tables.sql | 9 ++ maintenance/updaters.inc | 1 + 10 files changed, 148 insertions(+), 76 deletions(-) create mode 100644 maintenance/archives/patch-ss_total_articles.sql diff --git a/includes/Article.php b/includes/Article.php index 3d95135498..7884185871 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -28,7 +28,7 @@ class Article { */ var $mContent, $mContentLoaded; var $mUser, $mTimestamp, $mUserText; - var $mCounter, $mComment, $mCountAdjustment; + var $mCounter, $mComment, $mGoodAdjustment, $mTotalAdjustment; var $mMinorEdit, $mRedirectedFrom; var $mTouched, $mFileCache, $mTitle; var $mId, $mTable; @@ -65,7 +65,7 @@ class Article { $this->mCurID = $this->mUser = $this->mCounter = -1; # Not loaded $this->mRedirectedFrom = $this->mUserText = $this->mTimestamp = $this->mComment = $this->mFileCache = ''; - $this->mCountAdjustment = 0; + $this->mGoodAdjustment = $this->mTotalAdjustment = 0; $this->mTouched = '19700101000000'; $this->mForUpdate = false; $this->mIsRedirect = false; @@ -906,7 +906,8 @@ class Article { $fname = 'Article::insertNewArticle'; - $this->mCountAdjustment = $this->isCountable( $text ); + $this->mGoodAdjustment = $this->isCountable( $text ); + $this->mTotalAdjustment = 1; $ns = $this->mTitle->getNamespace(); $ttl = $this->mTitle->getDBkey(); @@ -1120,8 +1121,9 @@ class Article { $lastRevision = 0; if ( 0 != strcmp( $text, $oldtext ) ) { - $this->mCountAdjustment = $this->isCountable( $text ) + $this->mGoodAdjustment = $this->isCountable( $text ) - $this->isCountable( $oldtext ); + $this->mTotalAdjustment = 0; $now = wfTimestampNow(); $lastRevision = $dbw->selectField( @@ -1749,7 +1751,7 @@ class Article { return false; } - $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) ); + $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ), -1 ); array_push( $wgDeferredUpdateList, $u ); $linksTo = $this->mTitle->getLinksTo(); @@ -1985,11 +1987,10 @@ class Article { $title = $this->mTitle->getPrefixedDBkey(); $shortTitle = $this->mTitle->getDBkey(); - $adj = $this->mCountAdjustment; if ( 0 != $id ) { $u = new LinksUpdate( $id, $title ); array_push( $wgDeferredUpdateList, $u ); - $u = new SiteStatsUpdate( 0, 1, $adj ); + $u = new SiteStatsUpdate( 0, 1, $this->mGoodAdjustment, $this->mTotalAdjustment ); array_push( $wgDeferredUpdateList, $u ); $u = new SearchUpdate( $id, $title, $text ); array_push( $wgDeferredUpdateList, $u ); diff --git a/includes/Database.php b/includes/Database.php index 6509c7dedd..d46b4cd104 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -921,7 +921,9 @@ class Database { function update( $table, $values, $conds, $fname = 'Database::update' ) { $table = $this->tableName( $table ); $sql = "UPDATE $table SET " . $this->makeList( $values, LIST_SET ); - $sql .= " WHERE " . $this->makeList( $conds, LIST_AND ); + if ( $conds != '*' ) { + $sql .= " WHERE " . $this->makeList( $conds, LIST_AND ); + } $this->query( $sql, $fname ); } diff --git a/includes/SiteStatsUpdate.php b/includes/SiteStatsUpdate.php index d4c4b2e4ff..00e54943e2 100644 --- a/includes/SiteStatsUpdate.php +++ b/includes/SiteStatsUpdate.php @@ -11,39 +11,73 @@ */ class SiteStatsUpdate { - var $mViews, $mEdits, $mGood; + var $mViews, $mEdits, $mGood, $mPages, $mUsers; - function SiteStatsUpdate( $views, $edits, $good ) { + function SiteStatsUpdate( $views, $edits, $good, $pages = 0, $users = 0 ) { $this->mViews = $views; $this->mEdits = $edits; $this->mGood = $good; + $this->mPages = $pages; + $this->mUsers = $users; } + function appendUpdate( &$sql, $field, $delta ) { + if ( $delta ) { + if ( $sql ) { + $sql .= ','; + } + if ( $delta < 0 ) { + $sql .= "$field=$field-1"; + } else { + $sql .= "$field=$field+1"; + } + } + } + function doUpdate() { - $a = array(); - - if ( $this->mViews < 0 ) { $m = '-1'; } - else if ( $this->mViews > 0 ) { $m = '+1'; } - else $m = ''; - array_push( $a, "ss_total_views=(ss_total_views$m)" ); - - if ( $this->mEdits < 0 ) { $m = '-1'; } - else if ( $this->mEdits > 0 ) { $m = '+1'; } - else $m = ''; - array_push( $a, "ss_total_edits=(ss_total_edits$m)" ); - - if ( $this->mGood < 0 ) { $m = '-1'; } - else if ( $this->mGood > 0 ) { $m = '+1'; } - else $m = ''; - array_push( $a, "ss_good_articles=(ss_good_articles$m)" ); - - $db =& wfGetDB( DB_MASTER ); - $site_stats = $db->tableName( 'site_stats' ); - $lowpri = $db->lowPriorityOption(); - - $sql = "UPDATE $lowpri $site_stats SET " . implode ( ',', $a ) . - ' WHERE ss_row_id=1'; - $db->query( $sql, 'SiteStatsUpdate::doUpdate' ); + global $wgDBname; + $fname = 'SiteStatsUpdate::doUpdate'; + $dbw =& wfGetDB( DB_MASTER ); + + # First retrieve the row just to find out which schema we're in + $row = $dbw->selectRow( 'site_stats', '*', false, $fname ); + + $updates = ''; + + $this->appendUpdate( $updates, 'ss_total_views', $this->mViews ); + $this->appendUpdate( $updates, 'ss_total_edits', $this->mEdits ); + $this->appendUpdate( $updates, 'ss_good_articles', $this->mGood ); + + if ( isset( $row->ss_total_pages ) ) { + # Update schema if required + if ( $row->ss_total_pages == -1 && !$this->mViews ) { + $dbr =& wfGetDB( DB_SLAVE, array( 'SpecialStatistics', 'vslow') ); + extract( $dbr->tableNames( 'page', 'user' ) ); + + $sql = "SELECT COUNT(page_namespace) AS total FROM $page"; + $res = $dbr->query( $sql, $fname ); + $pageRow = $dbr->fetchObject( $res ); + $pages = $pageRow->total + $this->mPages; + + $sql = "SELECT COUNT(user_id) AS total FROM $user"; + $res = $dbr->query( $sql, $fname ); + $userRow = $dbr->fetchObject( $res ); + $users = $userRow->total + $this->mUsers; + + if ( $updates ) { + $updates .= ','; + } + $updates .= "ss_total_pages=$pages, ss_users=$users"; + } else { + $this->appendUpdate( $updates, 'ss_total_pages', $this->mPages ); + $this->appendUpdate( $updates, 'ss_users', $this->mUsers ); + } + } + if ( $updates ) { + $site_stats = $dbw->tableName( 'site_stats' ); + $sql = "UPDATE $site_stats SET $updates LIMIT 1"; + $dbw->query( $sql, $fname ); + } } } -?> \ No newline at end of file +?> diff --git a/includes/SpecialMovepage.php b/includes/SpecialMovepage.php index 39e5fac049..9fbf2b1ade 100644 --- a/includes/SpecialMovepage.php +++ b/includes/SpecialMovepage.php @@ -201,26 +201,6 @@ class MovePageForm { $this->showForm( $error ); return; } - - # Update counters if the article got moved into or out of NS_MAIN namespace - $ons = $ot->getNamespace(); - $nns = $nt->getNamespace(); - - # moved out of article namespace? - if ( $ons == NS_MAIN and $nns != NS_MAIN ) { - $u = new SiteStatsUpdate( 0, 1, -1); # not viewed, edited, removing - } - # moved into article namespace? - elseif ( $ons != NS_MAIN and $nns == NS_MAIN ) { - $u = new SiteStatsUpdate( 0, 1, +1 ); # not viewed, edited, adding - } else { - $u = false; - } - if ( $u !== false ) { - # save it for later update - array_push( $wgDeferredUpdateList, $u ); - unset($u); - } # Move talk page if # (1) the checkbox says to, diff --git a/includes/SpecialStatistics.php b/includes/SpecialStatistics.php index bb1793497b..73e6eb29ee 100644 --- a/includes/SpecialStatistics.php +++ b/includes/SpecialStatistics.php @@ -1,13 +1,13 @@ tableNames( 'page', 'site_stats', 'user', 'user_groups' ) ); - $sql = "SELECT COUNT(page_namespace) AS total FROM $page"; - $res = $dbr->query( $sql, $fname ); - $row = $dbr->fetchObject( $res ); - $total = $row->total; - - $sql = "SELECT ss_total_views, ss_total_edits, ss_good_articles " . - "FROM $site_stats WHERE ss_row_id=1"; - $res = $dbr->query( $sql, $fname ); - $row = $dbr->fetchObject( $res ); + $row = $dbr->selectRow( 'site_stats', '*', false, 'wfSpecialStatistics' ); $views = $row->ss_total_views; $edits = $row->ss_total_edits; $good = $row->ss_good_articles; + # This code is somewhat schema-agnostic, because I'm changing it in a minor release -- TS + if ( isset( $row->ss_total_pages ) && $row->ss_total_pages == -1 ) { + # Update schema + $u = new SiteStatsUpdate( 0, 0, 0 ); + $u->doUpdate(); + $row = $dbr->selectRow( 'site_stats', '*', false, 'wfSpecialStatistics' ); + } + + if ( isset( $row->ss_total_pages ) ) { + $total = $row->ss_total_pages; + } else { + $sql = "SELECT COUNT(page_namespace) AS total FROM $page"; + $res = $dbr->query( $sql, $fname ); + $pageRow = $dbr->fetchObject( $res ); + $total = $pageRow->total; + } + + if ( isset( $row->ss_users ) ) { + $totalUsers = $row->ss_users; + } else { + $sql = "SELECT MAX(user_id) AS total FROM $user"; + $res = $dbr->query( $sql, $fname ); + $userRow = $dbr->fetchObject( $res ); + $totalUsers = $userRow->total; + } + + $text = '==' . wfMsg( 'sitestats' ) . "==\n" ; $text .= wfMsg( 'sitestatstext', $wgLang->formatNum( $total ), @@ -39,18 +58,13 @@ function wfSpecialStatistics() { $text .= "\n==" . wfMsg( 'userstats' ) . "==\n"; - $sql = "SELECT COUNT(user_id) AS total FROM $user"; - $res = $dbr->query( $sql, $fname ); - $row = $dbr->fetchObject( $res ); - $total = $row->total; - $sql = "SELECT COUNT(*) AS total FROM $user_groups WHERE ug_group='sysop'"; $res = $dbr->query( $sql, $fname ); $row = $dbr->fetchObject( $res ); $admins = $row->total; $text .= wfMsg( 'userstatstext', - $wgLang->formatNum( $total ), + $wgLang->formatNum( $totalUsers ), $wgLang->formatNum( $admins ), '[[' . wfMsg( 'administrators' ) . ']]', // should logically be after #admins, danm backwards compatability! diff --git a/includes/SpecialUserlogin.php b/includes/SpecialUserlogin.php index 1913bbe857..b1d4886029 100644 --- a/includes/SpecialUserlogin.php +++ b/includes/SpecialUserlogin.php @@ -239,6 +239,10 @@ class LoginForm { return false; } + # Update user count + $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); + $ssUpdate->doUpdate(); + return $this->initUser( $u ); } diff --git a/includes/Title.php b/includes/Title.php index 92d813aad9..e84c236f66 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1536,8 +1536,10 @@ class Title { $pageid = $this->getArticleID(); if( $nt->exists() ) { $this->moveOverExistingRedirect( $nt, $reason ); + $pageCountChange = 0; } else { # Target didn't exist, do normal move. $this->moveToNewTitle( $nt, $newid, $reason ); + $pageCountChange = 1; } $redirid = $this->getArticleID(); @@ -1565,6 +1567,25 @@ class Title { $u->doUpdate(); $u = new SearchUpdate( $redirid, $this->getPrefixedDBkey(), '' ); $u->doUpdate(); + + # Update site_stats + if ( $this->getNamespace() == NS_MAIN and $nt->getNamespace() != NS_MAIN ) { + # Moved out of main namespace + # not viewed, edited, removing + $u = new SiteStatsUpdate( 0, 1, -1, $pageCountChange); + } elseif ( $this->getNamespace() != NS_MAIN and $nt->getNamespace() == NS_MAIN ) { + # Moved into main namespace + # not viewed, edited, adding + $u = new SiteStatsUpdate( 0, 1, +1, $pageCountChange ); + } elseif ( $pageCountChange ) { + # Added redirect + $u = new SiteStatsUpdate( 0, 0, 0, 1 ); + } else{ + $u = false; + } + if ( $u ) { + $u->doUpdate(); + } wfRunHooks( 'TitleMoveComplete', array( &$this, &$nt, &$wgUser, $pageid, $redirid ) ); return true; diff --git a/maintenance/archives/patch-ss_total_articles.sql b/maintenance/archives/patch-ss_total_articles.sql new file mode 100644 index 0000000000..b4a48cf7b3 --- /dev/null +++ b/maintenance/archives/patch-ss_total_articles.sql @@ -0,0 +1,6 @@ +-- Faster statistics, as of 1.4.3 + +ALTER TABLE /*$wgDBprefix*/site_stats + ADD ss_total_pages bigint(20) default -1, + ADD ss_users bigint(20) default -1, + ADD ss_admins int(10) default -1; diff --git a/maintenance/tables.sql b/maintenance/tables.sql index c03a3b880d..3521b63fac 100644 --- a/maintenance/tables.sql +++ b/maintenance/tables.sql @@ -437,6 +437,15 @@ CREATE TABLE /*$wgDBprefix*/site_stats ( -- See isCountable() in includes/Article.php ss_good_articles bigint(20) unsigned default '0', + -- Total pages, theoretically equal to SELECT COUNT(*) FROM cur; except faster + ss_total_pages bigint(20) default -1, + + -- Number of users, theoretically equal to SELECT COUNT(*) FROM user; + ss_users bigint(20) default -1, + + -- Deprecated, no longer updated as of 1.5 + ss_admins int(10) default -1, + UNIQUE KEY ss_row_id (ss_row_id) ) TYPE=InnoDB; diff --git a/maintenance/updaters.inc b/maintenance/updaters.inc index e17cb5c363..5978eede30 100644 --- a/maintenance/updaters.inc +++ b/maintenance/updaters.inc @@ -45,6 +45,7 @@ $wgNewFields = array( array( 'image', 'img_metadata', 'patch-img_metadata.sql' ), array( 'image', 'img_media_type', 'patch-img_media_type.sql' ), array( 'validate', 'val_ip', 'patch-val_ip.sql' ), + array( 'site_stats', 'ss_total_articles', 'patch-ss_total_articles.sql' ), ); function rename_table( $from, $to, $patch ) { -- 2.20.1