Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
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
23namespace MediaWiki\Api;
24
25use MediaWiki\Page\PageProps;
26use MediaWiki\Title\Title;
27use Wikimedia\ParamValidator\ParamValidator;
28
29/**
30 * A query module to show basic page information.
31 *
32 * @ingroup API
33 */
34class ApiQueryPageProps extends ApiQueryBase {
35
36    private PageProps $pageProps;
37
38    public function __construct(
39        ApiQuery $query,
40        string $moduleName,
41        PageProps $pageProps
42    ) {
43        parent::__construct( $query, $moduleName, 'pp' );
44        $this->pageProps = $pageProps;
45    }
46
47    public function execute() {
48        # Only operate on existing pages
49        $pages = $this->getPageSet()->getGoodPages();
50
51        $params = $this->extractRequestParams();
52        if ( $params['continue'] ) {
53            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int' ] );
54            $continueValue = $cont[0];
55            $filteredPages = [];
56            foreach ( $pages as $id => $page ) {
57                if ( $id >= $continueValue ) {
58                    $filteredPages[$id] = $page;
59                }
60            }
61            $pages = $filteredPages;
62        }
63
64        if ( $pages === [] ) {
65            # Nothing to do
66            return;
67        }
68
69        if ( $params['prop'] ) {
70            $properties = $this->pageProps->getProperties( $pages, $params['prop'] );
71        } else {
72            $properties = $this->pageProps->getAllProperties( $pages );
73        }
74
75        ksort( $properties );
76
77        $result = $this->getResult();
78        foreach ( $properties as $pageid => $props ) {
79            if ( !$this->addPageProps( $result, $pageid, $props ) ) {
80                break;
81            }
82        }
83    }
84
85    /**
86     * Add page properties to an ApiResult, adding a continue
87     * parameter if it doesn't fit.
88     *
89     * @param ApiResult $result
90     * @param int $page
91     * @param array $props
92     * @return bool True if it fits in the result
93     */
94    private function addPageProps( $result, $page, $props ) {
95        ApiResult::setArrayType( $props, 'assoc' );
96        $fit = $result->addValue( [ 'query', 'pages', $page ], 'pageprops', $props );
97
98        if ( !$fit ) {
99            $this->setContinueEnumParameter( 'continue', $page );
100        }
101
102        return $fit;
103    }
104
105    public function getCacheMode( $params ) {
106        return 'public';
107    }
108
109    public function getAllowedParams() {
110        return [
111            'continue' => [
112                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
113            ],
114            'prop' => [
115                ParamValidator::PARAM_ISMULTI => true,
116            ],
117        ];
118    }
119
120    protected function getExamplesMessages() {
121        $title = Title::newMainPage()->getPrefixedText();
122        $mp = rawurlencode( $title );
123
124        return [
125            "action=query&prop=pageprops&titles={$mp}|MediaWiki"
126                => 'apihelp-query+pageprops-example-simple',
127        ];
128    }
129
130    public function getHelpUrls() {
131        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageprops';
132    }
133}
134
135/** @deprecated class alias since 1.43 */
136class_alias( ApiQueryPageProps::class, 'ApiQueryPageProps' );