2013-10-24 01:13:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class SpecialThanks extends FormSpecialPage {
|
|
|
|
|
2017-04-16 10:46:40 +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
|
|
|
/**
|
2018-03-13 03:28:35 +00:00
|
|
|
* 'rev' for revision, 'log' for log entry, or 'flow' for Flow comment,
|
|
|
|
* null if no ID is specified
|
2014-04-15 04:31:52 +00:00
|
|
|
* @var string $type
|
|
|
|
*/
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
/**
|
2018-03-13 03:28:35 +00:00
|
|
|
* Revision or Log ID ('0' = invalid) or Flow UUID
|
2014-04-15 04:31:52 +00:00
|
|
|
* @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.
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param string $par The subpage name.
|
2014-04-15 04:31:52 +00:00
|
|
|
*/
|
|
|
|
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];
|
|
|
|
}
|
2018-03-13 03:28:35 +00:00
|
|
|
} elseif ( strtolower( $tokens[0] ) === 'log' ) {
|
|
|
|
$this->type = 'log';
|
|
|
|
// Make sure there's a numeric ID specified as the subpage.
|
|
|
|
if ( count( $tokens ) === 1 || $tokens[1] === '' || !( ctype_digit( $tokens[1] ) ) ) {
|
|
|
|
$this->id = '0';
|
|
|
|
} else {
|
|
|
|
$this->id = $tokens[1];
|
|
|
|
}
|
2014-04-15 04:31:52 +00:00
|
|
|
} 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
|
2018-02-09 07:09:43 +00:00
|
|
|
* @return string[][]
|
2013-10-24 01:13:43 +00:00
|
|
|
*/
|
|
|
|
protected function getFormFields() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2018-03-13 03:28:35 +00:00
|
|
|
'id' => [
|
|
|
|
'id' => 'mw-thanks-form-id',
|
|
|
|
'name' => 'id',
|
2014-04-15 04:31:52 +00:00
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $this->id,
|
2018-03-13 03:28:35 +00:00
|
|
|
],
|
|
|
|
'type' => [
|
|
|
|
'id' => 'mw-thanks-form-type',
|
|
|
|
'name' => 'type',
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $this->type,
|
|
|
|
],
|
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';
|
2018-03-13 03:28:35 +00:00
|
|
|
} elseif ( $this->type === 'log' && $this->id === '0' ) {
|
|
|
|
$msgKey = 'thanks-error-invalid-log-id';
|
2014-04-15 04:31:52 +00:00
|
|
|
} elseif ( $this->type === 'flow' ) {
|
|
|
|
$msgKey = 'flow-thanks-confirmation-special';
|
|
|
|
} else {
|
2018-03-13 03:28:35 +00:00
|
|
|
$msgKey = 'thanks-confirmation-special-' . $this->type;
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
|
|
|
return '<p>' . $this->msg( $msgKey )->escaped() . '</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the submission form.
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param HTMLForm $form The form object to modify.
|
2013-10-24 01:13:43 +00:00
|
|
|
*/
|
|
|
|
protected function alterForm( HTMLForm $form ) {
|
2018-03-13 03:28:35 +00:00
|
|
|
if ( $this->type === null
|
|
|
|
|| ( in_array( $this->type, [ 'rev', 'log', ] ) && $this->id === '0' )
|
|
|
|
) {
|
2014-04-15 04:31:52 +00:00
|
|
|
$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.
|
2018-02-09 07:09:43 +00:00
|
|
|
* @param string[] $data The form data.
|
2013-10-24 01:13:43 +00:00
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
public function onSubmit( array $data ) {
|
2018-03-13 03:28:35 +00:00
|
|
|
if ( !isset( $data['id'] ) ) {
|
2013-10-24 01:13:43 +00:00
|
|
|
return Status::newFatal( 'thanks-error-invalidrevision' );
|
|
|
|
}
|
|
|
|
|
2018-03-13 03:28:35 +00:00
|
|
|
if ( in_array( $this->type, [ 'rev', 'log' ] ) ) {
|
2016-04-22 20:13:56 +00:00
|
|
|
$requestData = [
|
2013-10-24 01:13:43 +00:00
|
|
|
'action' => 'thank',
|
2018-03-13 03:28:35 +00:00
|
|
|
$this->type => (int)$data['id'],
|
2013-10-24 01:13:43 +00:00
|
|
|
'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() {
|
2017-06-22 00:30:35 +00:00
|
|
|
$sender = $this->getUser();
|
2013-10-24 01:13:43 +00:00
|
|
|
$recipient = User::newFromName( $this->result['recipient'] );
|
|
|
|
$link = Linker::userLink( $recipient->getId(), $recipient->getName() );
|
2014-04-15 04:31:52 +00:00
|
|
|
|
2018-03-13 03:28:35 +00:00
|
|
|
if ( in_array( $this->type, [ 'rev', 'log' ] ) ) {
|
|
|
|
$msg = $this->msg( 'thanks-thanked-notice' )
|
|
|
|
->rawParams( $link )
|
|
|
|
->params( $sender->getName() );
|
2014-04-15 04:31:52 +00:00
|
|
|
} else {
|
2018-03-13 03:28:35 +00:00
|
|
|
$msg = $this->msg( 'flow-thanks-thanked-notice' )
|
|
|
|
->rawParams( $link )
|
|
|
|
->params( $recipient->getName(), $sender->getName() );
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
2018-03-13 03:28:35 +00:00
|
|
|
$this->getOutput()->addHTML( $msg->parse() );
|
2013-10-24 01:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isListed() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|