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 ApiQueryLinks extends ApiQueryGeneratorBase {
00037
00038 const LINKS = 'links';
00039 const TEMPLATES = 'templates';
00040
00041 private $table, $prefix, $description;
00042
00043 public function __construct($query, $moduleName) {
00044
00045 switch ($moduleName) {
00046 case self::LINKS :
00047 $this->table = 'pagelinks';
00048 $this->prefix = 'pl';
00049 $this->description = 'link';
00050 break;
00051 case self::TEMPLATES :
00052 $this->table = 'templatelinks';
00053 $this->prefix = 'tl';
00054 $this->description = 'template';
00055 break;
00056 default :
00057 ApiBase :: dieDebug(__METHOD__, 'Unknown module name');
00058 }
00059
00060 parent :: __construct($query, $moduleName, $this->prefix);
00061 }
00062
00063 public function execute() {
00064 $this->run();
00065 }
00066
00067 public function getCacheMode( $params ) {
00068 return 'public';
00069 }
00070
00071 public function executeGenerator($resultPageSet) {
00072 $this->run($resultPageSet);
00073 }
00074
00075 private function run($resultPageSet = null) {
00076
00077 if ($this->getPageSet()->getGoodTitleCount() == 0)
00078 return;
00079
00080 $params = $this->extractRequestParams();
00081
00082 $this->addFields(array (
00083 $this->prefix . '_from AS pl_from',
00084 $this->prefix . '_namespace AS pl_namespace',
00085 $this->prefix . '_title AS pl_title'
00086 ));
00087
00088 $this->addTables($this->table);
00089 $this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
00090 $this->addWhereFld($this->prefix . '_namespace', $params['namespace']);
00091
00092 if(!is_null($params['continue'])) {
00093 $cont = explode('|', $params['continue']);
00094 if(count($cont) != 3)
00095 $this->dieUsage("Invalid continue param. You should pass the " .
00096 "original value returned by the previous query", "_badcontinue");
00097 $plfrom = intval($cont[0]);
00098 $plns = intval($cont[1]);
00099 $pltitle = $this->getDB()->strencode($this->titleToKey($cont[2]));
00100 $this->addWhere("{$this->prefix}_from > $plfrom OR ".
00101 "({$this->prefix}_from = $plfrom AND ".
00102 "({$this->prefix}_namespace > $plns OR ".
00103 "({$this->prefix}_namespace = $plns AND ".
00104 "{$this->prefix}_title >= '$pltitle')))");
00105 }
00106
00107 # Here's some MySQL craziness going on: if you use WHERE foo='bar'
00108 # and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
00109 # but instead goes and filesorts, because the index for foo was used
00110 # already. To work around this, we drop constant fields in the WHERE
00111 # clause from the ORDER BY clause
00112 $order = array();
00113 if(count($this->getPageSet()->getGoodTitles()) != 1)
00114 $order[] = "{$this->prefix}_from";
00115 if(count($params['namespace']) != 1)
00116 $order[] = "{$this->prefix}_namespace";
00117 $order[] = "{$this->prefix}_title";
00118 $this->addOption('ORDER BY', implode(", ", $order));
00119 $this->addOption('USE INDEX', "{$this->prefix}_from");
00120 $this->addOption('LIMIT', $params['limit'] + 1);
00121
00122 $db = $this->getDB();
00123 $res = $this->select(__METHOD__);
00124
00125 if (is_null($resultPageSet)) {
00126 $count = 0;
00127 while ($row = $db->fetchObject($res)) {
00128 if(++$count > $params['limit']) {
00129
00130
00131 $this->setContinueEnumParameter('continue',
00132 "{$row->pl_from}|{$row->pl_namespace}|" .
00133 $this->keyToTitle($row->pl_title));
00134 break;
00135 }
00136 $vals = array();
00137 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->pl_namespace, $row->pl_title));
00138 $fit = $this->addPageSubItem($row->pl_from, $vals);
00139 if(!$fit)
00140 {
00141 $this->setContinueEnumParameter('continue',
00142 "{$row->pl_from}|{$row->pl_namespace}|" .
00143 $this->keyToTitle($row->pl_title));
00144 break;
00145 }
00146 }
00147 } else {
00148
00149 $titles = array();
00150 $count = 0;
00151 while ($row = $db->fetchObject($res)) {
00152 if(++$count > $params['limit']) {
00153
00154
00155 $this->setContinueEnumParameter('continue',
00156 "{$row->pl_from}|{$row->pl_namespace}|" .
00157 $this->keyToTitle($row->pl_title));
00158 break;
00159 }
00160 $titles[] = Title :: makeTitle($row->pl_namespace, $row->pl_title);
00161 }
00162 $resultPageSet->populateFromTitles($titles);
00163 }
00164
00165 $db->freeResult($res);
00166 }
00167
00168 public function getAllowedParams()
00169 {
00170 return array(
00171 'namespace' => array(
00172 ApiBase :: PARAM_TYPE => 'namespace',
00173 ApiBase :: PARAM_ISMULTI => true
00174 ),
00175 'limit' => array(
00176 ApiBase :: PARAM_DFLT => 10,
00177 ApiBase :: PARAM_TYPE => 'limit',
00178 ApiBase :: PARAM_MIN => 1,
00179 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00180 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00181 ),
00182 'continue' => null,
00183 );
00184 }
00185
00186 public function getParamDescription()
00187 {
00188 return array(
00189 'namespace' => "Show {$this->description}s in this namespace(s) only",
00190 'limit' => "How many {$this->description}s to return",
00191 'continue' => 'When more results are available, use this to continue',
00192 );
00193 }
00194
00195 public function getDescription() {
00196 return "Returns all {$this->description}s from the given page(s)";
00197 }
00198
00199 protected function getExamples() {
00200 return array (
00201 "Get {$this->description}s from the [[Main Page]]:",
00202 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
00203 "Get information about the {$this->description} pages in the [[Main Page]]:",
00204 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
00205 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
00206 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
00207 );
00208 }
00209
00210 public function getVersion() {
00211 return __CLASS__ . ': $Id: ApiQueryLinks.php 69986 2010-07-27 03:57:39Z tstarling $';
00212 }
00213 }