-
Notifications
You must be signed in to change notification settings - Fork 6
/
wiretapRecordPageHitCount.php
109 lines (93 loc) · 3.22 KB
/
wiretapRecordPageHitCount.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This script records the number of hits each page in the wiki has
* recieved in the last day/week/month/all-time.
*
* Usage:
* --type: whether to record all time or hits in a period
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @author James Montalvo
* @ingroup Maintenance
*/
// @todo: does this always work if extensions are not in $IP/extensions ??
// this was what was done by SMW
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false ? getenv( 'MW_INSTALL_PATH' ) : __DIR__ . '/../..';
require_once $basePath . '/maintenance/Maintenance.php';
class WiretapRecordPageHitCount extends Maintenance {
public function __construct() {
parent::__construct();
$this->mDescription = "Count the recent hits for each page.";
$this->addOption(
'type',
'Whether to record all-time count (--type=all) or period count (default, or --type=period)',
false, // required (will assume period)
true, // with arg @todo what does this mean?
false // short name
);
}
public function execute() {
global $wgWiretapCounterPeriod;
$type = $this->getOption( 'type', false );
if ( ! $type ) {
$type = 'period';
}
else if ( ! in_array( $type, array( 'period', 'all' ) ) ) {
$this->output( "\n \"type\" option must be set to either \"all\" or \"period\". \n" );
}
$readConditions = array( 'page_id != 0' );
if ( $type == 'all' ) {
$writeTable = array( 'c' => 'wiretap_counter_alltime' );
}
else {
date_default_timezone_set("UTC");
$ts = new MWTimestamp( date( 'YmdHis', strtotime( "now - $wgWiretapCounterPeriod days" ) ) );
$ts = $ts->format("YmdHis");
$this->output( "\nRecording hits since $ts\n\n" );
$writeTable = array( 'c' => 'wiretap_counter_period' );
$readConditions[] = "hit_timestamp > $ts";
}
$dbw = wfGetDB( DB_MASTER );
// clear the table
$res = $dbw->delete(
$writeTable['c'],
array( 'page_id > -1' ), // conditions = none; delete everything
__METHOD__
);
// query wiretap table, repopulate $writeTable
$res = $dbw->insertSelect(
$writeTable['c'],
array( 'w' => 'wiretap' ),
array(
// 'dest' => 'source'
'page_id' => 'page_id',
'count' => 'COUNT(*) AS total_hits',
'count_unique' => 'COUNT( DISTINCT user_name ) AS unique_hits',
),
$readConditions,
__METHOD__,
array(), // insert options
array( // select options
"GROUP BY" => "page_id",
"ORDER BY" => "page_id ASC",
)
);
$this->output( "\n Finished recording page traffic. \n" );
}
}
$maintClass = "WiretapRecordPageHitCount";
require_once( DO_MAINTENANCE );