diff --git a/README.md b/README.md index 71812b1..bb9d285 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # WSStats This MediaWiki extension counts pageviews by user +* 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 * Version 1.0.5 : Rewrote database queries to use MW database abstraction layer. diff --git a/extension.json b/extension.json index 8a3dbe0..e516fb0 100644 --- a/extension.json +++ b/extension.json @@ -1,6 +1,6 @@ { "name": "WSStats", - "version": "1.0.7", + "version": "1.0.8", "author": [ "Sen-Sai" ], @@ -14,15 +14,17 @@ }, "config": { "WSStats": { - "skip_anonymous": true, - "skip_user_groups": [ - "sysop" - ], - "count_all_usergroups": false, - "ignore_in_url": [ - "action=edit", - "veaction=edit" - ] + "value": { + "skip_anonymous": false, + "skip_user_groups": [ + "sysop" + ], + "count_all_usergroups": true, + "ignore_in_url": [ + "action=edit", + "veaction=edit" + ] + } } }, "AutoloadNamespaces": { diff --git a/src/WSStatsHooks.php b/src/WSStatsHooks.php index 9e54ac5..1bcbaf1 100644 --- a/src/WSStatsHooks.php +++ b/src/WSStatsHooks.php @@ -10,6 +10,7 @@ namespace WSStats; use Parser, Title, ALTree, OutputPage, Skin, WSStats\export\WSStatsExport, MediaWiki\MediaWikiServices; +use RequestContext; if ( ! defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, it is not a valid entry point' ); @@ -39,6 +40,21 @@ public static function isAnon() { return $wgUser->isAnon(); } + /** + * @param string $name + * + * @return mixed + */ + public static function getConfigSetting( string $name ) { + $config = MediaWikiServices::getInstance()->getMainConfig(); + if ( $config->has( 'WSStats' ) ) { + $WSStatsConfig = $config->get( 'WSStats' ); + if ( isset ( $WSStatsConfig[$name] ) ) { + return $WSStatsConfig[$name]; + } + } + return false; + } /** * @param int $id @@ -50,7 +66,6 @@ public static function getPageTitleFromID( $id ) { if ( is_null( $title ) ) { return null; } - return $title->getFullText(); } @@ -149,11 +164,11 @@ public static function addTables( $updater ) { if ( file_exists( $tables ) ) { $updater->addExtensionUpdate( array( - 'addTable', - self::DBTABLE, - $tables, - true - ) ); + 'addTable', + self::DBTABLE, + $tables, + true + ) ); } else { throw new \MWException( "WSStats does not support $dbt." ); } @@ -280,7 +295,7 @@ public static function getMostViewedPages( string $variable = "", int $limit = 10, int $pId = 0 - ) : string { + ): string { global $wgDBprefix; $cnt = '*'; @@ -293,8 +308,8 @@ public static function getMostViewedPages( $dbr = $lb->getConnectionRef( DB_REPLICA ); $dbResult = array(); - if( $pId === 0 ) { - $selectWhat = [ + if ( $pId === 0 ) { + $selectWhat = [ 'page_id', "count" => 'COUNT(' . $cnt . ')' ]; @@ -304,9 +319,9 @@ public static function getMostViewedPages( 'LIMIT' => $limit ]; } else { - $selectWhat = [ + $selectWhat = [ 'page_id', - 'Date' => 'DATE(added)', + 'Date' => 'DATE(added)', "count" => 'COUNT(' . $cnt . ')' ]; $selectOptions = [ @@ -318,7 +333,7 @@ public static function getMostViewedPages( $selectConditions = array(); - if( $pId !== 0 ){ + if ( $pId !== 0 ) { $selectConditions[] = "page_id = '" . $pId . "'"; } @@ -335,7 +350,7 @@ public static function getMostViewedPages( } } - $res = $dbr->select( + $res = $dbr->select( $wgDBprefix . self::DBTABLE, $selectWhat, $selectConditions, @@ -377,13 +392,13 @@ public static function getMostViewedPages( */ public static function getOptionSetting( array $options, string $k, bool $checkEmpty = true ) { if ( $checkEmpty ) { - if ( isset( $options[$k] ) && $options[$k] != '' ) { - return $options[$k]; + if ( isset( $options[ $k ] ) && $options[ $k ] != '' ) { + return $options[ $k ]; } else { return false; } } else { - if ( isset( $options[$k] ) ) { + if ( isset( $options[ $k ] ) ) { return true; } else { return false; @@ -404,15 +419,17 @@ public static function onParserFirstCallInit( Parser &$parser ) { /** * @return bool */ - private static function countAllUserGroups() : bool { - global $wgUser, $wgWSStats; - if ( $wgWSStats['count_all_usergroups'] !== true ) { - if ( isset( $wgWSStats['skip_user_groups'] ) && is_array( $wgWSStats['skip_user_groups'] ) ) { - $groups = $wgWSStats['skip_user_groups']; + private static function countAllUserGroups(): bool { + $user = RequestContext::getMain()->getUser(); + $uGroups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $user ); + $skipUserGroups = self::getConfigSetting( 'skip_user_groups' ); + if ( $skipUserGroups !== false ) { + if ( is_array( $skipUserGroups) ) { + $groups = $skipUserGroups; foreach ( $groups as $group ) { if ( in_array( $group, - $wgUser->getGroups() + $uGroups ) ) { return true; } @@ -428,15 +445,14 @@ private static function countAllUserGroups() : bool { * * @return bool */ - private static function ignoreInUrl( $ref ) : bool { - global $wgWSStats; - if ( isset( $wgWSStats['ignore_in_url'] ) && is_array( $wgWSStats['ignore_in_url'] ) && $ref !== false ) { - $ignore = $groups = $wgWSStats['ignore_in_url']; - foreach ( $ignore as $single ) { + private static function ignoreInUrl( $ref ): bool { + $ignoreInUrl = self::getConfigSetting( 'ignore_in_url' ); + if ( $ignoreInUrl !== false && is_array( $ignoreInUrl ) && $ref !== false ) { + foreach ( $ignoreInUrl as $single ) { if ( strpos( - $ref, - $single - ) !== false ) { + $ref, + $single + ) !== false ) { return true; } } @@ -448,9 +464,9 @@ private static function ignoreInUrl( $ref ) : bool { /** * @return bool */ - private static function removeDeletePages() : bool { - global $wgWSStats; - if ( $wgWSStats['remove_deleted_pages_from_stats'] === true ) { + private static function removeDeletePages(): bool { + $removeDeletePagesFromStats = self::getConfigSetting( 'remove_deleted_pages_from_stats' ); + if ( $removeDeletePagesFromStats === true ) { return true; } @@ -460,10 +476,11 @@ private static function removeDeletePages() : bool { /** * @return bool */ - private static function skipAnonymous() : bool { - global $wgUser, $wgWSStats; - if ( isset( $wgWSStats['skip_anonymous'] ) && $wgWSStats['skip_anonymous'] === true ) { - if ( $wgUser->isAnon() ) { + private static function skipAnonymous(): bool { + $user = RequestContext::getMain()->getUser(); + $skipAnonymous = self::getConfigSetting( 'skip_anonymous' ); + if ( $skipAnonymous === true ) { + if ( $user->isAnon() ) { return true; } } @@ -478,8 +495,8 @@ private static function skipAnonymous() : bool { * * @return bool */ - public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ) : bool { - global $wgUser; + public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ): bool { + $user = RequestContext::getMain()->getUser(); if ( isset( $_SERVER['HTTP_REFERER'] ) ) { $ref = $_SERVER['HTTP_REFERER']; @@ -497,10 +514,10 @@ public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ) : return true; } - if ( $wgUser->isAnon() ) { + if ( $user->isAnon() ) { $data['user_id'] = 0; } else { - $data['user_id'] = $wgUser->getID(); + $data['user_id'] = $user->getID(); } $title = $output->getTitle(); @@ -508,6 +525,7 @@ public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ) : return true; } $data['page_id'] = $title->getArticleID(); + if ( $data['page_id'] != 0 ) { WSStatsHooks::insertRecord( self::DBTABLE, @@ -539,7 +557,7 @@ public static function wsstats( Parser &$parser ) { $options, 'limit' ); - $limit = intval( $limit ); + $limit = intval( $limit ); if ( $limit === 0 ) { $limit = 10; } @@ -633,7 +651,7 @@ public static function wsstats( Parser &$parser ) { return "ok, move along. Nothing to see here.."; } - private static function deleteRecord( $table, $pId ) : bool { + private static function deleteRecord( $table, $pId ): bool { $dbw = wfGetDB( DB_MASTER ); $dbw->IngoreErrors = true; try { @@ -661,7 +679,7 @@ private static function deleteRecord( $table, $pId ) : bool { * * @return bool */ - public static function insertRecord( string $table, array $vals ) : bool { + public static function insertRecord( string $table, array $vals ): bool { $dbw = wfGetDB( DB_MASTER ); $dbw->IngoreErrors = true; try { @@ -710,11 +728,11 @@ public static function extractOptions( array $options ) { $value = strtolower( trim( $pair[1] ) ); } - $results[$name] = $value; + $results[ $name ] = $value; } if ( count( $pair ) === 1 ) { - $name = trim( $pair[0] ); - $results[$name] = true; + $name = trim( $pair[0] ); + $results[ $name ] = true; } } } diff --git a/src/export/WSStatsExport.php b/src/export/WSStatsExport.php index 24617d4..3c2b535 100644 --- a/src/export/WSStatsExport.php +++ b/src/export/WSStatsExport.php @@ -6,6 +6,7 @@ * Description : * Date : 6-4-2021 * Time : 13:37 + * * @version 0.8.0 2021 * * @author Sen-Sai @@ -26,9 +27,9 @@ class WSStatsExport { * * @return string */ - public function renderTable( \Wikimedia\Rdbms\IResultWrapper $q, int $pId ): string { + public function renderTable( \Wikimedia\Rdbms\IResultWrapper $q, int $pId ) : string { $data = "{| class=\"sortable wikitable smwtable jquery-tablesorter\"\n"; - if( $pId !== 0 ) { + if ( $pId !== 0 ) { $data .= "! " . wfMessage( 'wsstats-page-date' )->text() . "\n"; $data .= "! " . wfMessage( 'wsstats-page-hits' )->text() . "\n"; } else { @@ -38,9 +39,9 @@ public function renderTable( \Wikimedia\Rdbms\IResultWrapper $q, int $pId ): str } while ( $row = $q->fetchRow() ) { $pTitle = WSStatsHooks::getPageTitleFromID( $row['page_id'] ); - if( ! is_null( $pTitle ) ) { + if ( !is_null( $pTitle ) ) { $data .= "|-\n"; - if( $pId !== 0 ) { + if ( $pId !== 0 ) { $data .= "| " . $row['Date'] . "\n"; $data .= "| " . $row['count'] . "\n"; } else { @@ -52,6 +53,7 @@ public function renderTable( \Wikimedia\Rdbms\IResultWrapper $q, int $pId ): str } } $data .= "|}\n"; + return $data; } @@ -61,9 +63,9 @@ public function renderTable( \Wikimedia\Rdbms\IResultWrapper $q, int $pId ): str * * @return string */ - public function renderCSV( \Wikimedia\Rdbms\IResultWrapper $q, $pId ): string { + public function renderCSV( \Wikimedia\Rdbms\IResultWrapper $q, $pId ) : string { $data = ''; - if( $pId === 0 ) { + if ( $pId === 0 ) { while ( $row = $q->fetchRow() ) { $data .= $row['page_id'] . ";" . $row['count'] . ","; } @@ -72,7 +74,11 @@ public function renderCSV( \Wikimedia\Rdbms\IResultWrapper $q, $pId ): string { $data .= $row['Date'] . ";" . $row['count'] . ","; } } - return rtrim( $data, ',' ); + + return rtrim( + $data, + ',' + ); } /** @@ -80,11 +86,10 @@ public function renderCSV( \Wikimedia\Rdbms\IResultWrapper $q, $pId ): string { * * @return mixed */ - private function extensionInstalled ( $name ) { + private function extensionInstalled( $name ) { return extensionRegistry::getInstance()->isLoaded( $name ); } - /** * @param mysqli_result $q * @param string $wsArrayVariableName @@ -92,31 +97,40 @@ private function extensionInstalled ( $name ) { * * @return string */ - public function renderWSArrays( \Wikimedia\Rdbms\IResultWrapper $q, string $wsArrayVariableName, int $pId ): string { + public function renderWSArrays( + \Wikimedia\Rdbms\IResultWrapper $q, + string $wsArrayVariableName, + int $pId + ) : string { global $IP; - if( ! $this->extensionInstalled( 'WSArrays' ) ) return ""; - if( file_exists( $IP . '/extensions/WSArrays/ComplexArrayWrapper.php' ) ) { + if ( !$this->extensionInstalled( 'WSArrays' ) ) { + return ""; + } + if ( file_exists( $IP . '/extensions/WSArrays/ComplexArrayWrapper.php' ) ) { include_once( $IP . '/extensions/WSArrays/ComplexArrayWrapper.php' ); - } else return ""; + } else { + return ""; + } $wsWrapper = new \ComplexArrayWrapper(); - $result = array(); - $t = 0; + $result = []; + $t = 0; while ( $row = $q->fetchRow() ) { $pTitle = WSStatsHooks::getPageTitleFromID( $row['page_id'] ); - if( !is_null( $pTitle ) ) { - if( $pId === 0 ) { - $result[$t][wfMessage( 'wsstats-page-id' )->text()] = $row['page_id']; + if ( $pTitle !== null ) { + if ( $pId === 0 ) { + $result[$t][wfMessage( 'wsstats-page-id' )->text()] = $row['page_id']; $result[$t][wfMessage( 'wsstats-page-title' )->text()] = $pTitle; - $result[$t][wfMessage( 'wsstats-page-hits' )->text()] = $row['count']; + $result[$t][wfMessage( 'wsstats-page-hits' )->text()] = $row['count']; } else { - $result[$t][wfMessage( 'wsstats-page-date' )->text()] = $row['Date']; - $result[$t][wfMessage( 'wsstats-page-hits' )->text()] = $row['count']; + $result[$t][wfMessage( 'wsstats-page-date' )->text()] = $row['Date']; + $result[$t][wfMessage( 'wsstats-page-hits' )->text()] = $row['count']; } $t++; } } $wsWrapper->on( $wsArrayVariableName )->set( $result ); + return ""; } diff --git a/src/specials/SpecialWSStats.php b/src/specials/SpecialWSStats.php index 2267b47..7a25620 100644 --- a/src/specials/SpecialWSStats.php +++ b/src/specials/SpecialWSStats.php @@ -15,12 +15,10 @@ public function __construct() { parent::__construct( 'WSStats' ); } - /** * Show the page to the user * * @param string $sub The subpage string argument (if any). - * [[Special:HelloWorld/subpage]]. */ public function execute( $sub ) { $out = $this->getOutput();