2007-04-23 20:58:49 +00:00
|
|
|
<?php
|
|
|
|
|
2019-07-27 22:47:12 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2008-01-11 08:09:43 +00:00
|
|
|
class Editcount extends IncludableSpecialPage {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct( 'Editcount' );
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-18 14:50:48 +00:00
|
|
|
* Show the special page
|
|
|
|
*
|
|
|
|
* @param string|null $par User name, optionally followed by a namespace, or null
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
2008-01-11 08:09:43 +00:00
|
|
|
public function execute( $par ) {
|
2022-03-05 14:03:36 +00:00
|
|
|
$target = $par ?? $this->getRequest()->getText( 'username' );
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2019-07-28 00:26:35 +00:00
|
|
|
list( $username, $namespace ) = $this->extractParameters( $target );
|
2018-09-14 19:50:29 +00:00
|
|
|
$this->getOutput()->enableOOUI();
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2014-06-16 05:49:51 +00:00
|
|
|
$user = User::newFromName( $username );
|
2019-11-12 09:04:38 +00:00
|
|
|
$username = $user ? $user->getName() : '';
|
|
|
|
$uid = $user ? $user->getId() : 0;
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
if ( $this->including() ) {
|
2019-07-27 22:47:12 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
2007-04-23 20:58:49 +00:00
|
|
|
if ( $namespace === null ) {
|
2014-06-16 05:49:51 +00:00
|
|
|
if ( $uid != 0 ) {
|
2019-07-27 22:47:12 +00:00
|
|
|
$out = $contLang->formatNum( $user->getEditCount() );
|
2014-06-16 05:49:51 +00:00
|
|
|
} else {
|
2014-06-18 14:50:48 +00:00
|
|
|
$out = '';
|
2014-06-16 05:49:51 +00:00
|
|
|
}
|
2007-04-23 20:58:49 +00:00
|
|
|
} else {
|
2019-12-17 16:22:42 +00:00
|
|
|
$out = $contLang->formatNum( $this->editsInNs( $user, $namespace ) );
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
2022-03-05 14:11:43 +00:00
|
|
|
// @phan-suppress-next-line SecurityCheck-XSS
|
2014-06-16 05:49:51 +00:00
|
|
|
$this->getOutput()->addHTML( $out );
|
2007-04-23 20:58:49 +00:00
|
|
|
} else {
|
2019-12-17 16:22:42 +00:00
|
|
|
$nscount = $this->editsByNs( $user );
|
2007-04-23 20:58:49 +00:00
|
|
|
$html = new EditcountHTML;
|
2014-06-16 05:49:51 +00:00
|
|
|
$html->setContext( $this->getContext() );
|
2019-11-12 09:04:38 +00:00
|
|
|
$html->outputHTML( $username, $uid, $nscount );
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the username and namespace parts of the input and return them
|
|
|
|
*
|
|
|
|
* @param string $par
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-07-28 00:26:35 +00:00
|
|
|
private function extractParameters( $par ) {
|
2019-11-12 09:04:38 +00:00
|
|
|
$parts = explode( '/', $par, 2 );
|
|
|
|
$parts[1] = isset( $parts[1] )
|
|
|
|
? MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $parts[1] )
|
|
|
|
: null;
|
|
|
|
return $parts;
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the number of edits of a user by namespace
|
|
|
|
*
|
2019-12-17 16:22:42 +00:00
|
|
|
* @param User $user The user to check
|
2019-11-12 09:04:38 +00:00
|
|
|
* @return int[]
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
2019-12-17 16:22:42 +00:00
|
|
|
protected function editsByNs( $user ) {
|
|
|
|
if ( !$user || $user->getId() <= 0 ) {
|
2019-11-12 09:04:38 +00:00
|
|
|
return [];
|
|
|
|
}
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2018-07-27 07:34:01 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2019-12-17 16:22:42 +00:00
|
|
|
$actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user );
|
2007-04-23 20:58:49 +00:00
|
|
|
$res = $dbr->select(
|
2019-12-17 16:22:42 +00:00
|
|
|
[ 'revision', 'page' ] + $actorWhere['tables'],
|
2018-07-27 07:33:58 +00:00
|
|
|
[ 'page_namespace', 'COUNT(*) AS count' ],
|
2019-12-17 16:22:42 +00:00
|
|
|
[ $actorWhere['conds'] ],
|
2014-06-16 05:49:51 +00:00
|
|
|
__METHOD__,
|
2019-12-17 16:22:42 +00:00
|
|
|
[ 'GROUP BY' => 'page_namespace' ],
|
2020-11-17 02:10:52 +00:00
|
|
|
[ 'page' => [ 'JOIN', 'page_id = rev_page' ] ] + $actorWhere['joins']
|
2007-04-23 20:58:49 +00:00
|
|
|
);
|
|
|
|
|
2019-11-12 09:04:38 +00:00
|
|
|
$nscount = [];
|
2014-06-18 14:50:48 +00:00
|
|
|
foreach ( $res as $row ) {
|
2019-11-12 09:04:38 +00:00
|
|
|
$nscount[$row->page_namespace] = (int)$row->count;
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
|
|
|
return $nscount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the number of edits of a user in a given namespace
|
|
|
|
*
|
2019-12-17 16:22:42 +00:00
|
|
|
* @param User $user The user to check
|
2018-07-27 07:34:00 +00:00
|
|
|
* @param int $ns The namespace to check
|
2019-11-12 09:04:38 +00:00
|
|
|
* @return int
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
2019-12-17 16:22:42 +00:00
|
|
|
protected function editsInNs( $user, $ns ) {
|
|
|
|
if ( !$user || $user->getId() <= 0 ) {
|
2019-11-12 09:04:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-27 07:34:01 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2019-12-17 16:22:42 +00:00
|
|
|
$actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $user );
|
2019-11-12 09:04:38 +00:00
|
|
|
return (int)$dbr->selectField(
|
2019-12-17 16:22:42 +00:00
|
|
|
[ 'revision', 'page' ] + $actorWhere['tables'],
|
|
|
|
'COUNT(*)',
|
|
|
|
[ 'page_namespace' => $ns, $actorWhere['conds'] ],
|
2014-06-16 05:49:51 +00:00
|
|
|
__METHOD__,
|
2019-12-17 16:22:42 +00:00
|
|
|
[],
|
2021-08-27 23:41:28 +00:00
|
|
|
[ 'page' => [ 'JOIN', 'page_id = rev_page' ] ] + $actorWhere['joins']
|
2007-04-23 20:58:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|