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
00037 class ApiQueryAllCategories extends ApiQueryGeneratorBase {
00038
00039 public function __construct($query, $moduleName) {
00040 parent :: __construct($query, $moduleName, 'ac');
00041 }
00042
00043 public function execute() {
00044 $this->run();
00045 }
00046
00047 public function executeGenerator($resultPageSet) {
00048 $this->run($resultPageSet);
00049 }
00050
00051 private function run($resultPageSet = null) {
00052
00053 $db = $this->getDB();
00054 $params = $this->extractRequestParams();
00055
00056 $this->addTables('category');
00057 $this->addFields('cat_title');
00058
00059 $dir = ($params['dir'] == 'descending' ? 'older' : 'newer');
00060 $from = (is_null($params['from']) ? null : $this->titlePartToKey($params['from']));
00061 $this->addWhereRange('cat_title', $dir, $from, null);
00062 if (isset ($params['prefix']))
00063 $this->addWhere("cat_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
00064
00065 $this->addOption('LIMIT', $params['limit']+1);
00066 $this->addOption('ORDER BY', 'cat_title' . ($params['dir'] == 'descending' ? ' DESC' : ''));
00067
00068 $prop = array_flip($params['prop']);
00069 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset($prop['size']) );
00070 if(isset($prop['hidden']))
00071 {
00072 $this->addTables(array('page', 'page_props'));
00073 $this->addJoinConds(array(
00074 'page' => array('LEFT JOIN', array(
00075 'page_namespace' => NS_CATEGORY,
00076 'page_title=cat_title')),
00077 'page_props' => array('LEFT JOIN', array(
00078 'pp_page=page_id',
00079 'pp_propname' => 'hiddencat')),
00080 ));
00081 $this->addFields('pp_propname AS cat_hidden');
00082 }
00083
00084 $res = $this->select(__METHOD__);
00085
00086 $pages = array();
00087 $categories = array();
00088 $result = $this->getResult();
00089 $count = 0;
00090 while ($row = $db->fetchObject($res)) {
00091 if (++ $count > $params['limit']) {
00092
00093
00094 $this->setContinueEnumParameter('from', $this->keyToTitle($row->cat_title));
00095 break;
00096 }
00097
00098
00099 $titleObj = Title::makeTitle(NS_CATEGORY, $row->cat_title);
00100 if(!is_null($resultPageSet))
00101 $pages[] = $titleObj->getPrefixedText();
00102 else {
00103 $item = array();
00104 $result->setContent( $item, $titleObj->getText() );
00105 if( isset( $prop['size'] ) ) {
00106 $item['size'] = intval($row->cat_pages);
00107 $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
00108 $item['files'] = intval($row->cat_files);
00109 $item['subcats'] = intval($row->cat_subcats);
00110 }
00111 if( isset( $prop['hidden'] ) && $row->cat_hidden )
00112 $item['hidden'] = '';
00113 $fit = $result->addValue(array('query', $this->getModuleName()), null, $item);
00114 if(!$fit)
00115 {
00116 $this->setContinueEnumParameter('from', $this->keyToTitle($row->cat_title));
00117 break;
00118 }
00119 }
00120 }
00121 $db->freeResult($res);
00122
00123 if (is_null($resultPageSet)) {
00124 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'c');
00125 } else {
00126 $resultPageSet->populateFromTitles($pages);
00127 }
00128 }
00129
00130 public function getAllowedParams() {
00131 return array (
00132 'from' => null,
00133 'prefix' => null,
00134 'dir' => array(
00135 ApiBase :: PARAM_DFLT => 'ascending',
00136 ApiBase :: PARAM_TYPE => array(
00137 'ascending',
00138 'descending'
00139 ),
00140 ),
00141 'limit' => array (
00142 ApiBase :: PARAM_DFLT => 10,
00143 ApiBase :: PARAM_TYPE => 'limit',
00144 ApiBase :: PARAM_MIN => 1,
00145 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00146 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00147 ),
00148 'prop' => array (
00149 ApiBase :: PARAM_TYPE => array( 'size', 'hidden' ),
00150 ApiBase :: PARAM_DFLT => '',
00151 ApiBase :: PARAM_ISMULTI => true
00152 ),
00153 );
00154 }
00155
00156 public function getParamDescription() {
00157 return array (
00158 'from' => 'The category to start enumerating from.',
00159 'prefix' => 'Search for all category titles that begin with this value.',
00160 'dir' => 'Direction to sort in.',
00161 'limit' => 'How many categories to return.',
00162 'prop' => 'Which properties to get',
00163 );
00164 }
00165
00166 public function getDescription() {
00167 return 'Enumerate all categories';
00168 }
00169
00170 protected function getExamples() {
00171 return array (
00172 'api.php?action=query&list=allcategories&acprop=size',
00173 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info',
00174 );
00175 }
00176
00177 public function getVersion() {
00178 return __CLASS__ . ': $Id: ApiQueryAllCategories.php 47865 2009-02-27 16:03:01Z catrope $';
00179 }
00180 }