2013-10-24 01:13:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class SpecialThanks extends FormSpecialPage {
|
|
|
|
|
2014-04-15 04:31:52 +00:00
|
|
|
/**
|
|
|
|
* API result
|
|
|
|
* @var array $result
|
|
|
|
*/
|
2013-10-24 01:13:43 +00:00
|
|
|
protected $result;
|
|
|
|
|
2014-04-15 04:31:52 +00:00
|
|
|
/**
|
|
|
|
* 'rev' for revision or 'flow' for Flow comment, null if no ID is specified
|
|
|
|
* @var string $type
|
|
|
|
*/
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Revision ID ('0' = invalid) or Flow UUID
|
|
|
|
* @var string $id
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
2013-10-24 01:13:43 +00:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct( 'Thanks' );
|
|
|
|
}
|
|
|
|
|
2016-06-03 05:02:25 +00:00
|
|
|
public function doesWrites() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-15 04:31:52 +00:00
|
|
|
/**
|
|
|
|
* Set the type and ID or UUID of the request.
|
|
|
|
* @param string $par
|
|
|
|
*/
|
|
|
|
protected function setParameter( $par ) {
|
|
|
|
if ( $par === null || $par === '' ) {
|
|
|
|
$this->type = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tokens = explode( '/', $par );
|
|
|
|
if ( $tokens[0] === 'Flow' ) {
|
|
|
|
if ( count( $tokens ) === 1 || $tokens[1] === '' ) {
|
|
|
|
$this->type = null;
|
|
|
|
} else {
|
|
|
|
$this->type = 'flow';
|
|
|
|
$this->id = $tokens[1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->type = 'rev';
|
|
|
|
if ( !( ctype_digit( $par ) ) ) { // Revision ID is not an integer.
|
|
|
|
$this->id = '0';
|
|
|
|
} else {
|
|
|
|
$this->id = $par;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 01:13:43 +00:00
|
|
|
/**
|
|
|
|
* HTMLForm fields
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
protected function getFormFields() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
|
|
|
'revid' => [
|
2013-10-24 01:13:43 +00:00
|
|
|
'id' => 'mw-thanks-form-revid',
|
|
|
|
'name' => 'revid',
|
2014-04-15 04:31:52 +00:00
|
|
|
'type' => 'hidden',
|
2013-10-24 01:13:43 +00:00
|
|
|
'label-message' => 'thanks-form-revid',
|
2014-04-15 04:31:52 +00:00
|
|
|
'default' => $this->id,
|
2016-04-22 20:13:56 +00:00
|
|
|
]
|
|
|
|
];
|
2013-10-24 01:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 04:31:52 +00:00
|
|
|
* Return the confirmation or error message.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function preText() {
|
|
|
|
if ( $this->type === null ) {
|
|
|
|
$msgKey = 'thanks-error-no-id-specified';
|
|
|
|
} elseif ( $this->type === 'rev' && $this->id === '0' ) {
|
|
|
|
$msgKey = 'thanks-error-invalidrevision';
|
|
|
|
} elseif ( $this->type === 'flow' ) {
|
|
|
|
$msgKey = 'flow-thanks-confirmation-special';
|
|
|
|
} else {
|
|
|
|
$msgKey = 'thanks-confirmation-special';
|
|
|
|
}
|
|
|
|
return '<p>' . $this->msg( $msgKey )->escaped() . '</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the submission form.
|
2013-10-24 01:13:43 +00:00
|
|
|
* @param HTMLForm $form
|
|
|
|
*/
|
|
|
|
protected function alterForm( HTMLForm $form ) {
|
2014-04-15 04:31:52 +00:00
|
|
|
if ( $this->type === null || $this->type === 'rev' && $this->id === '0' ) {
|
|
|
|
$form->suppressDefaultSubmit( true );
|
|
|
|
} else {
|
2015-05-24 10:04:22 +00:00
|
|
|
$form->setSubmitText( $this->msg( 'thanks-submit' )->escaped() );
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
2013-10-24 01:13:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 14:35:28 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getDisplayFormat() {
|
2015-11-24 17:58:14 +00:00
|
|
|
return 'ooui';
|
2015-01-11 14:35:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-24 01:13:43 +00:00
|
|
|
/**
|
2014-04-15 04:31:52 +00:00
|
|
|
* Call the API internally.
|
2013-10-24 01:13:43 +00:00
|
|
|
* @param array $data
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
public function onSubmit( array $data ) {
|
|
|
|
if ( !isset( $data['revid'] ) ) {
|
|
|
|
return Status::newFatal( 'thanks-error-invalidrevision' );
|
|
|
|
}
|
|
|
|
|
2014-04-15 04:31:52 +00:00
|
|
|
if ( $this->type === 'rev' ) {
|
2016-04-22 20:13:56 +00:00
|
|
|
$requestData = [
|
2013-10-24 01:13:43 +00:00
|
|
|
'action' => 'thank',
|
|
|
|
'rev' => (int)$data['revid'],
|
|
|
|
'source' => 'specialpage',
|
|
|
|
'token' => $this->getUser()->getEditToken(),
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-04-15 04:31:52 +00:00
|
|
|
} else {
|
2016-04-22 20:13:56 +00:00
|
|
|
$requestData = [
|
2014-04-15 04:31:52 +00:00
|
|
|
'action' => 'flowthank',
|
|
|
|
'postid' => $data['revid'],
|
|
|
|
'token' => $this->getUser()->getEditToken(),
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$request = new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$requestData,
|
2013-10-24 01:13:43 +00:00
|
|
|
true // posted
|
|
|
|
);
|
|
|
|
|
|
|
|
$api = new ApiMain(
|
|
|
|
$request,
|
|
|
|
true // enable write mode
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$api->execute();
|
2016-11-03 19:16:57 +00:00
|
|
|
} catch ( ApiUsageException $e ) {
|
|
|
|
return Status::wrap( $e->getStatusValue() );
|
2013-10-24 01:13:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 19:26:18 +00:00
|
|
|
$this->result = $api->getResult()->getResultData( [ 'result' ] );
|
2013-10-24 01:13:43 +00:00
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-15 04:31:52 +00:00
|
|
|
* Display a message to the user.
|
2013-10-24 01:13:43 +00:00
|
|
|
*/
|
|
|
|
public function onSuccess() {
|
|
|
|
$recipient = User::newFromName( $this->result['recipient'] );
|
|
|
|
$link = Linker::userLink( $recipient->getId(), $recipient->getName() );
|
2014-04-15 04:31:52 +00:00
|
|
|
|
|
|
|
if ( $this->type === 'rev' ) {
|
|
|
|
$msgKey = 'thanks-thanked-notice';
|
|
|
|
} else {
|
|
|
|
$msgKey = 'flow-thanks-thanked-notice';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getOutput()->addHTML( $this->msg( $msgKey )
|
2013-10-24 01:13:43 +00:00
|
|
|
->rawParams( $link )
|
|
|
|
->params( $recipient->getName() )->parse()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isListed() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|