Merge "Fix ApiWatchTest"
[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
29 * images embedded into those pages.
30 *
31 * @ingroup API
32 */
33 class ApiQueryImages extends ApiQueryGeneratorBase {
34
35 public function __construct( $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'im' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
45 }
46
47 /**
48 * @param $resultPageSet ApiPageSet
49 */
50 private function run( $resultPageSet = null ) {
51 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
52 return; // nothing to do
53 }
54
55 $params = $this->extractRequestParams();
56 $this->addFields( array(
57 'il_from',
58 'il_to'
59 ) );
60
61 $this->addTables( 'imagelinks' );
62 $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
63 if ( !is_null( $params['continue'] ) ) {
64 $cont = explode( '|', $params['continue'] );
65 if ( count( $cont ) != 2 ) {
66 $this->dieUsage( 'Invalid continue param. You should pass the ' .
67 'original value returned by the previous query', '_badcontinue' );
68 }
69 $op = $params['dir'] == 'descending' ? '<' : '>';
70 $ilfrom = intval( $cont[0] );
71 $ilto = $this->getDB()->addQuotes( $cont[1] );
72 $this->addWhere(
73 "il_from $op $ilfrom OR " .
74 "(il_from = $ilfrom AND " .
75 "il_to $op= $ilto)"
76 );
77 }
78
79 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
80 // Don't order by il_from if it's constant in the WHERE clause
81 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
82 $this->addOption( 'ORDER BY', 'il_to' . $sort );
83 } else {
84 $this->addOption( 'ORDER BY', array(
85 'il_from' . $sort,
86 'il_to' . $sort
87 ));
88 }
89 $this->addOption( 'LIMIT', $params['limit'] + 1 );
90
91 if ( !is_null( $params['images'] ) ) {
92 $images = array();
93 foreach ( $params['images'] as $img ) {
94 $title = Title::newFromText( $img );
95 if ( !$title || $title->getNamespace() != NS_FILE ) {
96 $this->setWarning( "\"$img\" is not a file" );
97 } else {
98 $images[] = $title->getDBkey();
99 }
100 }
101 $this->addWhereFld( 'il_to', $images );
102 }
103
104 $res = $this->select( __METHOD__ );
105
106 if ( is_null( $resultPageSet ) ) {
107 $count = 0;
108 foreach ( $res as $row ) {
109 if ( ++$count > $params['limit'] ) {
110 // We've reached the one extra which shows that
111 // there are additional pages to be had. Stop here...
112 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $row->il_to );
113 break;
114 }
115 $vals = array();
116 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
117 $fit = $this->addPageSubItem( $row->il_from, $vals );
118 if ( !$fit ) {
119 $this->setContinueEnumParameter( 'continue', $row->il_from . '|' . $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 . '|' . $row->il_to );
131 break;
132 }
133 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
134 }
135 $resultPageSet->populateFromTitles( $titles );
136 }
137 }
138
139 public function getCacheMode( $params ) {
140 return 'public';
141 }
142
143 public function getAllowedParams() {
144 return array(
145 'limit' => array(
146 ApiBase::PARAM_DFLT => 10,
147 ApiBase::PARAM_TYPE => 'limit',
148 ApiBase::PARAM_MIN => 1,
149 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
150 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
151 ),
152 'continue' => null,
153 'images' => array(
154 ApiBase::PARAM_ISMULTI => true,
155 ),
156 'dir' => array(
157 ApiBase::PARAM_DFLT => 'ascending',
158 ApiBase::PARAM_TYPE => array(
159 'ascending',
160 'descending'
161 )
162 ),
163 );
164 }
165
166 public function getParamDescription() {
167 return array(
168 'limit' => 'How many images to return',
169 'continue' => 'When more results are available, use this to continue',
170 'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
171 'dir' => 'The direction in which to list',
172 );
173 }
174
175 public function getResultProperties() {
176 return array(
177 '' => array(
178 'ns' => 'namespace',
179 'title' => 'string'
180 )
181 );
182 }
183
184 public function getDescription() {
185 return 'Returns all images contained on the given page(s)';
186 }
187
188 public function getPossibleErrors() {
189 return array_merge( parent::getPossibleErrors(), array(
190 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
191 ) );
192 }
193
194 public function getExamples() {
195 return array(
196 'api.php?action=query&prop=images&titles=Main%20Page' => 'Get a list of images used in the [[Main Page]]',
197 'api.php?action=query&generator=images&titles=Main%20Page&prop=info' => 'Get information about all images used in the [[Main Page]]',
198 );
199 }
200
201 public function getHelpUrls() {
202 return 'https://www.mediawiki.org/wiki/API:Properties#images_.2F_im';
203 }
204
205 public function getVersion() {
206 return __CLASS__ . ': $Id$';
207 }
208 }