MediaWiki  REL1_19
SpecialListfiles.php
Go to the documentation of this file.
00001 <?php
00024 class SpecialListFiles extends IncludableSpecialPage {
00025 
00026         public function __construct(){
00027                 parent::__construct( 'Listfiles' );
00028         }
00029 
00030         public function execute( $par ){
00031                 $this->setHeaders();
00032                 $this->outputHeader();
00033 
00034                 if ( $this->including() ) {
00035                         $userName = $par;
00036                         $search = '';
00037                 } else {
00038                         $userName = $this->getRequest()->getText( 'user', $par );
00039                         $search = $this->getRequest()->getText( 'ilsearch', '' );
00040                 }
00041 
00042                 $pager = new ImageListPager( $this->getContext(), $userName, $search, $this->including() );
00043 
00044                 if ( $this->including() ) {
00045                         $html = $pager->getBody();
00046                 } else {
00047                         $form = $pager->getForm();
00048                         $body = $pager->getBody();
00049                         $nav = $pager->getNavigationBar();
00050                         $html = "$form<br />\n$body<br />\n$nav";
00051                 }
00052                 $this->getOutput()->addHTML( $html );
00053         }
00054 }
00055 
00059 class ImageListPager extends TablePager {
00060         var $mFieldNames = null;
00061         var $mQueryConds = array();
00062         var $mUserName = null;
00063         var $mSearch = '';
00064         var $mIncluding = false;
00065 
00066         function __construct( IContextSource $context, $userName = null, $search = '', $including = false ) {
00067                 global $wgMiserMode;
00068 
00069                 $this->mIncluding = $including;
00070 
00071                 if ( $userName ) {
00072                         $nt = Title::newFromText( $userName, NS_USER );
00073                         if ( !is_null( $nt ) ) {
00074                                 $this->mUserName = $nt->getText();
00075                                 $this->mQueryConds['img_user_text'] = $this->mUserName;
00076                         }
00077                 }
00078 
00079                 if ( $search != '' && !$wgMiserMode ) {
00080                         $this->mSearch = $search;
00081                         $nt = Title::newFromURL( $this->mSearch );
00082                         if ( $nt ) {
00083                                 $dbr = wfGetDB( DB_SLAVE );
00084                                 $this->mQueryConds[] = 'LOWER(img_name)' .
00085                                         $dbr->buildLike( $dbr->anyString(),
00086                                                 strtolower( $nt->getDBkey() ), $dbr->anyString() );
00087                         }
00088                 }
00089 
00090                 if ( !$including ) {
00091                         if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
00092                                 $this->mDefaultDirection = true;
00093                         } else {
00094                                 $this->mDefaultDirection = false;
00095                         }
00096                 } else {
00097                         $this->mDefaultDirection = true;
00098                 }
00099 
00100                 parent::__construct( $context );
00101         }
00102 
00106         function getFieldNames() {
00107                 if ( !$this->mFieldNames ) {
00108                         global $wgMiserMode;
00109                         $this->mFieldNames = array(
00110                                 'img_timestamp' => wfMsg( 'listfiles_date' ),
00111                                 'img_name' => wfMsg( 'listfiles_name' ),
00112                                 'thumb' => wfMsg( 'listfiles_thumb' ),
00113                                 'img_size' => wfMsg( 'listfiles_size' ),
00114                                 'img_user_text' => wfMsg( 'listfiles_user' ),
00115                                 'img_description' => wfMsg( 'listfiles_description' ),
00116                         );
00117                         if( !$wgMiserMode ) {
00118                                 $this->mFieldNames['count'] = wfMsg( 'listfiles_count' );
00119                         }
00120                 }
00121                 return $this->mFieldNames;
00122         }
00123 
00124         function isFieldSortable( $field ) {
00125                 if ( $this->mIncluding ) {
00126                         return false;
00127                 }
00128                 static $sortable = array( 'img_timestamp', 'img_name' );
00129                 if ( $field == 'img_size' ) {
00130                         # No index for both img_size and img_user_text
00131                         return !isset( $this->mQueryConds['img_user_text'] );
00132                 }
00133                 return in_array( $field, $sortable );
00134         }
00135 
00136         function getQueryInfo() {
00137                 $tables = array( 'image' );
00138                 $fields = array_keys( $this->getFieldNames() );
00139                 $fields[] = 'img_user';
00140                 $fields[array_search('thumb', $fields)] = 'img_name AS thumb';
00141                 $options = $join_conds = array();
00142 
00143                 # Depends on $wgMiserMode
00144                 if( isset( $this->mFieldNames['count'] ) ) {
00145                         $tables[] = 'oldimage';
00146 
00147                         # Need to rewrite this one
00148                         foreach ( $fields as &$field ) {
00149                                 if ( $field == 'count' ) {
00150                                         $field = 'COUNT(oi_archive_name) AS count';
00151                                 }
00152                         }
00153                         unset( $field );
00154 
00155                         $dbr = wfGetDB( DB_SLAVE );
00156                         if( $dbr->implicitGroupby() ) {
00157                                 $options = array( 'GROUP BY' => 'img_name' );
00158                         } else {
00159                                 $columnlist = implode( ',',
00160                                         preg_grep( '/^img/', array_keys( $this->getFieldNames() ) ) );
00161                                 $options = array( 'GROUP BY' => "img_user, $columnlist" );
00162                         }
00163                         $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
00164                 }
00165                 return array(
00166                         'tables'     => $tables,
00167                         'fields'     => $fields,
00168                         'conds'      => $this->mQueryConds,
00169                         'options'    => $options,
00170                         'join_conds' => $join_conds
00171                 );
00172         }
00173 
00174         function getDefaultSort() {
00175                 return 'img_timestamp';
00176         }
00177 
00178         function getStartBody() {
00179                 # Do a link batch query for user pages
00180                 if ( $this->mResult->numRows() ) {
00181                         $lb = new LinkBatch;
00182                         $this->mResult->seek( 0 );
00183                         foreach ( $this->mResult as $row ) {
00184                                 if ( $row->img_user ) {
00185                                         $lb->add( NS_USER, str_replace( ' ', '_', $row->img_user_text ) );
00186                                 }
00187                         }
00188                         $lb->execute();
00189                 }
00190 
00191                 return parent::getStartBody();
00192         }
00193 
00194         function formatValue( $field, $value ) {
00195                 switch ( $field ) {
00196                         case 'thumb':
00197                                 $file = wfLocalFile( $value );
00198                                 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
00199                                 return $thumb->toHtml( array( 'desc-link' => true ) );
00200                         case 'img_timestamp':
00201                                 return htmlspecialchars( $this->getLanguage()->timeanddate( $value, true ) );
00202                         case 'img_name':
00203                                 static $imgfile = null;
00204                                 if ( $imgfile === null ) $imgfile = wfMsg( 'imgfile' );
00205 
00206                                 // Weird files can maybe exist? Bug 22227
00207                                 $filePage = Title::makeTitleSafe( NS_FILE, $value );
00208                                 if( $filePage ) {
00209                                         $link = Linker::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
00210                                         $download = Xml::element( 'a',
00211                                                 array( 'href' => wfLocalFile( $filePage )->getURL() ),
00212                                                 $imgfile
00213                                         );
00214                                         return "$link ($download)";
00215                                 } else {
00216                                         return htmlspecialchars( $value );
00217                                 }
00218                         case 'img_user_text':
00219                                 if ( $this->mCurrentRow->img_user ) {
00220                                         $link = Linker::link(
00221                                                 Title::makeTitle( NS_USER, $value ),
00222                                                 htmlspecialchars( $value )
00223                                         );
00224                                 } else {
00225                                         $link = htmlspecialchars( $value );
00226                                 }
00227                                 return $link;
00228                         case 'img_size':
00229                                 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
00230                         case 'img_description':
00231                                 return Linker::commentBlock( $value );
00232                         case 'count':
00233                                 return intval( $value ) + 1;
00234                 }
00235         }
00236 
00237         function getForm() {
00238                 global $wgScript, $wgMiserMode;
00239                 $inputForm = array();
00240                 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
00241                 if ( !$wgMiserMode ) {
00242                         $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $this->mSearch, 'text',
00243                                 array(
00244                                         'size'          => '40',
00245                                         'maxlength' => '255',
00246                                         'id'            => 'mw-ilsearch',
00247                         ) );
00248                 }
00249                 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
00250                         'size'          => '40',
00251                         'maxlength' => '255',
00252                         'id'            => 'mw-listfiles-user',
00253                 ) );
00254                 return Html::openElement( 'form',
00255                                 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
00256                         Xml::fieldset( wfMsg( 'listfiles' ) ) .
00257                         Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
00258                         $this->getHiddenFields( array( 'limit', 'ilsearch', 'user' ) ) .
00259                         Html::closeElement( 'fieldset' ) .
00260                         Html::closeElement( 'form' ) . "\n";
00261         }
00262 
00263         function getTableClass() {
00264                 return 'listfiles ' . parent::getTableClass();
00265         }
00266 
00267         function getNavClass() {
00268                 return 'listfiles_nav ' . parent::getNavClass();
00269         }
00270 
00271         function getSortHeaderClass() {
00272                 return 'listfiles_sort ' . parent::getSortHeaderClass();
00273         }
00274 
00275         function getPagingQueries() {
00276                 $queries = parent::getPagingQueries();
00277                 if ( !is_null( $this->mUserName ) ) {
00278                         # Append the username to the query string
00279                         foreach ( $queries as &$query ) {
00280                                 $query['user'] = $this->mUserName;
00281                         }
00282                 }
00283                 return $queries;
00284         }
00285 
00286         function getDefaultQuery() {
00287                 $queries = parent::getDefaultQuery();
00288                 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
00289                         $queries['user'] = $this->mUserName;
00290                 }
00291                 return $queries;
00292         }
00293 }