MediaWiki  REL1_20
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' => $this->msg( 'listfiles_date' )->text(),
00111                                 'img_name' => $this->msg( 'listfiles_name' )->text(),
00112                                 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
00113                                 'img_size' => $this->msg( 'listfiles_size' )->text(),
00114                                 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
00115                                 'img_description' => $this->msg( 'listfiles_description' )->text(),
00116                         );
00117                         if( !$wgMiserMode ) {
00118                                 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
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 = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
00160                                 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
00161                         }
00162                         $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
00163                 }
00164                 return array(
00165                         'tables'     => $tables,
00166                         'fields'     => $fields,
00167                         'conds'      => $this->mQueryConds,
00168                         'options'    => $options,
00169                         'join_conds' => $join_conds
00170                 );
00171         }
00172 
00173         function getDefaultSort() {
00174                 return 'img_timestamp';
00175         }
00176 
00177         function doBatchLookups() {
00178                 $userIds = array();
00179                 $this->mResult->seek( 0 );
00180                 foreach ( $this->mResult as $row ) {
00181                         $userIds[] = $row->img_user;
00182                 }
00183                 # Do a link batch query for names and userpages
00184                 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
00185         }
00186 
00187         function formatValue( $field, $value ) {
00188                 switch ( $field ) {
00189                         case 'thumb':
00190                                 $file = wfLocalFile( $value );
00191                                 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
00192                                 return $thumb->toHtml( array( 'desc-link' => true ) );
00193                         case 'img_timestamp':
00194                                 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
00195                         case 'img_name':
00196                                 static $imgfile = null;
00197                                 if ( $imgfile === null ) $imgfile = $this->msg( 'imgfile' )->text();
00198 
00199                                 // Weird files can maybe exist? Bug 22227
00200                                 $filePage = Title::makeTitleSafe( NS_FILE, $value );
00201                                 if( $filePage ) {
00202                                         $link = Linker::linkKnown( $filePage, htmlspecialchars( $filePage->getText() ) );
00203                                         $download = Xml::element( 'a',
00204                                                 array( 'href' => wfLocalFile( $filePage )->getURL() ),
00205                                                 $imgfile
00206                                         );
00207                                         $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
00208                                         return "$link $download";
00209                                 } else {
00210                                         return htmlspecialchars( $value );
00211                                 }
00212                         case 'img_user_text':
00213                                 if ( $this->mCurrentRow->img_user ) {
00214                                         $name = User::whoIs( $this->mCurrentRow->img_user );
00215                                         $link = Linker::link(
00216                                                 Title::makeTitle( NS_USER, $name ),
00217                                                 htmlspecialchars( $name )
00218                                         );
00219                                 } else {
00220                                         $link = htmlspecialchars( $value );
00221                                 }
00222                                 return $link;
00223                         case 'img_size':
00224                                 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
00225                         case 'img_description':
00226                                 return Linker::commentBlock( $value );
00227                         case 'count':
00228                                 return intval( $value ) + 1;
00229                 }
00230         }
00231 
00232         function getForm() {
00233                 global $wgScript, $wgMiserMode;
00234                 $inputForm = array();
00235                 $inputForm['table_pager_limit_label'] = $this->getLimitSelect();
00236                 if ( !$wgMiserMode ) {
00237                         $inputForm['listfiles_search_for'] = Html::input( 'ilsearch', $this->mSearch, 'text',
00238                                 array(
00239                                         'size'          => '40',
00240                                         'maxlength' => '255',
00241                                         'id'            => 'mw-ilsearch',
00242                         ) );
00243                 }
00244                 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
00245                         'size'          => '40',
00246                         'maxlength' => '255',
00247                         'id'            => 'mw-listfiles-user',
00248                 ) );
00249                 return Html::openElement( 'form',
00250                                 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' ) ) .
00251                         Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
00252                         Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
00253                         Xml::buildForm( $inputForm, 'table_pager_limit_submit' ) .
00254                         $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title' ) ) .
00255                         Html::closeElement( 'fieldset' ) .
00256                         Html::closeElement( 'form' ) . "\n";
00257         }
00258 
00259         function getTableClass() {
00260                 return 'listfiles ' . parent::getTableClass();
00261         }
00262 
00263         function getNavClass() {
00264                 return 'listfiles_nav ' . parent::getNavClass();
00265         }
00266 
00267         function getSortHeaderClass() {
00268                 return 'listfiles_sort ' . parent::getSortHeaderClass();
00269         }
00270 
00271         function getPagingQueries() {
00272                 $queries = parent::getPagingQueries();
00273                 if ( !is_null( $this->mUserName ) ) {
00274                         # Append the username to the query string
00275                         foreach ( $queries as &$query ) {
00276                                 $query['user'] = $this->mUserName;
00277                         }
00278                 }
00279                 return $queries;
00280         }
00281 
00282         function getDefaultQuery() {
00283                 $queries = parent::getDefaultQuery();
00284                 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
00285                         $queries['user'] = $this->mUserName;
00286                 }
00287                 return $queries;
00288         }
00289 
00290         function getTitle() {
00291                 return SpecialPage::getTitleFor( 'Listfiles' );
00292         }
00293 }