API * Extra profiling for allpages * better help output
[lhc/web/wiklou.git] / includes / api / ApiQueryAllpages.php
1 <?php
2
3
4 /*
5 * Created on Sep 25, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 class ApiQueryAllpages extends ApiQueryGeneratorBase {
33
34 public function __construct($query, $moduleName) {
35 parent :: __construct($query, $moduleName, 'ap');
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator($resultPageSet) {
43 if ($resultPageSet->isResolvingRedirects())
44 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
45
46 $this->run($resultPageSet);
47 }
48
49 private function run($resultPageSet = null) {
50
51 wfProfileIn($this->getModuleProfileName() . '-getDB');
52 $db = $this->getDB();
53 wfProfileOut($this->getModuleProfileName() . '-getDB');
54
55 wfProfileIn($this->getModuleProfileName() . '-parseParams');
56 $limit = $from = $namespace = $filterredir = null;
57 extract($this->extractRequestParams());
58
59 $this->addTables('page');
60 if( !$this->addWhereIf('page_is_redirect = 1', $filterredir === 'redirects'))
61 $this->addWhereIf('page_is_redirect = 0', $filterredir === 'nonredirects');
62 $this->addWhereFld('page_namespace', $namespace);
63 if (isset ($from))
64 $this->addWhere('page_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($from)));
65 if (isset ($prefix))
66 $this->addWhere("page_title LIKE '{$db->strencode(ApiQueryBase :: titleToKey($prefix))}%'");
67
68 if (is_null($resultPageSet)) {
69 $this->addFields( array (
70 'page_id',
71 'page_namespace',
72 'page_title'
73 ));
74 } else {
75 $this->addFields( $resultPageSet->getPageTableFields());
76 }
77
78 $this->addOption( 'USE INDEX', 'name_title');
79 $this->addOption( 'LIMIT', $limit +1);
80 $this->addOption( 'ORDER BY', 'page_namespace, page_title');
81
82 $data = array ();
83 $count = 0;
84
85 wfProfileOut($this->getModuleProfileName() . '-parseParams');
86
87 $res = $this->select(__METHOD__);
88
89 wfProfileIn($this->getModuleProfileName() . '-saveResults');
90
91 while ($row = $db->fetchObject($res)) {
92 if (++ $count > $limit) {
93 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
94 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->page_title));
95 break;
96 }
97
98 if (is_null($resultPageSet)) {
99 $vals = $this->addRowInfo('page', $row);
100 if($vals)
101 $data[intval($row->page_id)] = $vals;
102 } else {
103 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
104 // skip any pages that user has no rights to read
105 if ($title->userCanRead()) {
106 $resultPageSet->processDbRow($row);
107 }
108 }
109 }
110 $db->freeResult($res);
111
112 if (is_null($resultPageSet)) {
113 $result = $this->getResult();
114 $result->setIndexedTagName($data, 'p');
115 $result->addValue('query', $this->getModuleName(), $data);
116 }
117
118 wfProfileOut($this->getModuleProfileName() . '-saveResults');
119 }
120
121 protected function getAllowedParams() {
122 $namespaces = $this->getQuery()->getValidNamespaces();
123 return array (
124 'from' => null,
125 'prefix' => null,
126 'namespace' => array (
127 ApiBase :: PARAM_DFLT => 0,
128 ApiBase :: PARAM_TYPE => $namespaces
129 ),
130 'filterredir' => array (
131 ApiBase :: PARAM_DFLT => 'all',
132 ApiBase :: PARAM_TYPE => array (
133 'all',
134 'redirects',
135 'nonredirects'
136 )
137 ),
138 'limit' => array (
139 ApiBase :: PARAM_DFLT => 10,
140 ApiBase :: PARAM_TYPE => 'limit',
141 ApiBase :: PARAM_MIN => 1,
142 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
143 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
144 )
145 );
146 }
147
148 protected function getParamDescription() {
149 return array (
150 'from' => 'The page title to start enumerating from.',
151 'prefix' => 'Search for all page titles that begin with this value.',
152 'namespace' => 'The namespace to enumerate.',
153 'filterredir' => 'Which pages to list.',
154 'limit' => 'How many total pages to return.'
155 );
156 }
157
158 protected function getDescription() {
159 return 'Enumerate all pages sequentially in a given namespace';
160 }
161
162 protected function getExamples() {
163 return array (
164 'Simple Use',
165 ' Show a list of pages starting at the letter "B"',
166 ' api.php?action=query&list=allpages&apfrom=B',
167 'Using as Generator',
168 ' Show info about 4 pages starting at the letter "T"',
169 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
170 ' Show content of first 2 non-redirect pages begining at "Re"',
171 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
172 );
173 }
174
175 public function getVersion() {
176 return __CLASS__ . ': $Id$';
177 }
178 }
179 ?>