00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00036 class ApiQueryAllpages extends ApiQueryGeneratorBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'ap');
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function getCacheMode( $params ) {
00047 return 'public';
00048 }
00049
00050 public function executeGenerator($resultPageSet) {
00051 if ($resultPageSet->isResolvingRedirects())
00052 $this->dieUsage('Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params');
00053
00054 $this->run($resultPageSet);
00055 }
00056
00057 private function run($resultPageSet = null) {
00058
00059 $db = $this->getDB();
00060
00061 $params = $this->extractRequestParams();
00062
00063
00064 $this->addTables('page');
00065 if (!$this->addWhereIf('page_is_redirect = 1', $params['filterredir'] === 'redirects'))
00066 $this->addWhereIf('page_is_redirect = 0', $params['filterredir'] === 'nonredirects');
00067 $this->addWhereFld('page_namespace', $params['namespace']);
00068 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
00069 $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
00070 $this->addWhereRange('page_title', $dir, $from, null);
00071 if (isset ($params['prefix']))
00072 $this->addWhere("page_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
00073
00074 if (is_null($resultPageSet)) {
00075 $selectFields = array (
00076 'page_namespace',
00077 'page_title',
00078 'page_id'
00079 );
00080 } else {
00081 $selectFields = $resultPageSet->getPageTableFields();
00082 }
00083 $this->addFields($selectFields);
00084 $forceNameTitleIndex = true;
00085 if (isset ($params['minsize'])) {
00086 $this->addWhere('page_len>=' . intval($params['minsize']));
00087 $forceNameTitleIndex = false;
00088 }
00089
00090 if (isset ($params['maxsize'])) {
00091 $this->addWhere('page_len<=' . intval($params['maxsize']));
00092 $forceNameTitleIndex = false;
00093 }
00094
00095
00096 if (!empty ($params['prtype'])) {
00097 $this->addTables('page_restrictions');
00098 $this->addWhere('page_id=pr_page');
00099 $this->addWhere('pr_expiry>' . $db->addQuotes($db->timestamp()));
00100 $this->addWhereFld('pr_type', $params['prtype']);
00101
00102
00103 $prlevel = array_diff($params['prlevel'], array('', '*'));
00104 if (!empty($prlevel))
00105 $this->addWhereFld('pr_level', $prlevel);
00106 if ($params['prfiltercascade'] == 'cascading')
00107 $this->addWhereFld('pr_cascade', 1);
00108 if ($params['prfiltercascade'] == 'noncascading')
00109 $this->addWhereFld('pr_cascade', 0);
00110
00111 $this->addOption('DISTINCT');
00112
00113 $forceNameTitleIndex = false;
00114
00115 } else if (isset ($params['prlevel'])) {
00116 $this->dieUsage('prlevel may not be used without prtype', 'params');
00117 }
00118
00119 if($params['filterlanglinks'] == 'withoutlanglinks') {
00120 $this->addTables('langlinks');
00121 $this->addJoinConds(array('langlinks' => array('LEFT JOIN', 'page_id=ll_from')));
00122 $this->addWhere('ll_from IS NULL');
00123 $forceNameTitleIndex = false;
00124 } else if($params['filterlanglinks'] == 'withlanglinks') {
00125 $this->addTables('langlinks');
00126 $this->addWhere('page_id=ll_from');
00127 $this->addOption('STRAIGHT_JOIN');
00128
00129
00130 $this->addOption('GROUP BY', implode(', ', $selectFields));
00131 $forceNameTitleIndex = false;
00132 }
00133 if ($forceNameTitleIndex)
00134 $this->addOption('USE INDEX', 'name_title');
00135
00136
00137
00138 $limit = $params['limit'];
00139 $this->addOption('LIMIT', $limit+1);
00140 $res = $this->select(__METHOD__);
00141
00142 $count = 0;
00143 $result = $this->getResult();
00144 while ($row = $db->fetchObject($res)) {
00145 if (++ $count > $limit) {
00146
00147
00148 $this->setContinueEnumParameter('from', $this->keyToTitle($row->page_title));
00149 break;
00150 }
00151
00152 if (is_null($resultPageSet)) {
00153 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
00154 $vals = array(
00155 'pageid' => intval($row->page_id),
00156 'ns' => intval($title->getNamespace()),
00157 'title' => $title->getPrefixedText());
00158 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
00159 if(!$fit)
00160 {
00161 $this->setContinueEnumParameter('from', $this->keyToTitle($row->page_title));
00162 break;
00163 }
00164 } else {
00165 $resultPageSet->processDbRow($row);
00166 }
00167 }
00168 $db->freeResult($res);
00169
00170 if (is_null($resultPageSet)) {
00171 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
00172 }
00173 }
00174
00175 public function getAllowedParams() {
00176 global $wgRestrictionTypes, $wgRestrictionLevels;
00177
00178 return array (
00179 'from' => null,
00180 'prefix' => null,
00181 'namespace' => array (
00182 ApiBase :: PARAM_DFLT => 0,
00183 ApiBase :: PARAM_TYPE => 'namespace',
00184 ),
00185 'filterredir' => array (
00186 ApiBase :: PARAM_DFLT => 'all',
00187 ApiBase :: PARAM_TYPE => array (
00188 'all',
00189 'redirects',
00190 'nonredirects'
00191 )
00192 ),
00193 'minsize' => array (
00194 ApiBase :: PARAM_TYPE => 'integer',
00195 ),
00196 'maxsize' => array (
00197 ApiBase :: PARAM_TYPE => 'integer',
00198 ),
00199 'prtype' => array (
00200 ApiBase :: PARAM_TYPE => $wgRestrictionTypes,
00201 ApiBase :: PARAM_ISMULTI => true
00202 ),
00203 'prlevel' => array (
00204 ApiBase :: PARAM_TYPE => $wgRestrictionLevels,
00205 ApiBase :: PARAM_ISMULTI => true
00206 ),
00207 'prfiltercascade' => array (
00208 ApiBase :: PARAM_DFLT => 'all',
00209 ApiBase :: PARAM_TYPE => array (
00210 'cascading',
00211 'noncascading',
00212 'all'
00213 ),
00214 ),
00215 'limit' => array (
00216 ApiBase :: PARAM_DFLT => 10,
00217 ApiBase :: PARAM_TYPE => 'limit',
00218 ApiBase :: PARAM_MIN => 1,
00219 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00220 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00221 ),
00222 'dir' => array (
00223 ApiBase :: PARAM_DFLT => 'ascending',
00224 ApiBase :: PARAM_TYPE => array (
00225 'ascending',
00226 'descending'
00227 )
00228 ),
00229 'filterlanglinks' => array(
00230 ApiBase :: PARAM_TYPE => array(
00231 'withlanglinks',
00232 'withoutlanglinks',
00233 'all'
00234 ),
00235 ApiBase :: PARAM_DFLT => 'all'
00236 )
00237 );
00238 }
00239
00240 public function getParamDescription() {
00241 return array (
00242 'from' => 'The page title to start enumerating from.',
00243 'prefix' => 'Search for all page titles that begin with this value.',
00244 'namespace' => 'The namespace to enumerate.',
00245 'filterredir' => 'Which pages to list.',
00246 'dir' => 'The direction in which to list',
00247 'minsize' => 'Limit to pages with at least this many bytes',
00248 'maxsize' => 'Limit to pages with at most this many bytes',
00249 'prtype' => 'Limit to protected pages only',
00250 'prlevel' => 'The protection level (must be used with apprtype= parameter)',
00251 'prfiltercascade' => 'Filter protections based on cascadingness (ignored when apprtype isn\'t set)',
00252 'filterlanglinks' => 'Filter based on whether a page has langlinks',
00253 'limit' => 'How many total pages to return.'
00254 );
00255 }
00256
00257 public function getDescription() {
00258 return 'Enumerate all pages sequentially in a given namespace';
00259 }
00260
00261 protected function getExamples() {
00262 return array (
00263 'Simple Use',
00264 ' Show a list of pages starting at the letter "B"',
00265 ' api.php?action=query&list=allpages&apfrom=B',
00266 'Using as Generator',
00267 ' Show info about 4 pages starting at the letter "T"',
00268 ' api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info',
00269 ' Show content of first 2 non-redirect pages begining at "Re"',
00270 ' api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
00271 );
00272 }
00273
00274 public function getVersion() {
00275 return __CLASS__ . ': $Id: ApiQueryAllpages.php 69986 2010-07-27 03:57:39Z tstarling $';
00276 }
00277 }