(bug 23845) Special:ListFiles now uses correct file names without underscores
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 function wfSpecialListfiles( $par = null ) {
11 global $wgOut;
12
13 $pager = new ImageListPager( $par );
14
15 $limit = $pager->getForm();
16 $body = $pager->getBody();
17 $nav = $pager->getNavigationBar();
18 $wgOut->addHTML( "$limit<br />\n$body<br />\n$nav" );
19 }
20
21 /**
22 * @ingroup SpecialPage Pager
23 */
24 class ImageListPager extends TablePager {
25 var $mFieldNames = null;
26 var $mQueryConds = array();
27 var $mUserName = null;
28
29 function __construct( $par = null ) {
30 global $wgRequest, $wgMiserMode;
31 if ( $wgRequest->getText( 'sort', 'img_date' ) == 'img_date' ) {
32 $this->mDefaultDirection = true;
33 } else {
34 $this->mDefaultDirection = false;
35 }
36
37 $userName = $wgRequest->getText( 'username', $par );
38 if ( $userName ) {
39 $nt = Title::newFromText( $userName, NS_USER );
40 if ( !is_null( $nt ) ) {
41 $this->mUserName = $nt->getText();
42 $this->mQueryConds['img_user_text'] = $this->mUserName;
43 }
44 }
45
46 $search = $wgRequest->getText( 'ilsearch' );
47 if ( $search != '' && !$wgMiserMode ) {
48 $nt = Title::newFromURL( $search );
49 if ( $nt ) {
50 $dbr = wfGetDB( DB_SLAVE );
51 $this->mQueryConds[] = 'LOWER(img_name)' . $dbr->buildLike( $dbr->anyString(),
52 strtolower( $nt->getDBkey() ), $dbr->anyString() );
53 }
54 }
55
56 parent::__construct();
57 }
58
59 function getFieldNames() {
60 if ( !$this->mFieldNames ) {
61 global $wgMiserMode;
62 $this->mFieldNames = array(
63 'img_timestamp' => wfMsg( 'listfiles_date' ),
64 'img_name' => wfMsg( 'listfiles_name' ),
65 'img_user_text' => wfMsg( 'listfiles_user' ),
66 'img_size' => wfMsg( 'listfiles_size' ),
67 'img_description' => wfMsg( 'listfiles_description' ),
68 );
69 if( !$wgMiserMode ) {
70 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
71 }
72 }
73 return $this->mFieldNames;
74 }
75
76 function isFieldSortable( $field ) {
77 static $sortable = array( 'img_timestamp', 'img_name' );
78 if ( $field == 'img_size' ) {
79 # No index for both img_size and img_user_text
80 return !isset( $this->mQueryConds['img_user_text'] );
81 }
82 return in_array( $field, $sortable );
83 }
84
85 function getQueryInfo() {
86 $tables = array( 'image' );
87 $fields = array_keys( $this->getFieldNames() );
88 $fields[] = 'img_user';
89 $options = $join_conds = array();
90
91 # Depends on $wgMiserMode
92 if( isset( $this->mFieldNames['count'] ) ) {
93 $tables[] = 'oldimage';
94
95 # Need to rewrite this one
96 foreach ( $fields as &$field )
97 if ( $field == 'count' )
98 $field = 'COUNT(oi_archive_name) as count';
99 unset( $field );
100
101 $dbr = wfGetDB( DB_SLAVE );
102 if( $dbr->implicitGroupby() ) {
103 $options = array( 'GROUP BY' => 'img_name' );
104 } else {
105 $columnlist = implode( ',', preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
106 $options = array( 'GROUP BY' => "img_user, $columnlist" );
107 }
108 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
109 }
110 return array(
111 'tables' => $tables,
112 'fields' => $fields,
113 'conds' => $this->mQueryConds,
114 'options' => $options,
115 'join_conds' => $join_conds
116 );
117 }
118
119 function getDefaultSort() {
120 return 'img_timestamp';
121 }
122
123 function getStartBody() {
124 # Do a link batch query for user pages
125 if ( $this->mResult->numRows() ) {
126 $lb = new LinkBatch;
127 $this->mResult->seek( 0 );
128 while ( $row = $this->mResult->fetchObject() ) {
129 if ( $row->img_user ) {
130 $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
131 }
132 }
133 $lb->execute();
134 }
135
136 return parent::getStartBody();
137 }
138
139 function formatValue( $field, $value ) {
140 global $wgLang;
141 switch ( $field ) {
142 case 'img_timestamp':
143 return htmlspecialchars( $wgLang->timeanddate( $value, true ) );
144 case 'img_name':
145 static $imgfile = null;
146 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
147
148 $filePage = Title::makeTitle( NS_FILE, $value );
149 $link = $this->getSkin()->linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
150 $image = wfLocalFile( $value );
151 $url = $image->getURL();
152 $download = Xml::element('a', array( 'href' => $url ), $imgfile );
153 return "$link ($download)";
154 case 'img_user_text':
155 if ( $this->mCurrentRow->img_user ) {
156 $link = $this->getSkin()->link(
157 Title::makeTitle( NS_USER, $value ),
158 htmlspecialchars( $value )
159 );
160 } else {
161 $link = htmlspecialchars( $value );
162 }
163 return $link;
164 case 'img_size':
165 return $this->getSkin()->formatSize( $value );
166 case 'img_description':
167 return $this->getSkin()->commentBlock( $value );
168 case 'count':
169 return intval($value)+1;
170 }
171 }
172
173 function getForm() {
174 global $wgRequest, $wgScript, $wgMiserMode;
175 $search = $wgRequest->getText( 'ilsearch' );
176
177 $s = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
178 Xml::openElement( 'fieldset' ) .
179 Xml::element( 'legend', null, wfMsg( 'listfiles' ) ) .
180 Xml::tags( 'label', null, wfMsgHtml( 'table_pager_limit', $this->getLimitSelect() ) );
181
182 if ( !$wgMiserMode ) {
183 $s .= "<br />\n" .
184 Xml::inputLabel( wfMsg( 'listfiles_search_for' ), 'ilsearch', 'mw-ilsearch', 20, $search );
185 }
186 $s .= ' ' .
187 Xml::submitButton( wfMsg( 'table_pager_limit_submit' ) ) ."\n" .
188 $this->getHiddenFields( array( 'limit', 'ilsearch' ) ) .
189 Xml::closeElement( 'fieldset' ) .
190 Xml::closeElement( 'form' ) . "\n";
191 return $s;
192 }
193
194 function getTableClass() {
195 return 'listfiles ' . parent::getTableClass();
196 }
197
198 function getNavClass() {
199 return 'listfiles_nav ' . parent::getNavClass();
200 }
201
202 function getSortHeaderClass() {
203 return 'listfiles_sort ' . parent::getSortHeaderClass();
204 }
205
206 function getPagingQueries() {
207 $queries = parent::getPagingQueries();
208 if ( !is_null( $this->mUserName ) ) {
209 # Append the username to the query string
210 foreach ( $queries as $key => &$query ) {
211 $query['username'] = $this->mUserName;
212 }
213 }
214 return $queries;
215 }
216 }