Followup r87347, bug 22227, use makeTitleSafe.
[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;
32 $this->setHeaders();
33 $this->outputHeader();
34
35 $pager = new ImageListPager( $par, $this->including() );
36
37 if ( $this->including() ) {
38 $html = $pager->getBody();
39 } else {
40 $limit = $pager->getForm();
41 $body = $pager->getBody();
42 $nav = $pager->getNavigationBar();
43 $html = "$limit<br />\n$body<br />\n$nav";
44 }
45 $wgOut->addHTML( $html );
46 }
47 }
48
49 /**
50 * @ingroup SpecialPage Pager
51 */
52 class ImageListPager extends TablePager {
53 var $mFieldNames = null;
54 var $mQueryConds = array();
55 var $mUserName = null;
56 var $mIncluding = false;
57
58 function __construct( $par = null, $including = false ) {
59 global $wgRequest, $wgMiserMode;
60
61 $this->mIncluding = $including;
62
63
64 $userName = $including ? $par : $wgRequest->getText( 'user', $par );
65 if ( $userName ) {
66 $nt = Title::newFromText( $userName, NS_USER );
67 if ( !is_null( $nt ) ) {
68 $this->mUserName = $nt->getText();
69 $this->mQueryConds['img_user_text'] = $this->mUserName;
70 }
71 }
72
73 if ( !$including ) {
74 if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ) {
75 $this->mDefaultDirection = true;
76 } else {
77 $this->mDefaultDirection = false;
78 }
79
80 $search = $wgRequest->getText( 'ilsearch' );
81 if ( $search != '' && !$wgMiserMode ) {
82 $nt = Title::newFromURL( $search );
83 if ( $nt ) {
84 $dbr = wfGetDB( DB_SLAVE );
85 $this->mQueryConds[] = 'LOWER(img_name)' . $dbr->buildLike( $dbr->anyString(),
86 strtolower( $nt->getDBkey() ), $dbr->anyString() );
87 }
88 }
89 } else {
90 $this->mDefaultDirection = true;
91 }
92
93 parent::__construct();
94 }
95
96 /**
97 * @return Array
98 */
99 function getFieldNames() {
100 if ( !$this->mFieldNames ) {
101 global $wgMiserMode;
102 $this->mFieldNames = array(
103 'img_timestamp' => wfMsg( 'listfiles_date' ),
104 'img_name' => wfMsg( 'listfiles_name' ),
105 'thumb' => wfMsg( 'listfiles_thumb' ),
106 'img_size' => wfMsg( 'listfiles_size' ),
107 'img_user_text' => wfMsg( 'listfiles_user' ),
108 'img_description' => wfMsg( 'listfiles_description' ),
109 );
110 if( !$wgMiserMode ) {
111 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
112 }
113 }
114 return $this->mFieldNames;
115 }
116
117 function isFieldSortable( $field ) {
118 if ( $this->mIncluding ) {
119 return false;
120 }
121 static $sortable = array( 'img_timestamp', 'img_name' );
122 if ( $field == 'img_size' ) {
123 # No index for both img_size and img_user_text
124 return !isset( $this->mQueryConds['img_user_text'] );
125 }
126 return in_array( $field, $sortable );
127 }
128
129 function getQueryInfo() {
130 $tables = array( 'image' );
131 $fields = array_keys( $this->getFieldNames() );
132 $fields[] = 'img_user';
133 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
134 $options = $join_conds = array();
135
136 # Depends on $wgMiserMode
137 if( isset( $this->mFieldNames['count'] ) ) {
138 $tables[] = 'oldimage';
139
140 # Need to rewrite this one
141 foreach ( $fields as &$field ) {
142 if ( $field == 'count' ) {
143 $field = 'COUNT(oi_archive_name) AS count';
144 }
145 }
146 unset( $field );
147
148 $dbr = wfGetDB( DB_SLAVE );
149 if( $dbr->implicitGroupby() ) {
150 $options = array( 'GROUP BY' => 'img_name' );
151 } else {
152 $columnlist = implode( ',', preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
153 $options = array( 'GROUP BY' => "img_user, $columnlist" );
154 }
155 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
156 }
157 return array(
158 'tables' => $tables,
159 'fields' => $fields,
160 'conds' => $this->mQueryConds,
161 'options' => $options,
162 'join_conds' => $join_conds
163 );
164 }
165
166 function getDefaultSort() {
167 return 'img_timestamp';
168 }
169
170 function getStartBody() {
171 # Do a link batch query for user pages
172 if ( $this->mResult->numRows() ) {
173 $lb = new LinkBatch;
174 $this->mResult->seek( 0 );
175 foreach ( $this->mResult as $row ) {
176 if ( $row->img_user ) {
177 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
178 }
179 }
180 $lb->execute();
181 }
182
183 return parent::getStartBody();
184 }
185
186 function formatValue( $field, $value ) {
187 global $wgLang;
188 switch ( $field ) {
189 case 'thumb':
190 $file = wfLocalFile( $value );
191 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
192 return $thumb->toHtml( array( 'desc-link' => true ) );
193 case 'img_timestamp':
194 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
195 case 'img_name':
196 static $imgfile = null;
197 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
198
199 // Weird files can maybe exist? Bug
200 $filePage = Title::makeTitleSafe( NS_FILE, $value );
201 if( $filePage ) {
202 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
203 $download = Xml::element( 'a',
204 array( 'href' => wfLocalFile( $filePage )->getURL() ),
205 $imgfile
206 );
207 return "$link ($download)";
208 } else {
209 return htmlspecialchars( $value );
210 }
211 case 'img_user_text':
212 if ( $this->mCurrentRow->img_user ) {
213 $link = $this->getSkin()->link(
214 Title::makeTitle( NS_USER, $value ),
215 htmlspecialchars( $value )
216 );
217 } else {
218 $link = htmlspecialchars( $value );
219 }
220 return $link;
221 case 'img_size':
222 return $this->getSkin()->formatSize( $value );
223 case 'img_description':
224 return $this->getSkin()->commentBlock( $value, null, false, false );
225 case 'count':
226 return intval( $value ) + 1;
227 }
228 }
229
230 function getForm() {
231 global $wgRequest, $wgScript, $wgMiserMode;
232 $search = $wgRequest->getText( 'ilsearch' );
233 $inputForm = array();
234 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
235 if ( !$wgMiserMode ) {
236 $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $search, 'text', array(
237 'size' => '40',
238 'maxlength' => '255',
239 'id' => 'mw-ilsearch',
240 ) );
241 }
242 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
243 'size' => '40',
244 'maxlength' => '255',
245 'id' => 'mw-listfiles-user',
246 ) );
247 $s = Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
248 Xml::fieldset( wfMsg( 'listfiles' ) ) .
249 Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
250 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
251 Html::closeElement( 'fieldset' ) .
252 Html::closeElement( 'form' ) . "\n";
253 return $s;
254 }
255
256 function getTableClass() {
257 return 'listfiles ' . parent::getTableClass();
258 }
259
260 function getNavClass() {
261 return 'listfiles_nav ' . parent::getNavClass();
262 }
263
264 function getSortHeaderClass() {
265 return 'listfiles_sort ' . parent::getSortHeaderClass();
266 }
267
268 function getPagingQueries() {
269 $queries = parent::getPagingQueries();
270 if ( !is_null( $this->mUserName ) ) {
271 # Append the username to the query string
272 foreach ( $queries as &$query ) {
273 $query['user'] = $this->mUserName;
274 }
275 }
276 return $queries;
277 }
278
279 function getDefaultQuery() {
280 $queries = parent::getDefaultQuery();
281 if ( !isset( $queries['user'] )
282 && !is_null( $this->mUserName ) )
283 {
284 $queries['user'] = $this->mUserName;
285 }
286 return $queries;
287 }
288 }