2017-06-08 16:38:06 +00:00
|
|
|
<?php
|
|
|
|
|
2022-10-02 22:44:38 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use DateInterval;
|
|
|
|
use DateTime;
|
|
|
|
use EchoEvent;
|
|
|
|
use MWTimestamp;
|
2022-10-02 22:48:37 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2022-10-02 22:44:38 +00:00
|
|
|
|
2017-06-08 16:38:06 +00:00
|
|
|
class ApiEchoArticleReminder extends ApiBase {
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$this->getMain()->setCacheMode( 'private' );
|
|
|
|
$user = $this->getUser();
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2017-06-08 16:38:06 +00:00
|
|
|
$this->dieWithError( 'apierror-mustbeloggedin-generic', 'login-required' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$result = [];
|
|
|
|
$userTimestamp = new MWTimestamp( $params['timestamp'] );
|
|
|
|
$nowTimestamp = new MWTimestamp();
|
|
|
|
// We need $params['timestamp'] to be a future timestamp:
|
|
|
|
// $userTimestamp < $nowTimestamp = invert 0
|
|
|
|
// $userTimestamp > $nowTimestamp = invert 1
|
|
|
|
if ( $userTimestamp->diff( $nowTimestamp )->invert === 0 ) {
|
|
|
|
$this->dieWithError( [ 'apierror-badparameter', 'timestamp' ], 'timestamp-not-in-future', null, 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$eventCreation = EchoEvent::create( [
|
|
|
|
'type' => 'article-reminder',
|
|
|
|
'agent' => $user,
|
|
|
|
'title' => $this->getTitleFromTitleOrPageId( $params ),
|
|
|
|
'extra' => [
|
|
|
|
'comment' => $params['comment'],
|
|
|
|
],
|
|
|
|
] );
|
|
|
|
|
|
|
|
if ( !$eventCreation ) {
|
|
|
|
$this->dieWithError( 'apierror-echo-event-creation-failed', null, null, 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Temp - removing the delay just for now:
|
|
|
|
$job = new JobSpecification(
|
|
|
|
'articleReminder',
|
|
|
|
[
|
|
|
|
'userId' => $user->getId(),
|
|
|
|
'timestamp' => $params['timestamp'],
|
|
|
|
'comment' => $params['comment'],
|
|
|
|
],
|
|
|
|
[ 'removeDuplicates' => true ],
|
|
|
|
Title::newFromID( $params['pageid'] )
|
|
|
|
);
|
2022-01-25 21:24:53 +00:00
|
|
|
MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );*/
|
2017-06-08 16:38:06 +00:00
|
|
|
$result += [
|
|
|
|
'result' => 'success'
|
|
|
|
];
|
|
|
|
$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
|
|
|
'pageid' => [
|
2022-10-02 22:48:37 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2017-06-08 16:38:06 +00:00
|
|
|
],
|
|
|
|
'title' => [
|
2022-10-02 22:48:37 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2017-06-08 16:38:06 +00:00
|
|
|
],
|
|
|
|
'comment' => [
|
2022-10-02 22:48:37 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2017-06-08 16:38:06 +00:00
|
|
|
],
|
|
|
|
'timestamp' => [
|
2022-10-02 22:48:37 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
|
|
|
ParamValidator::PARAM_TYPE => 'timestamp',
|
2017-06-08 16:38:06 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2022-10-02 22:48:37 +00:00
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2017-06-08 16:38:06 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
|
|
|
return 'csrf';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return string[]
|
2017-06-08 16:38:06 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
$todayDate = new DateTime();
|
|
|
|
$oneDay = new DateInterval( 'P1D' );
|
|
|
|
$tomorrowDate = $todayDate->add( $oneDay );
|
|
|
|
$tomorrowDateTimestamp = new MWTimestamp( $tomorrowDate );
|
|
|
|
$tomorrowTimestampStr = $tomorrowDateTimestamp->getTimestamp( TS_ISO_8601 );
|
|
|
|
return [
|
|
|
|
"action=echoarticlereminder&pageid=1×tamp=$tomorrowTimestampStr&comment=example"
|
|
|
|
=> 'apihelp-echoarticlereminder-example-1',
|
|
|
|
"action=echoarticlereminder&title=Main_Page×tamp=$tomorrowTimestampStr"
|
|
|
|
=> 'apihelp-echoarticlereminder-example-2',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
2019-08-27 06:13:35 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API';
|
2017-06-08 16:38:06 +00:00
|
|
|
}
|
|
|
|
}
|