Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.76% covered (warning)
75.76%
25 / 33
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiGetPopupData
75.76% covered (warning)
75.76%
25 / 33
50.00% covered (danger)
50.00%
2 / 4
8.91
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 execute
78.95% covered (warning)
78.95%
15 / 19
0.00% covered (danger)
0.00%
0 / 1
5.23
 getAllowedParams
100.00% covered (success)
100.00%
6 / 6
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\Page\WikiPageFactory;
28use MediaWiki\Title\Title;
29use MediaWiki\Title\TitleFactory;
30use Wikimedia\ParamValidator\ParamValidator;
31
32class ApiGetPopupData extends ApiBase {
33    use CollectionTrait;
34
35    /** @var TitleFactory */
36    private $titleFactory;
37    /** @var WikiPageFactory */
38    private $wikiPageFactory;
39
40    /**
41     * @param ApiBase $parent
42     * @param string $action
43     * @param TitleFactory $titleFactory
44     * @param WikiPageFactory $wikiPageFactory
45     */
46    public function __construct(
47        ApiBase $parent,
48        $action,
49        TitleFactory $titleFactory,
50        WikiPageFactory $wikiPageFactory
51    ) {
52        parent::__construct( $parent->getMain(), $action );
53        $this->parent = $parent;
54        $this->titleFactory = $titleFactory;
55        $this->wikiPageFactory = $wikiPageFactory;
56    }
57
58    /**
59     * execute the API request
60     */
61    public function execute() {
62        [ 'title' => $title ] = $this->extractRequestParams();
63
64        $extensionAssetsPath = $this->getConfig()->get( 'ExtensionAssetsPath' );
65        $result = [];
66        $imagePath = "$extensionAssetsPath/Collection/images";
67        $t = $this->titleFactory->newFromText( $title );
68        if ( $t && $t->isRedirect() ) {
69            $wikiPage = $this->wikiPageFactory->newFromTitle( $t );
70            $t = $wikiPage->followRedirect();
71            if ( $t instanceof Title ) {
72                $title = $t->getPrefixedText();
73            }
74        }
75        if ( Session::findArticle( $title ) == -1 ) {
76            $result['action'] = 'add';
77            $result['text'] = $this->msg( 'coll-add_linked_article' )->text();
78            $result['img'] = "$imagePath/silk-add.png";
79        } else {
80            $result['action'] = 'remove';
81            $result['text'] = $this->msg( 'coll-remove_linked_article' )->text();
82            $result['img'] = "$imagePath/silk-remove.png";
83        }
84        $result['title'] = $title;
85
86        $this->getResult()->addValue( null, $this->getModuleName(), $result );
87    }
88
89    /** @inheritDoc */
90    public function getAllowedParams() {
91        return [
92            'title' => [
93                ParamValidator::PARAM_TYPE => 'string',
94                ParamValidator::PARAM_REQUIRED => true,
95            ]
96        ];
97    }
98
99    /**
100     * @return array examples of the use of this API module
101     */
102    protected function getExamplesMessages(): array {
103        return [
104            'action=collection&submodule=getpopupdata&title=foobar'
105                => 'apihelp-collection+getpopupdata-example'
106        ];
107    }
108}