fd9ea9d63a2cc56d41e540c85adb403079c371eb
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to get the results of a QueryPage-based special page
34 *
35 * @ingroup API
36 */
37 class ApiQueryQueryPage extends ApiQueryGeneratorBase {
38 private $qpMap;
39
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
43 // This SUCKS
44 global $IP;
45 require_once( "$IP/includes/QueryPage.php" );
46
47 // Build mapping from special page names to QueryPage classes
48 global $wgQueryPages;
49 $this->qpMap = array();
50 foreach ( $wgQueryPages as $page ) {
51 $this->qpMap[$page[1]] = $page[0];
52 }
53 }
54
55 public function execute() {
56 $this->run();
57 }
58
59 public function executeGenerator( $resultPageSet ) {
60 $this->run( $resultPageSet );
61 }
62
63 public function run( $resultPageSet = null ) {
64 global $wgUser;
65 $params = $this->extractRequestParams();
66 $result = $this->getResult();
67
68 $qp = new $this->qpMap[$params['page']]();
69 if ( !$qp->userCanExecute( $wgUser ) ) {
70 $this->dieUsageMsg( array( 'specialpage-cantexecute' ) );
71 }
72
73 $r = array( 'name' => $params['page'] );
74 if ( $qp->isCached() ) {
75 if ( !$qp->isCacheable() ) {
76 $r['disabled'] = '';
77 } else {
78 $r['cached'] = '';
79 $ts = $qp->getCachedTimestamp();
80 if ( $ts ) {
81 $r['cachedtimestamp'] = wfTimestamp( TS_ISO_8601, $ts );
82 }
83 }
84 }
85 $result->addValue( array( 'query' ), $this->getModuleName(), $r );
86
87 $res = $qp->doQuery( $params['limit'] + 1, $params['offset'] );
88 $count = 0;
89 $titles = array();
90 foreach ( $res as $row ) {
91 if ( ++$count > $params['limit'] ) {
92 // We've had enough
93 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
94 break;
95 }
96
97 $title = Title::makeTitle( $row->namespace, $row->title );
98 if ( is_null( $resultPageSet ) ) {
99 $data = array( 'value' => $row->value );
100 if ( $qp->usesTimestamps() ) {
101 $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value );
102 }
103 self::addTitleInfo( $data, $title );
104
105 foreach ( $row as $field => $value ) {
106 if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) {
107 $data['databaseResult'][$field] = $value;
108 }
109 }
110
111 $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data );
112 if ( !$fit ) {
113 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
114 break;
115 }
116 } else {
117 $titles[] = $title;
118 }
119 }
120 if ( is_null( $resultPageSet ) ) {
121 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' );
122 } else {
123 $resultPageSet->populateFromTitles( $titles );
124 }
125 }
126
127 public function getCacheMode( $params ) {
128 $qp = new $this->qpMap[$params['page']]();
129 if ( $qp->getRestriction() != '' ) {
130 return 'private';
131 }
132 return 'public';
133 }
134
135 public function getAllowedParams() {
136 return array(
137 'page' => array(
138 ApiBase::PARAM_TYPE => array_keys( $this->qpMap ),
139 ApiBase::PARAM_REQUIRED => true
140 ),
141 'offset' => 0,
142 'limit' => array(
143 ApiBase::PARAM_DFLT => 10,
144 ApiBase::PARAM_TYPE => 'limit',
145 ApiBase::PARAM_MIN => 1,
146 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
147 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
148 ),
149 );
150 }
151
152 public function getParamDescription() {
153 return array(
154 'page' => 'The name of the special page. Note, this is case sensitive',
155 'offset' => 'When more results are available, use this to continue',
156 'limit' => 'Number of results to return',
157 );
158 }
159
160 public function getDescription() {
161 return 'Get a list provide by a QueryPage-based special page';
162 }
163
164 public function getPossibleErrors() {
165 return array_merge( parent::getPossibleErrors(), array(
166 ) );
167 }
168
169 protected function getExamples() {
170 return array(
171 'api.php?action=query&list=querypages&qppage=Ancientpages'
172 );
173 }
174
175 public function getVersion() {
176 return __CLASS__ . ': $Id$';
177 }
178 }