Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryImages
0.00% covered (danger)
0.00%
0 / 92
0.00% covered (danger)
0.00%
0 / 8
600
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
306
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23use MediaWiki\Title\Title;
24use Wikimedia\ParamValidator\ParamValidator;
25use Wikimedia\ParamValidator\TypeDef\IntegerDef;
26
27/**
28 * This query adds an "<images>" subelement to all pages with the list of
29 * images embedded into those pages.
30 *
31 * @ingroup API
32 */
33class ApiQueryImages extends ApiQueryGeneratorBase {
34
35    /**
36     * @param ApiQuery $query
37     * @param string $moduleName
38     */
39    public function __construct( ApiQuery $query, $moduleName ) {
40        parent::__construct( $query, $moduleName, 'im' );
41    }
42
43    public function execute() {
44        $this->run();
45    }
46
47    public function executeGenerator( $resultPageSet ) {
48        $this->run( $resultPageSet );
49    }
50
51    /**
52     * @param ApiPageSet|null $resultPageSet
53     */
54    private function run( $resultPageSet = null ) {
55        $pages = $this->getPageSet()->getGoodPages();
56        if ( $pages === [] ) {
57            return; // nothing to do
58        }
59
60        $params = $this->extractRequestParams();
61        $this->addFields( [
62            'il_from',
63            'il_to'
64        ] );
65
66        $this->addTables( 'imagelinks' );
67        $this->addWhereFld( 'il_from', array_keys( $pages ) );
68        if ( $params['continue'] !== null ) {
69            $db = $this->getDB();
70            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
71            $op = $params['dir'] == 'descending' ? '<=' : '>=';
72            $this->addWhere( $db->buildComparison( $op, [
73                'il_from' => $cont[0],
74                'il_to' => $cont[1],
75            ] ) );
76        }
77
78        $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
79        // Don't order by il_from if it's constant in the WHERE clause
80        if ( count( $pages ) === 1 ) {
81            $this->addOption( 'ORDER BY', 'il_to' . $sort );
82        } else {
83            $this->addOption( 'ORDER BY', [
84                'il_from' . $sort,
85                'il_to' . $sort
86            ] );
87        }
88        $this->addOption( 'LIMIT', $params['limit'] + 1 );
89
90        if ( $params['images'] ) {
91            $images = [];
92            foreach ( $params['images'] as $img ) {
93                $title = Title::newFromText( $img );
94                if ( !$title || $title->getNamespace() !== NS_FILE ) {
95                    $this->addWarning( [ 'apiwarn-notfile', wfEscapeWikiText( $img ) ] );
96                } else {
97                    $images[] = $title->getDBkey();
98                }
99            }
100            if ( !$images ) {
101                // No titles so no results
102                return;
103            }
104            $this->addWhereFld( 'il_to', $images );
105        }
106
107        $res = $this->select( __METHOD__ );
108
109        if ( $resultPageSet === null ) {
110            $count = 0;
111            foreach ( $res as $row ) {
112                if ( ++$count > $params['limit'] ) {
113                    // We've reached the one extra which shows that
114                    // there are additional pages to be had. Stop here...
115                    $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
116                    break;
117                }
118                $vals = [];
119                ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
120                $fit = $this->addPageSubItem( $row->il_from, $vals );
121                if ( !$fit ) {
122                    $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
123                    break;
124                }
125            }
126        } else {
127            $titles = [];
128            $count = 0;
129            foreach ( $res as $row ) {
130                if ( ++$count > $params['limit'] ) {
131                    // We've reached the one extra which shows that
132                    // there are additional pages to be had. Stop here...
133                    $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
134                    break;
135                }
136                $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
137            }
138            $resultPageSet->populateFromTitles( $titles );
139        }
140    }
141
142    public function getCacheMode( $params ) {
143        return 'public';
144    }
145
146    public function getAllowedParams() {
147        return [
148            'limit' => [
149                ParamValidator::PARAM_DEFAULT => 10,
150                ParamValidator::PARAM_TYPE => 'limit',
151                IntegerDef::PARAM_MIN => 1,
152                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
153                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
154            ],
155            'continue' => [
156                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
157            ],
158            'images' => [
159                ParamValidator::PARAM_ISMULTI => true,
160            ],
161            'dir' => [
162                ParamValidator::PARAM_DEFAULT => 'ascending',
163                ParamValidator::PARAM_TYPE => [
164                    'ascending',
165                    'descending'
166                ]
167            ],
168        ];
169    }
170
171    protected function getExamplesMessages() {
172        $title = Title::newMainPage()->getPrefixedText();
173        $mp = rawurlencode( $title );
174
175        return [
176            "action=query&prop=images&titles={$mp}"
177                => 'apihelp-query+images-example-simple',
178            "action=query&generator=images&titles={$mp}&prop=info"
179                => 'apihelp-query+images-example-generator',
180        ];
181    }
182
183    public function getHelpUrls() {
184        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Images';
185    }
186}