Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
28 / 32
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSuggestArticleAction
87.50% covered (warning)
87.50%
28 / 32
66.67% covered (warning)
66.67%
2 / 3
3.02
0.00% covered (danger)
0.00%
0 / 1
 execute
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/*
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23namespace MediaWiki\Extension\Collection\Api;
24
25use ApiBase;
26use MediaWiki\Extension\Collection\Session;
27use MediaWiki\Extension\Collection\Suggest;
28use MediaWiki\Html\Html;
29use SkinTemplate;
30use Wikimedia\ParamValidator\ParamValidator;
31
32class ApiSuggestArticleAction extends ApiBase {
33    use CollectionTrait;
34
35    public const VALID_ACTIONS = [ 'add', 'remove', 'ban' ];
36
37    /**
38     * execute the API request
39     */
40    public function execute() {
41        [ 'suggestaction' => $action, 'title' => $title ] = $this->extractRequestParams();
42
43        $result = Suggest::refresh( $action, $title );
44
45        $undoLink = Html::element( 'a',
46            [
47                'href' => SkinTemplate::makeSpecialUrl(
48                    'Book',
49                    [ 'bookcmd' => 'suggest', 'undo' => $action, 'arttitle' => $title ]
50                ),
51                'onclick' => "collectionSuggestCall('UndoArticle'," .
52                    Html::encodeJsVar( [ $action, $title ] ) . "); return false;",
53                'title' => $this->msg( 'coll-suggest_undo_tooltip' ),
54            ],
55            $this->msg( 'coll-suggest_undo' )->text()
56        );
57
58        // Message keys used:
59        // coll-suggest_article_ban
60        // coll-suggest_article_add
61        // coll-suggest_article_remove
62        $result['last_action'] = $this->msg( "coll-suggest_article_$action", $title )
63            ->rawParams( $undoLink )->parse();
64        $result['collection'] = Session::getCollection();
65
66        $this->getResult()->addValue( null, $this->getModuleName(), $result );
67    }
68
69    /**
70     * @return array
71     */
72    public function getAllowedParams() {
73        return [
74            'suggestaction' => [
75                ParamValidator::PARAM_TYPE => self::VALID_ACTIONS,
76                ParamValidator::PARAM_REQUIRED => true,
77            ],
78            'title' => [
79                ParamValidator::PARAM_TYPE => 'string',
80                ParamValidator::PARAM_REQUIRED => true,
81            ]
82        ];
83    }
84
85    /**
86     * @return array examples of the use of this API module
87     */
88    protected function getExamplesMessages(): array {
89        return [
90            'action=collection&submodule=suggestarticleaction&suggestaction=add&title=Main_Page'
91                => 'apihelp-collection+suggestarticleaction-example',
92        ];
93    }
94}