Article.php:
authorMr. E23 <e23@users.mediawiki.org>
Sat, 13 Dec 2003 21:32:32 +0000 (21:32 +0000)
committerMr. E23 <e23@users.mediawiki.org>
Sat, 13 Dec 2003 21:32:32 +0000 (21:32 +0000)
* 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

includes/Article.php
maintenance/archives/patch-hitcounter.sql [new file with mode: 0644]
maintenance/tables.sql
update.php

index 53e6221..0c3afaf 100644 (file)
@@ -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 (file)
index 0000000..169081f
--- /dev/null
@@ -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;
index 7e99f5b..9d9b454 100644 (file)
@@ -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,
index f91a80c..9e87d3a 100644 (file)
@@ -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";
+       }
+}
+
 
 ?>