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