Merge "Fix trailing whitespace (and mixed spaces) in XSD files"
[lhc/web/wiklou.git] / includes / ViewCountUpdate.php
1 <?php
2 /**
3 * Update for the 'page_counter' field
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * Update for the 'page_counter' field, when $wgDisableCounters is false.
25 *
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.
29 */
30 class ViewCountUpdate implements DeferrableUpdate {
31 protected $id;
32
33 /**
34 * Constructor
35 *
36 * @param $id Integer: page ID to increment the view count
37 */
38 public function __construct( $id ) {
39 $this->id = intval( $id );
40 }
41
42 /**
43 * Run the update
44 */
45 public function doUpdate() {
46 global $wgHitcounterUpdateFreq;
47
48 $dbw = wfGetDB( DB_MASTER );
49
50 if ( $wgHitcounterUpdateFreq <= 1 || $dbw->getType() == 'sqlite' ) {
51 $dbw->update( 'page', array( 'page_counter = page_counter + 1' ), array( 'page_id' => $this->id ), __METHOD__ );
52 return;
53 }
54
55 # Not important enough to warrant an error page in case of failure
56 $oldignore = $dbw->ignoreErrors( true );
57
58 $dbw->insert( 'hitcounter', array( 'hc_id' => $this->id ), __METHOD__ );
59
60 $checkfreq = intval( $wgHitcounterUpdateFreq / 25 + 1 );
61 if ( rand() % $checkfreq == 0 && $dbw->lastErrno() == 0 ) {
62 $this->collect();
63 }
64
65 $dbw->ignoreErrors( $oldignore );
66 }
67
68 protected function collect() {
69 global $wgHitcounterUpdateFreq;
70
71 $dbw = wfGetDB( DB_MASTER );
72
73 $rown = $dbw->selectField( 'hitcounter', 'COUNT(*)', array(), __METHOD__ );
74
75 if ( $rown < $wgHitcounterUpdateFreq ) {
76 return;
77 }
78
79 wfProfileIn( __METHOD__ . '-collect' );
80 $old_user_abort = ignore_user_abort( true );
81
82 $dbw->lockTables( array(), array( 'hitcounter' ), __METHOD__, false );
83
84 $dbType = $dbw->getType();
85 $tabletype = $dbType == 'mysql' ? "ENGINE=HEAP " : '';
86 $hitcounterTable = $dbw->tableName( 'hitcounter' );
87 $acchitsTable = $dbw->tableName( 'acchits' );
88 $pageTable = $dbw->tableName( 'page' );
89
90 $dbw->query( "CREATE TEMPORARY TABLE $acchitsTable $tabletype AS " .
91 "SELECT hc_id,COUNT(*) AS hc_n FROM $hitcounterTable " .
92 'GROUP BY hc_id', __METHOD__ );
93 $dbw->delete( 'hitcounter', '*', __METHOD__ );
94 $dbw->unlockTables( __METHOD__ );
95
96 if ( $dbType == 'mysql' ) {
97 $dbw->query( "UPDATE $pageTable,$acchitsTable SET page_counter=page_counter + hc_n " .
98 'WHERE page_id = hc_id', __METHOD__ );
99 } else {
100 $dbw->query( "UPDATE $pageTable SET page_counter=page_counter + hc_n " .
101 "FROM $acchitsTable WHERE page_id = hc_id", __METHOD__ );
102 }
103 $dbw->query( "DROP TABLE $acchitsTable", __METHOD__ );
104
105 ignore_user_abort( $old_user_abort );
106 wfProfileOut( __METHOD__ . '-collect' );
107 }
108 }