2013-10-24 01:13:43 +00:00
|
|
|
<?php
|
|
|
|
|
2021-03-14 03:43:32 +00:00
|
|
|
namespace MediaWiki\Extension\Thanks;
|
|
|
|
|
2024-10-20 11:21:21 +00:00
|
|
|
use MediaWiki\Api\ApiMain;
|
|
|
|
use MediaWiki\Api\ApiUsageException;
|
2024-05-11 08:57:36 +00:00
|
|
|
use MediaWiki\HTMLForm\HTMLForm;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\Linker\Linker;
|
2023-08-19 23:40:49 +00:00
|
|
|
use MediaWiki\Request\DerivativeRequest;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\SpecialPage\FormSpecialPage;
|
|
|
|
use MediaWiki\Status\Status;
|
2023-05-20 11:51:09 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2024-01-23 21:28:02 +00:00
|
|
|
use MediaWiki\User\UserRigorOptions;
|
2021-03-14 03:43:32 +00:00
|
|
|
|
2013-10-24 01:13:43 +00:00
|
|
|
class SpecialThanks extends FormSpecialPage {
|
|
|
|
|
2017-04-16 10:46:40 +00:00
|
|
|
/**
|
|
|
|
* API result
|
|
|
|
*/
|
2023-05-20 11:51:09 +00:00
|
|
|
protected array $result;
|
2013-10-24 01:13:43 +00:00
|
|
|
|
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
|
|
|
*/
|
2023-05-20 11:51:09 +00:00
|
|
|
protected ?string $type;
|
2014-04-15 04:31:52 +00:00
|
|
|
|
|
|
|
/**
|
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
|
|
|
*/
|
2023-05-20 11:51:09 +00:00
|
|
|
protected ?string $id;
|
2014-04-15 04:31:52 +00:00
|
|
|
|
2023-05-20 11:51:09 +00:00
|
|
|
private UserFactory $userFactory;
|
|
|
|
|
|
|
|
public function __construct( UserFactory $userFactory ) {
|
2013-10-24 01:13:43 +00:00
|
|
|
parent::__construct( 'Thanks' );
|
2023-05-20 11:51:09 +00:00
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
$this->id = null;
|
2013-10-24 01:13:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 11:51:09 +00:00
|
|
|
public function doesWrites(): bool {
|
2016-06-03 05:02:25 +00:00
|
|
|
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;
|
2023-05-20 11:51:09 +00:00
|
|
|
return;
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
2023-05-20 11:51:09 +00:00
|
|
|
$this->type = 'flow';
|
|
|
|
$this->id = $tokens[1];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( strtolower( $tokens[0] ) === 'log' ) {
|
2018-03-13 03:28:35 +00:00
|
|
|
$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';
|
2023-05-20 11:51:09 +00:00
|
|
|
return;
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
2023-05-20 11:51:09 +00:00
|
|
|
$this->id = $tokens[1];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->type = 'rev';
|
|
|
|
if ( !( ctype_digit( $par ) ) ) { // Revision ID is not an integer.
|
|
|
|
$this->id = '0';
|
|
|
|
return;
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
2023-05-20 11:51:09 +00:00
|
|
|
|
|
|
|
$this->id = $par;
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2023-05-20 11:51:09 +00:00
|
|
|
protected function getFormFields(): array {
|
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',
|
2023-05-20 11:51:09 +00:00
|
|
|
'default' => $this->id ?? ''
|
2018-03-13 03:28:35 +00:00
|
|
|
],
|
|
|
|
'type' => [
|
|
|
|
'id' => 'mw-thanks-form-type',
|
|
|
|
'name' => 'type',
|
|
|
|
'type' => 'hidden',
|
2023-05-20 11:51:09 +00:00
|
|
|
'default' => $this->type ?? '',
|
2018-03-13 03:28:35 +00:00
|
|
|
],
|
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.
|
|
|
|
*/
|
2023-05-20 11:51:09 +00:00
|
|
|
protected function preHtml(): string {
|
2014-04-15 04:31:52 +00:00
|
|
|
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 {
|
2024-02-23 16:34:49 +00:00
|
|
|
// The following messages are used here
|
|
|
|
// * thanks-confirmation-special-rev
|
|
|
|
// * thanks-confirmation-special-log
|
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
|
|
|
}
|
|
|
|
|
2023-05-20 11:51:09 +00:00
|
|
|
protected function getDisplayFormat(): string {
|
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
|
|
|
*/
|
2023-05-20 11:51:09 +00:00
|
|
|
public function onSubmit( array $data ): Status {
|
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',
|
2023-05-20 11:51:09 +00:00
|
|
|
'token' => $this->getOutput()->getCsrfTokenSet()->getToken(),
|
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',
|
2018-09-18 18:52:20 +00:00
|
|
|
'postid' => $data['id'],
|
2023-05-20 11:51:09 +00:00
|
|
|
'token' => $this->getOutput()->getCsrfTokenSet()->getToken(),
|
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();
|
2024-01-23 21:28:02 +00:00
|
|
|
$recipient = $this->userFactory->newFromName( $this->result['recipient'], UserRigorOptions::RIGOR_NONE );
|
2013-10-24 01:13:43 +00:00
|
|
|
$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' ] ) ) {
|
2018-03-21 16:33:46 +00:00
|
|
|
$msgKey = 'thanks-thanked-notice';
|
2014-04-15 04:31:52 +00:00
|
|
|
} else {
|
2018-03-21 16:33:46 +00:00
|
|
|
$msgKey = 'flow-thanks-thanked-notice';
|
2014-04-15 04:31:52 +00:00
|
|
|
}
|
2018-03-21 16:33:46 +00:00
|
|
|
$msg = $this->msg( $msgKey )
|
|
|
|
->rawParams( $link )
|
|
|
|
->params( $recipient->getName(), $sender->getName() );
|
2018-03-13 03:28:35 +00:00
|
|
|
$this->getOutput()->addHTML( $msg->parse() );
|
2013-10-24 01:13:43 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 11:51:09 +00:00
|
|
|
public function isListed(): bool {
|
2013-10-24 01:13:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|