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