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