Merge "Document $mDb"
[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 /**
36 * Some query pages are useless because they're available elsewhere in the API
37 */
38 private $uselessQueryPages = array(
39 'MIMEsearch', // aiprop=mime
40 'LinkSearch', // list=exturlusage
41 'FileDuplicateSearch', // prop=duplicatefiles
42 );
43
44 public function __construct( $query, $moduleName ) {
45 parent::__construct( $query, $moduleName, 'qp' );
46 // We need to do this to make sure $wgQueryPages is set up
47 // This SUCKS
48 global $IP;
49 require_once( "$IP/includes/QueryPage.php" );
50
51 // Build mapping from special page names to QueryPage classes
52 global $wgQueryPages;
53 $this->qpMap = array();
54 foreach ( $wgQueryPages as $page ) {
55 if( !in_array( $page[1], $this->uselessQueryPages ) ) {
56 $this->qpMap[$page[1]] = $page[0];
57 }
58 }
59 }
60
61 public function execute() {
62 $this->run();
63 }
64
65 public function executeGenerator( $resultPageSet ) {
66 $this->run( $resultPageSet );
67 }
68
69 /**
70 * @param $resultPageSet ApiPageSet
71 */
72 public function run( $resultPageSet = null ) {
73 $params = $this->extractRequestParams();
74 $result = $this->getResult();
75
76 $qp = new $this->qpMap[$params['page']]();
77 if ( !$qp->userCanExecute( $this->getUser() ) ) {
78 $this->dieUsageMsg( 'specialpage-cantexecute' );
79 }
80
81 $r = array( 'name' => $params['page'] );
82 if ( $qp->isCached() ) {
83 if ( !$qp->isCacheable() ) {
84 $r['disabled'] = '';
85 } else {
86 $r['cached'] = '';
87 $ts = $qp->getCachedTimestamp();
88 if ( $ts ) {
89 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
90 }
91 }
92 }
93 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
94
95 if ( $qp->isCached() && !$qp->isCacheable() ) {
96 // Disabled query page, don't run the query
97 return;
98 }
99
100 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
101 $count = 0;
102 $titles = array();
103 foreach ( $res as $row ) {
104 if ( ++$count > $params['limit'] ) {
105 // We've had enough
106 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
107 break;
108 }
109
110 $title = Title::makeTitle( $row->namespace, $row->title );
111 if ( is_null( $resultPageSet ) ) {
112 $data = array( 'value' => $row->value );
113 if ( $qp->usesTimestamps() ) {
114 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
115 }
116 self::addTitleInfo( $data, $title );
117
118 foreach ( $row as $field => $value ) {
119 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
120 $data['databaseResult'][$field] = $value;
121 }
122 }
123
124 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
125 if ( !$fit ) {
126 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
127 break;
128 }
129 } else {
130 $titles[] = $title;
131 }
132 }
133 if ( is_null( $resultPageSet ) ) {
134 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
135 } else {
136 $resultPageSet->populateFromTitles( $titles );
137 }
138 }
139
140 public function getCacheMode( $params ) {
141 $qp = new $this->qpMap[$params['page']]();
142 if ( $qp->getRestriction() != '' ) {
143 return 'private';
144 }
145 return 'public';
146 }
147
148 public function getAllowedParams() {
149 return array(
150 'page' => array(
151 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
152 ApiBase::PARAM_REQUIRED => true
153 ),
154 'offset' => 0,
155 'limit' => array(
156 ApiBase::PARAM_DFLT => 10,
157 ApiBase::PARAM_TYPE => 'limit',
158 ApiBase::PARAM_MIN => 1,
159 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
160 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
161 ),
162 );
163 }
164
165 public function getParamDescription() {
166 return array(
167 'page' => 'The name of the special page. Note, this is case sensitive',
168 'offset' => 'When more results are available, use this to continue',
169 'limit' => 'Number of results to return',
170 );
171 }
172
173 public function getDescription() {
174 return 'Get a list provided by a QueryPage-based special page';
175 }
176
177 public function getPossibleErrors() {
178 return array_merge( parent::getPossibleErrors(), array(
179 array( 'specialpage-cantexecute' )
180 ) );
181 }
182
183 public function getExamples() {
184 return array(
185 'api.php?action=query&list=querypage&qppage=Ancientpages'
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }