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 * This query adds an <images> subelement to all pages with the list of images embedded into those pages.
32 class ApiQueryImages
extends ApiQueryGeneratorBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'im' );
38 public function execute() {
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
47 * @param $resultPageSet ApiPageSet
49 private function run( $resultPageSet = null ) {
50 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
51 return; // nothing to do
54 $params = $this->extractRequestParams();
55 $this->addFields( array(
60 $this->addTables( 'imagelinks' );
61 $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
62 if ( !is_null( $params['continue'] ) ) {
63 $cont = explode( '|', $params['continue'] );
64 if ( count( $cont ) != 2 ) {
65 $this->dieUsage( 'Invalid continue param. You should pass the ' .
66 'original value returned by the previous query', '_badcontinue' );
68 $ilfrom = intval( $cont[0] );
69 $ilto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
71 "il_from > $ilfrom OR " .
72 "(il_from = $ilfrom AND " .
77 $dir = ( $params['dir'] == 'descending' ?
' DESC' : '' );
78 // Don't order by il_from if it's constant in the WHERE clause
79 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
80 $this->addOption( 'ORDER BY', 'il_to' . $dir );
82 $this->addOption( 'ORDER BY', array(
87 $this->addOption( 'LIMIT', $params['limit'] +
1 );
89 if ( !is_null( $params['images'] ) ) {
91 foreach ( $params['images'] as $img ) {
92 $title = Title
::newFromText( $img );
93 if ( !$title ||
$title->getNamespace() != NS_FILE
) {
94 $this->setWarning( "\"$img\" is not a file" );
96 $images[] = $title->getDBkey();
99 $this->addWhereFld( 'il_to', $images );
102 $res = $this->select( __METHOD__
);
104 if ( is_null( $resultPageSet ) ) {
106 foreach ( $res as $row ) {
107 if ( ++
$count > $params['limit'] ) {
108 // We've reached the one extra which shows that
109 // there are additional pages to be had. Stop here...
110 $this->setContinueEnumParameter( 'continue', $row->il_from
.
111 '|' . $this->keyToTitle( $row->il_to
) );
115 ApiQueryBase
::addTitleInfo( $vals, Title
::makeTitle( NS_FILE
, $row->il_to
) );
116 $fit = $this->addPageSubItem( $row->il_from
, $vals );
118 $this->setContinueEnumParameter( 'continue', $row->il_from
.
119 '|' . $this->keyToTitle( $row->il_to
) );
126 foreach ( $res as $row ) {
127 if ( ++
$count > $params['limit'] ) {
128 // We've reached the one extra which shows that
129 // there are additional pages to be had. Stop here...
130 $this->setContinueEnumParameter( 'continue', $row->il_from
.
131 '|' . $this->keyToTitle( $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 ApiBase
::PARAM_DFLT
=> 10,
148 ApiBase
::PARAM_TYPE
=> 'limit',
149 ApiBase
::PARAM_MIN
=> 1,
150 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
151 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
155 ApiBase
::PARAM_ISMULTI
=> true,
158 ApiBase
::PARAM_DFLT
=> 'ascending',
159 ApiBase
::PARAM_TYPE
=> array(
167 public function getParamDescription() {
169 'limit' => 'How many images to return',
170 'continue' => 'When more results are available, use this to continue',
171 'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
172 'dir' => 'The direction in which to list',
176 public function getDescription() {
177 return 'Returns all images contained on the given page(s)';
180 public function getPossibleErrors() {
181 return array_merge( parent
::getPossibleErrors(), array(
182 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
186 public function getExamples() {
188 'api.php?action=query&prop=images&titles=Main%20Page' => 'Get a list of images used in the [[Main Page]]',
189 'api.php?action=query&generator=images&titles=Main%20Page&prop=info' => 'Get information about all images used in the [[Main Page]]',
193 public function getHelpUrls() {
194 return 'https://www.mediawiki.org/wiki/API:Properties#images_.2F_im';
197 public function getVersion() {
198 return __CLASS__
. ': $Id$';