Removed the 'eclipse helper' bit on top of every API module
[lhc/web/wiklou.git] / includes / api / ApiQueryImages.php
1 <?php
2 /**
3 *
4 *
5 * Created on May 13, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * This query adds an <images> subelement to all pages with the list of images embedded into those pages.
29 *
30 * @ingroup API
31 */
32 class ApiQueryImages extends ApiQueryGeneratorBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'im' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param $resultPageSet ApiPageSet
48 */
49 private function run( $resultPageSet = null ) {
50 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
51 return; // nothing to do
52 }
53
54 $params = $this->extractRequestParams();
55 $this->addFields( array(
56 'il_from',
57 'il_to'
58 ) );
59
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' );
67 }
68 $ilfrom = intval( $cont[0] );
69 $ilto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
70 $this->addWhere(
71 "il_from > $ilfrom OR " .
72 "(il_from = $ilfrom AND " .
73 "il_to >= '$ilto')"
74 );
75 }
76
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 );
81 } else {
82 $this->addOption( 'ORDER BY', array(
83 'il_from' . $dir,
84 'il_to' . $dir
85 ));
86 }
87 $this->addOption( 'LIMIT', $params['limit'] + 1 );
88
89 if ( !is_null( $params['images'] ) ) {
90 $images = array();
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" );
95 } else {
96 $images[] = $title->getDBkey();
97 }
98 }
99 $this->addWhereFld( 'il_to', $images );
100 }
101
102 $res = $this->select( __METHOD__ );
103
104 if ( is_null( $resultPageSet ) ) {
105 $count = 0;
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 ) );
112 break;
113 }
114 $vals = array();
115 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
116 $fit = $this->addPageSubItem( $row->il_from, $vals );
117 if ( !$fit ) {
118 $this->setContinueEnumParameter( 'continue', $row->il_from .
119 '|' . $this->keyToTitle( $row->il_to ) );
120 break;
121 }
122 }
123 } else {
124 $titles = array();
125 $count = 0;
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 ) );
132 break;
133 }
134 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
135 }
136 $resultPageSet->populateFromTitles( $titles );
137 }
138 }
139
140 public function getCacheMode( $params ) {
141 return 'public';
142 }
143
144 public function getAllowedParams() {
145 return array(
146 'limit' => array(
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
152 ),
153 'continue' => null,
154 'images' => array(
155 ApiBase::PARAM_ISMULTI => true,
156 ),
157 'dir' => array(
158 ApiBase::PARAM_DFLT => 'ascending',
159 ApiBase::PARAM_TYPE => array(
160 'ascending',
161 'descending'
162 )
163 ),
164 );
165 }
166
167 public function getParamDescription() {
168 return array(
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',
173 );
174 }
175
176 public function getDescription() {
177 return 'Returns all images contained on the given page(s)';
178 }
179
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' ),
183 ) );
184 }
185
186 public function getExamples() {
187 return array(
188 'Get a list of images used in the [[Main Page]]:',
189 ' api.php?action=query&prop=images&titles=Main%20Page',
190 'Get information about all images used in the [[Main Page]]:',
191 ' api.php?action=query&generator=images&titles=Main%20Page&prop=info'
192 );
193 }
194
195 public function getHelpUrls() {
196 return 'http://www.mediawiki.org/wiki/API:Properties#images_.2F_im';
197 }
198
199 public function getVersion() {
200 return __CLASS__ . ': $Id$';
201 }
202 }