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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to get the results of a QueryPage-based special page
37 class ApiQueryQueryPage
extends ApiQueryGeneratorBase
{
40 public function __construct( $query, $moduleName ) {
41 parent
::__construct( $query, $moduleName, 'qp' );
42 // We need to do this to make sure $wgQueryPages is set up
45 require_once( "$IP/includes/QueryPage.php" );
47 // Build mapping from special page names to QueryPage classes
49 $this->qpMap
= array();
50 foreach ( $wgQueryPages as $page ) {
51 $this->qpMap
[$page[1]] = $page[0];
55 public function execute() {
59 public function executeGenerator( $resultPageSet ) {
60 $this->run( $resultPageSet );
64 * @param $resultPageSet ApiPageSet
67 public function run( $resultPageSet = null ) {
69 $params = $this->extractRequestParams();
70 $result = $this->getResult();
72 $qp = new $this->qpMap
[$params['page']]();
73 if ( !$qp->userCanExecute( $wgUser ) ) {
74 $this->dieUsageMsg( array( 'specialpage-cantexecute' ) );
77 $r = array( 'name' => $params['page'] );
78 if ( $qp->isCached() ) {
79 if ( !$qp->isCacheable() ) {
83 $ts = $qp->getCachedTimestamp();
85 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601
, $ts );
89 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
91 $res = $qp->doQuery( $params['limit'] +
1, $params['offset'] );
94 foreach ( $res as $row ) {
95 if ( ++
$count > $params['limit'] ) {
97 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
101 $title = Title
::makeTitle( $row->namespace, $row->title
);
102 if ( is_null( $resultPageSet ) ) {
103 $data = array( 'value' => $row->value
);
104 if ( $qp->usesTimestamps() ) {
105 $data['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->value
);
107 self
::addTitleInfo( $data, $title );
109 foreach ( $row as $field => $value ) {
110 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
111 $data['databaseResult'][$field] = $value;
115 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
117 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
124 if ( is_null( $resultPageSet ) ) {
125 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
127 $resultPageSet->populateFromTitles( $titles );
131 public function getCacheMode( $params ) {
132 $qp = new $this->qpMap
[$params['page']]();
133 if ( $qp->getRestriction() != '' ) {
139 public function getAllowedParams() {
142 ApiBase
::PARAM_TYPE
=> array_keys( $this->qpMap
),
143 ApiBase
::PARAM_REQUIRED
=> true
147 ApiBase
::PARAM_DFLT
=> 10,
148 ApiBase
::PARAM_TYPE
=> 'limit',
149 ApiBase
::PARAM_MIN
=> 1,
150 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
151 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
156 public function getParamDescription() {
158 'page' => 'The name of the special page. Note, this is case sensitive',
159 'offset' => 'When more results are available, use this to continue',
160 'limit' => 'Number of results to return',
164 public function getDescription() {
165 return 'Get a list provide by a QueryPage-based special page';
168 public function getPossibleErrors() {
169 return array_merge( parent
::getPossibleErrors(), array(
173 protected function getExamples() {
175 'api.php?action=query&list=querypage&qppage=Ancientpages'
179 public function getVersion() {
180 return __CLASS__
. ': $Id$';