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