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