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