MediaWiki  master
NewFilesPager.php
Go to the documentation of this file.
1 <?php
27 
29 
33  protected $gallery;
34 
38  protected $opts;
39 
47  ) {
48  parent::__construct( $context, $linkRenderer );
49 
50  $this->opts = $opts;
51  $this->setLimit( $opts->getValue( 'limit' ) );
52 
53  $startTimestamp = '';
54  $endTimestamp = '';
55  if ( $opts->getValue( 'start' ) ) {
56  $startTimestamp = $opts->getValue( 'start' ) . ' 00:00:00';
57  }
58  if ( $opts->getValue( 'end' ) ) {
59  $endTimestamp = $opts->getValue( 'end' ) . ' 23:59:59';
60  }
61  $this->getDateRangeCond( $startTimestamp, $endTimestamp );
62  }
63 
64  function getQueryInfo() {
66  $conds = [];
67  $actorQuery = ActorMigration::newMigration()->getJoin( 'img_user' );
68  $tables = [ 'image' ] + $actorQuery['tables'];
69  $fields = [ 'img_name', 'img_timestamp' ] + $actorQuery['fields'];
70  $options = [];
71  $jconds = $actorQuery['joins'];
72 
73  $user = $opts->getValue( 'user' );
74  if ( $user !== '' ) {
75  $conds[] = ActorMigration::newMigration()
76  ->getWhere( wfGetDB( DB_REPLICA ), 'img_user', User::newFromName( $user, false ) )['conds'];
77  }
78 
79  if ( !$opts->getValue( 'showbots' ) ) {
80  $groupsWithBotPermission = MediaWikiServices::getInstance()
81  ->getPermissionManager()
82  ->getGroupsWithPermission( 'bot' );
83 
84  if ( count( $groupsWithBotPermission ) ) {
85  $dbr = wfGetDB( DB_REPLICA );
86  $tables[] = 'user_groups';
87  $conds[] = 'ug_group IS NULL';
88  $jconds['user_groups'] = [
89  'LEFT JOIN',
90  [
91  'ug_group' => $groupsWithBotPermission,
92  'ug_user = ' . $actorQuery['fields']['img_user'],
93  'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
94  ]
95  ];
96  }
97  }
98 
99  if ( $opts->getValue( 'hidepatrolled' ) ) {
100  $tables[] = 'recentchanges';
101  $conds['rc_type'] = RC_LOG;
102  $conds['rc_log_type'] = 'upload';
103  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
104  $conds['rc_namespace'] = NS_FILE;
105 
106  $jconds['recentchanges'] = [
107  'JOIN',
108  [
109  'rc_title = img_name',
110  'rc_actor = ' . $actorQuery['fields']['img_actor'],
111  'rc_timestamp = img_timestamp'
112  ]
113  ];
114  // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
115  // It sometimes decides to query `recentchanges` first and filesort the result set later
116  // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
117  $options[] = 'STRAIGHT_JOIN';
118  }
119 
120  if ( $opts->getValue( 'mediatype' ) ) {
121  $conds['img_media_type'] = $opts->getValue( 'mediatype' );
122  }
123 
124  $likeVal = $opts->getValue( 'like' );
125  if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
126  $dbr = wfGetDB( DB_REPLICA );
127  $likeObj = Title::newFromText( $likeVal );
128  if ( $likeObj instanceof Title ) {
129  $like = $dbr->buildLike(
130  $dbr->anyString(),
131  strtolower( $likeObj->getDBkey() ),
132  $dbr->anyString()
133  );
134  $conds[] = "LOWER(img_name) $like";
135  }
136  }
137 
138  $query = [
139  'tables' => $tables,
140  'fields' => $fields,
141  'join_conds' => $jconds,
142  'conds' => $conds,
143  'options' => $options,
144  ];
145 
146  return $query;
147  }
148 
149  function getIndexField() {
150  return 'img_timestamp';
151  }
152 
153  protected function getStartBody() {
154  if ( !$this->gallery ) {
155  // Note that null for mode is taken to mean use default.
156  $mode = $this->getRequest()->getVal( 'gallerymode', null );
157  try {
158  $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
159  } catch ( Exception $e ) {
160  // User specified something invalid, fallback to default.
161  $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
162  }
163  }
164 
165  return '';
166  }
167 
168  protected function getEndBody() {
169  return $this->gallery->toHTML();
170  }
171 
172  function formatRow( $row ) {
173  $name = $row->img_name;
174  $user = User::newFromId( $row->img_user );
175 
176  $title = Title::makeTitle( NS_FILE, $name );
177  $ul = $this->getLinkRenderer()->makeLink(
178  $user->getUserPage(),
179  $user->getName()
180  );
181  $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
182 
183  $this->gallery->add(
184  $title,
185  "$ul<br />\n<i>"
186  . htmlspecialchars( $time )
187  . "</i><br />\n"
188  );
189  return '';
190  }
191 }
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
FormOptions $opts
__construct(IContextSource $context, FormOptions $opts, LinkRenderer $linkRenderer)
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
ImageGalleryBase $gallery
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
LinkRenderer $linkRenderer
Definition: IndexPager.php:162
IContextSource $context
Class that generates HTML links for pages.
static newMigration()
Static constructor.
getDateRangeCond( $startStamp, $endStamp)
Set and return a date range condition using timestamps provided by the user.
getContext()
Get the base IContextSource object.
const NS_FILE
Definition: Defines.php:66
const PRC_UNPATROLLED
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
setLimit( $limit)
Set the limit from an other source than the request.
Definition: IndexPager.php:306
const DB_REPLICA
Definition: defines.php:25
Pager for filtering by a range of dates.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
getValue( $name)
Get the value for the given option name.
const RC_LOG
Definition: Defines.php:124
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319