MediaWiki master
ApiQueryCategoryList.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Api;
4
9
11
12 protected function createQuery( IReadableDatabase $db, array $params ): void {
13 $this->addTables( 'category' );
14 $this->addFields( 'cat_title' );
15
16 if ( $params['continue'] !== null ) {
17 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string' ] );
18 $op = $params['dir'] == 'descending' ? '<=' : '>=';
19 $this->addWhere( $db->expr( 'cat_title', $op, $cont[0] ) );
20 }
21
22 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
23 $from = ( $params['from'] === null
24 ? null
25 : $this->titlePartToKey( $params['from'], NS_CATEGORY ) );
26 $to = ( $params['to'] === null
27 ? null
28 : $this->titlePartToKey( $params['to'], NS_CATEGORY ) );
29 $this->addWhereRange( 'cat_title', $dir, $from, $to );
30
31 $min = $params['min'];
32 $max = $params['max'];
33 if ( $dir == 'newer' ) {
34 $this->addWhereRange( 'cat_pages', 'newer', $min, $max );
35 } else {
36 $this->addWhereRange( 'cat_pages', 'older', $max, $min );
37 }
38
39 if ( isset( $params['prefix'] ) ) {
40 $this->addWhere(
41 $db->expr(
42 'cat_title',
43 IExpression::LIKE,
44 new LikeValue( $this->titlePartToKey( $params['prefix'], NS_CATEGORY ), $db->anyString() )
45 )
46 );
47 }
48
49 $this->addOption( 'LIMIT', $params['limit'] + 1 );
50 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
51 $this->addOption( 'ORDER BY', 'cat_title' . $sort );
52
53 $prop = array_fill_keys( $params['prop'], true );
54 $this->addFieldsIf( [ 'cat_pages', 'cat_subcats', 'cat_files' ], isset( $prop['size'] ) );
55 if ( isset( $prop['hidden'] ) ) {
56 $this->addTables( [ 'page', 'page_props' ] );
57 $this->addJoinConds( [
58 'page' => [ 'LEFT JOIN', [
59 'page_namespace' => NS_CATEGORY,
60 'page_title=cat_title' ] ],
61 'page_props' => [ 'LEFT JOIN', [
62 'pp_page=page_id',
63 'pp_propname' => 'hiddencat' ] ],
64 ] );
65 $this->addFields( [ 'cat_hidden' => 'pp_propname' ] );
66 }
67 }
68
69 protected function executeQuery( array $params, ?ApiPageSet $resultPageSet = null, array $options = [] ) {
70 $res = $this->select( __METHOD__ );
71 $pages = [];
72
73 $result = $this->getResult();
74 $count = 0;
75 foreach ( $res as $row ) {
76 if ( ++$count > $params['limit'] ) {
77 // We've reached the one extra which shows that there are
78 // additional cats to be had. Stop here...
79 $this->setContinueEnumParameter( 'continue', $row->cat_title );
80 break;
81 }
82
83 // Normalize titles
84 $titleObj = Title::makeTitle( NS_CATEGORY, $row->cat_title );
85 if ( $resultPageSet !== null ) {
86 $pages[] = $titleObj;
87 } else {
88 $item = [];
89 ApiResult::setContentValue( $item, 'category', $titleObj->getText() );
90 $prop = array_fill_keys( $params['prop'], true );
91 if ( isset( $prop['size'] ) ) {
92 $item['size'] = (int)$row->cat_pages;
93 $item['pages'] = $row->cat_pages - $row->cat_subcats - $row->cat_files;
94 $item['files'] = (int)$row->cat_files;
95 $item['subcats'] = (int)$row->cat_subcats;
96 }
97 if ( isset( $prop['hidden'] ) ) {
98 $item['hidden'] = (bool)$row->cat_hidden;
99 }
100 if ( isset( $options['catNames'] ) ) {
101 $catNameList = $options['catNames'];
102 $item['catid'] = $catNameList[ $titleObj->getDBkey() ];
103 }
104 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $item );
105 if ( !$fit ) {
106 $this->setContinueEnumParameter( 'continue', $row->cat_title );
107 break;
108 }
109 }
110 }
111
112 if ( $resultPageSet === null ) {
113 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'c' );
114 } else {
115 $resultPageSet->populateFromTitles( $pages );
116 }
117 }
118}
const NS_CATEGORY
Definition Defines.php:65
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1696
This class contains a list of pages that the client has requested.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
titlePartToKey( $titlePart, $namespace=NS_MAIN)
Convert an input title or title prefix into a dbkey.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
addFields( $value)
Add a set of fields to select to the internal array.
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
createQuery(IReadableDatabase $db, array $params)
executeQuery(array $params, ?ApiPageSet $resultPageSet=null, array $options=[])
static setContentValue(array &$arr, $name, $value, $flags=0)
Add an output value to the array by name and mark as META_CONTENT.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Represents a title within MediaWiki.
Definition Title.php:69
Content of like value.
Definition LikeValue.php:14
A database connection without write operations.
expr(string $field, string $op, $value)
See Expression::__construct()
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
array $params
The job parameters.