Index: trunk/phase3/includes/api/ApiQuery.php |
— | — | @@ -88,6 +88,7 @@ |
89 | 89 | 'users' => 'ApiQueryUsers', |
90 | 90 | 'random' => 'ApiQueryRandom', |
91 | 91 | 'protectedtitles' => 'ApiQueryProtectedTitles', |
| 92 | + 'querypage' => 'ApiQueryQueryPage', |
92 | 93 | ); |
93 | 94 | |
94 | 95 | private $mQueryMetaModules = array( |
Index: trunk/phase3/includes/api/ApiQueryQueryPage.php |
— | — | @@ -0,0 +1,179 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * API for MediaWiki 1.8+ |
| 5 | + * |
| 6 | + * Created on Sep 10, 2007 |
| 7 | + * |
| 8 | + * Copyright © 2010 Roan Kattouw <Firstname>.<Lastname>@gmail.com |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation; either version 2 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License along |
| 21 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | + * http://www.gnu.org/copyleft/gpl.html |
| 24 | + * |
| 25 | + * @file |
| 26 | + */ |
| 27 | + |
| 28 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 29 | + // Eclipse helper - will be ignored in production |
| 30 | + require_once( 'ApiQueryBase.php' ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Query module to get the results of a QueryPage-based special page |
| 35 | + * |
| 36 | + * @ingroup API |
| 37 | + */ |
| 38 | +class ApiQueryQueryPage extends ApiQueryGeneratorBase { |
| 39 | + private $qpMap; |
| 40 | + |
| 41 | + public function __construct( $query, $moduleName ) { |
| 42 | + parent::__construct( $query, $moduleName, 'qp' ); |
| 43 | + |
| 44 | + // We need to do this to make sure $wgQueryPages is set up |
| 45 | + // This SUCKS |
| 46 | + global $IP; |
| 47 | + require_once( "$IP/includes/QueryPage.php" ); |
| 48 | + |
| 49 | + // Build mapping from special page names to QueryPage classes |
| 50 | + global $wgQueryPages; |
| 51 | + $this->qpMap = array(); |
| 52 | + foreach ( $wgQueryPages as $page ) { |
| 53 | + $this->qpMap[$page[1]] = $page[0]; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function execute() { |
| 58 | + $this->run(); |
| 59 | + } |
| 60 | + |
| 61 | + public function executeGenerator( $resultPageSet ) { |
| 62 | + $this->run( $resultPageSet ); |
| 63 | + } |
| 64 | + |
| 65 | + public function run( $resultPageSet = null ) { |
| 66 | + global $wgUser; |
| 67 | + $params = $this->extractRequestParams(); |
| 68 | + $result = $this->getResult(); |
| 69 | + |
| 70 | + $qp = new $this->qpMap[$params['page']](); |
| 71 | + if ( !$qp->userCanExecute( $wgUser ) ) { |
| 72 | + $this->dieUsageMsg( array( 'specialpage-cantexecute' ) ); |
| 73 | + } |
| 74 | + |
| 75 | + $r = array( 'name' => $params['page'] ); |
| 76 | + if ( $qp->isCached() ) { |
| 77 | + if ( !$qp->isCacheable() ) { |
| 78 | + $r['disabled'] = ''; |
| 79 | + } else { |
| 80 | + $r['cached'] = ''; |
| 81 | + $ts = $qp->getCachedTimestamp(); |
| 82 | + if ( $ts ) { |
| 83 | + $r['cachedTimestamp'] = wfTimestamp( TS_ISO_8601, $ts ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + $result->addValue( array( 'query' ), $this->getModuleName(), $r ); |
| 88 | + |
| 89 | + $res = $qp->doQuery( $params['limit'] + 1, $params['offset'] ); |
| 90 | + $count = 0; |
| 91 | + $titles = array(); |
| 92 | + foreach ( $res as $row ) { |
| 93 | + if ( ++$count > $params['limit'] ) { |
| 94 | + // We've had enough |
| 95 | + $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] ); |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + $title = Title::makeTitle( $row->namespace, $row->title ); |
| 100 | + if ( is_null( $resultPageSet ) ) { |
| 101 | + $data = array( 'value' => $row->value ); |
| 102 | + if ( $qp->usesTimestamps() ) { |
| 103 | + $data['timestamp'] = wfTimestamp( TS_ISO_8601, $row->value ); |
| 104 | + } |
| 105 | + self::addTitleInfo( $data, $title ); |
| 106 | + |
| 107 | + foreach ( $row as $field => $value ) { |
| 108 | + if ( !in_array( $field, array( 'namespace', 'title', 'value', 'qc_type' ) ) ) { |
| 109 | + $data['databaseResult'][$field] = $value; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + $fit = $result->addValue( array( 'query', $this->getModuleName(), 'results' ), null, $data ); |
| 114 | + if ( !$fit ) { |
| 115 | + $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 ); |
| 116 | + break; |
| 117 | + } |
| 118 | + } else { |
| 119 | + $titles[] = $title; |
| 120 | + } |
| 121 | + } |
| 122 | + if ( is_null( $resultPageSet ) ) { |
| 123 | + $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'results' ), 'page' ); |
| 124 | + } else { |
| 125 | + $resultPageSet->populateFromTitles( $titles ); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public function getCacheMode( $params ) { |
| 130 | + $qp = new $this->qpMap[$params['page']](); |
| 131 | + if ( $qp->getRestriction() != '' ) { |
| 132 | + return 'private'; |
| 133 | + } |
| 134 | + return 'public'; |
| 135 | + } |
| 136 | + |
| 137 | + public function getAllowedParams() { |
| 138 | + return array( |
| 139 | + 'page' => array( |
| 140 | + ApiBase::PARAM_TYPE => array_keys( $this->qpMap ), |
| 141 | + ApiBase::PARAM_REQUIRED => true |
| 142 | + ), |
| 143 | + 'offset' => 0, |
| 144 | + 'limit' => array( |
| 145 | + ApiBase::PARAM_DFLT => 10, |
| 146 | + ApiBase::PARAM_TYPE => 'limit', |
| 147 | + ApiBase::PARAM_MIN => 1, |
| 148 | + ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 149 | + ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 150 | + ), |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + public function getParamDescription() { |
| 155 | + return array( |
| 156 | + 'page' => 'The name of the special page', |
| 157 | + 'offset' => 'When more results are available, use this to continue', |
| 158 | + 'limit' => 'Number of results to return', |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + public function getDescription() { |
| 163 | + return 'Get a list provide by a QueryPage-based special page'; |
| 164 | + } |
| 165 | + |
| 166 | + public function getPossibleErrors() { |
| 167 | + return array_merge( parent::getPossibleErrors(), array( |
| 168 | + ) ); |
| 169 | + } |
| 170 | + |
| 171 | + protected function getExamples() { |
| 172 | + return array( |
| 173 | + |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + public function getVersion() { |
| 178 | + return __CLASS__ . ': $Id$'; |
| 179 | + } |
| 180 | +} |
Property changes on: trunk/phase3/includes/api/ApiQueryQueryPage.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 181 | + native |
Added: svn:keywords |
2 | 182 | + Id |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -332,6 +332,7 @@ |
333 | 333 | 'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php', |
334 | 334 | 'ApiQueryPageProps' => 'includes/api/ApiQueryPageProps.php', |
335 | 335 | 'ApiQueryProtectedTitles' => 'includes/api/ApiQueryProtectedTitles.php', |
| 336 | + 'ApiQueryQueryPage' => 'includes/api/ApiQueryQueryPage.php', |
336 | 337 | 'ApiQueryRandom' => 'includes/api/ApiQueryRandom.php', |
337 | 338 | 'ApiQueryRecentChanges' => 'includes/api/ApiQueryRecentChanges.php', |
338 | 339 | 'ApiQueryRevisions' => 'includes/api/ApiQueryRevisions.php', |