Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / api / ApiQueryQueryPage.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 22, 2010
6 *
7 * Copyright © 2010 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to get the results of a QueryPage-based special page
29 *
30 * @ingroup API
31 */
32 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
33 private $qpMap;
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'qp' );
37 // Build mapping from special page names to QueryPage classes
38 $uselessQueryPages = $this->getConfig()->get( 'APIUselessQueryPages' );
39 $this->qpMap = [];
40 foreach ( QueryPage::getPages() as $page ) {
41 if ( !in_array( $page[1], $uselessQueryPages ) ) {
42 $this->qpMap[$page[1]] = $page[0];
43 }
44 }
45 }
46
47 public function execute() {
48 $this->run();
49 }
50
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
53 }
54
55 /**
56 * @param ApiPageSet $resultPageSet
57 */
58 public function run( $resultPageSet = null ) {
59 $params = $this->extractRequestParams();
60 $result = $this->getResult();
61
62 /** @var $qp QueryPage */
63 $qp = new $this->qpMap[$params['page']]();
64 if ( !$qp->userCanExecute( $this->getUser() ) ) {
65 $this->dieUsageMsg( 'specialpage-cantexecute' );
66 }
67
68 $r = [ 'name' => $params['page'] ];
69 if ( $qp->isCached() ) {
70 if ( !$qp->isCacheable() ) {
71 $r['disabled'] = true;
72 } else {
73 $r['cached'] = true;
74 $ts = $qp->getCachedTimestamp();
75 if ( $ts ) {
76 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
77 }
78 $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
79 }
80 }
81 $result->addValue( [ 'query' ], $this->getModuleName(), $r );
82
83 if ( $qp->isCached() && !$qp->isCacheable() ) {
84 // Disabled query page, don't run the query
85 return;
86 }
87
88 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
89 $count = 0;
90 $titles = [];
91 foreach ( $res as $row ) {
92 if ( ++$count > $params['limit'] ) {
93 // We've had enough
94 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
95 break;
96 }
97
98 $title = Title::makeTitle( $row->namespace, $row->title );
99 if ( is_null( $resultPageSet ) ) {
100 $data = [ 'value' => $row->value ];
101 if ( $qp->usesTimestamps() ) {
102 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
103 }
104 self::addTitleInfo( $data, $title );
105
106 foreach ( $row as $field => $value ) {
107 if ( !in_array( $field, [ 'namespace', 'title', 'value', 'qc_type' ] ) ) {
108 $data['databaseResult'][$field] = $value;
109 }
110 }
111
112 $fit = $result->addValue( [ 'query', $this->getModuleName(), 'results' ], null, $data );
113 if ( !$fit ) {
114 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
115 break;
116 }
117 } else {
118 $titles[] = $title;
119 }
120 }
121 if ( is_null( $resultPageSet ) ) {
122 $result->addIndexedTagName(
123 [ 'query', $this->getModuleName(), 'results' ],
124 'page'
125 );
126 } else {
127 $resultPageSet->populateFromTitles( $titles );
128 }
129 }
130
131 public function getCacheMode( $params ) {
132 /** @var $qp QueryPage */
133 $qp = new $this->qpMap[$params['page']]();
134 if ( $qp->getRestriction() != '' ) {
135 return 'private';
136 }
137
138 return 'public';
139 }
140
141 public function getAllowedParams() {
142 return [
143 'page' => [
144 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
145 ApiBase::PARAM_REQUIRED => true
146 ],
147 'offset' => [
148 ApiBase::PARAM_DFLT => 0,
149 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
150 ],
151 'limit' => [
152 ApiBase::PARAM_DFLT => 10,
153 ApiBase::PARAM_TYPE => 'limit',
154 ApiBase::PARAM_MIN => 1,
155 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
156 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
157 ],
158 ];
159 }
160
161 protected function getExamplesMessages() {
162 return [
163 'action=query&list=querypage&qppage=Ancientpages'
164 => 'apihelp-query+querypage-example-ancientpages',
165 ];
166 }
167
168 public function getHelpUrls() {
169 return 'https://www.mediawiki.org/wiki/API:Querypage';
170 }
171 }