SpecialListfiles.php 5.1 KB

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