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