Merge "Fix sessionfailure i18n message during authentication"
[lhc/web/wiklou.git] / includes / api / ApiQueryQueryPage.php
1 <?php
2 /**
3 * Copyright © 2010 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 * Query module to get the results of a QueryPage-based special page
25 *
26 * @ingroup API
27 */
28 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
29 private $qpMap;
30
31 public function __construct( ApiQuery $query, $moduleName ) {
32 parent::__construct( $query, $moduleName, 'qp' );
33 // Build mapping from special page names to QueryPage classes
34 $uselessQueryPages = $this->getConfig()->get( 'APIUselessQueryPages' );
35 $this->qpMap = [];
36 foreach ( QueryPage::getPages() as $page ) {
37 if ( !in_array( $page[1], $uselessQueryPages ) ) {
38 $this->qpMap[$page[1]] = $page[0];
39 }
40 }
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 /**
52 * @param ApiPageSet $resultPageSet
53 */
54 public function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56 $result = $this->getResult();
57
58 /** @var QueryPage $qp */
59 $qp = new $this->qpMap[$params['page']]();
60 if ( !$qp->userCanExecute( $this->getUser() ) ) {
61 $this->dieWithError( 'apierror-specialpage-cantexecute' );
62 }
63
64 $r = [ 'name' => $params['page'] ];
65 if ( $qp->isCached() ) {
66 if ( !$qp->isCacheable() ) {
67 $r['disabled'] = true;
68 } else {
69 $r['cached'] = true;
70 $ts = $qp->getCachedTimestamp();
71 if ( $ts ) {
72 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
73 }
74 $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
75 }
76 }
77 $result->addValue( [ 'query' ], $this->getModuleName(), $r );
78
79 if ( $qp->isCached() && !$qp->isCacheable() ) {
80 // Disabled query page, don't run the query
81 return;
82 }
83
84 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
85 $count = 0;
86 $titles = [];
87 foreach ( $res as $row ) {
88 if ( ++$count > $params['limit'] ) {
89 // We've had enough
90 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
91 break;
92 }
93
94 $title = Title::makeTitle( $row->namespace, $row->title );
95 if ( is_null( $resultPageSet ) ) {
96 $data = [ 'value' => $row->value ];
97 if ( $qp->usesTimestamps() ) {
98 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
99 }
100 self::addTitleInfo( $data, $title );
101
102 foreach ( $row as $field => $value ) {
103 if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
104 $data['databaseResult'][$field] = $value;
105 }
106 }
107
108 $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
109 if ( !$fit ) {
110 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
111 break;
112 }
113 } else {
114 $titles[] = $title;
115 }
116 }
117 if ( is_null( $resultPageSet ) ) {
118 $result->addIndexedTagName(
119 [ 'query', $this->getModuleName(), 'results' ],
120 'page'
121 );
122 } else {
123 $resultPageSet->populateFromTitles( $titles );
124 }
125 }
126
127 public function getCacheMode( $params ) {
128 /** @var QueryPage $qp */
129 $qp = new $this->qpMap[$params['page']]();
130 if ( $qp->getRestriction() != '' ) {
131 return 'private';
132 }
133
134 return 'public';
135 }
136
137 public function getAllowedParams() {
138 return [
139 'page' => [
140 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
141 ApiBase::PARAM_REQUIRED => true
142 ],
143 'offset' => [
144 ApiBase::PARAM_DFLT => 0,
145 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
146 ],
147 'limit' => [
148 ApiBase::PARAM_DFLT => 10,
149 ApiBase::PARAM_TYPE => 'limit',
150 ApiBase::PARAM_MIN => 1,
151 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
152 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
153 ],
154 ];
155 }
156
157 protected function getExamplesMessages() {
158 return [
159 'action=query&list=querypage&qppage=Ancientpages'
160 => 'apihelp-query+querypage-example-ancientpages',
161 ];
162 }
163
164 public function getHelpUrls() {
165 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Querypage';
166 }
167 }