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