Followup to r86053 - fix special page cases
[mediawiki.git] / includes / api / ApiQueryQueryPage.php
blob1414ad512d03f0577ae6a995c18ad378bcdbd16d
1 <?php
2 /**
5 * Created on Dec 22, 2010
7 * Copyright © 2010 Roan Kattouw <Firstname>.<Lastname>@gmail.com
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.
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.
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
24 * @file
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
32 /**
33 * Query module to get the results of a QueryPage-based special page
35 * @ingroup API
37 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
38 private $qpMap;
40 /**
41 * Some query pages are useless because they're available elsewhere in the API
43 private $uselessQueryPages = array(
44 'MIMEsearch', // aiprop=mime
45 'LinkSearch', // list=exturlusage
46 'FileDuplicateSearch', // prop=duplicatefiles
49 public function __construct( $query, $moduleName ) {
50 parent::__construct( $query, $moduleName, 'qp' );
51 // We need to do this to make sure $wgQueryPages is set up
52 // This SUCKS
53 global $IP;
54 require_once( "$IP/includes/QueryPage.php" );
56 // Build mapping from special page names to QueryPage classes
57 global $wgQueryPages;
58 $this->qpMap = array();
59 foreach ( $wgQueryPages as $page ) {
60 if( !in_array( $page[1], $this->uselessQueryPages ) ) {
61 $this->qpMap[$page[1]] = $page[0];
66 public function execute() {
67 $this->run();
70 public function executeGenerator( $resultPageSet ) {
71 $this->run( $resultPageSet );
74 /**
75 * @param $resultPageSet ApiPageSet
77 public function run( $resultPageSet = null ) {
78 $params = $this->extractRequestParams();
79 $result = $this->getResult();
81 $qp = new $this->qpMap[$params['page']]();
82 if ( !$qp->userCanExecute( $this->getUser() ) ) {
83 $this->dieUsageMsg( 'specialpage-cantexecute' );
86 $r = array( 'name' => $params['page'] );
87 if ( $qp->isCached() ) {
88 if ( !$qp->isCacheable() ) {
89 $r['disabled'] = '';
90 } else {
91 $r['cached'] = '';
92 $ts = $qp->getCachedTimestamp();
93 if ( $ts ) {
94 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
98 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
100 if ( $qp->isCached() && !$qp->isCacheable() ) {
101 // Disabled query page, don't run the query
102 return;
105 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
106 $count = 0;
107 $titles = array();
108 foreach ( $res as $row ) {
109 if ( ++$count > $params['limit'] ) {
110 // We've had enough
111 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
112 break;
115 $title = Title::makeTitle( $row->namespace, $row->title );
116 if ( is_null( $resultPageSet ) ) {
117 $data = array( 'value' => $row->value );
118 if ( $qp->usesTimestamps() ) {
119 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
121 self::addTitleInfo( $data, $title );
123 foreach ( $row as $field => $value ) {
124 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
125 $data['databaseResult'][$field] = $value;
129 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
130 if ( !$fit ) {
131 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
132 break;
134 } else {
135 $titles[] = $title;
138 if ( is_null( $resultPageSet ) ) {
139 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
140 } else {
141 $resultPageSet->populateFromTitles( $titles );
145 public function getCacheMode( $params ) {
146 $qp = new $this->qpMap[$params['page']]();
147 if ( $qp->getRestriction() != '' ) {
148 return 'private';
150 return 'public';
153 public function getAllowedParams() {
154 return array(
155 'page' => array(
156 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
157 ApiBase::PARAM_REQUIRED => true
159 'offset' => 0,
160 'limit' => array(
161 ApiBase::PARAM_DFLT => 10,
162 ApiBase::PARAM_TYPE => 'limit',
163 ApiBase::PARAM_MIN => 1,
164 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
165 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
170 public function getParamDescription() {
171 return array(
172 'page' => 'The name of the special page. Note, this is case sensitive',
173 'offset' => 'When more results are available, use this to continue',
174 'limit' => 'Number of results to return',
178 public function getDescription() {
179 return 'Get a list provided by a QueryPage-based special page';
182 public function getPossibleErrors() {
183 return array_merge( parent::getPossibleErrors(), array(
184 array( 'specialpage-cantexecute' )
185 ) );
188 public function getExamples() {
189 return array(
190 'api.php?action=query&list=querypage&qppage=Ancientpages'
194 public function getVersion() {
195 return __CLASS__ . ': $Id$';