5 * Created on May 13, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * A query module to enumerate categories the set of pages belong to.
32 class ApiQueryCategories
extends ApiQueryGeneratorBase
{
34 public function __construct( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'cl' );
38 public function execute() {
42 public function getCacheMode( $params ) {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
51 * @param ApiPageSet $resultPageSet
53 private function run( $resultPageSet = null ) {
54 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
55 return; // nothing to do
58 $params = $this->extractRequestParams();
59 $prop = array_flip( (array)$params['prop'] );
60 $show = array_flip( (array)$params['show'] );
67 $this->addFieldsIf( [ 'cl_sortkey', 'cl_sortkey_prefix' ], isset( $prop['sortkey'] ) );
68 $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
70 $this->addTables( 'categorylinks' );
71 $this->addWhereFld( 'cl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
72 if ( $params['categories'] ) {
74 foreach ( $params['categories'] as $cat ) {
75 $title = Title
::newFromText( $cat );
76 if ( !$title ||
$title->getNamespace() != NS_CATEGORY
) {
77 $this->addWarning( [ 'apiwarn-invalidcategory', wfEscapeWikiText( $cat ) ] );
79 $cats[] = $title->getDBkey();
83 // No titles so no results
86 $this->addWhereFld( 'cl_to', $cats );
89 if ( !is_null( $params['continue'] ) ) {
90 $cont = explode( '|', $params['continue'] );
91 $this->dieContinueUsageIf( count( $cont ) != 2 );
92 $op = $params['dir'] == 'descending' ?
'<' : '>';
93 $clfrom = intval( $cont[0] );
94 $clto = $this->getDB()->addQuotes( $cont[1] );
96 "cl_from $op $clfrom OR " .
97 "(cl_from = $clfrom AND " .
102 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
103 $this->dieWithError( 'apierror-show' );
105 if ( isset( $show['hidden'] ) ||
isset( $show['!hidden'] ) ||
isset( $prop['hidden'] ) ) {
106 $this->addOption( 'STRAIGHT_JOIN' );
107 $this->addTables( [ 'page', 'page_props' ] );
108 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
109 $this->addJoinConds( [
110 'page' => [ 'LEFT JOIN', [
111 'page_namespace' => NS_CATEGORY
,
112 'page_title = cl_to' ] ],
113 'page_props' => [ 'LEFT JOIN', [
115 'pp_propname' => 'hiddencat' ] ]
117 if ( isset( $show['hidden'] ) ) {
118 $this->addWhere( [ 'pp_propname IS NOT NULL' ] );
119 } elseif ( isset( $show['!hidden'] ) ) {
120 $this->addWhere( [ 'pp_propname IS NULL' ] );
124 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
125 // Don't order by cl_from if it's constant in the WHERE clause
126 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
127 $this->addOption( 'ORDER BY', 'cl_to' . $sort );
129 $this->addOption( 'ORDER BY', [
135 $res = $this->select( __METHOD__
);
138 if ( is_null( $resultPageSet ) ) {
139 foreach ( $res as $row ) {
140 if ( ++
$count > $params['limit'] ) {
141 // We've reached the one extra which shows that
142 // there are additional pages to be had. Stop here...
143 $this->setContinueEnumParameter( 'continue', $row->cl_from
. '|' . $row->cl_to
);
147 $title = Title
::makeTitle( NS_CATEGORY
, $row->cl_to
);
149 ApiQueryBase
::addTitleInfo( $vals, $title );
150 if ( isset( $prop['sortkey'] ) ) {
151 $vals['sortkey'] = bin2hex( $row->cl_sortkey
);
152 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
154 if ( isset( $prop['timestamp'] ) ) {
155 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
157 if ( isset( $prop['hidden'] ) ) {
158 $vals['hidden'] = !is_null( $row->pp_propname
);
161 $fit = $this->addPageSubItem( $row->cl_from
, $vals );
163 $this->setContinueEnumParameter( 'continue', $row->cl_from
. '|' . $row->cl_to
);
169 foreach ( $res as $row ) {
170 if ( ++
$count > $params['limit'] ) {
171 // We've reached the one extra which shows that
172 // there are additional pages to be had. Stop here...
173 $this->setContinueEnumParameter( 'continue', $row->cl_from
. '|' . $row->cl_to
);
177 $titles[] = Title
::makeTitle( NS_CATEGORY
, $row->cl_to
);
179 $resultPageSet->populateFromTitles( $titles );
183 public function getAllowedParams() {
186 ApiBase
::PARAM_ISMULTI
=> true,
187 ApiBase
::PARAM_TYPE
=> [
192 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
195 ApiBase
::PARAM_ISMULTI
=> true,
196 ApiBase
::PARAM_TYPE
=> [
202 ApiBase
::PARAM_DFLT
=> 10,
203 ApiBase
::PARAM_TYPE
=> 'limit',
204 ApiBase
::PARAM_MIN
=> 1,
205 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
206 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
209 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
212 ApiBase
::PARAM_ISMULTI
=> true,
215 ApiBase
::PARAM_DFLT
=> 'ascending',
216 ApiBase
::PARAM_TYPE
=> [
224 protected function getExamplesMessages() {
226 'action=query&prop=categories&titles=Albert%20Einstein'
227 => 'apihelp-query+categories-example-simple',
228 'action=query&generator=categories&titles=Albert%20Einstein&prop=info'
229 => 'apihelp-query+categories-example-generator',
233 public function getHelpUrls() {
234 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categories';