Merge "Revert "selenium: add new message banner test to user spec""
[lhc/web/wiklou.git] / includes / api / ApiQueryPageProps.php
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
23 /**
24 * A query module to show basic page information.
25 *
26 * @ingroup API
27 */
28 class ApiQueryPageProps extends ApiQueryBase {
29
30 private $params;
31
32 public function __construct( ApiQuery $query, $moduleName ) {
33 parent::__construct( $query, $moduleName, 'pp' );
34 }
35
36 public function execute() {
37 # Only operate on existing pages
38 $pages = $this->getPageSet()->getGoodTitles();
39
40 $this->params = $this->extractRequestParams();
41 if ( $this->params['continue'] ) {
42 $continueValue = intval( $this->params['continue'] );
43 $this->dieContinueUsageIf( strval( $continueValue ) !== $this->params['continue'] );
44 $filteredPages = [];
45 foreach ( $pages as $id => $page ) {
46 if ( $id >= $continueValue ) {
47 $filteredPages[$id] = $page;
48 }
49 }
50 $pages = $filteredPages;
51 }
52
53 if ( !count( $pages ) ) {
54 # Nothing to do
55 return;
56 }
57
58 $pageProps = PageProps::getInstance();
59 $result = $this->getResult();
60 if ( $this->params['prop'] ) {
61 $propnames = $this->params['prop'];
62 $properties = $pageProps->getProperties( $pages, $propnames );
63 } else {
64 $properties = $pageProps->getAllProperties( $pages );
65 }
66
67 ksort( $properties );
68
69 foreach ( $properties as $page => $props ) {
70 if ( !$this->addPageProps( $result, $page, $props ) ) {
71 break;
72 }
73 }
74 }
75
76 /**
77 * Add page properties to an ApiResult, adding a continue
78 * parameter if it doesn't fit.
79 *
80 * @param ApiResult $result
81 * @param int $page
82 * @param array $props
83 * @return bool True if it fits in the result
84 */
85 private function addPageProps( $result, $page, $props ) {
86 ApiResult::setArrayType( $props, 'assoc' );
87 $fit = $result->addValue( [ 'query', 'pages', $page ], 'pageprops', $props );
88
89 if ( !$fit ) {
90 $this->setContinueEnumParameter( 'continue', $page );
91 }
92
93 return $fit;
94 }
95
96 public function getCacheMode( $params ) {
97 return 'public';
98 }
99
100 public function getAllowedParams() {
101 return [
102 'continue' => [
103 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
104 ],
105 'prop' => [
106 ApiBase::PARAM_ISMULTI => true,
107 ],
108 ];
109 }
110
111 protected function getExamplesMessages() {
112 return [
113 'action=query&prop=pageprops&titles=Main%20Page|MediaWiki'
114 => 'apihelp-query+pageprops-example-simple',
115 ];
116 }
117
118 public function getHelpUrls() {
119 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageprops';
120 }
121 }