Convert ContributionScores to use extension registration

Bug: T174046
Change-Id: I293050ddc01699420b7dad884b51f5241e46137f
This commit is contained in:
func 2020-12-24 23:45:19 +08:00
parent e51c7662d1
commit b8a91c6731
4 changed files with 129 additions and 107 deletions

View file

@ -6,6 +6,7 @@
<exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationProtected" /> <exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationProtected" />
<exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic" /> <exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic" />
<exclude name="Squiz.Scope.MethodScope.Missing" /> <exclude name="Squiz.Scope.MethodScope.Missing" />
<exclude name="PSR12.Properties.ConstantVisibility.NotFound" />
</rule> </rule>
<file>.</file> <file>.</file>
<arg name="extensions" value="php" /> <arg name="extensions" value="php" />

View file

@ -1,109 +1,16 @@
<?php <?php
/** \file
* \brief Contains setup code for the Contribution Scores Extension.
*/
# Not a valid entry point, skip unless MEDIAWIKI is defined if ( function_exists( 'wfLoadExtension' ) ) {
if ( !defined( 'MEDIAWIKI' ) ) { wfLoadExtension( 'ContributionScores' );
echo 'Contribution Scores extension'; // Keep i18n globals so mergeMessageFileList.php doesn't break
exit( 1 ); $wgMessageDirs['ContributionScores'] = __DIR__ . '/i18n';
} $wgExtensionMessagesFiles['ContributionScoresAlias'] = __DIR__ . '/ContributionScores.alias.php';
$wgExtensionMessagesFiles['ContributionScoresMagic'] = __DIR__ . '/ContributionScores.i18n.magic.php';
$wgExtensionCredits['specialpage'][] = [ wfWarn(
'path' => __FILE__, 'Deprecated PHP entry point used for ContributionScores extension. ' .
'name' => 'Contribution Scores', 'Please use wfLoadExtension instead, ' .
'url' => 'https://www.mediawiki.org/wiki/Extension:Contribution_Scores', 'see https://www.mediawiki.org/wiki/Extension_registration for more details.'
'author' => 'Tim Laqua', );
'descriptionmsg' => 'contributionscores-desc', } else {
'version' => '1.25.0' die( 'This version of the ContributionScores extension requires MediaWiki 1.29+' );
];
define( 'CONTRIBUTIONSCORES_MAXINCLUDELIMIT', 50 );
$wgContribScoreReports = null;
// These settings can be overridden in LocalSettings.php.
// Set to true to exclude bots from the reporting.
$wgContribScoreIgnoreBlockedUsers = false;
// Set to true to exclude blocked users from the reporting.
$wgContribScoreIgnoreBots = false;
// Set to true to use real user names when available. Only for MediaWiki 1.19 and later.
$wgContribScoresUseRealName = false;
// Set to true to disable cache for parser function and inclusion of table.
$wgContribScoreDisableCache = false;
$wgAutoloadClasses['ContributionScores'] = __DIR__ . '/ContributionScores_body.php';
$wgSpecialPages['ContributionScores'] = 'ContributionScores';
$wgMessagesDirs['ContributionScores'] = __DIR__ . '/i18n';
$wgExtensionMessagesFiles['ContributionScoresAlias'] = __DIR__ . '/ContributionScores.alias.php';
$wgExtensionMessagesFiles['ContributionScoresMagic'] =
__DIR__ . '/ContributionScores.i18n.magic.php';
$wgHooks['ParserFirstCallInit'][] = 'efContributionScores_Setup';
function efContributionScores_Setup( &$parser ) {
$parser->setFunctionHook( 'cscore', 'efContributionScores_Render' );
return true;
}
function efContributionScores_Render( &$parser, $usertext, $metric = 'score' ) {
global $wgContribScoreDisableCache;
if ( $wgContribScoreDisableCache ) {
$parser->getOutput()->updateCacheExpiry( 0 );
}
$user = User::newFromName( $usertext );
$dbr = wfGetDB( DB_REPLICA );
if ( $user instanceof User && $user->isLoggedIn() ) {
global $wgLang;
$revWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user );
if ( $metric == 'score' ) {
$res = $dbr->select(
[ 'revision' ] + $revWhere['tables'],
'COUNT(DISTINCT rev_page)+SQRT(COUNT(rev_id)-COUNT(DISTINCT rev_page))*2 AS wiki_rank',
$revWhere['conds'],
__METHOD__,
[],
$revWhere['joins']
);
$row = $dbr->fetchObject( $res );
$output = $wgLang->formatNum( round( $row->wiki_rank, 0 ) );
} elseif ( $metric == 'changes' ) {
$res = $dbr->select(
[ 'revision' ] + $revWhere['tables'],
'COUNT(rev_id) AS rev_count',
$revWhere['conds'],
__METHOD__,
[],
$revWhere['joins']
);
$row = $dbr->fetchObject( $res );
$output = $wgLang->formatNum( $row->rev_count );
} elseif ( $metric == 'pages' ) {
$res = $dbr->select(
[ 'revision' ] + $revWhere['tables'],
'COUNT(DISTINCT rev_page) AS page_count',
$revWhere['conds'],
__METHOD__,
[],
$revWhere['joins']
);
$row = $dbr->fetchObject( $res );
$output = $wgLang->formatNum( $row->page_count );
} else {
$output = wfMessage( 'contributionscores-invalidmetric' )->text();
}
} else {
$output = wfMessage( 'contributionscores-invalidusername' )->text();
}
return $parser->insertStripItem( $output, $parser->mStripState );
} }

View file

@ -15,10 +15,72 @@ use MediaWiki\MediaWikiServices;
* @author Tim Laqua <t.laqua@gmail.com> * @author Tim Laqua <t.laqua@gmail.com>
*/ */
class ContributionScores extends IncludableSpecialPage { class ContributionScores extends IncludableSpecialPage {
const CONTRIBUTIONSCORES_MAXINCLUDELIMIT = 50;
public function __construct() { public function __construct() {
parent::__construct( 'ContributionScores' ); parent::__construct( 'ContributionScores' );
} }
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setFunctionHook( 'cscore', [ self::class, 'efContributionScoresRender' ] );
}
public static function efContributionScoresRender( $parser, $usertext, $metric = 'score' ) {
global $wgContribScoreDisableCache;
if ( $wgContribScoreDisableCache ) {
$parser->getOutput()->updateCacheExpiry( 0 );
}
$user = User::newFromName( $usertext );
$dbr = wfGetDB( DB_REPLICA );
if ( $user instanceof User && $user->isLoggedIn() ) {
global $wgLang;
$revWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user );
if ( $metric == 'score' ) {
$res = $dbr->select(
[ 'revision' ] + $revWhere['tables'],
'COUNT(DISTINCT rev_page)+SQRT(COUNT(rev_id)-COUNT(DISTINCT rev_page))*2 AS wiki_rank',
$revWhere['conds'],
__METHOD__,
[],
$revWhere['joins']
);
$row = $dbr->fetchObject( $res );
$output = $wgLang->formatNum( round( $row->wiki_rank, 0 ) );
} elseif ( $metric == 'changes' ) {
$res = $dbr->select(
[ 'revision' ] + $revWhere['tables'],
'COUNT(rev_id) AS rev_count',
$revWhere['conds'],
__METHOD__,
[],
$revWhere['joins']
);
$row = $dbr->fetchObject( $res );
$output = $wgLang->formatNum( $row->rev_count );
} elseif ( $metric == 'pages' ) {
$res = $dbr->select(
[ 'revision' ] + $revWhere['tables'],
'COUNT(DISTINCT rev_page) AS page_count',
$revWhere['conds'],
__METHOD__,
[],
$revWhere['joins']
);
$row = $dbr->fetchObject( $res );
$output = $wgLang->formatNum( $row->page_count );
} else {
$output = wfMessage( 'contributionscores-invalidmetric' )->text();
}
} else {
$output = wfMessage( 'contributionscores-invalidusername' )->text();
}
return $parser->insertStripItem( $output, $parser->mStripState );
}
/// Generates a "Contribution Scores" table for a given LIMIT and date range /// Generates a "Contribution Scores" table for a given LIMIT and date range
/** /**
@ -257,7 +319,7 @@ class ContributionScores extends IncludableSpecialPage {
} }
} }
if ( empty( $limit ) || $limit < 1 || $limit > CONTRIBUTIONSCORES_MAXINCLUDELIMIT ) { if ( empty( $limit ) || $limit < 1 || $limit > self::CONTRIBUTIONSCORES_MAXINCLUDELIMIT ) {
$limit = 10; $limit = 10;
} }
if ( $days === null || $days < 0 ) { if ( $days === null || $days < 0 ) {

52
extension.json Normal file
View file

@ -0,0 +1,52 @@
{
"name": "ContributionScores",
"author": "Tim Laqua",
"url": "https://www.mediawiki.org/wiki/Extension:Contribution_Scores",
"descriptionmsg": "contributionscore-desc",
"version": "1.25.0",
"type": "specialpage",
"requires": {
"MediaWiki": ">= 1.33.0"
},
"SpecialPages": {
"ContributionScores": "ContributionScores"
},
"AutoloadClasses": {
"ContributionScores": "ContributionScores_body.php"
},
"Hooks": {
"ParserFirstCallInit": "ContributionScores::onParserFirstCallInit"
},
"MessagesDirs": {
"ContributionScores": [
"i18n"
]
},
"ExtensionMessagesFiles": {
"ContribScoreAlias": "ContributionScores.alias.php",
"ContribScoreMagic": "ContributionScores.i18n.magic.php"
},
"config": {
"ContribScoreReports": {
"value": null,
"description": "Each array defines a report - 7,50 is \"past 7 days \" and \"LIMIT 50 \" - Can be omitted."
},
"ContribScoreIgnoreBlockedUsers": {
"value": false,
"description": "Set to true to exclude blocked users from the reporting."
},
"ContribScoreIgnoreBots": {
"value": false,
"description": "Set to true to exclude bots users from the reporting."
},
"ContribScoresUseRealName": {
"value": false,
"description": "Set to true to use real user names when available."
},
"ContribScoreDisableCache": {
"value": false,
"description": "Set to true to disable cache for parser function and inclusion of table."
}
},
"manifest_version": 2
}