Swap and for &&
[mediawiki.git] / includes / api / ApiQueryQueryPage.php
blob2da5777ef447b56c320dd667ef6a66e998f4e169
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 /**
28 * Query module to get the results of a QueryPage-based special page
30 * @ingroup API
32 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
33 private $qpMap;
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 = array();
40 foreach ( QueryPage::getPages() as $page ) {
41 if ( !in_array( $page[1], $uselessQueryPages ) ) {
42 $this->qpMap[$page[1]] = $page[0];
47 public function execute() {
48 $this->run();
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
55 /**
56 * @param ApiPageSet $resultPageSet
58 public function run( $resultPageSet = null ) {
59 $params = $this->extractRequestParams();
60 $result = $this->getResult();
62 /** @var $qp QueryPage */
63 $qp = new $this->qpMap[$params['page']]();
64 if ( !$qp->userCanExecute( $this->getUser() ) ) {
65 $this->dieUsageMsg( 'specialpage-cantexecute' );
68 $r = array( 'name' => $params['page'] );
69 if ( $qp->isCached() ) {
70 if ( !$qp->isCacheable() ) {
71 $r['disabled'] = '';
72 } else {
73 $r['cached'] = '';
74 $ts = $qp->getCachedTimestamp();
75 if ( $ts ) {
76 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
78 $r['maxresults'] = $this->getConfig()->get( 'QueryCacheLimit' );
81 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
83 if ( $qp->isCached() && !$qp->isCacheable() ) {
84 // Disabled query page, don't run the query
85 return;
88 $res = $qp->doQuery( $params['offset'], $params['limit'] + 1 );
89 $count = 0;
90 $titles = array();
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;
98 $title = Title::makeTitle( $row->namespace, $row->title );
99 if ( is_null( $resultPageSet ) ) {
100 $data = array( 'value' => $row->value );
101 if ( $qp->usesTimestamps() ) {
102 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
104 self::addTitleInfo( $data, $title );
106 foreach ( $row as $field => $value ) {
107 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
108 $data['databaseResult'][$field] = $value;
112 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
113 if ( !$fit ) {
114 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
115 break;
117 } else {
118 $titles[] = $title;
121 if ( is_null( $resultPageSet ) ) {
122 $result->setIndexedTagName_internal(
123 array( 'query', $this->getModuleName(), 'results' ),
124 'page'
126 } else {
127 $resultPageSet->populateFromTitles( $titles );
131 public function getCacheMode( $params ) {
132 /** @var $qp QueryPage */
133 $qp = new $this->qpMap[$params['page']]();
134 if ( $qp->getRestriction() != '' ) {
135 return 'private';
138 return 'public';
141 public function getAllowedParams() {
142 return array(
143 'page' => array(
144 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
145 ApiBase::PARAM_REQUIRED => true
147 'offset' => array(
148 ApiBase::PARAM_DFLT => 0,
149 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
151 'limit' => array(
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
161 public function getExamplesMessages() {
162 return array(
163 'action=query&list=querypage&qppage=Ancientpages'
164 => 'apihelp-query+querypage-example-ancientpages',
168 public function getHelpUrls() {
169 return 'https://www.mediawiki.org/wiki/API:Querypage';