Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryPageProps
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 7
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
72
 addPageProps
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 6
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/**
3 * Copyright © 2010 soxred93, Bryan Tong Minh
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23use MediaWiki\Page\PageProps;
24use MediaWiki\Title\Title;
25use Wikimedia\ParamValidator\ParamValidator;
26
27/**
28 * A query module to show basic page information.
29 *
30 * @ingroup API
31 */
32class ApiQueryPageProps extends ApiQueryBase {
33
34    private PageProps $pageProps;
35
36    /**
37     * @param ApiQuery $query
38     * @param string $moduleName
39     * @param PageProps $pageProps
40     */
41    public function __construct(
42        ApiQuery $query,
43        $moduleName,
44        PageProps $pageProps
45    ) {
46        parent::__construct( $query, $moduleName, 'pp' );
47        $this->pageProps = $pageProps;
48    }
49
50    public function execute() {
51        # Only operate on existing pages
52        $pages = $this->getPageSet()->getGoodPages();
53
54        $params = $this->extractRequestParams();
55        if ( $params['continue'] ) {
56            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
57            $continueValue = $cont[0];
58            $filteredPages = [];
59            foreach ( $pages as $id => $page ) {
60                if ( $id >= $continueValue ) {
61                    $filteredPages[$id] = $page;
62                }
63            }
64            $pages = $filteredPages;
65        }
66
67        if ( $pages === [] ) {
68            # Nothing to do
69            return;
70        }
71
72        if ( $params['prop'] ) {
73            $properties = $this->pageProps->getProperties( $pages, $params['prop'] );
74        } else {
75            $properties = $this->pageProps->getAllProperties( $pages );
76        }
77
78        ksort( $properties );
79
80        $result = $this->getResult();
81        foreach ( $properties as $pageid => $props ) {
82            if ( !$this->addPageProps( $result, $pageid, $props ) ) {
83                break;
84            }
85        }
86    }
87
88    /**
89     * Add page properties to an ApiResult, adding a continue
90     * parameter if it doesn't fit.
91     *
92     * @param ApiResult $result
93     * @param int $page
94     * @param array $props
95     * @return bool True if it fits in the result
96     */
97    private function addPageProps( $result, $page, $props ) {
98        ApiResult::setArrayType( $props, 'assoc' );
99        $fit = $result->addValue( [ 'query', 'pages', $page ], 'pageprops', $props );
100
101        if ( !$fit ) {
102            $this->setContinueEnumParameter( 'continue', $page );
103        }
104
105        return $fit;
106    }
107
108    public function getCacheMode( $params ) {
109        return 'public';
110    }
111
112    public function getAllowedParams() {
113        return [
114            'continue' => [
115                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
116            ],
117            'prop' => [
118                ParamValidator::PARAM_ISMULTI => true,
119            ],
120        ];
121    }
122
123    protected function getExamplesMessages() {
124        $title = Title::newMainPage()->getPrefixedText();
125        $mp = rawurlencode( $title );
126
127        return [
128            "action=query&prop=pageprops&titles={$mp}|MediaWiki"
129                => 'apihelp-query+pageprops-example-simple',
130        ];
131    }
132
133    public function getHelpUrls() {
134        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageprops';
135    }
136}