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 MediaWiki\Api\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    private TitleFactory $titleFactory;
36    private WikiPageFactory $wikiPageFactory;
37
38    public function __construct(
39        ApiBase $parent,
40        string $action,
41        TitleFactory $titleFactory,
42        WikiPageFactory $wikiPageFactory
43    ) {
44        parent::__construct( $parent->getMain(), $action );
45        $this->parent = $parent;
46        $this->titleFactory = $titleFactory;
47        $this->wikiPageFactory = $wikiPageFactory;
48    }
49
50    /**
51     * execute the API request
52     */
53    public function execute() {
54        [ 'title' => $title ] = $this->extractRequestParams();
55
56        $extensionAssetsPath = $this->getConfig()->get( 'ExtensionAssetsPath' );
57        $result = [];
58        $imagePath = "$extensionAssetsPath/Collection/images";
59        $t = $this->titleFactory->newFromText( $title );
60        if ( $t && $t->isRedirect() ) {
61            $wikiPage = $this->wikiPageFactory->newFromTitle( $t );
62            $t = $wikiPage->followRedirect();
63            if ( $t instanceof Title ) {
64                $title = $t->getPrefixedText();
65            }
66        }
67        if ( Session::findArticle( $title ) == -1 ) {
68            $result['action'] = 'add';
69            $result['text'] = $this->msg( 'coll-add_linked_article' )->text();
70            $result['img'] = "$imagePath/silk-add.png";
71        } else {
72            $result['action'] = 'remove';
73            $result['text'] = $this->msg( 'coll-remove_linked_article' )->text();
74            $result['img'] = "$imagePath/silk-remove.png";
75        }
76        $result['title'] = $title;
77
78        $this->getResult()->addValue( null, $this->getModuleName(), $result );
79    }
80
81    /** @inheritDoc */
82    public function getAllowedParams() {
83        return [
84            'title' => [
85                ParamValidator::PARAM_TYPE => 'string',
86                ParamValidator::PARAM_REQUIRED => true,
87            ]
88        ];
89    }
90
91    /**
92     * @return array examples of the use of this API module
93     */
94    protected function getExamplesMessages(): array {
95        return [
96            'action=collection&submodule=getpopupdata&title=foobar'
97                => 'apihelp-collection+getpopupdata-example'
98        ];
99    }
100}