3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
25 use MediaWiki\Title\Title
;
26 use Wikimedia\ParamValidator\ParamValidator
;
27 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
30 * This query adds an "<images>" subelement to all pages with the list of
31 * images embedded into those pages.
35 class ApiQueryImages
extends ApiQueryGeneratorBase
{
37 public function __construct( ApiQuery
$query, string $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'im' );
41 public function execute() {
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
50 * @param ApiPageSet|null $resultPageSet
52 private function run( $resultPageSet = null ) {
53 $pages = $this->getPageSet()->getGoodPages();
54 if ( $pages === [] ) {
55 return; // nothing to do
58 $params = $this->extractRequestParams();
64 $this->addTables( 'imagelinks' );
65 $this->addWhereFld( 'il_from', array_keys( $pages ) );
66 if ( $params['continue'] !== null ) {
68 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
69 $op = $params['dir'] == 'descending' ?
'<=' : '>=';
70 $this->addWhere( $db->buildComparison( $op, [
71 'il_from' => $cont[0],
76 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
77 // Don't order by il_from if it's constant in the WHERE clause
78 if ( count( $pages ) === 1 ) {
79 $this->addOption( 'ORDER BY', 'il_to' . $sort );
81 $this->addOption( 'ORDER BY', [
86 $this->addOption( 'LIMIT', $params['limit'] +
1 );
88 if ( $params['images'] ) {
90 foreach ( $params['images'] as $img ) {
91 $title = Title
::newFromText( $img );
92 if ( !$title ||
$title->getNamespace() !== NS_FILE
) {
93 $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
95 $images[] = $title->getDBkey();
99 // No titles so no results
102 $this->addWhereFld( 'il_to', $images );
105 $res = $this->select( __METHOD__
);
107 if ( $resultPageSet === null ) {
109 foreach ( $res as $row ) {
110 if ( ++
$count > $params['limit'] ) {
111 // We've reached the one extra which shows that
112 // there are additional pages to be had. Stop here...
113 $this->setContinueEnumParameter( 'continue', $row->il_from
. '|' . $row->il_to
);
117 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( NS_FILE
, $row->il_to
) );
118 $fit = $this->addPageSubItem( $row->il_from
, $vals );
120 $this->setContinueEnumParameter( 'continue', $row->il_from
. '|' . $row->il_to
);
127 foreach ( $res as $row ) {
128 if ( ++
$count > $params['limit'] ) {
129 // We've reached the one extra which shows that
130 // there are additional pages to be had. Stop here...
131 $this->setContinueEnumParameter( 'continue', $row->il_from
. '|' . $row->il_to
);
134 $titles[] = Title
::makeTitle( NS_FILE
, $row->il_to
);
136 $resultPageSet->populateFromTitles( $titles );
140 public function getCacheMode( $params ) {
144 public function getAllowedParams() {
147 ParamValidator
::PARAM_DEFAULT
=> 10,
148 ParamValidator
::PARAM_TYPE
=> 'limit',
149 IntegerDef
::PARAM_MIN
=> 1,
150 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
151 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
154 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
157 ParamValidator
::PARAM_ISMULTI
=> true,
160 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
161 ParamValidator
::PARAM_TYPE
=> [
169 protected function getExamplesMessages() {
170 $title = Title
::newMainPage()->getPrefixedText();
171 $mp = rawurlencode( $title );
174 "action=query&prop=images&titles={$mp}"
175 => 'apihelp-query+images-example-simple',
176 "action=query&generator=images&titles={$mp}&prop=info"
177 => 'apihelp-query+images-example-generator',
181 public function getHelpUrls() {
182 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
186 /** @deprecated class alias since 1.43 */
187 class_alias( ApiQueryImages
::class, 'ApiQueryImages' );