Skip to content

Commit

Permalink
Merge pull request #6 from WikibaseSolutions/REL1_39
Browse files Browse the repository at this point in the history
Rel1 39
  • Loading branch information
Designburo authored Sep 28, 2023
2 parents a0ce376 + c0d65dd commit bec92be
Show file tree
Hide file tree
Showing 14 changed files with 732 additions and 220 deletions.
96 changes: 83 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# WSStats
This MediaWiki extension counts pageviews by user
This MediaWiki 1.39.x extension counts pageviews by user

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

##Installation
## Installation

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

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

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

#Configuration
## Upgrading
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.
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.

# Configuration

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

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

By default Special Pages are counted as well. To omit Special pages set the following :
````
# Special Pages statistics
$wgWSStats['countSpecialPages'] = false; // defaults to true
````

To skip users in certain groups, just add the groupname to "skip_user_groups" :
````
# Record anonymous users
Expand All @@ -58,16 +69,11 @@ $wgWSStats['skip_anonymous'] = false;
# Skip if user is in following groups
$wgWSStats['skip_user_groups'][] = 'sysop';
$wgWSStats['skip_user_groups'][] = 'admin';
````
Count all hits:
````
$wgWSStats = array();
$wgWSStats['count_all_usergroups'] = true;
# Allow all usergroups, including sysop
$wgWSStats['skip_user_groups'] = [];
````

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

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

#Using the parser function
# Using the parser function
To retrieve statistics you can use the following parser functions :


#### Ask number of hits for page id : 9868
This returns a number
```
{{#wsstats:id=9868}}
```

You can also ask for statistics based on title, instead of Page Id's.
#### Ask number of hits for page with title : Main Page
This returns a number
```
{{#wsstats:title=Main Page}}
```

About dates. Dates are in a format of YYYY-MM-DD. Internally WSStats works with Date and Time.
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.


#### Ask number of hits for page id : 714 since start date 2018-02-01
This returns a number
```
Expand All @@ -100,12 +118,21 @@ This returns a number
|end date=2018-02-08}}
```

#### 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
This returns a number
```
{{#wsstats:id=714
|start date=2023-10-30 14:00:00
|end date=2023-10-30 16:00:00
}}
```

#### Filter results on registered users or anonymous users
This returns a number
```
{{#wsstats:id=714
|start date=2018-02-01
|end date=2018-02-08
|start date=2023-10-30 14:00:00
|end date=2023-10-30 16:00:00
|type=only anonymous}}
```

Expand Down Expand Up @@ -184,4 +211,47 @@ This returns a table
{{#wsstats:stats
|unique
|limit=20}}
```

# Using the lua functions
New in version 2.0

There are two Lua function you can use.

For the parser function that returns a table ( {{#wsstats:stats.. ) you can use wsstats.stats().

For the parser function that returns a number ( {{wsstats:... ) you can use wsstats.stat();

All the arguments are the same as for the parser functions, except :

* start date = startDate
* end date = endDate

### Example

If you create a Module called WSStats and you add the following content :
```lua
local p = {}

function p.stats(frame)
stats = mw.wsstats.stats( frame.args )
return stats

end

function p.stat(frame)
stat = mw.wsstats.stat( frame.args )
return stat

end

return p
```

You can then do calls like :
```
{{#invoke:wsstats|stats}} // returns a Lua table
```
```
{{#invoke:wsstats|stat|id=1|startDate=2023-09-25|endDate=2023-09-26}}
```
29 changes: 0 additions & 29 deletions WSStats.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["mediawiki", "wsstats", "extension", "statistics", "views"],
"license": "GPL-2.0-or-later",
"require": {
"php": "^7.0",
"php": "^8.0",
"ext-zip": "*"
},
"require-dev": {
Expand Down
7 changes: 4 additions & 3 deletions extension.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "WSStats",
"version": "1.0.8",
"version": "2.0.0",
"author": [
"Sen-Sai"
],
Expand All @@ -19,7 +19,7 @@
"skip_user_groups": [
"sysop"
],
"count_all_usergroups": true,
"countSpecialPages" : true,
"ignore_in_url": [
"action=edit",
"veaction=edit"
Expand All @@ -34,7 +34,8 @@
"ParserFirstCallInit": "WSStats\\WSStatsHooks::onParserFirstCallInit",
"BeforePageDisplay": "WSStats\\WSStatsHooks::onBeforePageDisplay",
"LoadExtensionSchemaUpdates": "WSStats\\WSStatsHooks::addTables",
"AdminLinks": "WSStats\\WSStatsHooks::addToAdminLinks"
"AdminLinks": "WSStats\\WSStatsHooks::addToAdminLinks",
"ScribuntoExternalLibraries": "\\WSStats\\WSStatsHooks::onScribuntoExternalLibraries"
},
"MessagesDirs": {
"WSStats": [
Expand Down
6 changes: 5 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"wsstats-page-date": "Date",
"wsstats-page-title": "Page title",
"wsstats-page-id": "Page ID",
"wsstats-page-hits": "Page hits"
"wsstats-page-hits": "Page hits",
"wsstats-special-list": "Top visited pages of this wiki",
"wsstats-special-db-need-update": "The WSStats Database needs an update to return correct values.",
"wsstats-special-db-need-update-btn": "Click here to update $1 records in the database.",
"wsstats-special-db-need-update-result": "$1 records haven been successfully updated"
}
2 changes: 2 additions & 0 deletions sql/WSStats.mysql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ CREATE TABLE /*_*/WSPS (
`page_id` int(11) NOT NULL default '0',
`user_id` int(11) NOT NULL default '0',
`added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
`title` varchar(250) NOT NULL DEFAULT ''
`isSpecialPage` INT(1) NOT NULL DEFAULT '0'
) /*$wgDBTableOptions*/;
2 changes: 2 additions & 0 deletions sql/WSStatsAddSpecialBool.mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE /*_*/WSPS
ADD COLUMN isSpecialPage INT(1) NOT NULL DEFAULT '0';
2 changes: 2 additions & 0 deletions sql/WSStatsAddTitle.mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE /*_*/WSPS
ADD COLUMN title VARCHAR(250) NOT NULL DEFAULT '';
138 changes: 138 additions & 0 deletions src/Helpers/SelectionMaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace WSStats\Helpers;

use WSStats\WSStatsHooks;

class SelectionMaker {

/**
* @param int $id
* @param string $title
* @param string|bool $dbType
*
* @return array
*/
public function createSelectionNoDates( int $id, string $title, string|bool $dbType ): array {
// Set Conditions
$countSpecialPages = WSStatsHooks::getConfigSetting( 'countSpecialPages' );
if ( !$dbType ) {
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
$selectConditions = [ "title = '" . $title . "'" ];
} else {
$selectConditions = [ "page_id = " . $id ];
}
} else {
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
$selectConditions = [ "title = '" . $title . "'", $dbType ];
} else {
$selectConditions = [ "page_id = " . $id,
$dbType ];
}
}

return $selectConditions;
}

/**
* @param int $id
* @param string $title
* @param string|bool $dbType
* @param array $dates
*
* @return array
*/
public function createSelectionUsingDates( int $id, string $title, string|bool $dbType, array $dates ): array {
$countSpecialPages = WSStatsHooks::getConfigSetting( 'countSpecialPages' );
if ( $dates['e'] === false ) {
// Set Conditions
if ( !$dbType ) {
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
$selectConditions = [ "title = '" . $title . "'",
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
} else {
$selectConditions = [ "page_id = " . $id,
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
}
} else {
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
$selectConditions = [ "title = '" . $title . "'",
$dbType,
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
} else {
$selectConditions = [ "page_id = " . $id,
$dbType,
'added BETWEEN \'' . $dates["b"] . '\' AND NOW()' ];
}
}
//$sql = 'SELECT page_id, COUNT(' . $cnt . ') AS count FROM ' . $wgDBprefix . 'WSPS WHERE page_id=\'' . $id . '\' ' . $dbType . 'AND added BETWEEN \'' . $dates["b"] . '\' AND NOW()';
} else {
// Set Conditions
if ( !$dbType ) {
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
$selectConditions = [ "title = " . $title,
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
} else {
$selectConditions = [ "page_id = " . $id,
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
}
} else {
if ( $countSpecialPages && $title !== '' && $id == "0" ) {
$selectConditions = [ "title = " . $title,
$dbType,
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
} else {
$selectConditions = [ "page_id = " . $id,
$dbType,
'added >= \'' . $dates["b"] . '\' AND added <= \'' . $dates['e'] . '\'' ];
}
}
}

return $selectConditions;
}

/**
* @param string|bool $startDate
* @param string|bool $endDate
*
* @return array
*/
public function setDatesArray( string|bool $startDate, string|bool $endDate ): array {
$dates = [];
$dates['b'] = $startDate;
$dates['e'] = $endDate;

if ( $dates['b'] !== false && !strpos( $dates['b'], ' ' ) ) {
$dates['b'] = $dates['b'] . ' 00:00:00';
}
if ( $dates['e'] !== false && !strpos( $dates['e'], ' ' ) ) {
$dates['e'] = $dates['e'] . ' 00:00:00';
}
if ( $dates['b'] !== false && WSStatsHooks::validateDate( $dates['b'] ) === false ) {
$dates['b'] = false;
}
if ( $dates['e'] !== false && WSStatsHooks::validateDate( $dates['e'] ) === false ) {
$dates['e'] = false;
}
return $dates;
}

/**
* @param array $dates
*
* @return array|false
*/
public function checkDates( array $dates ): bool|array {
if ( $dates['e'] === false && $dates['b'] !== false ) {
$dates['e'] = false;
}
if ( $dates['b'] === false && $dates['e'] !== false ) {
$dates = false;
}
if ( $dates['b'] === false && $dates['e'] === false ) {
$dates = false;
}
return $dates;
}
}
Loading

0 comments on commit bec92be

Please sign in to comment.