MediaWiki
master
|
00001 <?php 00033 class ApiQueryAllCategories extends ApiQueryGeneratorBase { 00034 00035 public function __construct( $query, $moduleName ) { 00036 parent::__construct( $query, $moduleName, 'ac' ); 00037 } 00038 00039 public function execute() { 00040 $this->run(); 00041 } 00042 00043 public function getCacheMode( $params ) { 00044 return 'public'; 00045 } 00046 00047 public function executeGenerator( $resultPageSet ) { 00048 $this->run( $resultPageSet ); 00049 } 00050 00054 private function run( $resultPageSet = null ) { 00055 $db = $this->getDB(); 00056 $params = $this->extractRequestParams(); 00057 00058 $this->addTables( 'category' ); 00059 $this->addFields( 'cat_title' ); 00060 00061 if ( !is_null( $params['continue'] ) ) { 00062 $cont = explode( '|', $params['continue'] ); 00063 if ( count( $cont ) != 1 ) { 00064 $this->dieUsage( "Invalid continue param. You should pass the " . 00065 "original value returned by the previous query", "_badcontinue" ); 00066 } 00067 $op = $params['dir'] == 'descending' ? '<' : '>'; 00068 $cont_from = $db->addQuotes( $cont[0] ); 00069 $this->addWhere( "cat_title $op= $cont_from" ); 00070 } 00071 00072 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' ); 00073 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) ); 00074 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) ); 00075 $this->addWhereRange( 'cat_title', $dir, $from, $to ); 00076 00077 $min = $params['min']; 00078 $max = $params['max']; 00079 if ( $dir == 'newer' ) { 00080 $this->addWhereRange( 'cat_pages', 'newer', $min, $max ); 00081 } else { 00082 $this->addWhereRange( 'cat_pages', 'older', $max, $min); 00083 } 00084 00085 if ( isset( $params['prefix'] ) ) { 00086 $this->addWhere( 'cat_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) ); 00087 } 00088 00089 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 00090 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); 00091 $this->addOption( 'ORDER BY', 'cat_title' . $sort ); 00092 00093 $prop = array_flip( $params['prop'] ); 00094 $this->addFieldsIf( array( 'cat_pages', 'cat_subcats', 'cat_files' ), isset( $prop['size'] ) ); 00095 if ( isset( $prop['hidden'] ) ) { 00096 $this->addTables( array( 'page', 'page_props' ) ); 00097 $this->addJoinConds( array( 00098 'page' => array( 'LEFT JOIN', array( 00099 'page_namespace' => NS_CATEGORY, 00100 'page_title=cat_title' ) ), 00101 'page_props' => array( 'LEFT JOIN', array( 00102 'pp_page=page_id', 00103 'pp_propname' => 'hiddencat' ) ), 00104 ) ); 00105 $this->addFields( array( 'cat_hidden' => 'pp_propname' ) ); 00106 } 00107 00108 $res = $this->select( __METHOD__ ); 00109 00110 $pages = array(); 00111 00112 $result = $this->getResult(); 00113 $count = 0; 00114 foreach ( $res as $row ) { 00115 if ( ++ $count > $params['limit'] ) { 00116 // We've reached the one extra which shows that there are additional cats to be had. Stop here... 00117 $this->setContinueEnumParameter( 'continue', $row->cat_title ); 00118 break; 00119 } 00120 00121 // Normalize titles 00122 $titleObj = Title::makeTitle( NS_CATEGORY, $row->cat_title ); 00123 if ( !is_null( $resultPageSet ) ) { 00124 $pages[] = $titleObj; 00125 } else { 00126 $item = array(); 00127 $result->setContent( $item, $titleObj->getText() ); 00128 if ( isset( $prop['size'] ) ) { 00129 $item['size'] = intval( $row->cat_pages ); 00130 $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files; 00131 $item['files'] = intval( $row->cat_files ); 00132 $item['subcats'] = intval( $row->cat_subcats ); 00133 } 00134 if ( isset( $prop['hidden'] ) && $row->cat_hidden ) { 00135 $item['hidden'] = ''; 00136 } 00137 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $item ); 00138 if ( !$fit ) { 00139 $this->setContinueEnumParameter( 'continue', $row->cat_title ); 00140 break; 00141 } 00142 } 00143 } 00144 00145 if ( is_null( $resultPageSet ) ) { 00146 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'c' ); 00147 } else { 00148 $resultPageSet->populateFromTitles( $pages ); 00149 } 00150 } 00151 00152 public function getAllowedParams() { 00153 return array( 00154 'from' => null, 00155 'continue' => null, 00156 'to' => null, 00157 'prefix' => null, 00158 'dir' => array( 00159 ApiBase::PARAM_DFLT => 'ascending', 00160 ApiBase::PARAM_TYPE => array( 00161 'ascending', 00162 'descending' 00163 ), 00164 ), 00165 'min' => array( 00166 ApiBase::PARAM_DFLT => null, 00167 ApiBase::PARAM_TYPE => 'integer' 00168 ), 00169 'max' => array( 00170 ApiBase::PARAM_DFLT => null, 00171 ApiBase::PARAM_TYPE => 'integer' 00172 ), 00173 'limit' => array( 00174 ApiBase::PARAM_DFLT => 10, 00175 ApiBase::PARAM_TYPE => 'limit', 00176 ApiBase::PARAM_MIN => 1, 00177 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00178 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00179 ), 00180 'prop' => array( 00181 ApiBase::PARAM_TYPE => array( 'size', 'hidden' ), 00182 ApiBase::PARAM_DFLT => '', 00183 ApiBase::PARAM_ISMULTI => true 00184 ), 00185 ); 00186 } 00187 00188 public function getParamDescription() { 00189 return array( 00190 'from' => 'The category to start enumerating from', 00191 'continue' => 'When more results are available, use this to continue', 00192 'to' => 'The category to stop enumerating at', 00193 'prefix' => 'Search for all category titles that begin with this value', 00194 'dir' => 'Direction to sort in', 00195 'min' => 'Minimum number of category members', 00196 'max' => 'Maximum number of category members', 00197 'limit' => 'How many categories to return', 00198 'prop' => array( 00199 'Which properties to get', 00200 ' size - Adds number of pages in the category', 00201 ' hidden - Tags categories that are hidden with __HIDDENCAT__', 00202 ), 00203 ); 00204 } 00205 00206 public function getResultProperties() { 00207 return array( 00208 '' => array( 00209 '*' => 'string' 00210 ), 00211 'size' => array( 00212 'size' => 'integer', 00213 'pages' => 'integer', 00214 'files' => 'integer', 00215 'subcats' => 'integer' 00216 ), 00217 'hidden' => array( 00218 'hidden' => 'boolean' 00219 ) 00220 ); 00221 } 00222 00223 public function getDescription() { 00224 return 'Enumerate all categories'; 00225 } 00226 00227 public function getPossibleErrors() { 00228 return array_merge( parent::getPossibleErrors(), array( 00229 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ), 00230 ) ); 00231 } 00232 00233 public function getExamples() { 00234 return array( 00235 'api.php?action=query&list=allcategories&acprop=size', 00236 'api.php?action=query&generator=allcategories&gacprefix=List&prop=info', 00237 ); 00238 } 00239 00240 public function getHelpUrls() { 00241 return 'https://www.mediawiki.org/wiki/API:Allcategories'; 00242 } 00243 00244 public function getVersion() { 00245 return __CLASS__ . ': $Id$'; 00246 } 00247 }