MediaWiki  1.23.14
ApiQueryCategories.php
Go to the documentation of this file.
1 <?php
33 
34  public function __construct( $query, $moduleName ) {
35  parent::__construct( $query, $moduleName, 'cl' );
36  }
37 
38  public function execute() {
39  $this->run();
40  }
41 
42  public function getCacheMode( $params ) {
43  return 'public';
44  }
45 
46  public function executeGenerator( $resultPageSet ) {
47  $this->run( $resultPageSet );
48  }
49 
53  private function run( $resultPageSet = null ) {
54  if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
55  return; // nothing to do
56  }
57 
58  $params = $this->extractRequestParams();
59  $prop = array_flip( (array)$params['prop'] );
60  $show = array_flip( (array)$params['show'] );
61 
62  $this->addFields( array(
63  'cl_from',
64  'cl_to'
65  ) );
66 
67  $this->addFieldsIf( array( 'cl_sortkey', 'cl_sortkey_prefix' ), isset( $prop['sortkey'] ) );
68  $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
69 
70  $this->addTables( 'categorylinks' );
71  $this->addWhereFld( 'cl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
72  if ( !is_null( $params['categories'] ) ) {
73  $cats = array();
74  foreach ( $params['categories'] as $cat ) {
75  $title = Title::newFromText( $cat );
76  if ( !$title || $title->getNamespace() != NS_CATEGORY ) {
77  $this->setWarning( "\"$cat\" is not a category" );
78  } else {
79  $cats[] = $title->getDBkey();
80  }
81  }
82  $this->addWhereFld( 'cl_to', $cats );
83  }
84 
85  if ( !is_null( $params['continue'] ) ) {
86  $cont = explode( '|', $params['continue'] );
87  $this->dieContinueUsageIf( count( $cont ) != 2 );
88  $op = $params['dir'] == 'descending' ? '<' : '>';
89  $clfrom = intval( $cont[0] );
90  $clto = $this->getDB()->addQuotes( $cont[1] );
91  $this->addWhere(
92  "cl_from $op $clfrom OR " .
93  "(cl_from = $clfrom AND " .
94  "cl_to $op= $clto)"
95  );
96  }
97 
98  if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
99  $this->dieUsageMsg( 'show' );
100  }
101  if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) ) {
102  $this->addOption( 'STRAIGHT_JOIN' );
103  $this->addTables( array( 'page', 'page_props' ) );
104  $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
105  $this->addJoinConds( array(
106  'page' => array( 'LEFT JOIN', array(
107  'page_namespace' => NS_CATEGORY,
108  'page_title = cl_to' ) ),
109  'page_props' => array( 'LEFT JOIN', array(
110  'pp_page=page_id',
111  'pp_propname' => 'hiddencat' ) )
112  ) );
113  if ( isset( $show['hidden'] ) ) {
114  $this->addWhere( array( 'pp_propname IS NOT NULL' ) );
115  } elseif ( isset( $show['!hidden'] ) ) {
116  $this->addWhere( array( 'pp_propname IS NULL' ) );
117  }
118  }
119 
120  $this->addOption( 'USE INDEX', array( 'categorylinks' => 'cl_from' ) );
121 
122  $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
123  // Don't order by cl_from if it's constant in the WHERE clause
124  if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
125  $this->addOption( 'ORDER BY', 'cl_to' . $sort );
126  } else {
127  $this->addOption( 'ORDER BY', array(
128  'cl_from' . $sort,
129  'cl_to' . $sort
130  ) );
131  }
132 
133  $res = $this->select( __METHOD__ );
134 
135  $count = 0;
136  if ( is_null( $resultPageSet ) ) {
137  foreach ( $res as $row ) {
138  if ( ++$count > $params['limit'] ) {
139  // We've reached the one extra which shows that
140  // there are additional pages to be had. Stop here...
141  $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
142  break;
143  }
144 
145  $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
146  $vals = array();
148  if ( isset( $prop['sortkey'] ) ) {
149  $vals['sortkey'] = bin2hex( $row->cl_sortkey );
150  $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
151  }
152  if ( isset( $prop['timestamp'] ) ) {
153  $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
154  }
155  if ( isset( $prop['hidden'] ) && !is_null( $row->pp_propname ) ) {
156  $vals['hidden'] = '';
157  }
158 
159  $fit = $this->addPageSubItem( $row->cl_from, $vals );
160  if ( !$fit ) {
161  $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
162  break;
163  }
164  }
165  } else {
166  $titles = array();
167  foreach ( $res as $row ) {
168  if ( ++$count > $params['limit'] ) {
169  // We've reached the one extra which shows that
170  // there are additional pages to be had. Stop here...
171  $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
172  break;
173  }
174 
175  $titles[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
176  }
177  $resultPageSet->populateFromTitles( $titles );
178  }
179  }
180 
181  public function getAllowedParams() {
182  return array(
183  'prop' => array(
184  ApiBase::PARAM_ISMULTI => true,
186  'sortkey',
187  'timestamp',
188  'hidden',
189  )
190  ),
191  'show' => array(
192  ApiBase::PARAM_ISMULTI => true,
194  'hidden',
195  '!hidden',
196  )
197  ),
198  'limit' => array(
199  ApiBase::PARAM_DFLT => 10,
200  ApiBase::PARAM_TYPE => 'limit',
201  ApiBase::PARAM_MIN => 1,
204  ),
205  'continue' => null,
206  'categories' => array(
207  ApiBase::PARAM_ISMULTI => true,
208  ),
209  'dir' => array(
210  ApiBase::PARAM_DFLT => 'ascending',
212  'ascending',
213  'descending'
214  )
215  ),
216  );
217  }
218 
219  public function getParamDescription() {
220  return array(
221  'prop' => array(
222  'Which additional properties to get for each category',
223  ' sortkey - Adds the sortkey (hexadecimal string) and sortkey prefix',
224  ' (human-readable part) for the category',
225  ' timestamp - Adds timestamp of when the category was added',
226  ' hidden - Tags categories that are hidden with __HIDDENCAT__',
227  ),
228  'limit' => 'How many categories to return',
229  'show' => 'Which kind of categories to show',
230  'continue' => 'When more results are available, use this to continue',
231  'categories' => 'Only list these categories. Useful for checking ' .
232  'whether a certain page is in a certain category',
233  'dir' => 'The direction in which to list',
234  );
235  }
236 
237  public function getResultProperties() {
238  return array(
239  '' => array(
240  'ns' => 'namespace',
241  'title' => 'string'
242  ),
243  'sortkey' => array(
244  'sortkey' => 'string',
245  'sortkeyprefix' => 'string'
246  ),
247  'timestamp' => array(
248  'timestamp' => 'timestamp'
249  ),
250  'hidden' => array(
251  'hidden' => 'boolean'
252  )
253  );
254  }
255 
256  public function getDescription() {
257  return 'List all categories the page(s) belong to.';
258  }
259 
260  public function getPossibleErrors() {
261  return array_merge( parent::getPossibleErrors(), array(
262  array( 'show' ),
263  ) );
264  }
265 
266  public function getExamples() {
267  return array(
268  'api.php?action=query&prop=categories&titles=Albert%20Einstein'
269  => 'Get a list of categories [[Albert Einstein]] belongs to',
270  'api.php?action=query&generator=categories&titles=Albert%20Einstein&prop=info'
271  => 'Get information about all categories used in the [[Albert Einstein]]',
272  );
273  }
274 
275  public function getHelpUrls() {
276  return 'https://www.mediawiki.org/wiki/API:Properties#categories_.2F_cl';
277  }
278 }
ApiQueryCategories\__construct
__construct( $query, $moduleName)
Definition: ApiQueryCategories.php:34
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
ApiQueryCategories
A query module to enumerate categories the set of pages belong to.
Definition: ApiQueryCategories.php:32
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:117
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiQueryCategories\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryCategories.php:181
ApiQueryCategories\run
run( $resultPageSet=null)
Definition: ApiQueryCategories.php:53
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1953
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiQueryBase\select
select( $method, $extraQuery=array())
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:274
$params
$params
Definition: styleTest.css.php:40
ApiQueryCategories\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiQueryCategories.php:266
ApiQueryCategories\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiQueryCategories.php:256
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:252
ApiQueryBase\addFieldsIf
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
Definition: ApiQueryBase.php:131
ApiQueryCategories\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiQueryCategories.php:237
ApiQueryCategories\getPossibleErrors
getPossibleErrors()
Definition: ApiQueryCategories.php:260
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overrides base in case of generator & smart continue to notify ApiQueryMain instead of adding them to...
Definition: ApiQueryBase.php:676
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:56
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryBase.php:649
$titles
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition: linkcache.txt:17
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Definition: ApiBase.php:78
TS_ISO_8601
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition: GlobalFunctions.php:2495
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:417
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:52
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:82
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:93
$sort
$sort
Definition: profileinfo.php:301
ApiQueryCategories\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryCategories.php:46
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the $prefix.
Definition: ApiBase.php:1989
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:106
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere(array($field => $value))
Definition: ApiQueryBase.php:185
$count
$count
Definition: UtfNormalTest2.php:96
ApiBase\setWarning
setWarning( $warning)
Set warning section for this module.
Definition: ApiBase.php:245
ApiQueryGeneratorBase
Definition: ApiQueryBase.php:626
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Definition: ApiBase.php:79
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:46
ApiQueryCategories\getHelpUrls
getHelpUrls()
Definition: ApiQueryCategories.php:275
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:48
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:54
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:152
ApiQueryCategories\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryCategories.php:42
ApiQueryCategories\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiQueryCategories.php:219
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
$res
$res
Definition: database.txt:21
ApiQueryCategories\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryCategories.php:38
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:383
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:339