mediawiki-extensions-Thanks/SpecialThanks.php
Kunal Mehta 06360b6d0d Add Special:Thanks as a non-JS fallback
If the user has JavaScript disabled, clicking
on "thank" will send the user to Special:Thanks
with the revision id already filled in. When
submitted, the form calls the API internally
and then shows the user an error or confirmation
message.

Special:Thanks is not a listed special page
since it's mainly intended as a fallback.

The API was modified to return the user name of the
editor who is being thanked if a new notification
was created.

Bug: 49161
Change-Id: I7ba4664b92bb0da425784350487ed0e7ca352b4e
2013-12-05 23:23:21 -08:00

112 lines
2.3 KiB
PHP

<?php
class SpecialThanks extends FormSpecialPage {
/** @var array $result */
protected $result;
public function __construct() {
parent::__construct( 'Thanks' );
}
/**
* HTMLForm fields
* @return Array
*/
protected function getFormFields() {
return array(
'revid' => array(
'id' => 'mw-thanks-form-revid',
'name' => 'revid',
'type' => 'int',
'label-message' => 'thanks-form-revid',
'default' => $this->getRequest()->getInt( 'revid', $this->par ) ?: '',
)
);
}
/**
* Make it look pretty
* @param HTMLForm $form
*/
protected function alterForm( HTMLForm $form ) {
$form->setDisplayFormat( 'vform' );
$form->setWrapperLegend( false );
}
/**
* Calls the API internally
* @param array $data
* @return Status
*/
public function onSubmit( array $data ) {
if ( !isset( $data['revid'] ) ) {
return Status::newFatal( 'thanks-error-invalidrevision' );
}
$request = new DerivativeRequest(
$this->getRequest(),
array(
'action' => 'thank',
'rev' => (int)$data['revid'],
'source' => 'specialpage',
'token' => $this->getUser()->getEditToken(),
),
true // posted
);
$api = new ApiMain(
$request,
true // enable write mode
);
try {
$api->execute();
} catch ( UsageException $e ) {
return $this->handleErrorCodes( $e->getCodeString() );
}
$result = $api->getResult()->getData();
$this->result = $result['result'];
return Status::newGood();
}
/**
* Handles error codes returned by the API
* @param $code
* @return Status
*/
protected function handleErrorCodes( $code ) {
$status = new Status;
switch ( $code ) {
case 'invalidrevision':
case 'ratelimited':
$status->fatal( "thanks-error-$code" );
break;
case 'notloggedin':
case 'blockedtext':
$status->fatal( $code );
break;
default:
$status->fatal( 'thanks-error-undefined' );
}
return $status;
}
/**
* Display a message to the user
*/
public function onSuccess() {
$recipient = User::newFromName( $this->result['recipient'] );
$link = Linker::userLink( $recipient->getId(), $recipient->getName() );
$this->getOutput()->addHTML( $this->msg( 'thanks-thanked-notice' )
->rawParams( $link )
->params( $recipient->getName() )->parse()
);
}
public function isListed() {
return false;
}
}