Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / specials / SpecialListfiles.php
blob24bd19fc88aec61ab6830ed74858b10b3cecaa64
1 <?php
2 /**
3 * Implements Special:Listfiles
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 class SpecialListFiles extends IncludableSpecialPage {
25 public function __construct() {
26 parent::__construct( 'Listfiles' );
29 public function execute( $par ) {
30 $this->setHeaders();
31 $this->outputHeader();
33 if ( $this->including() ) {
34 $userName = $par;
35 $search = '';
36 } else {
37 $userName = $this->getRequest()->getText( 'user', $par );
38 $search = $this->getRequest()->getText( 'ilsearch', '' );
41 $pager = new ImageListPager(
42 $this->getContext(),
43 $userName,
44 $search,
45 $this->including()
48 if ( $this->including() ) {
49 $html = $pager->getBody();
50 } else {
51 $form = $pager->getForm();
52 $body = $pager->getBody();
53 $nav = $pager->getNavigationBar();
54 $html = "$form<br />\n$body<br />\n$nav";
56 $this->getOutput()->addHTML( $html );
59 protected function getGroupName() {
60 return 'media';
64 /**
65 * @ingroup SpecialPage Pager
67 class ImageListPager extends TablePager {
68 var $mFieldNames = null;
69 var $mQueryConds = array();
70 var $mUserName = null;
71 var $mSearch = '';
72 var $mIncluding = false;
74 function __construct( IContextSource $context, $userName = null, $search = '',
75 $including = false
76 ) {
77 global $wgMiserMode;
79 $this->mIncluding = $including;
81 if ( $userName ) {
82 $nt = Title::newFromText( $userName, NS_USER );
83 if ( !is_null( $nt ) ) {
84 $this->mUserName = $nt->getText();
85 $this->mQueryConds['img_user_text'] = $this->mUserName;
89 if ( $search != '' && !$wgMiserMode ) {
90 $this->mSearch = $search;
91 $nt = Title::newFromURL( $this->mSearch );
93 if ( $nt ) {
94 $dbr = wfGetDB( DB_SLAVE );
95 $this->mQueryConds[] = 'LOWER(img_name)' .
96 $dbr->buildLike( $dbr->anyString(),
97 strtolower( $nt->getDBkey() ), $dbr->anyString() );
101 if ( !$including ) {
102 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
103 $this->mDefaultDirection = true;
104 } else {
105 $this->mDefaultDirection = false;
107 } else {
108 $this->mDefaultDirection = true;
111 parent::__construct( $context );
115 * @return Array
117 function getFieldNames() {
118 if ( !$this->mFieldNames ) {
119 global $wgMiserMode;
120 $this->mFieldNames = array(
121 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
122 'img_name' => $this->msg( 'listfiles_name' )->text(),
123 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
124 'img_size' => $this->msg( 'listfiles_size' )->text(),
125 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
126 'img_description' => $this->msg( 'listfiles_description' )->text(),
128 if ( !$wgMiserMode ) {
129 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
133 return $this->mFieldNames;
136 function isFieldSortable( $field ) {
137 global $wgMiserMode;
138 if ( $this->mIncluding ) {
139 return false;
141 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
142 if ( $wgMiserMode && isset( $this->mQueryConds['img_user_text'] ) ) {
143 // If we're sorting by user, the index only supports sorting by time
144 if ( $field === 'img_timestamp' ) {
145 return true;
146 } else {
147 return false;
151 return in_array( $field, $sortable );
154 function getQueryInfo() {
155 $tables = array( 'image' );
156 $fields = array_keys( $this->getFieldNames() );
157 $fields[] = 'img_user';
158 $fields[array_search( 'thumb', $fields )] = 'img_name AS thumb';
159 $options = $join_conds = array();
161 # Depends on $wgMiserMode
162 if ( isset( $this->mFieldNames['count'] ) ) {
163 $tables[] = 'oldimage';
165 # Need to rewrite this one
166 foreach ( $fields as &$field ) {
167 if ( $field == 'count' ) {
168 $field = 'COUNT(oi_archive_name) AS count';
171 unset( $field );
173 $dbr = wfGetDB( DB_SLAVE );
174 if ( $dbr->implicitGroupby() ) {
175 $options = array( 'GROUP BY' => 'img_name' );
176 } else {
177 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
178 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
180 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
183 return array(
184 'tables' => $tables,
185 'fields' => $fields,
186 'conds' => $this->mQueryConds,
187 'options' => $options,
188 'join_conds' => $join_conds
192 function getDefaultSort() {
193 return 'img_timestamp';
196 function doBatchLookups() {
197 $userIds = array();
198 $this->mResult->seek( 0 );
199 foreach ( $this->mResult as $row ) {
200 $userIds[] = $row->img_user;
202 # Do a link batch query for names and userpages
203 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
206 function formatValue( $field, $value ) {
207 switch ( $field ) {
208 case 'thumb':
209 $file = wfLocalFile( $value );
210 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
212 return $thumb->toHtml( array( 'desc-link' => true ) );
213 case 'img_timestamp':
214 $timeAndDate = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
215 return htmlspecialchars( $timeAndDate );
216 case 'img_name':
217 static $imgfile = null;
218 if ( $imgfile === null ) {
219 $imgfile = $this->msg( 'imgfile' )->text();
222 // Weird files can maybe exist? Bug 22227
223 $filePage = Title::makeTitleSafe( NS_FILE, $value );
224 if ( $filePage ) {
225 $link = Linker::linkKnown(
226 $filePage,
227 htmlspecialchars( $filePage->getText() )
229 $download = Xml::element( 'a',
230 array( 'href' => wfLocalFile( $filePage )->getURL() ),
231 $imgfile
233 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
235 return "$link $download";
236 } else {
237 return htmlspecialchars( $value );
239 case 'img_user_text':
240 if ( $this->mCurrentRow->img_user ) {
241 $name = User::whoIs( $this->mCurrentRow->img_user );
242 $link = Linker::link(
243 Title::makeTitle( NS_USER, $name ),
244 htmlspecialchars( $name )
246 } else {
247 $link = htmlspecialchars( $value );
250 return $link;
251 case 'img_size':
252 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
253 case 'img_description':
254 return Linker::formatComment( $value );
255 case 'count':
256 return intval( $value ) + 1;
260 function getForm() {
261 global $wgScript, $wgMiserMode;
262 $inputForm = array();
263 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
264 if ( !$wgMiserMode ) {
265 $inputForm['listfiles_search_for'] = Html::input(
266 'ilsearch',
267 $this->mSearch,
268 'text',
269 array(
270 'size' => '40',
271 'maxlength' => '255',
272 'id' => 'mw-ilsearch',
273 'tabindex' => 2,
277 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
278 'size' => '40',
279 'maxlength' => '255',
280 'id' => 'mw-listfiles-user',
281 'tabindex' => 3,
282 ) );
284 return Html::openElement( 'form',
285 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
287 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
288 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
289 Xml::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 4 ) ) .
290 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title' ) ) .
291 Html::closeElement( 'fieldset' ) .
292 Html::closeElement( 'form' ) . "\n";
295 function getTableClass() {
296 return 'listfiles ' . parent::getTableClass();
299 function getNavClass() {
300 return 'listfiles_nav ' . parent::getNavClass();
303 function getSortHeaderClass() {
304 return 'listfiles_sort ' . parent::getSortHeaderClass();
307 function getPagingQueries() {
308 $queries = parent::getPagingQueries();
309 if ( !is_null( $this->mUserName ) ) {
310 # Append the username to the query string
311 foreach ( $queries as &$query ) {
312 $query['user'] = $this->mUserName;
316 return $queries;
319 function getDefaultQuery() {
320 $queries = parent::getDefaultQuery();
321 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
322 $queries['user'] = $this->mUserName;
325 return $queries;
328 function getTitle() {
329 return SpecialPage::getTitleFor( 'Listfiles' );