Merge "Title::moveToInternal doesn't return anything, but it does throw an exception"
[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 $op = $params['dir'] == 'descending' ? '<' : '>';
69 $ilfrom = intval( $cont[0] );
70 $ilto = $this->getDB()->addQuotes( $this->titleToKey( $cont[1] ) );
71 $this->addWhere(
72 "il_from $op $ilfrom OR " .
73 "(il_from = $ilfrom AND " .
74 "il_to $op= $ilto)"
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( $this->getPageSet()->getGoodTitles() ) == 1 ) {
81 $this->addOption( 'ORDER BY', 'il_to' . $sort );
82 } else {
83 $this->addOption( 'ORDER BY', array(
84 'il_from' . $sort,
85 'il_to' . $sort
86 ));
87 }
88 $this->addOption( 'LIMIT', $params['limit'] + 1 );
89
90 if ( !is_null( $params['images'] ) ) {
91 $images = array();
92 foreach ( $params['images'] as $img ) {
93 $title = Title::newFromText( $img );
94 if ( !$title || $title->getNamespace() != NS_FILE ) {
95 $this->setWarning( "\"$img\" is not a file" );
96 } else {
97 $images[] = $title->getDBkey();
98 }
99 }
100 $this->addWhereFld( 'il_to', $images );
101 }
102
103 $res = $this->select( __METHOD__ );
104
105 if ( is_null( $resultPageSet ) ) {
106 $count = 0;
107 foreach ( $res as $row ) {
108 if ( ++$count > $params['limit'] ) {
109 // We've reached the one extra which shows that
110 // there are additional pages to be had. Stop here...
111 $this->setContinueEnumParameter( 'continue', $row->il_from .
112 '|' . $this->keyToTitle( $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 .
120 '|' . $this->keyToTitle( $row->il_to ) );
121 break;
122 }
123 }
124 } else {
125 $titles = array();
126 $count = 0;
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 .
132 '|' . $this->keyToTitle( $row->il_to ) );
133 break;
134 }
135 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
136 }
137 $resultPageSet->populateFromTitles( $titles );
138 }
139 }
140
141 public function getCacheMode( $params ) {
142 return 'public';
143 }
144
145 public function getAllowedParams() {
146 return array(
147 'limit' => array(
148 ApiBase::PARAM_DFLT => 10,
149 ApiBase::PARAM_TYPE => 'limit',
150 ApiBase::PARAM_MIN => 1,
151 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
152 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
153 ),
154 'continue' => null,
155 'images' => array(
156 ApiBase::PARAM_ISMULTI => true,
157 ),
158 'dir' => array(
159 ApiBase::PARAM_DFLT => 'ascending',
160 ApiBase::PARAM_TYPE => array(
161 'ascending',
162 'descending'
163 )
164 ),
165 );
166 }
167
168 public function getParamDescription() {
169 return array(
170 'limit' => 'How many images to return',
171 'continue' => 'When more results are available, use this to continue',
172 'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
173 'dir' => 'The direction in which to list',
174 );
175 }
176
177 public function getDescription() {
178 return 'Returns all images contained on the given page(s)';
179 }
180
181 public function getPossibleErrors() {
182 return array_merge( parent::getPossibleErrors(), array(
183 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
184 ) );
185 }
186
187 public function getExamples() {
188 return array(
189 'api.php?action=query&prop=images&titles=Main%20Page' => 'Get a list of images used in the [[Main Page]]',
190 'api.php?action=query&generator=images&titles=Main%20Page&prop=info' => 'Get information about all images used in the [[Main Page]]',
191 );
192 }
193
194 public function getHelpUrls() {
195 return 'https://www.mediawiki.org/wiki/API:Properties#images_.2F_im';
196 }
197
198 public function getVersion() {
199 return __CLASS__ . ': $Id$';
200 }
201 }