MediaWiki  master
NewFilesPager.php
Go to the documentation of this file.
1 <?php
26 
31 
35  protected $gallery;
36 
40  protected $opts;
41 
44 
47 
56  public function __construct(
61  ILoadBalancer $loadBalancer,
63  ) {
64  // Set database before parent constructor to avoid setting it there with wfGetDB
65  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
66 
67  parent::__construct( $context, $linkRenderer );
68 
69  $this->opts = $opts;
70  $this->groupPermissionsLookup = $groupPermissionsLookup;
71  $this->linkBatchFactory = $linkBatchFactory;
72  $this->setLimit( $opts->getValue( 'limit' ) );
73 
74  $startTimestamp = '';
75  $endTimestamp = '';
76  if ( $opts->getValue( 'start' ) ) {
77  $startTimestamp = $opts->getValue( 'start' ) . ' 00:00:00';
78  }
79  if ( $opts->getValue( 'end' ) ) {
80  $endTimestamp = $opts->getValue( 'end' ) . ' 23:59:59';
81  }
82  $this->getDateRangeCond( $startTimestamp, $endTimestamp );
83  }
84 
85  public function getQueryInfo() {
87  $conds = [];
88  $dbr = $this->getDatabase();
89  $tables = [ 'image', 'actor' ];
90  $fields = [ 'img_name', 'img_timestamp', 'actor_user', 'actor_name' ];
91  $options = [];
92  $jconds = [ 'actor' => [ 'JOIN', 'actor_id=img_actor' ] ];
93 
94  $user = $opts->getValue( 'user' );
95  if ( $user !== '' ) {
96  $conds['actor_name'] = $user;
97  }
98 
99  if ( !$opts->getValue( 'showbots' ) ) {
100  $groupsWithBotPermission = $this->groupPermissionsLookup->getGroupsWithPermission( 'bot' );
101 
102  if ( count( $groupsWithBotPermission ) ) {
103  $tables[] = 'user_groups';
104  $conds[] = 'ug_group IS NULL';
105  $jconds['user_groups'] = [
106  'LEFT JOIN',
107  [
108  'ug_group' => $groupsWithBotPermission,
109  'ug_user = actor_user',
110  'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
111  ]
112  ];
113  }
114  }
115 
116  if ( $opts->getValue( 'hidepatrolled' ) ) {
117  $tables[] = 'recentchanges';
118  $conds['rc_type'] = RC_LOG;
119  $conds['rc_log_type'] = 'upload';
120  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
121  $conds['rc_namespace'] = NS_FILE;
122 
123  $jconds['recentchanges'] = [
124  'JOIN',
125  [
126  'rc_title = img_name',
127  'rc_actor = img_actor',
128  'rc_timestamp = img_timestamp'
129  ]
130  ];
131  }
132 
133  if ( $opts->getValue( 'mediatype' ) ) {
134  $conds['img_media_type'] = $opts->getValue( 'mediatype' );
135  }
136 
137  // We're ordering by img_timestamp, but MariaDB sometimes likes to query other tables first
138  // and filesort the result set later.
139  // See T124205 / https://mariadb.atlassian.net/browse/MDEV-8880, and T244533
140  $options[] = 'STRAIGHT_JOIN';
141 
142  $query = [
143  'tables' => $tables,
144  'fields' => $fields,
145  'join_conds' => $jconds,
146  'conds' => $conds,
147  'options' => $options,
148  ];
149 
150  return $query;
151  }
152 
153  public function getIndexField() {
154  return 'img_timestamp';
155  }
156 
157  protected function getStartBody() {
158  if ( !$this->gallery ) {
159  // Note that null for mode is taken to mean use default.
160  $mode = $this->getRequest()->getVal( 'gallerymode', null );
161  try {
162  $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
163  } catch ( Exception $e ) {
164  // User specified something invalid, fallback to default.
165  $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
166  }
167  }
168 
169  return '';
170  }
171 
172  protected function getEndBody() {
173  return $this->gallery->toHTML();
174  }
175 
176  protected function doBatchLookups() {
177  $this->mResult->seek( 0 );
178  $lb = $this->linkBatchFactory->newLinkBatch();
179  foreach ( $this->mResult as $row ) {
180  if ( $row->actor_user ) {
181  $lb->add( NS_USER, $row->actor_name );
182  }
183  }
184  $lb->execute();
185  }
186 
187  public function formatRow( $row ) {
188  $username = $row->actor_name;
189 
190  if ( ExternalUserNames::isExternal( $username ) ) {
191  $ul = htmlspecialchars( $username );
192  } else {
193  $ul = $this->getLinkRenderer()->makeLink(
194  new TitleValue( NS_USER, $username ),
195  $username
196  );
197  }
198  $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
199 
200  $this->gallery->add(
201  Title::makeTitle( NS_FILE, $row->img_name ),
202  "$ul<br />\n<i>"
203  . htmlspecialchars( $time )
204  . "</i><br />\n",
205  '',
206  '',
207  [],
209  );
210 
211  return '';
212  }
213 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:39
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:47
MediaWiki\Permissions\GroupPermissionsLookup
Definition: GroupPermissionsLookup.php:30
FormOptions\getValue
getValue( $name)
Get the value for the given option name.
Definition: FormOptions.php:182
ImageGalleryBase
Image gallery.
Definition: ImageGalleryBase.php:32
MediaWiki\Linker\LinkRenderer
Class that generates HTML anchor link elements for pages.
Definition: LinkRenderer.php:43
ImageGalleryBase\LOADING_LAZY
const LOADING_LAZY
Definition: ImageGalleryBase.php:34
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:248
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
NewFilesPager\$gallery
ImageGalleryBase $gallery
Definition: NewFilesPager.php:35
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:81
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a server index.
NewFilesPager\$groupPermissionsLookup
GroupPermissionsLookup $groupPermissionsLookup
Definition: NewFilesPager.php:43
$dbr
$dbr
Definition: testCompression.php:54
NewFilesPager\formatRow
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition: NewFilesPager.php:187
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:153
RC_LOG
const RC_LOG
Definition: Defines.php:117
NewFilesPager\__construct
__construct(IContextSource $context, FormOptions $opts, LinkRenderer $linkRenderer, GroupPermissionsLookup $groupPermissionsLookup, ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
Definition: NewFilesPager.php:56
IndexPager\setLimit
setLimit( $limit)
Set the limit from an other source than the request.
Definition: IndexPager.php:336
RangeChronologicalPager\getDateRangeCond
getDateRangeCond( $startStamp, $endStamp)
Set and return a date range condition using timestamps provided by the user.
Definition: RangeChronologicalPager.php:46
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
NewFilesPager\getStartBody
getStartBody()
Hook into getBody(), allows text to be inserted at the start.
Definition: NewFilesPager.php:157
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:648
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
RangeChronologicalPager
Pager for filtering by a range of dates.
Definition: RangeChronologicalPager.php:28
NewFilesPager\doBatchLookups
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
Definition: NewFilesPager.php:176
NewFilesPager\$opts
FormOptions $opts
Definition: NewFilesPager.php:40
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:1003
NS_USER
const NS_USER
Definition: Defines.php:66
NewFilesPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: NewFilesPager.php:46
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
NewFilesPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: NewFilesPager.php:85
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:92
NewFilesPager\getEndBody
getEndBody()
Hook into getBody() for the end of the list.
Definition: NewFilesPager.php:172
NewFilesPager\getIndexField
getIndexField()
Returns the name of the index field.
Definition: NewFilesPager.php:153
ImageGalleryBase\factory
static factory( $mode=false, IContextSource $context=null)
Get a new image gallery.
Definition: ImageGalleryBase.php:116
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
NS_FILE
const NS_FILE
Definition: Defines.php:70
ExternalUserNames\isExternal
static isExternal( $username)
Tells whether the username is external or not.
Definition: ExternalUserNames.php:149
NewFilesPager
Definition: NewFilesPager.php:30
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40