Merge "Align image frame padding with core"
[mediawiki.git] / includes / deferred / ViewCountUpdate.php
blobddd2e09fbef47cb3ed283db63e842505d40c4485
1 <?php
2 /**
3 * Update for the 'page_counter' field
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Update for the 'page_counter' field, when $wgDisableCounters is false.
26 * Depending on $wgHitcounterUpdateFreq, this will directly increment the
27 * 'page_counter' field or use the 'hitcounter' table and then collect the data
28 * from that table to update the 'page_counter' field in a batch operation.
30 class ViewCountUpdate implements DeferrableUpdate {
31 /** @var int Page ID to increment the view count */
32 protected $id;
34 /**
35 * Constructor
37 * @param int $id Page ID to increment the view count
39 public function __construct( $id ) {
40 $this->id = intval( $id );
43 /**
44 * Run the update
46 public function doUpdate() {
47 global $wgHitcounterUpdateFreq;
49 $dbw = wfGetDB( DB_MASTER );
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__
58 return;
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();
68 } catch ( DBError $e ) {
72 protected function collect() {
73 global $wgHitcounterUpdateFreq;
75 $dbw = wfGetDB( DB_MASTER );
77 $rown = $dbw->selectField( 'hitcounter', 'COUNT(*)', array(), __METHOD__ );
79 if ( $rown < $wgHitcounterUpdateFreq ) {
80 return;
83 wfProfileIn( __METHOD__ . '-collect' );
84 $old_user_abort = ignore_user_abort( true );
86 $dbw->lockTables( array(), array( 'hitcounter' ), __METHOD__, false );
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' );
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__ );
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__ );
107 $dbw->query( "DROP TABLE $acchitsTable", __METHOD__ );
109 ignore_user_abort( $old_user_abort );
110 wfProfileOut( __METHOD__ . '-collect' );