Skip to content

Commit

Permalink
fix: Normalize username and fix User:0 detection
Browse files Browse the repository at this point in the history
Bug: #27
Bug: #28
Bug: #29
Bug: #30
  • Loading branch information
winstonsung authored and AlPha5130 committed Oct 5, 2023
1 parent a786486 commit 020b6ba
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/SpecialEditCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,38 @@ public function execute( $par ) {
$output = $this->getOutput();
$this->setHeaders();
$this->outputHeader();

$username = $par ?? $request->getText( 'wpUsername' ) ?? $request->getText( 'wpuser' );
if ( !$username ) {

// Check URL parameters passed from the submit button first.
// Usernames don't allow leading and trailing whitespaces, so trim them.
// WebRequest::getText return '' by default but ?? only allow null-detection.
$trimChars = "_ \n\r\t\v\x00";
$usernameParams = [];
$usernameParams[] = $request->getText( 'wpUsername' );
$usernameParams[] = $request->getText( 'wpuser' );
$usernameParams[] = $par !== null ? $par : '';

foreach ( $usernameParams as $usernameParam ) {
$username = trim( $usernameParam, $trimChars );
if ( $par === '' || ( $usernameParam !== '' && $par !== $username ) ) {
$username = $username !== '' ? $username : null;
$url = SpecialPage::getTitleFor( 'EditCount', $username )->getLocalURL();
$output->redirect( $url );
}
}

$username = $par;

// PHP considers '0' to be falsy –
// but '0' is a valid title and valid user name in MediaWiki.
if ( $username === null || $username === '' ) {
$this->outputHTMLForm();
return;
}

$user = $this->userIdentityLookup
->getUserIdentityByName( $username );
if ( !$user || $user->getId() === 0 ) {
$this->outputHTMLForm();
$this->outputHTMLForm( null, $username );
$output->addHTML( '<br>' . Html::element(
'strong',
[ 'class' => 'error' ],
Expand All @@ -105,8 +126,9 @@ public function execute( $par ) {
/**
* Output form for query.
* @param ?UserIdentity $user
* @param string $username Usually for nonexistent username query
*/
protected function outputHTMLForm( ?UserIdentity $user = null ) {
protected function outputHTMLForm( ?UserIdentity $user = null, string $username = '' ) {
$formDescriptor = [
'username' => [
'type' => 'user',
Expand All @@ -115,13 +137,14 @@ protected function outputHTMLForm( ?UserIdentity $user = null ) {
'exists' => true,
'label-message' => 'editcountneue-form-username',
'required' => true,
'default' => $user ? $user->getName() : ''
'default' => $user ? $user->getName() : $username
]
];

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
$htmlForm
->setMethod( 'get' )
->setAction( SpecialPage::getTitleFor( 'EditCount' )->getLocalURL() )
->setWrapperLegendMsg( 'editcountneue-form-legend' )
->setSubmitTextMsg( 'editcountneue-form-submit' )
->prepareForm()
Expand Down

0 comments on commit 020b6ba

Please sign in to comment.