More parameter documentation
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33 * This query adds an <images> subelement to all pages with the list of images embedded into those pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryImages extends ApiQueryGeneratorBase {
38
39 public function __construct( $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 $resultPageSet ApiPageSet
53 * @return
54 */
55 private function run( $resultPageSet = null ) {
56 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
57 return; // nothing to do
58 }
59
60 $params = $this->extractRequestParams();
61 $this->addFields( array(
62 'il_from',
63 'il_to'
64 ) );
65
66 $this->addTables( 'imagelinks' );
67 $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
68 if ( !is_null( $params['continue'] ) ) {
69 $cont = explode( '|', $params['continue'] );
70 if ( count( $cont ) != 2 ) {
71 $this->dieUsage( 'Invalid continue param. You should pass the ' .
72 'original value returned by the previous query', '_badcontinue' );
73 }
74 $ilfrom = intval( $cont[0] );
75 $ilto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
76 $this->addWhere(
77 "il_from > $ilfrom OR " .
78 "(il_from = $ilfrom AND " .
79 "il_to >= '$ilto')"
80 );
81 }
82
83 // Don't order by il_from if it's constant in the WHERE clause
84 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
85 $this->addOption( 'ORDER BY', 'il_to' );
86 } else {
87 $this->addOption( 'ORDER BY', 'il_from, il_to' );
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 .
113 '|' . $this->keyToTitle( $row->il_to ) );
114 break;
115 }
116 $vals = array();
117 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
118 $fit = $this->addPageSubItem( $row->il_from, $vals );
119 if ( !$fit ) {
120 $this->setContinueEnumParameter( 'continue', $row->il_from .
121 '|' . $this->keyToTitle( $row->il_to ) );
122 break;
123 }
124 }
125 } else {
126 $titles = array();
127 $count = 0;
128 foreach ( $res as $row ) {
129 if ( ++$count > $params['limit'] ) {
130 // We've reached the one extra which shows that
131 // there are additional pages to be had. Stop here...
132 $this->setContinueEnumParameter( 'continue', $row->il_from .
133 '|' . $this->keyToTitle( $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 array(
148 'limit' => array(
149 ApiBase::PARAM_DFLT => 10,
150 ApiBase::PARAM_TYPE => 'limit',
151 ApiBase::PARAM_MIN => 1,
152 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
153 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
154 ),
155 'continue' => null,
156 'images' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 )
159 );
160 }
161
162 public function getParamDescription() {
163 return array(
164 'limit' => 'How many images to return',
165 'continue' => 'When more results are available, use this to continue',
166 'images' => 'Only list these images. Useful for checking whether a certain page has a certain Image.',
167 );
168 }
169
170 public function getDescription() {
171 return 'Returns all images contained on the given page(s)';
172 }
173
174 public function getPossibleErrors() {
175 return array_merge( parent::getPossibleErrors(), array(
176 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
177 ) );
178 }
179
180 protected function getExamples() {
181 return array(
182 'Get a list of images used in the [[Main Page]]:',
183 ' api.php?action=query&prop=images&titles=Main%20Page',
184 'Get information about all images used in the [[Main Page]]:',
185 ' api.php?action=query&generator=images&titles=Main%20Page&prop=info'
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }