Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiEchoArticleReminder
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 7
110
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Notifications\Api;
4
5use ApiBase;
6use DateInterval;
7use DateTime;
8use MediaWiki\Extension\Notifications\Model\Event;
9use MediaWiki\Utils\MWTimestamp;
10use Wikimedia\ParamValidator\ParamValidator;
11
12class ApiEchoArticleReminder extends ApiBase {
13
14    public function execute() {
15        $this->getMain()->setCacheMode( 'private' );
16        $user = $this->getUser();
17        if ( !$user->isRegistered() ) {
18            $this->dieWithError( 'apierror-mustbeloggedin-generic', 'login-required' );
19        }
20
21        $params = $this->extractRequestParams();
22        $result = [];
23        $userTimestamp = new MWTimestamp( $params['timestamp'] );
24        $nowTimestamp = new MWTimestamp();
25        // We need $params['timestamp'] to be a future timestamp:
26        // $userTimestamp < $nowTimestamp = invert 0
27        // $userTimestamp > $nowTimestamp = invert 1
28        if ( $userTimestamp->diff( $nowTimestamp )->invert === 0 ) {
29            $this->dieWithError( [ 'apierror-badparameter', 'timestamp' ], 'timestamp-not-in-future', null, 400 );
30        }
31
32        $eventCreation = Event::create( [
33            'type' => 'article-reminder',
34            'agent' => $user,
35            'title' => $this->getTitleFromTitleOrPageId( $params ),
36            'extra' => [
37                'comment' => $params['comment'],
38            ],
39        ] );
40
41        if ( !$eventCreation ) {
42            $this->dieWithError( 'apierror-echo-event-creation-failed', null, null, 500 );
43        }
44
45        /* Temp - removing the delay just for now:
46        $job = new JobSpecification(
47            'articleReminder',
48            [
49                'userId' => $user->getId(),
50                'timestamp' => $params['timestamp'],
51                'comment' => $params['comment'],
52            ],
53            [ 'removeDuplicates' => true ],
54            Title::newFromID( $params['pageid'] )
55        );
56        MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );*/
57        $result += [
58            'result' => 'success'
59        ];
60        $this->getResult()->addValue( 'query', $this->getModuleName(), $result );
61    }
62
63    public function getAllowedParams() {
64        return [
65            'pageid' => [
66                ParamValidator::PARAM_TYPE => 'integer',
67            ],
68            'title' => [
69                ParamValidator::PARAM_TYPE => 'string',
70            ],
71            'comment' => [
72                ParamValidator::PARAM_TYPE => 'string',
73            ],
74            'timestamp' => [
75                ParamValidator::PARAM_REQUIRED => true,
76                ParamValidator::PARAM_TYPE => 'timestamp',
77            ],
78            'token' => [
79                ParamValidator::PARAM_REQUIRED => true,
80            ],
81        ];
82    }
83
84    public function needsToken() {
85        return 'csrf';
86    }
87
88    public function mustBePosted() {
89        return true;
90    }
91
92    public function isWriteMode() {
93        return true;
94    }
95
96    /**
97     * @see ApiBase::getExamplesMessages()
98     * @return string[]
99     */
100    protected function getExamplesMessages() {
101        $todayDate = new DateTime();
102        $oneDay = new DateInterval( 'P1D' );
103        $tomorrowDate = $todayDate->add( $oneDay );
104        $tomorrowDateTimestamp = new MWTimestamp( $tomorrowDate );
105        $tomorrowTimestampStr = $tomorrowDateTimestamp->getTimestamp( TS_ISO_8601 );
106        return [
107            "action=echoarticlereminder&pageid=1&timestamp=$tomorrowTimestampStr&comment=example"
108                => 'apihelp-echoarticlereminder-example-1',
109            "action=echoarticlereminder&title=Main_Page&timestamp=$tomorrowTimestampStr"
110                => 'apihelp-echoarticlereminder-example-2',
111        ];
112    }
113
114    public function getHelpUrls() {
115        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API';
116    }
117}