If the scaler URL is protocol-relative, things just break here. Sets a sane default.
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * Implements Special:Listfiles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 class SpecialListFiles extends IncludableSpecialPage {
25
26 public function __construct(){
27 parent::__construct( 'Listfiles' );
28 }
29
30 public function execute( $par ){
31 global $wgOut, $wgRequest;
32 $this->setHeaders();
33 $this->outputHeader();
34
35 if ( $this->including() ) {
36 $userName = $par;
37 $search = '';
38 } else {
39 $userName = $wgRequest->getText( 'user', $par );
40 $search = $wgRequest->getText( 'ilsearch', '' );
41 }
42
43 $pager = new ImageListPager( $userName, $search, $this->including() );
44
45 if ( $this->including() ) {
46 $html = $pager->getBody();
47 } else {
48 $form = $pager->getForm();
49 $body = $pager->getBody();
50 $nav = $pager->getNavigationBar();
51 $html = "$form<br />\n$body<br />\n$nav";
52 }
53 $wgOut->addHTML( $html );
54 }
55 }
56
57 /**
58 * @ingroup SpecialPage Pager
59 */
60 class ImageListPager extends TablePager {
61 var $mFieldNames = null;
62 var $mQueryConds = array();
63 var $mUserName = null;
64 var $mSearch = '';
65 var $mIncluding = false;
66
67 function __construct( $userName = null, $search = '', $including = false ) {
68 global $wgRequest, $wgMiserMode;
69
70 $this->mIncluding = $including;
71
72 if ( $userName ) {
73 $nt = Title::newFromText( $userName, NS_USER );
74 if ( !is_null( $nt ) ) {
75 $this->mUserName = $nt->getText();
76 $this->mQueryConds['img_user_text'] = $this->mUserName;
77 }
78 }
79
80 if ( $search != '' && !$wgMiserMode ) {
81 $this->mSearch = $search;
82 $nt = Title::newFromURL( $this->mSearch );
83 if ( $nt ) {
84 $dbr = wfGetDB( DB_SLAVE );
85 $this->mQueryConds[] = 'LOWER(img_name)' .
86 $dbr->buildLike( $dbr->anyString(),
87 strtolower( $nt->getDBkey() ), $dbr->anyString() );
88 }
89 }
90
91 if ( !$including ) {
92 if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ) {
93 $this->mDefaultDirection = true;
94 } else {
95 $this->mDefaultDirection = false;
96 }
97 } else {
98 $this->mDefaultDirection = true;
99 }
100
101 parent::__construct();
102 }
103
104 function getTitle() {
105 return SpecialPage::getTitleFor( 'Listfiles' );
106 }
107
108 /**
109 * @return Array
110 */
111 function getFieldNames() {
112 if ( !$this->mFieldNames ) {
113 global $wgMiserMode;
114 $this->mFieldNames = array(
115 'img_timestamp' => wfMsg( 'listfiles_date' ),
116 'img_name' => wfMsg( 'listfiles_name' ),
117 'thumb' => wfMsg( 'listfiles_thumb' ),
118 'img_size' => wfMsg( 'listfiles_size' ),
119 'img_user_text' => wfMsg( 'listfiles_user' ),
120 'img_description' => wfMsg( 'listfiles_description' ),
121 );
122 if( !$wgMiserMode ) {
123 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
124 }
125 }
126 return $this->mFieldNames;
127 }
128
129 function isFieldSortable( $field ) {
130 if ( $this->mIncluding ) {
131 return false;
132 }
133 static $sortable = array( 'img_timestamp', 'img_name' );
134 if ( $field == 'img_size' ) {
135 # No index for both img_size and img_user_text
136 return !isset( $this->mQueryConds['img_user_text'] );
137 }
138 return in_array( $field, $sortable );
139 }
140
141 function getQueryInfo() {
142 $tables = array( 'image' );
143 $fields = array_keys( $this->getFieldNames() );
144 $fields[] = 'img_user';
145 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
146 $options = $join_conds = array();
147
148 # Depends on $wgMiserMode
149 if( isset( $this->mFieldNames['count'] ) ) {
150 $tables[] = 'oldimage';
151
152 # Need to rewrite this one
153 foreach ( $fields as &$field ) {
154 if ( $field == 'count' ) {
155 $field = 'COUNT(oi_archive_name) AS count';
156 }
157 }
158 unset( $field );
159
160 $dbr = wfGetDB( DB_SLAVE );
161 if( $dbr->implicitGroupby() ) {
162 $options = array( 'GROUP BY' => 'img_name' );
163 } else {
164 $columnlist = implode( ',',
165 preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
166 $options = array( 'GROUP BY' => "img_user, $columnlist" );
167 }
168 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
169 }
170 return array(
171 'tables' => $tables,
172 'fields' => $fields,
173 'conds' => $this->mQueryConds,
174 'options' => $options,
175 'join_conds' => $join_conds
176 );
177 }
178
179 function getDefaultSort() {
180 return 'img_timestamp';
181 }
182
183 function getStartBody() {
184 # Do a link batch query for user pages
185 if ( $this->mResult->numRows() ) {
186 $lb = new LinkBatch;
187 $this->mResult->seek( 0 );
188 foreach ( $this->mResult as $row ) {
189 if ( $row->img_user ) {
190 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
191 }
192 }
193 $lb->execute();
194 }
195
196 return parent::getStartBody();
197 }
198
199 function formatValue( $field, $value ) {
200 global $wgLang;
201 switch ( $field ) {
202 case 'thumb':
203 $file = wfLocalFile( $value );
204 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
205 return $thumb->toHtml( array( 'desc-link' => true ) );
206 case 'img_timestamp':
207 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
208 case 'img_name':
209 static $imgfile = null;
210 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
211
212 // Weird files can maybe exist? Bug 22227
213 $filePage = Title::makeTitleSafe( NS_FILE, $value );
214 if( $filePage ) {
215 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
216 $download = Xml::element( 'a',
217 array( 'href' => wfLocalFile( $filePage )->getURL() ),
218 $imgfile
219 );
220 return "$link ($download)";
221 } else {
222 return htmlspecialchars( $value );
223 }
224 case 'img_user_text':
225 if ( $this->mCurrentRow->img_user ) {
226 $link = $this->getSkin()->link(
227 Title::makeTitle( NS_USER, $value ),
228 htmlspecialchars( $value )
229 );
230 } else {
231 $link = htmlspecialchars( $value );
232 }
233 return $link;
234 case 'img_size':
235 return $this->getSkin()->formatSize( $value );
236 case 'img_description':
237 return $this->getSkin()->commentBlock( $value );
238 case 'count':
239 return intval( $value ) + 1;
240 }
241 }
242
243 function getForm() {
244 global $wgScript, $wgMiserMode;
245 $inputForm = array();
246 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
247 if ( !$wgMiserMode ) {
248 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $this->mSearch, 'text',
249 array(
250 'size' => '40',
251 'maxlength' => '255',
252 'id' => 'mw-ilsearch',
253 ) );
254 }
255 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
256 'size' => '40',
257 'maxlength' => '255',
258 'id' => 'mw-listfiles-user',
259 ) );
260 return Html::openElement( 'form',
261 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
262 Xml::fieldset( wfMsg( 'listfiles' ) ) .
263 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
264 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
265 Html::closeElement( 'fieldset' ) .
266 Html::closeElement( 'form' ) . "\n";
267 }
268
269 function getTableClass() {
270 return 'listfiles ' . parent::getTableClass();
271 }
272
273 function getNavClass() {
274 return 'listfiles_nav ' . parent::getNavClass();
275 }
276
277 function getSortHeaderClass() {
278 return 'listfiles_sort ' . parent::getSortHeaderClass();
279 }
280
281 function getPagingQueries() {
282 $queries = parent::getPagingQueries();
283 if ( !is_null( $this->mUserName ) ) {
284 # Append the username to the query string
285 foreach ( $queries as &$query ) {
286 $query['user'] = $this->mUserName;
287 }
288 }
289 return $queries;
290 }
291
292 function getDefaultQuery() {
293 $queries = parent::getDefaultQuery();
294 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
295 $queries['user'] = $this->mUserName;
296 }
297 return $queries;
298 }
299 }