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 {
|
2007-04-23 20:58:49 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2008-01-11 08:09:43 +00:00
|
|
|
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
|
|
|
|
|
|
|
list( $username, $namespace ) = $this->extractParamaters( $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 );
|
|
|
|
$username = is_object( $user ) ? $user->getName() : '';
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2014-12-08 10:16:04 +00:00
|
|
|
$uid = ( $user instanceof 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-07-27 22:47:12 +00:00
|
|
|
$out = $contLang->formatNum( $this->editsInNs( $uid, $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 {
|
2014-06-16 05:49:51 +00:00
|
|
|
if ( $uid != 0 ) {
|
|
|
|
$nscount = $this->editsByNs( $uid );
|
|
|
|
$total = $this->getTotal( $nscount );
|
|
|
|
}
|
2007-04-23 20:58:49 +00:00
|
|
|
$html = new EditcountHTML;
|
2014-06-16 05:49:51 +00:00
|
|
|
$html->setContext( $this->getContext() );
|
|
|
|
// @fixme don't use @
|
2007-04-23 20:58:49 +00:00
|
|
|
$html->outputHTML( $username, $uid, @$nscount, @$total );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the username and namespace parts of the input and return them
|
|
|
|
*
|
2019-02-06 13:07:47 +00:00
|
|
|
* @private
|
2007-04-23 20:58:49 +00:00
|
|
|
*
|
|
|
|
* @param string $par
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function extractParamaters( $par ) {
|
2014-06-16 05:49:51 +00:00
|
|
|
// @fixme don't use @
|
|
|
|
@list( $user, $namespace ) = explode( '/', $par, 2 );
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
// str*cmp sucks
|
2014-06-18 14:50:48 +00:00
|
|
|
if ( isset( $namespace ) ) {
|
2019-07-27 22:47:12 +00:00
|
|
|
$namespace = MediaWikiServices::getInstance()->getContentLanguage()->getNsIndex( $namespace );
|
2014-06-18 14:50:48 +00:00
|
|
|
}
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2018-07-27 07:33:58 +00:00
|
|
|
return [ $user, $namespace ];
|
2007-04-23 20:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute and return the total edits in all namespaces
|
|
|
|
*
|
2019-02-06 13:07:47 +00:00
|
|
|
* @private
|
2007-04-23 20:58:49 +00:00
|
|
|
*
|
|
|
|
* @param array $nscount An associative array
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function getTotal( $nscount ) {
|
|
|
|
$total = 0;
|
2014-06-18 14:50:48 +00:00
|
|
|
foreach ( array_values( $nscount ) as $i ) {
|
2007-04-23 20:58:49 +00:00
|
|
|
$total += $i;
|
2014-06-18 14:50:48 +00:00
|
|
|
}
|
2007-04-23 20:58:49 +00:00
|
|
|
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the number of edits of a user by namespace
|
|
|
|
*
|
|
|
|
* @param int $uid The user ID to check
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function editsByNs( $uid ) {
|
2018-07-27 07:33:58 +00:00
|
|
|
$nscount = [];
|
2007-04-23 20:58:49 +00:00
|
|
|
|
2018-07-27 07:34:01 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2007-04-23 20:58:49 +00:00
|
|
|
$res = $dbr->select(
|
2018-07-27 07:33:58 +00:00
|
|
|
[ 'user', 'revision', 'page' ],
|
|
|
|
[ 'page_namespace', 'COUNT(*) AS count' ],
|
|
|
|
[
|
2007-04-23 20:58:49 +00:00
|
|
|
'user_id' => $uid,
|
|
|
|
'rev_user = user_id',
|
|
|
|
'rev_page = page_id'
|
2018-07-27 07:33:58 +00:00
|
|
|
],
|
2014-06-16 05:49:51 +00:00
|
|
|
__METHOD__,
|
2018-07-27 07:33:58 +00:00
|
|
|
[ 'GROUP BY' => 'page_namespace' ]
|
2007-04-23 20:58:49 +00:00
|
|
|
);
|
|
|
|
|
2014-06-18 14:50:48 +00:00
|
|
|
foreach ( $res as $row ) {
|
2007-04-23 20:58:49 +00:00
|
|
|
$nscount[$row->page_namespace] = $row->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nscount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count the number of edits of a user in a given namespace
|
|
|
|
*
|
|
|
|
* @param int $uid The user ID to check
|
2018-07-27 07:34:00 +00:00
|
|
|
* @param int $ns The namespace to check
|
2007-04-23 20:58:49 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function editsInNs( $uid, $ns ) {
|
2018-07-27 07:34:01 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2007-04-23 20:58:49 +00:00
|
|
|
$res = $dbr->selectField(
|
2018-07-27 07:33:58 +00:00
|
|
|
[ 'user', 'revision', 'page' ],
|
|
|
|
[ 'COUNT(*) AS count' ],
|
|
|
|
[
|
2007-04-23 20:58:49 +00:00
|
|
|
'user_id' => $uid,
|
|
|
|
'page_namespace' => $ns,
|
|
|
|
'rev_user = user_id',
|
|
|
|
'rev_page = page_id'
|
2018-07-27 07:33:58 +00:00
|
|
|
],
|
2014-06-16 05:49:51 +00:00
|
|
|
__METHOD__,
|
2018-07-27 07:33:58 +00:00
|
|
|
[ 'GROUP BY' => 'page_namespace' ]
|
2007-04-23 20:58:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EditcountHTML extends Editcount {
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
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
|
|
|
|
* @param array $nscount
|
|
|
|
* @param int $total
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
|
|
|
function outputHTML( $username, $uid, $nscount, $total ) {
|
|
|
|
$this->nscount = $nscount;
|
|
|
|
$this->total = $total;
|
|
|
|
|
|
|
|
$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-02-06 13:07:47 +00:00
|
|
|
* @private
|
2007-04-23 20:58:49 +00:00
|
|
|
*/
|
|
|
|
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 );
|
2018-07-27 07:33:59 +00:00
|
|
|
$percent = $this->total > 0 ? wfPercent( $this->total / $this->total * 100, 2 ) : wfPercent( 0 ); // @bug 4400
|
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;
|
|
|
|
}
|
|
|
|
}
|