445198500c8a7a5bc46952952b2188b308fae999
[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 private function run( $resultPageSet = null ) {
52 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
53 return; // nothing to do
54 }
55
56 $params = $this->extractRequestParams();
57 $this->addFields( array(
58 'il_from',
59 'il_to'
60 ) );
61
62 $this->addTables( 'imagelinks' );
63 $this->addWhereFld( 'il_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
64 if ( !is_null( $params['continue'] ) ) {
65 $cont = explode( '|', $params['continue'] );
66 if ( count( $cont ) != 2 ) {
67 $this->dieUsage( 'Invalid continue param. You should pass the ' .
68 'original value returned by the previous query', '_badcontinue' );
69 }
70 $ilfrom = intval( $cont[0] );
71 $ilto = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
72 $this->addWhere(
73 "il_from > $ilfrom OR " .
74 "(il_from = $ilfrom AND " .
75 "il_to >= '$ilto')"
76 );
77 }
78
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' );
82 } else {
83 $this->addOption( 'ORDER BY', 'il_from, il_to' );
84 }
85 $this->addOption( 'LIMIT', $params['limit'] + 1 );
86
87 $res = $this->select( __METHOD__ );
88
89 if ( is_null( $resultPageSet ) ) {
90 $count = 0;
91 foreach ( $res as $row ) {
92 if ( ++$count > $params['limit'] ) {
93 // We've reached the one extra which shows that
94 // there are additional pages to be had. Stop here...
95 $this->setContinueEnumParameter( 'continue', $row->il_from .
96 '|' . $this->keyToTitle( $row->il_to ) );
97 break;
98 }
99 $vals = array();
100 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( NS_FILE, $row->il_to ) );
101 $fit = $this->addPageSubItem( $row->il_from, $vals );
102 if ( !$fit ) {
103 $this->setContinueEnumParameter( 'continue', $row->il_from .
104 '|' . $this->keyToTitle( $row->il_to ) );
105 break;
106 }
107 }
108 } else {
109 $titles = array();
110 $count = 0;
111 foreach ( $res as $row ) {
112 if ( ++$count > $params['limit'] ) {
113 // We've reached the one extra which shows that
114 // there are additional pages to be had. Stop here...
115 $this->setContinueEnumParameter( 'continue', $row->il_from .
116 '|' . $this->keyToTitle( $row->il_to ) );
117 break;
118 }
119 $titles[] = Title::makeTitle( NS_FILE, $row->il_to );
120 }
121 $resultPageSet->populateFromTitles( $titles );
122 }
123 }
124
125 public function getCacheMode( $params ) {
126 return 'public';
127 }
128
129 public function getAllowedParams() {
130 return array(
131 'limit' => array(
132 ApiBase::PARAM_DFLT => 10,
133 ApiBase::PARAM_TYPE => 'limit',
134 ApiBase::PARAM_MIN => 1,
135 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
136 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
137 ),
138 'continue' => null,
139 );
140 }
141
142 public function getParamDescription() {
143 return array(
144 'limit' => 'How many images to return',
145 'continue' => 'When more results are available, use this to continue',
146 );
147 }
148
149 public function getDescription() {
150 return 'Returns all images contained on the given page(s)';
151 }
152
153 public function getPossibleErrors() {
154 return array_merge( parent::getPossibleErrors(), array(
155 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
156 ) );
157 }
158
159 protected function getExamples() {
160 return array(
161 'Get a list of images used in the [[Main Page]]:',
162 ' api.php?action=query&prop=images&titles=Main%20Page',
163 'Get information about all images used in the [[Main Page]]:',
164 ' api.php?action=query&generator=images&titles=Main%20Page&prop=info'
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }