NewFilesPager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Pager
  20. */
  21. /**
  22. * @ingroup Pager
  23. */
  24. use MediaWiki\Linker\LinkRenderer;
  25. use MediaWiki\MediaWikiServices;
  26. class NewFilesPager extends RangeChronologicalPager {
  27. /**
  28. * @var ImageGalleryBase
  29. */
  30. protected $gallery;
  31. /**
  32. * @var FormOptions
  33. */
  34. protected $opts;
  35. /**
  36. * @param IContextSource $context
  37. * @param FormOptions $opts
  38. * @param LinkRenderer $linkRenderer
  39. */
  40. public function __construct( IContextSource $context, FormOptions $opts,
  41. LinkRenderer $linkRenderer
  42. ) {
  43. parent::__construct( $context, $linkRenderer );
  44. $this->opts = $opts;
  45. $this->setLimit( $opts->getValue( 'limit' ) );
  46. $startTimestamp = '';
  47. $endTimestamp = '';
  48. if ( $opts->getValue( 'start' ) ) {
  49. $startTimestamp = $opts->getValue( 'start' ) . ' 00:00:00';
  50. }
  51. if ( $opts->getValue( 'end' ) ) {
  52. $endTimestamp = $opts->getValue( 'end' ) . ' 23:59:59';
  53. }
  54. $this->getDateRangeCond( $startTimestamp, $endTimestamp );
  55. }
  56. function getQueryInfo() {
  57. $opts = $this->opts;
  58. $conds = [];
  59. $actorQuery = ActorMigration::newMigration()->getJoin( 'img_user' );
  60. $tables = [ 'image' ] + $actorQuery['tables'];
  61. $fields = [ 'img_name', 'img_timestamp' ] + $actorQuery['fields'];
  62. $options = [];
  63. $jconds = $actorQuery['joins'];
  64. $user = $opts->getValue( 'user' );
  65. if ( $user !== '' ) {
  66. $conds[] = ActorMigration::newMigration()
  67. ->getWhere( wfGetDB( DB_REPLICA ), 'img_user', User::newFromName( $user, false ) )['conds'];
  68. }
  69. if ( !$opts->getValue( 'showbots' ) ) {
  70. $groupsWithBotPermission = MediaWikiServices::getInstance()
  71. ->getPermissionManager()
  72. ->getGroupsWithPermission( 'bot' );
  73. if ( count( $groupsWithBotPermission ) ) {
  74. $dbr = wfGetDB( DB_REPLICA );
  75. $tables[] = 'user_groups';
  76. $conds[] = 'ug_group IS NULL';
  77. $jconds['user_groups'] = [
  78. 'LEFT JOIN',
  79. [
  80. 'ug_group' => $groupsWithBotPermission,
  81. 'ug_user = ' . $actorQuery['fields']['img_user'],
  82. 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
  83. ]
  84. ];
  85. }
  86. }
  87. if ( $opts->getValue( 'hidepatrolled' ) ) {
  88. $tables[] = 'recentchanges';
  89. $conds['rc_type'] = RC_LOG;
  90. $conds['rc_log_type'] = 'upload';
  91. $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
  92. $conds['rc_namespace'] = NS_FILE;
  93. $jconds['recentchanges'] = [
  94. 'JOIN',
  95. [
  96. 'rc_title = img_name',
  97. 'rc_actor = ' . $actorQuery['fields']['img_actor'],
  98. 'rc_timestamp = img_timestamp'
  99. ]
  100. ];
  101. // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
  102. // It sometimes decides to query `recentchanges` first and filesort the result set later
  103. // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
  104. $options[] = 'STRAIGHT_JOIN';
  105. }
  106. if ( $opts->getValue( 'mediatype' ) ) {
  107. $conds['img_media_type'] = $opts->getValue( 'mediatype' );
  108. }
  109. $likeVal = $opts->getValue( 'like' );
  110. if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
  111. $dbr = wfGetDB( DB_REPLICA );
  112. $likeObj = Title::newFromText( $likeVal );
  113. if ( $likeObj instanceof Title ) {
  114. $like = $dbr->buildLike(
  115. $dbr->anyString(),
  116. strtolower( $likeObj->getDBkey() ),
  117. $dbr->anyString()
  118. );
  119. $conds[] = "LOWER(img_name) $like";
  120. }
  121. }
  122. $query = [
  123. 'tables' => $tables,
  124. 'fields' => $fields,
  125. 'join_conds' => $jconds,
  126. 'conds' => $conds,
  127. 'options' => $options,
  128. ];
  129. return $query;
  130. }
  131. function getIndexField() {
  132. return 'img_timestamp';
  133. }
  134. protected function getStartBody() {
  135. if ( !$this->gallery ) {
  136. // Note that null for mode is taken to mean use default.
  137. $mode = $this->getRequest()->getVal( 'gallerymode', null );
  138. try {
  139. $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
  140. } catch ( Exception $e ) {
  141. // User specified something invalid, fallback to default.
  142. $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
  143. }
  144. }
  145. return '';
  146. }
  147. protected function getEndBody() {
  148. return $this->gallery->toHTML();
  149. }
  150. function formatRow( $row ) {
  151. $name = $row->img_name;
  152. $user = User::newFromId( $row->img_user );
  153. $title = Title::makeTitle( NS_FILE, $name );
  154. $ul = $this->getLinkRenderer()->makeLink(
  155. $user->getUserPage(),
  156. $user->getName()
  157. );
  158. $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
  159. $this->gallery->add(
  160. $title,
  161. "$ul<br />\n<i>"
  162. . htmlspecialchars( $time )
  163. . "</i><br />\n"
  164. );
  165. return '';
  166. }
  167. }