From: Mr. E23 Date: Sat, 13 Dec 2003 21:32:32 +0000 (+0000) Subject: Article.php: X-Git-Tag: 1.3.0beta1~1252 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/operations/recherche.php?a=commitdiff_plain;h=6a41de71d7fc3f4a1db1b56e23ac83bb68cac4fd;p=lhc%2Fweb%2Fwiklou.git Article.php: * Made updating of page view stats faster my buffering them in a HEAP table until many pages can be updated at once. tables.sql: * hitcounter table patch-hitcounter.sql: * hitcounter table update.php: * Creating hitcounter table when necessary --- diff --git a/includes/Article.php b/includes/Article.php index 53e622122f..0c3afaf912 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -301,6 +301,7 @@ class Article { } else if ( $this->tryFileCache() ) { # tell wgOut that output is taken care of $wgOut->disable(); + $this->viewUpdates(); return; } } @@ -1041,12 +1042,10 @@ class Article { /* private */ function viewUpdates() { global $wgDeferredUpdateList; - if ( 0 != $this->getID() ) { global $wgDisableCounters; if( !$wgDisableCounters ) { - $u = new ViewCountUpdate( $this->getID() ); - array_push( $wgDeferredUpdateList, $u ); + Article::incViewCount( $this->getID() ); $u = new SiteStatsUpdate( 1, 0, 0 ); array_push( $wgDeferredUpdateList, $u ); } @@ -1266,6 +1265,44 @@ class Article { return false; } } + + /* static */ function incViewCount( $id ) + { + $id = intval( $id ); + + # Not important enough to warrant an error page in case of failure + $oldignore = wfIgnoreSQLErrors( true ); + + wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE); + + if( (rand() % 23 != 0) or (wfLastErrno() != 0) ){ + # Most of the time (or on SQL errors), skip row count check + wfIgnoreSQLErrors( $oldignore ); + return; + } + + $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE); + $row = wfFetchObject( $res ); + $rown = intval( $row->n ); + if( $rown > 1000 ){ // update counters after ~1000 hits + wfProfileIn( "Article::incViewCount-collect" ); + $old_user_abort = ignore_user_abort( true ); + + wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE); + wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ". + "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ". + "GROUP BY hc_id", DB_WRITE); + wfQuery("DELETE FROM hitcounter", DB_WRITE); + wfQuery("UNLOCK TABLES", DB_WRITE); + wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ". + "WHERE cur_id = hc_id", DB_WRITE); + wfQuery("DROP TABLE acchits", DB_WRITE); + + ignore_user_abort( $old_user_abort ); + wfProfileOut( "Article::incViewCount-collect" ); + } + wfIgnoreSQLErrors( $oldignore ); + } } function wfReplaceSubstVar( $matches ) { diff --git a/maintenance/archives/patch-hitcounter.sql b/maintenance/archives/patch-hitcounter.sql new file mode 100644 index 0000000000..169081ff1d --- /dev/null +++ b/maintenance/archives/patch-hitcounter.sql @@ -0,0 +1,9 @@ +-- +-- hitcounter table is used to buffer page hits before they are periodically +-- counted and added to the cur_counter column in the cur table. +-- December 2003 +-- + +CREATE TABLE hitcounter ( + hc_id INTEGER UNSIGNED NOT NULL +) TYPE=HEAP MAX_ROWS=25000; diff --git a/maintenance/tables.sql b/maintenance/tables.sql index 7e99f5bb19..9d9b454002 100644 --- a/maintenance/tables.sql +++ b/maintenance/tables.sql @@ -112,6 +112,11 @@ CREATE TABLE site_stats ( UNIQUE KEY ss_row_id (ss_row_id) ) TYPE=MyISAM; +DROP TABLE IF EXISTS hitcounter; +CREATE TABLE hitcounter ( + hc_id INTEGER UNSIGNED NOT NULL +) TYPE=HEAP MAX_ROWS=25000; + DROP TABLE IF EXISTS ipblocks; CREATE TABLE ipblocks ( ipb_id int(8) NOT NULL auto_increment, diff --git a/update.php b/update.php index f91a80ce3f..9e87d3af5c 100644 --- a/update.php +++ b/update.php @@ -52,6 +52,7 @@ $wgTitle = Title::newFromText( "Update script" ); do_interwiki_update(); do_index_update(); do_linkscc_update(); + do_hitcounter_update(); initialiseMessages(); @@ -183,5 +184,17 @@ function do_linkscc_update() { } } +function do_hitcounter_update() { + // Create hitcounter if necessary + global $rconn; + if( table_exists( "hitcounter" ) ) { + echo "...have hitcounter table.\n"; + } else { + echo "Adding hitcounter table... "; + dbsource( "maintenance/archives/patch-hitcounter.sql" ); + echo "ok\n"; + } +} + ?>