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 ) {
|
2014-06-16 05:49:51 +00:00
|
|
|
$target = isset( $par ) ? $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
|
|
|
}
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EditcountHTML extends Editcount {
|
|
|
|
/**
|
2019-11-12 09:04:38 +00:00
|
|
|
* @var int[]
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
2014-06-16 05:49:51 +00:00
|
|
|
private $nscount;
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2014-06-16 05:49:51 +00:00
|
|
|
private $total;
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the HTML form on Special:Editcount
|
|
|
|
*
|
|
|
|
* @param string $username
|
2018-07-27 07:34:00 +00:00
|
|
|
* @param int $uid
|
2019-11-12 09:04:38 +00:00
|
|
|
* @param int[] $nscount
|
|
|
|
* @param int|null $total
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
2019-11-12 09:04:38 +00:00
|
|
|
public function outputHTML( $username, $uid, array $nscount, $total = null ) {
|
2007-04-23 20:58:49 +00:00
|
|
|
$this->nscount = $nscount;
|
2019-11-12 09:04:38 +00:00
|
|
|
$this->total = $total ?: array_sum( $nscount );
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
$this->setHeaders();
|
|
|
|
|
2014-06-18 14:50:48 +00:00
|
|
|
$action = htmlspecialchars( $this->getPageTitle()->getLocalURL() );
|
2014-06-16 05:49:51 +00:00
|
|
|
$user = $this->msg( 'editcount_username' )->escaped();
|
2007-04-23 20:58:49 +00:00
|
|
|
$out = "
|
|
|
|
<form id='editcount' method='post' action=\"$action\">
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>$user</td>
|
2018-09-14 19:50:29 +00:00
|
|
|
<td>" . new OOUI\TextInputWidget( [
|
|
|
|
'name' => 'username',
|
|
|
|
'value' => $username,
|
|
|
|
'autofocus' => true,
|
|
|
|
] ) . "</td>
|
|
|
|
<td>" . new OOUI\ButtonInputWidget( [
|
|
|
|
'label' => $this->msg( 'editcount_submit' )->text(),
|
|
|
|
'flags' => [ 'primary', 'progressive' ],
|
|
|
|
'type' => 'submit',
|
|
|
|
] ) . " </td>
|
2007-04-23 20:58:49 +00:00
|
|
|
</tr>";
|
2014-06-16 05:49:51 +00:00
|
|
|
if ( $username != null && $uid != 0 ) {
|
2007-04-23 20:58:49 +00:00
|
|
|
$editcounttable = $this->makeTable();
|
|
|
|
$out .= "
|
|
|
|
<tr>
|
2010-05-30 17:33:59 +00:00
|
|
|
<td> </td>
|
2007-04-23 20:58:49 +00:00
|
|
|
<td>$editcounttable</td>
|
2010-05-30 17:33:59 +00:00
|
|
|
<td> </td>
|
2007-04-23 20:58:49 +00:00
|
|
|
</tr>";
|
|
|
|
}
|
2014-06-18 14:50:48 +00:00
|
|
|
$out .= '
|
2007-04-23 20:58:49 +00:00
|
|
|
</table>
|
2014-06-18 14:50:48 +00:00
|
|
|
</form>';
|
2014-06-16 05:49:51 +00:00
|
|
|
$this->getOutput()->addHTML( $out );
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the editcount-by-namespaces HTML table
|
|
|
|
*
|
2019-07-27 22:49:41 +00:00
|
|
|
* @return string
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
2019-07-27 22:49:41 +00:00
|
|
|
private function makeTable() {
|
2014-06-16 05:49:51 +00:00
|
|
|
$lang = $this->getLanguage();
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2014-06-16 05:49:51 +00:00
|
|
|
$total = $this->msg( 'editcount_total' )->escaped();
|
|
|
|
$ftotal = $lang->formatNum( $this->total );
|
2019-11-12 09:04:38 +00:00
|
|
|
$percent = wfPercent( $this->total ? 100 : 0 );
|
2014-06-16 05:49:51 +00:00
|
|
|
// @fixme don't use inline styles
|
2007-04-23 20:58:49 +00:00
|
|
|
$ret = "<table border='1' style='background-color: #fff; border: 1px #aaa solid; border-collapse: collapse;'>
|
|
|
|
<tr>
|
|
|
|
<th>$total</th>
|
|
|
|
<th>$ftotal</th>
|
|
|
|
<th>$percent</th>
|
|
|
|
</tr>
|
|
|
|
";
|
|
|
|
|
2014-06-18 14:50:48 +00:00
|
|
|
foreach ( $this->nscount as $ns => $edits ) {
|
2014-06-16 05:49:51 +00:00
|
|
|
$fedits = $lang->formatNum( $edits );
|
2014-06-18 14:50:48 +00:00
|
|
|
$fns = ( $ns == NS_MAIN ) ? $this->msg( 'blanknamespace' ) : $lang->getFormattedNsText( $ns );
|
2007-04-23 20:58:49 +00:00
|
|
|
$percent = wfPercent( $edits / $this->total * 100 );
|
2014-06-16 05:49:51 +00:00
|
|
|
$fpercent = $lang->formatNum( $percent );
|
2014-06-18 14:50:48 +00:00
|
|
|
$ret .= "
|
2007-04-23 20:58:49 +00:00
|
|
|
<tr>
|
|
|
|
<td>$fns</td>
|
|
|
|
<td>$fedits</td>
|
|
|
|
<td>$fpercent</td>
|
|
|
|
</tr>
|
|
|
|
";
|
|
|
|
}
|
2014-06-18 14:50:48 +00:00
|
|
|
$ret .= '</table>
|
|
|
|
';
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|