MediaWiki  REL1_23
ViewCountUpdate.php
Go to the documentation of this file.
00001 <?php
00030 class ViewCountUpdate implements DeferrableUpdate {
00032     protected $id;
00033 
00039     public function __construct( $id ) {
00040         $this->id = intval( $id );
00041     }
00042 
00046     public function doUpdate() {
00047         global $wgHitcounterUpdateFreq;
00048 
00049         $dbw = wfGetDB( DB_MASTER );
00050 
00051         if ( $wgHitcounterUpdateFreq <= 1 || $dbw->getType() == 'sqlite' ) {
00052             $dbw->update(
00053                 'page', array( 'page_counter = page_counter + 1' ),
00054                 array( 'page_id' => $this->id ),
00055                 __METHOD__
00056             );
00057 
00058             return;
00059         }
00060 
00061         # Not important enough to warrant an error page in case of failure
00062         try {
00063             $dbw->insert( 'hitcounter', array( 'hc_id' => $this->id ), __METHOD__ );
00064             $checkfreq = intval( $wgHitcounterUpdateFreq / 25 + 1 );
00065             if ( rand() % $checkfreq == 0 && $dbw->lastErrno() == 0 ) {
00066                 $this->collect();
00067             }
00068         } catch ( DBError $e ) {
00069         }
00070     }
00071 
00072     protected function collect() {
00073         global $wgHitcounterUpdateFreq;
00074 
00075         $dbw = wfGetDB( DB_MASTER );
00076 
00077         $rown = $dbw->selectField( 'hitcounter', 'COUNT(*)', array(), __METHOD__ );
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         $hitcounterTable = $dbw->tableName( 'hitcounter' );
00091         $acchitsTable = $dbw->tableName( 'acchits' );
00092         $pageTable = $dbw->tableName( 'page' );
00093 
00094         $dbw->query( "CREATE TEMPORARY TABLE $acchitsTable $tabletype AS " .
00095             "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable " .
00096             'GROUP BY hc_id', __METHOD__ );
00097         $dbw->delete( 'hitcounter', '*', __METHOD__ );
00098         $dbw->unlockTables( __METHOD__ );
00099 
00100         if ( $dbType == 'mysql' ) {
00101             $dbw->query( "UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n " .
00102                 'WHERE page_id = hc_id', __METHOD__ );
00103         } else {
00104             $dbw->query( "UPDATE $pageTable SET page_counter=page_counter + hc_n " .
00105                 "FROM $acchitsTable WHERE page_id = hc_id", __METHOD__ );
00106         }
00107         $dbw->query( "DROP TABLE $acchitsTable", __METHOD__ );
00108 
00109         ignore_user_abort( $old_user_abort );
00110         wfProfileOut( __METHOD__ . '-collect' );
00111     }
00112 }