MediaWiki  REL1_21
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                         $dbw->update( 'page', array( 'page_counter = page_counter + 1' ), array( 'page_id' => $this->id ), __METHOD__ );
00052                         return;
00053                 }
00054 
00055                 # Not important enough to warrant an error page in case of failure
00056                 try {
00057                         $dbw->insert( 'hitcounter', array( 'hc_id' => $this->id ), __METHOD__ );
00058                         $checkfreq = intval( $wgHitcounterUpdateFreq / 25 + 1 );
00059                         if ( rand() % $checkfreq == 0 && $dbw->lastErrno() == 0 ) {
00060                                 $this->collect();
00061                         }
00062                 } catch ( DBError $e ) {}
00063         }
00064 
00065         protected function collect() {
00066                 global $wgHitcounterUpdateFreq;
00067 
00068                 $dbw = wfGetDB( DB_MASTER );
00069 
00070                 $rown = $dbw->selectField( 'hitcounter', 'COUNT(*)', array(), __METHOD__ );
00071 
00072                 if ( $rown < $wgHitcounterUpdateFreq ) {
00073                         return;
00074                 }
00075 
00076                 wfProfileIn( __METHOD__ . '-collect' );
00077                 $old_user_abort = ignore_user_abort( true );
00078 
00079                 $dbw->lockTables( array(), array( 'hitcounter' ), __METHOD__, false );
00080 
00081                 $dbType = $dbw->getType();
00082                 $tabletype = $dbType == 'mysql' ? "ENGINE=HEAP " : '';
00083                 $hitcounterTable = $dbw->tableName( 'hitcounter' );
00084                 $acchitsTable = $dbw->tableName( 'acchits' );
00085                 $pageTable = $dbw->tableName( 'page' );
00086 
00087                 $dbw->query( "CREATE TEMPORARY TABLE $acchitsTable $tabletype AS " .
00088                         "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable " .
00089                         'GROUP BY hc_id', __METHOD__ );
00090                 $dbw->delete( 'hitcounter', '*', __METHOD__ );
00091                 $dbw->unlockTables( __METHOD__ );
00092 
00093                 if ( $dbType == 'mysql' ) {
00094                         $dbw->query( "UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n " .
00095                                 'WHERE page_id = hc_id', __METHOD__ );
00096                 } else {
00097                         $dbw->query( "UPDATE $pageTable SET page_counter=page_counter + hc_n " .
00098                                 "FROM $acchitsTable WHERE page_id = hc_id", __METHOD__ );
00099                 }
00100                 $dbw->query( "DROP TABLE $acchitsTable", __METHOD__ );
00101 
00102                 ignore_user_abort( $old_user_abort );
00103                 wfProfileOut( __METHOD__ . '-collect' );
00104         }
00105 }