MediaWiki  1.23.2
ViewCountUpdate.php
Go to the documentation of this file.
1 <?php
32  protected $id;
33 
39  public function __construct( $id ) {
40  $this->id = intval( $id );
41  }
42 
46  public function doUpdate() {
47  global $wgHitcounterUpdateFreq;
48 
49  $dbw = wfGetDB( DB_MASTER );
50 
51  if ( $wgHitcounterUpdateFreq <= 1 || $dbw->getType() == 'sqlite' ) {
52  $dbw->update(
53  'page', array( 'page_counter = page_counter + 1' ),
54  array( 'page_id' => $this->id ),
55  __METHOD__
56  );
57 
58  return;
59  }
60 
61  # Not important enough to warrant an error page in case of failure
62  try {
63  $dbw->insert( 'hitcounter', array( 'hc_id' => $this->id ), __METHOD__ );
64  $checkfreq = intval( $wgHitcounterUpdateFreq / 25 + 1 );
65  if ( rand() % $checkfreq == 0 && $dbw->lastErrno() == 0 ) {
66  $this->collect();
67  }
68  } catch ( DBError $e ) {
69  }
70  }
71 
72  protected function collect() {
73  global $wgHitcounterUpdateFreq;
74 
75  $dbw = wfGetDB( DB_MASTER );
76 
77  $rown = $dbw->selectField( 'hitcounter', 'COUNT(*)', array(), __METHOD__ );
78 
79  if ( $rown < $wgHitcounterUpdateFreq ) {
80  return;
81  }
82 
83  wfProfileIn( __METHOD__ . '-collect' );
84  $old_user_abort = ignore_user_abort( true );
85 
86  $dbw->lockTables( array(), array( 'hitcounter' ), __METHOD__, false );
87 
88  $dbType = $dbw->getType();
89  $tabletype = $dbType == 'mysql' ? "ENGINE=HEAP " : '';
90  $hitcounterTable = $dbw->tableName( 'hitcounter' );
91  $acchitsTable = $dbw->tableName( 'acchits' );
92  $pageTable = $dbw->tableName( 'page' );
93 
94  $dbw->query( "CREATE TEMPORARY TABLE $acchitsTable $tabletype AS " .
95  "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable " .
96  'GROUP BY hc_id', __METHOD__ );
97  $dbw->delete( 'hitcounter', '*', __METHOD__ );
98  $dbw->unlockTables( __METHOD__ );
99 
100  if ( $dbType == 'mysql' ) {
101  $dbw->query( "UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n " .
102  'WHERE page_id = hc_id', __METHOD__ );
103  } else {
104  $dbw->query( "UPDATE $pageTable SET page_counter=page_counter + hc_n " .
105  "FROM $acchitsTable WHERE page_id = hc_id", __METHOD__ );
106  }
107  $dbw->query( "DROP TABLE $acchitsTable", __METHOD__ );
108 
109  ignore_user_abort( $old_user_abort );
110  wfProfileOut( __METHOD__ . '-collect' );
111  }
112 }
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
ViewCountUpdate\$id
int $id
Page ID to increment the view count *.
Definition: ViewCountUpdate.php:31
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ViewCountUpdate\__construct
__construct( $id)
Constructor.
Definition: ViewCountUpdate.php:38
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ViewCountUpdate
Update for the 'page_counter' field, when $wgDisableCounters is false.
Definition: ViewCountUpdate.php:30
DBError
Database error base class.
Definition: DatabaseError.php:28
ViewCountUpdate\collect
collect()
Definition: ViewCountUpdate.php:71
ViewCountUpdate\doUpdate
doUpdate()
Run the update.
Definition: ViewCountUpdate.php:45
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferredUpdates.php:29
$e
if( $useReadline) $e
Definition: eval.php:66