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 ApiQueryCategories extends ApiQueryGeneratorBase {
00037
00038 public function __construct($query, $moduleName) {
00039 parent :: __construct($query, $moduleName, 'cl');
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 if ($this->getPageSet()->getGoodTitleCount() == 0)
00053 return;
00054
00055 $params = $this->extractRequestParams();
00056 $prop = $params['prop'];
00057 $show = array_flip((array)$params['show']);
00058
00059 $this->addFields(array (
00060 'cl_from',
00061 'cl_to'
00062 ));
00063
00064 $fld_sortkey = $fld_timestamp = false;
00065 if (!is_null($prop)) {
00066 foreach($prop as $p) {
00067 switch ($p) {
00068 case 'sortkey':
00069 $this->addFields('cl_sortkey');
00070 $fld_sortkey = true;
00071 break;
00072 case 'timestamp':
00073 $this->addFields('cl_timestamp');
00074 $fld_timestamp = true;
00075 break;
00076 default :
00077 ApiBase :: dieDebug(__METHOD__, "Unknown prop=$p");
00078 }
00079 }
00080 }
00081
00082 $this->addTables('categorylinks');
00083 $this->addWhereFld('cl_from', array_keys($this->getPageSet()->getGoodTitles()));
00084 if(!is_null($params['categories']))
00085 {
00086 $cats = array();
00087 foreach($params['categories'] as $cat)
00088 {
00089 $title = Title::newFromText($cat);
00090 if(!$title || $title->getNamespace() != NS_CATEGORY)
00091 $this->setWarning("``$cat'' is not a category");
00092 else
00093 $cats[] = $title->getDBkey();
00094 }
00095 $this->addWhereFld('cl_to', $cats);
00096 }
00097 if(!is_null($params['continue'])) {
00098 $cont = explode('|', $params['continue']);
00099 if(count($cont) != 2)
00100 $this->dieUsage("Invalid continue param. You should pass the " .
00101 "original value returned by the previous query", "_badcontinue");
00102 $clfrom = intval($cont[0]);
00103 $clto = $this->getDB()->strencode($this->titleToKey($cont[1]));
00104 $this->addWhere("cl_from > $clfrom OR ".
00105 "(cl_from = $clfrom AND ".
00106 "cl_to >= '$clto')");
00107 }
00108 if(isset($show['hidden']) && isset($show['!hidden']))
00109 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
00110 if(isset($show['hidden']) || isset($show['!hidden']))
00111 {
00112 $this->addOption('STRAIGHT_JOIN');
00113 $this->addTables(array('page', 'page_props'));
00114 $this->addJoinConds(array(
00115 'page' => array('LEFT JOIN', array(
00116 'page_namespace' => NS_CATEGORY,
00117 'page_title = cl_to')),
00118 'page_props' => array('LEFT JOIN', array(
00119 'pp_page=page_id',
00120 'pp_propname' => 'hiddencat'))
00121 ));
00122 if(isset($show['hidden']))
00123 $this->addWhere(array('pp_propname IS NOT NULL'));
00124 else
00125 $this->addWhere(array('pp_propname IS NULL'));
00126 }
00127
00128 $this->addOption('USE INDEX', array('categorylinks' => 'cl_from'));
00129 # Don't order by cl_from if it's constant in the WHERE clause
00130 if(count($this->getPageSet()->getGoodTitles()) == 1)
00131 $this->addOption('ORDER BY', 'cl_to');
00132 else
00133 $this->addOption('ORDER BY', "cl_from, cl_to");
00134
00135 $db = $this->getDB();
00136 $res = $this->select(__METHOD__);
00137
00138 if (is_null($resultPageSet)) {
00139
00140 $count = 0;
00141 while ($row = $db->fetchObject($res)) {
00142 if (++$count > $params['limit']) {
00143
00144
00145 $this->setContinueEnumParameter('continue', $row->cl_from .
00146 '|' . $this->keyToTitle($row->cl_to));
00147 break;
00148 }
00149
00150 $title = Title :: makeTitle(NS_CATEGORY, $row->cl_to);
00151 $vals = array();
00152 ApiQueryBase :: addTitleInfo($vals, $title);
00153 if ($fld_sortkey)
00154 $vals['sortkey'] = $row->cl_sortkey;
00155 if ($fld_timestamp)
00156 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->cl_timestamp);
00157
00158 $fit = $this->addPageSubItem($row->cl_from, $vals);
00159 if(!$fit)
00160 {
00161 $this->setContinueEnumParameter('continue', $row->cl_from .
00162 '|' . $this->keyToTitle($row->cl_to));
00163 break;
00164 }
00165 }
00166 } else {
00167
00168 $titles = array();
00169 while ($row = $db->fetchObject($res)) {
00170 if (++$count > $params['limit']) {
00171
00172
00173 $this->setContinueEnumParameter('continue', $row->cl_from .
00174 '|' . $this->keyToTitle($row->cl_to));
00175 break;
00176 }
00177
00178 $titles[] = Title :: makeTitle(NS_CATEGORY, $row->cl_to);
00179 }
00180 $resultPageSet->populateFromTitles($titles);
00181 }
00182
00183 $db->freeResult($res);
00184 }
00185
00186 public function getAllowedParams() {
00187 return array (
00188 'prop' => array (
00189 ApiBase :: PARAM_ISMULTI => true,
00190 ApiBase :: PARAM_TYPE => array (
00191 'sortkey',
00192 'timestamp',
00193 )
00194 ),
00195 'show' => array(
00196 ApiBase :: PARAM_ISMULTI => true,
00197 ApiBase :: PARAM_TYPE => array(
00198 'hidden',
00199 '!hidden',
00200 )
00201 ),
00202 'limit' => array(
00203 ApiBase :: PARAM_DFLT => 10,
00204 ApiBase :: PARAM_TYPE => 'limit',
00205 ApiBase :: PARAM_MIN => 1,
00206 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00207 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00208 ),
00209 'continue' => null,
00210 'categories' => array(
00211 ApiBase :: PARAM_ISMULTI => true,
00212 ),
00213 );
00214 }
00215
00216 public function getParamDescription() {
00217 return array (
00218 'prop' => 'Which additional properties to get for each category.',
00219 'limit' => 'How many categories to return',
00220 'show' => 'Which kind of categories to show',
00221 'continue' => 'When more results are available, use this to continue',
00222 'categories' => 'Only list these categories. Useful for checking whether a certain page is in a certain category',
00223 );
00224 }
00225
00226 public function getDescription() {
00227 return 'List all categories the page(s) belong to';
00228 }
00229
00230 protected function getExamples() {
00231 return array (
00232 "Get a list of categories [[Albert Einstein]] belongs to:",
00233 " api.php?action=query&prop=categories&titles=Albert%20Einstein",
00234 "Get information about all categories used in the [[Albert Einstein]]:",
00235 " api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info"
00236 );
00237 }
00238
00239 public function getVersion() {
00240 return __CLASS__ . ': $Id: ApiQueryCategories.php 50097 2009-05-01 06:35:57Z tstarling $';
00241 }
00242 }