Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
28 / 35
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryMMContent
80.00% covered (warning)
80.00%
28 / 35
50.00% covered (danger)
50.00%
2 / 4
11.97
0.00% covered (danger)
0.00%
0 / 1
 execute
89.66% covered (warning)
89.66%
26 / 29
0.00% covered (danger)
0.00%
0 / 1
8.07
 getCacheMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllowedParams
100.00% covered (success)
100.00%
1 / 1
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
2declare( strict_types = 1 );
3
4namespace MediaWiki\MassMessage\Api;
5
6use ApiQueryBase;
7use MediaWiki\MassMessage\Content\MassMessageListContent;
8use MediaWiki\MediaWikiServices;
9use MediaWiki\Title\Title;
10
11/**
12 * API module to retrieve the content of a mass message distribution list
13 *
14 * @ingroup API
15 */
16
17class ApiQueryMMContent extends ApiQueryBase {
18
19    public function execute() {
20        $pageSet = $this->getPageSet();
21        $pageids = array_keys( $pageSet->getGoodTitles() );
22        if ( !$pageids ) {
23            return true;
24        }
25
26        $spamlists = [];
27        foreach ( $pageids as $pageid ) {
28            $spamlist = Title::newFromId( $pageid );
29            if ( $spamlist === null
30                || !$spamlist->exists()
31                || !$spamlist->hasContentModel( 'MassMessageListContent' )
32            ) {
33                $this->dieWithError( 'apierror-massmessage-invalidspamlist', 'invalidspamlist' );
34            }
35            $spamlists[ $pageid ] = $spamlist;
36        }
37
38        $result = $this->getResult();
39        $wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory();
40
41        foreach ( $spamlists as $pageid => $spamlist ) {
42            $content = $wikiPageFactory->newFromTitle( $spamlist )->getContent();
43            if ( !$content instanceof MassMessageListContent ) {
44                $this->dieWithError( 'apierror-massmessage-invalidspamlist', 'invalidspamlist' );
45            }
46
47            $result->addValue(
48                [ 'query', 'pages', $pageid, 'mmcontent' ],
49                'description',
50                $content->getDescription()
51            );
52            $result->addValue(
53                [ 'query', 'pages', $pageid, 'mmcontent' ],
54                'targets',
55                $content->getTargetStrings()
56            );
57        }
58        return true;
59    }
60
61    /**
62     * @param array $params
63     * @return string
64     */
65    public function getCacheMode( $params ) {
66        return 'public';
67    }
68
69    /** @inheritDoc */
70    public function getAllowedParams() {
71        return [];
72    }
73
74    /** @inheritDoc */
75    protected function getExamplesMessages() {
76        return [
77            'action=query&prop=info|mmcontent&titles=Spam%20list'
78                => 'apihelp-query+mmcontent-example-1',
79        ];
80    }
81}