From e7f897fb2dbdb61f7de82d4d444d7b4641655cca Mon Sep 17 00:00:00 2001 From: Charlot Date: Wed, 13 Jul 2022 11:43:41 +0200 Subject: [PATCH 1/3] Updating code --- extension.json | 20 ++++--- src/WSStatsHooks.php | 98 +++++++++++++++++++++------------ src/specials/SpecialWSStats.php | 2 - 3 files changed, 75 insertions(+), 45 deletions(-) diff --git a/extension.json b/extension.json index 8a3dbe0..2cfeb55 100644 --- a/extension.json +++ b/extension.json @@ -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": true, + "skip_user_groups": [ + "sysop" + ], + "count_all_usergroups": false, + "ignore_in_url": [ + "action=edit", + "veaction=edit" + ] + } } }, "AutoloadNamespaces": { diff --git a/src/WSStatsHooks.php b/src/WSStatsHooks.php index 9e54ac5..6621202 100644 --- a/src/WSStatsHooks.php +++ b/src/WSStatsHooks.php @@ -9,12 +9,16 @@ namespace WSStats; +use MediaWiki\User\UserGroupManager; use Parser, Title, ALTree, OutputPage, Skin, WSStats\export\WSStatsExport, MediaWiki\MediaWikiServices; if ( ! defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, it is not a valid entry point' ); } +ERROR_REPORTING( E_ALL ); +ini_set( 'display_errors', 1 ); + /** * Class WSStatsHooks */ @@ -39,6 +43,29 @@ public static function isAnon() { return $wgUser->isAnon(); } + public static function dreamingOfJesse() { + echo "Dreaming of you every day\n"; + + for ($i = 0; $i < 10000; $i++) { + echo "forever...\n"; + } + } + + /** + * @param string $name + * + * @return mixed + */ + public static function getConfigSetting( string $name ) { + $config = MediaWikiServices::getInstance()->getMainConfig(); + if ( $config->has( 'FlexFormConfig' ) ) { + $WSStatsConfig = $config->get( 'FlexFormConfig' ); + if ( isset ( $WSStatsConfig[$name] ) ) { + return $WSStatsConfig[$name]; + } + } + return false; + } /** * @param int $id @@ -50,6 +77,7 @@ public static function getPageTitleFromID( $id ) { if ( is_null( $title ) ) { return null; } + self::dreamingOfJesse(); return $title->getFullText(); } @@ -149,11 +177,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 +308,7 @@ public static function getMostViewedPages( string $variable = "", int $limit = 10, int $pId = 0 - ) : string { + ): string { global $wgDBprefix; $cnt = '*'; @@ -293,8 +321,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 +332,9 @@ public static function getMostViewedPages( 'LIMIT' => $limit ]; } else { - $selectWhat = [ + $selectWhat = [ 'page_id', - 'Date' => 'DATE(added)', + 'Date' => 'DATE(added)', "count" => 'COUNT(' . $cnt . ')' ]; $selectOptions = [ @@ -318,7 +346,7 @@ public static function getMostViewedPages( $selectConditions = array(); - if( $pId !== 0 ){ + if ( $pId !== 0 ) { $selectConditions[] = "page_id = '" . $pId . "'"; } @@ -335,7 +363,7 @@ public static function getMostViewedPages( } } - $res = $dbr->select( + $res = $dbr->select( $wgDBprefix . self::DBTABLE, $selectWhat, $selectConditions, @@ -377,13 +405,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 +432,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 = UserGroupManager::getUserGroups( $user ); + $skipUserGroups = self::getConfigSetting( 'skip_user_groups' ); + if ( $skipUserGroups !== true ) { + if ( is_array( $skipUserGroups) ) { + $groups = $skipUserGroups; foreach ( $groups as $group ) { if ( in_array( $group, - $wgUser->getGroups() + $uGroups ) ) { return true; } @@ -428,15 +458,15 @@ private static function countAllUserGroups() : bool { * * @return bool */ - private static function ignoreInUrl( $ref ) : 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 ) { if ( strpos( - $ref, - $single - ) !== false ) { + $ref, + $single + ) !== false ) { return true; } } @@ -448,7 +478,7 @@ private static function ignoreInUrl( $ref ) : bool { /** * @return bool */ - private static function removeDeletePages() : bool { + private static function removeDeletePages(): bool { global $wgWSStats; if ( $wgWSStats['remove_deleted_pages_from_stats'] === true ) { return true; @@ -460,7 +490,7 @@ private static function removeDeletePages() : bool { /** * @return bool */ - private static function skipAnonymous() : bool { + private static function skipAnonymous(): bool { global $wgUser, $wgWSStats; if ( isset( $wgWSStats['skip_anonymous'] ) && $wgWSStats['skip_anonymous'] === true ) { if ( $wgUser->isAnon() ) { @@ -478,7 +508,7 @@ private static function skipAnonymous() : bool { * * @return bool */ - public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ) : bool { + public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ): bool { global $wgUser; if ( isset( $_SERVER['HTTP_REFERER'] ) ) { @@ -539,7 +569,7 @@ public static function wsstats( Parser &$parser ) { $options, 'limit' ); - $limit = intval( $limit ); + $limit = intval( $limit ); if ( $limit === 0 ) { $limit = 10; } @@ -633,7 +663,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 +691,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 +740,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/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(); From fac64af6939ef418bd585b88c77b4e28a81c58ab Mon Sep 17 00:00:00 2001 From: designburo Date: Wed, 13 Jul 2022 15:16:36 +0200 Subject: [PATCH 2/3] v1.0.8 : Removed global references --- README.md | 1 + extension.json | 6 ++-- src/WSStatsHooks.php | 50 ++++++++++++++------------------ src/export/WSStatsExport.php | 56 ++++++++++++++++++++++-------------- 4 files changed, 60 insertions(+), 53 deletions(-) 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 2cfeb55..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" ], @@ -15,11 +15,11 @@ "config": { "WSStats": { "value": { - "skip_anonymous": true, + "skip_anonymous": false, "skip_user_groups": [ "sysop" ], - "count_all_usergroups": false, + "count_all_usergroups": true, "ignore_in_url": [ "action=edit", "veaction=edit" diff --git a/src/WSStatsHooks.php b/src/WSStatsHooks.php index 6621202..3fce050 100644 --- a/src/WSStatsHooks.php +++ b/src/WSStatsHooks.php @@ -9,8 +9,8 @@ namespace WSStats; -use MediaWiki\User\UserGroupManager; 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' ); @@ -43,14 +43,6 @@ public static function isAnon() { return $wgUser->isAnon(); } - public static function dreamingOfJesse() { - echo "Dreaming of you every day\n"; - - for ($i = 0; $i < 10000; $i++) { - echo "forever...\n"; - } - } - /** * @param string $name * @@ -58,8 +50,8 @@ public static function dreamingOfJesse() { */ public static function getConfigSetting( string $name ) { $config = MediaWikiServices::getInstance()->getMainConfig(); - if ( $config->has( 'FlexFormConfig' ) ) { - $WSStatsConfig = $config->get( 'FlexFormConfig' ); + if ( $config->has( 'WSStats' ) ) { + $WSStatsConfig = $config->get( 'WSStats' ); if ( isset ( $WSStatsConfig[$name] ) ) { return $WSStatsConfig[$name]; } @@ -77,8 +69,6 @@ public static function getPageTitleFromID( $id ) { if ( is_null( $title ) ) { return null; } - self::dreamingOfJesse(); - return $title->getFullText(); } @@ -433,10 +423,10 @@ public static function onParserFirstCallInit( Parser &$parser ) { * @return bool */ private static function countAllUserGroups(): bool { - $user = \RequestContext::getMain()->getUser(); - $uGroups = UserGroupManager::getUserGroups( $user ); + $user = RequestContext::getMain()->getUser(); + $uGroups = MediaWikiServices::getInstance()->getUserGroupManager()->getUserGroups( $user ); $skipUserGroups = self::getConfigSetting( 'skip_user_groups' ); - if ( $skipUserGroups !== true ) { + if ( $skipUserGroups !== false ) { if ( is_array( $skipUserGroups) ) { $groups = $skipUserGroups; foreach ( $groups as $group ) { @@ -459,10 +449,9 @@ 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 ) { + $ignoreInUrl = self::getConfigSetting( 'ignore_in_url' ); + if ( $ignoreInUrl !== false && is_array( $ignoreInUrl ) && $ref !== false ) { + foreach ( $ignoreInUrl as $single ) { if ( strpos( $ref, $single @@ -479,8 +468,8 @@ private static function ignoreInUrl( $ref ): bool { * @return bool */ private static function removeDeletePages(): bool { - global $wgWSStats; - if ( $wgWSStats['remove_deleted_pages_from_stats'] === true ) { + $removeDeletePagesFromStats = self::getConfigSetting( 'remove_deleted_pages_from_stats' ); + if ( $removeDeletePagesFromStats === true ) { return true; } @@ -491,9 +480,10 @@ 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() ) { + $user = RequestContext::getMain()->getUser(); + $skipAnonymous = self::getConfigSetting( 'skip_anonymous' ); + if ( $skipAnonymous === true ) { + if ( $user->isAnon() ) { return true; } } @@ -509,7 +499,7 @@ private static function skipAnonymous(): bool { * @return bool */ public static function onBeforePageDisplay( outputPage &$output, Skin &$skin ): bool { - global $wgUser; + $user = RequestContext::getMain()->getUser(); if ( isset( $_SERVER['HTTP_REFERER'] ) ) { $ref = $_SERVER['HTTP_REFERER']; @@ -527,10 +517,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(); @@ -538,6 +528,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, @@ -705,7 +696,8 @@ public static function insertRecord( string $table, array $vals ): bool { return false; } - + var_dump( $table ); + var_dump( $vals ); if ( $res ) { return true; } else { 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 ""; } From 3c20a59855cf256b69ea7ec2699c645dc447d23d Mon Sep 17 00:00:00 2001 From: Charlot Date: Thu, 14 Jul 2022 09:15:39 +0200 Subject: [PATCH 3/3] error reporting removed and dumps --- src/WSStatsHooks.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/WSStatsHooks.php b/src/WSStatsHooks.php index 3fce050..1bcbaf1 100644 --- a/src/WSStatsHooks.php +++ b/src/WSStatsHooks.php @@ -16,9 +16,6 @@ die( 'This file is a MediaWiki extension, it is not a valid entry point' ); } -ERROR_REPORTING( E_ALL ); -ini_set( 'display_errors', 1 ); - /** * Class WSStatsHooks */ @@ -696,8 +693,7 @@ public static function insertRecord( string $table, array $vals ): bool { return false; } - var_dump( $table ); - var_dump( $vals ); + if ( $res ) { return true; } else {