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