MediaWiki  REL1_19
ViewCountUpdate.php
Go to the documentation of this file.
00001 <?php
00030 class ViewCountUpdate implements DeferrableUpdate {
00031         protected $id;
00032 
00038         public function __construct( $id ) {
00039                 $this->id = intval( $id );
00040         }
00041 
00045         public function doUpdate() {
00046                 global $wgHitcounterUpdateFreq;
00047 
00048                 $dbw = wfGetDB( DB_MASTER );
00049 
00050                 if ( $wgHitcounterUpdateFreq <= 1 || $dbw->getType() == 'sqlite' ) {
00051                         $pageTable = $dbw->tableName( 'page' );
00052                         $dbw->query( "UPDATE $pageTable SET page_counter = page_counter + 1 WHERE page_id = {$this->id}" );
00053                         return;
00054                 }
00055 
00056                 # Not important enough to warrant an error page in case of failure
00057                 $oldignore = $dbw->ignoreErrors( true );
00058 
00059                 $dbw->insert( 'hitcounter', array( 'hc_id' => $this->id ), __METHOD__ );
00060 
00061                 $checkfreq = intval( $wgHitcounterUpdateFreq / 25 + 1 );
00062                 if ( rand() % $checkfreq == 0 && $dbw->lastErrno() == 0 ) {
00063                         $this->collect();
00064                 }
00065 
00066                 $dbw->ignoreErrors( $oldignore );
00067         }
00068 
00069         protected function collect() {
00070                 global $wgHitcounterUpdateFreq;
00071 
00072                 $dbw = wfGetDB( DB_MASTER );
00073 
00074                 $hitcounterTable = $dbw->tableName( 'hitcounter' );
00075                 $res = $dbw->query( "SELECT COUNT(*) as n FROM $hitcounterTable" );
00076                 $row = $dbw->fetchObject( $res );
00077                 $rown = intval( $row->n );
00078 
00079                 if ( $rown < $wgHitcounterUpdateFreq ) {
00080                         return;
00081                 }
00082 
00083                 wfProfileIn( __METHOD__ . '-collect' );
00084                 $old_user_abort = ignore_user_abort( true );
00085 
00086                 $dbw->lockTables( array(), array( 'hitcounter' ), __METHOD__, false );
00087 
00088                 $dbType = $dbw->getType();
00089                 $tabletype = $dbType == 'mysql' ? "ENGINE=HEAP " : '';
00090                 $acchitsTable = $dbw->tableName( 'acchits' );
00091                 $pageTable = $dbw->tableName( 'page' );
00092 
00093                 $dbw->query( "CREATE TEMPORARY TABLE $acchitsTable $tabletype AS " .
00094                         "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable " .
00095                         'GROUP BY hc_id', __METHOD__ );
00096                 $dbw->delete( 'hitcounter', '*', __METHOD__ );
00097                 $dbw->unlockTables( __METHOD__ );
00098 
00099                 if ( $dbType == 'mysql' ) {
00100                         $dbw->query( "UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n " .
00101                                 'WHERE page_id = hc_id', __METHOD__ );
00102                 } else {
00103                         $dbw->query( "UPDATE $pageTable SET page_counter=page_counter + hc_n " .
00104                                 "FROM $acchitsTable WHERE page_id = hc_id", __METHOD__ );
00105                 }
00106                 $dbw->query( "DROP TABLE $acchitsTable", __METHOD__ );
00107 
00108                 ignore_user_abort( $old_user_abort );
00109                 wfProfileOut( __METHOD__ . '-collect' );
00110         }
00111 }