Skip to content

Commit bec92be

Browse files
authored
Merge pull request #6 from WikibaseSolutions/REL1_39
Rel1 39
2 parents a0ce376 + c0d65dd commit bec92be

14 files changed

Lines changed: 732 additions & 220 deletions

README.md

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# WSStats
2-
This MediaWiki extension counts pageviews by user
2+
This MediaWiki 1.39.x extension counts pageviews by user
33

4+
* Version 2.0.0 : REL 1.39 only. Added statistics for Special Pages. Lua equivalent functions for statistics. Special Page added.
45
* Version 1.0.8 : Removed global references
56
* Version 1.0.7 : Added statistics over time for pages
67
* Version 1.0.6 : Fixed path to sql tables
@@ -22,7 +23,7 @@ This MediaWiki extension counts pageviews by user
2223
* Version 0.1.2 : Skip usergroup results
2324
* Version 0.1.1 : Initial release
2425

25-
##Installation
26+
## Installation
2627

2728
Create a folder called WSStats in the MediaWiki extensions folder. Copy the content from bitbucket inside this new folder.
2829

@@ -36,7 +37,11 @@ Run the [update script](https://www.mediawiki.org/wiki/Manual:Update.php) which
3637

3738
Navigate to Special:Version on your wiki to verify that the extension is successfully installed.
3839

39-
#Configuration
40+
## Upgrading
41+
If you are upgrading from a version before 2.0, then you must visit the Special Page : Special:WSStats. If the database tables need updating, it will show you there and you can update the tables.
42+
You will have to update the tables for statistics to be accurate. This is because we now allow to get statistics based on page titles, while the previous version of WSStats did not store titles.
43+
44+
# Configuration
4045

4146
By default Anonymous users and sysops are skipped from stats recording. To change this add following to LocalSettings.php..
4247

@@ -50,6 +55,12 @@ Allow statistics for anonymous users:
5055
$wgWSStats['skip_anonymous'] = false;
5156
````
5257

58+
By default Special Pages are counted as well. To omit Special pages set the following :
59+
````
60+
# Special Pages statistics
61+
$wgWSStats['countSpecialPages'] = false; // defaults to true
62+
````
63+
5364
To skip users in certain groups, just add the groupname to "skip_user_groups" :
5465
````
5566
# Record anonymous users
@@ -58,16 +69,11 @@ $wgWSStats['skip_anonymous'] = false;
5869
# Skip if user is in following groups
5970
$wgWSStats['skip_user_groups'][] = 'sysop';
6071
$wgWSStats['skip_user_groups'][] = 'admin';
61-
````
6272
63-
Count all hits:
64-
````
65-
$wgWSStats = array();
66-
$wgWSStats['count_all_usergroups'] = true;
73+
# Allow all usergroups, including sysop
74+
$wgWSStats['skip_user_groups'] = [];
6775
````
6876

69-
***NOTE**: If you have set $wgWSStats['count_all']=true; then $wgWSStats['skip_user_groups'] is ignored.*
70-
''
7177

7278
Skip page with certain text in their referer url. Default action=edit and veaction=edit are ignored. This configuration option is case sensitive:
7379
````
@@ -76,15 +82,27 @@ $wgWSStats['ignore_in_url'][] = 'Template:Test';
7682
$wgWSStats['ignore_in_url'][] = 'action=edit';
7783
````
7884

79-
#Using the parser function
85+
# Using the parser function
8086
To retrieve statistics you can use the following parser functions :
8187

88+
8289
#### Ask number of hits for page id : 9868
8390
This returns a number
8491
```
8592
{{#wsstats:id=9868}}
8693
```
8794

95+
You can also ask for statistics based on title, instead of Page Id's.
96+
#### Ask number of hits for page with title : Main Page
97+
This returns a number
98+
```
99+
{{#wsstats:title=Main Page}}
100+
```
101+
102+
About dates. Dates are in a format of YYYY-MM-DD. Internally WSStats works with Date and Time.
103+
This means a date of **2023-10-30** will internally become **2023-10-30 00:00:00**. You can also search by date and time. See the example about this.
104+
105+
88106
#### Ask number of hits for page id : 714 since start date 2018-02-01
89107
This returns a number
90108
```
@@ -100,12 +118,21 @@ This returns a number
100118
|end date=2018-02-08}}
101119
```
102120

121+
#### You can also get statistics based on date and time Get number of hits for page id : 714 from start date 2023-10-30 14:00:00 and end date 2023-10-30 16:00:00
122+
This returns a number
123+
```
124+
{{#wsstats:id=714
125+
|start date=2023-10-30 14:00:00
126+
|end date=2023-10-30 16:00:00
127+
}}
128+
```
129+
103130
#### Filter results on registered users or anonymous users
104131
This returns a number
105132
```
106133
{{#wsstats:id=714
107-
|start date=2018-02-01
108-
|end date=2018-02-08
134+
|start date=2023-10-30 14:00:00
135+
|end date=2023-10-30 16:00:00
109136
|type=only anonymous}}
110137
```
111138

@@ -184,4 +211,47 @@ This returns a table
184211
{{#wsstats:stats
185212
|unique
186213
|limit=20}}
214+
```
215+
216+
# Using the lua functions
217+
New in version 2.0
218+
219+
There are two Lua function you can use.
220+
221+
For the parser function that returns a table ( {{#wsstats:stats.. ) you can use wsstats.stats().
222+
223+
For the parser function that returns a number ( {{wsstats:... ) you can use wsstats.stat();
224+
225+
All the arguments are the same as for the parser functions, except :
226+
227+
* start date = startDate
228+
* end date = endDate
229+
230+
### Example
231+
232+
If you create a Module called WSStats and you add the following content :
233+
```lua
234+
local p = {}
235+
236+
function p.stats(frame)
237+
stats = mw.wsstats.stats( frame.args )
238+
return stats
239+
240+
end
241+
242+
function p.stat(frame)
243+
stat = mw.wsstats.stat( frame.args )
244+
return stat
245+
246+
end
247+
248+
return p
249+
```
250+
251+
You can then do calls like :
252+
```
253+
{{#invoke:wsstats|stats}} // returns a Lua table
254+
```
255+
```
256+
{{#invoke:wsstats|stat|id=1|startDate=2023-09-25|endDate=2023-09-26}}
187257
```

WSStats.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"keywords": ["mediawiki", "wsstats", "extension", "statistics", "views"],
66
"license": "GPL-2.0-or-later",
77
"require": {
8-
"php": "^7.0",
8+
"php": "^8.0",
99
"ext-zip": "*"
1010
},
1111
"require-dev": {

extension.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "WSStats",
3-
"version": "1.0.8",
3+
"version": "2.0.0",
44
"author": [
55
"Sen-Sai"
66
],
@@ -19,7 +19,7 @@
1919
"skip_user_groups": [
2020
"sysop"
2121
],
22-
"count_all_usergroups": true,
22+
"countSpecialPages" : true,
2323
"ignore_in_url": [
2424
"action=edit",
2525
"veaction=edit"
@@ -34,7 +34,8 @@
3434
"ParserFirstCallInit": "WSStats\\WSStatsHooks::onParserFirstCallInit",
3535
"BeforePageDisplay": "WSStats\\WSStatsHooks::onBeforePageDisplay",
3636
"LoadExtensionSchemaUpdates": "WSStats\\WSStatsHooks::addTables",
37-
"AdminLinks": "WSStats\\WSStatsHooks::addToAdminLinks"
37+
"AdminLinks": "WSStats\\WSStatsHooks::addToAdminLinks",
38+
"ScribuntoExternalLibraries": "\\WSStats\\WSStatsHooks::onScribuntoExternalLibraries"
3839
},
3940
"MessagesDirs": {
4041
"WSStats": [

i18n/en.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
"wsstats-page-date": "Date",
1111
"wsstats-page-title": "Page title",
1212
"wsstats-page-id": "Page ID",
13-
"wsstats-page-hits": "Page hits"
13+
"wsstats-page-hits": "Page hits",
14+
"wsstats-special-list": "Top visited pages of this wiki",
15+
"wsstats-special-db-need-update": "The WSStats Database needs an update to return correct values.",
16+
"wsstats-special-db-need-update-btn": "Click here to update $1 records in the database.",
17+
"wsstats-special-db-need-update-result": "$1 records haven been successfully updated"
1418
}

sql/WSStats.mysql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ CREATE TABLE /*_*/WSPS (
33
`page_id` int(11) NOT NULL default '0',
44
`user_id` int(11) NOT NULL default '0',
55
`added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
6+
`title` varchar(250) NOT NULL DEFAULT ''
7+
`isSpecialPage` INT(1) NOT NULL DEFAULT '0'
68
) /*$wgDBTableOptions*/;

sql/WSStatsAddSpecialBool.mysql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE /*_*/WSPS
2+
ADD COLUMN isSpecialPage INT(1) NOT NULL DEFAULT '0';

sql/WSStatsAddTitle.mysql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE /*_*/WSPS
2+
ADD COLUMN title VARCHAR(250) NOT NULL DEFAULT '';

src/Helpers/SelectionMaker.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace WSStats\Helpers;
4+
5+
use WSStats\WSStatsHooks;
6+
7+
class SelectionMaker {
8+
9+
/**
10+
* @param int $id
11+
* @param string $title
12+
* @param string|bool $dbType
13+
*
14+
* @return array
15+
*/
16+
public function createSelectionNoDates( int $id, string $title, string|bool $dbType ): array {
17+
// Set Conditions
18+
$countSpecialPages = WSStatsHooks::getConfigSetting( 'countSpecialPages' );
19+
if ( !$dbType ) {
20+
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
21+
$selectConditions = [ "title = '" . $title . "'" ];
22+
} else {
23+
$selectConditions = [ "page_id = " . $id ];
24+
}
25+
} else {
26+
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
27+
$selectConditions = [ "title = '" . $title . "'", $dbType ];
28+
} else {
29+
$selectConditions = [ "page_id = " . $id,
30+
$dbType ];
31+
}
32+
}
33+
34+
return $selectConditions;
35+
}
36+
37+
/**
38+
* @param int $id
39+
* @param string $title
40+
* @param string|bool $dbType
41+
* @param array $dates
42+
*
43+
* @return array
44+
*/
45+
public function createSelectionUsingDates( int $id, string $title, string|bool $dbType, array $dates ): array {
46+
$countSpecialPages = WSStatsHooks::getConfigSetting( 'countSpecialPages' );
47+
if ( $dates['e'] === false ) {
48+
// Set Conditions
49+
if ( !$dbType ) {
50+
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
51+
$selectConditions = [ "title = '" . $title . "'",
52+
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
53+
} else {
54+
$selectConditions = [ "page_id = " . $id,
55+
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
56+
}
57+
} else {
58+
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
59+
$selectConditions = [ "title = '" . $title . "'",
60+
$dbType,
61+
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
62+
} else {
63+
$selectConditions = [ "page_id = " . $id,
64+
$dbType,
65+
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
66+
}
67+
}
68+
//$sql = 'SELECT page_id, COUNT(' . $cnt . ') AS count FROM ' . $wgDBprefix . 'WSPS WHERE page_id=\'' . $id . '\' ' . $dbType . 'AND added BETWEEN \'' . $dates["b"] . '\' AND NOW()';
69+
} else {
70+
// Set Conditions
71+
if ( !$dbType ) {
72+
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
73+
$selectConditions = [ "title = " . $title,
74+
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
75+
} else {
76+
$selectConditions = [ "page_id = " . $id,
77+
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
78+
}
79+
} else {
80+
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
81+
$selectConditions = [ "title = " . $title,
82+
$dbType,
83+
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
84+
} else {
85+
$selectConditions = [ "page_id = " . $id,
86+
$dbType,
87+
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
88+
}
89+
}
90+
}
91+
92+
return $selectConditions;
93+
}
94+
95+
/**
96+
* @param string|bool $startDate
97+
* @param string|bool $endDate
98+
*
99+
* @return array
100+
*/
101+
public function setDatesArray( string|bool $startDate, string|bool $endDate ): array {
102+
$dates = [];
103+
$dates['b'] = $startDate;
104+
$dates['e'] = $endDate;
105+
106+
if ( $dates['b'] !== false && !strpos( $dates['b'], ' ' ) ) {
107+
$dates['b'] = $dates['b'] . ' 00:00:00';
108+
}
109+
if ( $dates['e'] !== false && !strpos( $dates['e'], ' ' ) ) {
110+
$dates['e'] = $dates['e'] . ' 00:00:00';
111+
}
112+
if ( $dates['b'] !== false && WSStatsHooks::validateDate( $dates['b'] ) === false ) {
113+
$dates['b'] = false;
114+
}
115+
if ( $dates['e'] !== false && WSStatsHooks::validateDate( $dates['e'] ) === false ) {
116+
$dates['e'] = false;
117+
}
118+
return $dates;
119+
}
120+
121+
/**
122+
* @param array $dates
123+
*
124+
* @return array|false
125+
*/
126+
public function checkDates( array $dates ): bool|array {
127+
if ( $dates['e'] === false && $dates['b'] !== false ) {
128+
$dates['e'] = false;
129+
}
130+
if ( $dates['b'] === false && $dates['e'] !== false ) {
131+
$dates = false;
132+
}
133+
if ( $dates['b'] === false && $dates['e'] === false ) {
134+
$dates = false;
135+
}
136+
return $dates;
137+
}
138+
}

0 commit comments

Comments
 (0)