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 ApiQueryAllLinks extends ApiQueryGeneratorBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'al');
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function executeGenerator($resultPageSet) {
00047 $this->run($resultPageSet);
00048 }
00049
00050 private function run($resultPageSet = null) {
00051
00052 $db = $this->getDB();
00053 $params = $this->extractRequestParams();
00054
00055 $prop = array_flip($params['prop']);
00056 $fld_ids = isset($prop['ids']);
00057 $fld_title = isset($prop['title']);
00058
00059 if ($params['unique']) {
00060 if (!is_null($resultPageSet))
00061 $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
00062 if ($fld_ids)
00063 $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
00064 $this->addOption('DISTINCT');
00065 }
00066
00067 $this->addTables('pagelinks');
00068 $this->addWhereFld('pl_namespace', $params['namespace']);
00069
00070 if (!is_null($params['from']) && !is_null($params['continue']))
00071 $this->dieUsage('alcontinue and alfrom cannot be used together', 'params');
00072 if (!is_null($params['continue']))
00073 {
00074 $arr = explode('|', $params['continue']);
00075 if(count($arr) != 2)
00076 $this->dieUsage("Invalid continue parameter", 'badcontinue');
00077 $from = $this->getDB()->strencode($this->titleToKey($arr[0]));
00078 $id = intval($arr[1]);
00079 $this->addWhere("pl_title > '$from' OR " .
00080 "(pl_title = '$from' AND " .
00081 "pl_from > $id)");
00082 }
00083
00084 if (!is_null($params['from']))
00085 $this->addWhere('pl_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
00086 if (isset ($params['prefix']))
00087 $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
00088
00089 $this->addFields(array (
00090 'pl_title',
00091 ));
00092 $this->addFieldsIf('pl_from', !$params['unique']);
00093
00094 $this->addOption('USE INDEX', 'pl_namespace');
00095 $limit = $params['limit'];
00096 $this->addOption('LIMIT', $limit+1);
00097 if($params['unique'])
00098 $this->addOption('ORDER BY', 'pl_title');
00099 else
00100 $this->addOption('ORDER BY', 'pl_title, pl_from');
00101
00102 $res = $this->select(__METHOD__);
00103
00104 $pageids = array ();
00105 $count = 0;
00106 $result = $this->getResult();
00107 while ($row = $db->fetchObject($res)) {
00108 if (++ $count > $limit) {
00109
00110
00111 if($params['unique'])
00112 $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title));
00113 else
00114 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title) . "|" . $row->pl_from);
00115 break;
00116 }
00117
00118 if (is_null($resultPageSet)) {
00119 $vals = array();
00120 if ($fld_ids)
00121 $vals['fromid'] = intval($row->pl_from);
00122 if ($fld_title) {
00123 $title = Title :: makeTitle($params['namespace'], $row->pl_title);
00124 ApiQueryBase::addTitleInfo($vals, $title);
00125 }
00126 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
00127 if(!$fit)
00128 {
00129 if($params['unique'])
00130 $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title));
00131 else
00132 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title) . "|" . $row->pl_from);
00133 break;
00134 }
00135 } else {
00136 $pageids[] = $row->pl_from;
00137 }
00138 }
00139 $db->freeResult($res);
00140
00141 if (is_null($resultPageSet)) {
00142 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'l');
00143 } else {
00144 $resultPageSet->populateFromPageIDs($pageids);
00145 }
00146 }
00147
00148 public function getAllowedParams() {
00149 return array (
00150 'continue' => null,
00151 'from' => null,
00152 'prefix' => null,
00153 'unique' => false,
00154 'prop' => array (
00155 ApiBase :: PARAM_ISMULTI => true,
00156 ApiBase :: PARAM_DFLT => 'title',
00157 ApiBase :: PARAM_TYPE => array (
00158 'ids',
00159 'title'
00160 )
00161 ),
00162 'namespace' => array (
00163 ApiBase :: PARAM_DFLT => 0,
00164 ApiBase :: PARAM_TYPE => 'namespace'
00165 ),
00166 'limit' => array (
00167 ApiBase :: PARAM_DFLT => 10,
00168 ApiBase :: PARAM_TYPE => 'limit',
00169 ApiBase :: PARAM_MIN => 1,
00170 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00171 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00172 )
00173 );
00174 }
00175
00176 public function getParamDescription() {
00177 return array (
00178 'from' => 'The page title to start enumerating from.',
00179 'prefix' => 'Search for all page titles that begin with this value.',
00180 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
00181 'prop' => 'What pieces of information to include',
00182 'namespace' => 'The namespace to enumerate.',
00183 'limit' => 'How many total links to return.',
00184 'continue' => 'When more results are available, use this to continue.',
00185 );
00186 }
00187
00188 public function getDescription() {
00189 return 'Enumerate all links that point to a given namespace';
00190 }
00191
00192 protected function getExamples() {
00193 return array (
00194 'api.php?action=query&list=alllinks&alunique&alfrom=B',
00195 );
00196 }
00197
00198 public function getVersion() {
00199 return __CLASS__ . ': $Id: ApiQueryAllLinks.php 47865 2009-02-27 16:03:01Z catrope $';
00200 }
00201 }