MediaWiki  master
NewFilesPager.php
Go to the documentation of this file.
1 <?php
25 
30 
34  protected $gallery;
35 
39  protected $opts;
40 
43 
45  private $actorMigration;
46 
48  private $userCache;
49 
59  public function __construct(
65  ILoadBalancer $loadBalancer,
67  ) {
68  // Set database before parent constructor to avoid setting it there with wfGetDB
69  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
70 
71  parent::__construct( $context, $linkRenderer );
72 
73  $this->opts = $opts;
74  $this->permissionManager = $permissionManager;
75  $this->actorMigration = $actorMigration;
76  $this->userCache = $userCache;
77  $this->setLimit( $opts->getValue( 'limit' ) );
78 
79  $startTimestamp = '';
80  $endTimestamp = '';
81  if ( $opts->getValue( 'start' ) ) {
82  $startTimestamp = $opts->getValue( 'start' ) . ' 00:00:00';
83  }
84  if ( $opts->getValue( 'end' ) ) {
85  $endTimestamp = $opts->getValue( 'end' ) . ' 23:59:59';
86  }
87  $this->getDateRangeCond( $startTimestamp, $endTimestamp );
88  }
89 
90  public function getQueryInfo() {
92  $conds = [];
93  $dbr = $this->getDatabase();
94  $actorQuery = $this->actorMigration->getJoin( 'img_user' );
95  $tables = [ 'image' ] + $actorQuery['tables'];
96  $fields = [ 'img_name', 'img_timestamp' ] + $actorQuery['fields'];
97  $options = [];
98  $jconds = $actorQuery['joins'];
99 
100  $user = $opts->getValue( 'user' );
101  if ( $user !== '' ) {
102  $conds[] = $this->actorMigration
103  ->getWhere( $dbr, 'img_user', User::newFromName( $user, false ) )['conds'];
104  }
105 
106  if ( !$opts->getValue( 'showbots' ) ) {
107  $groupsWithBotPermission = $this->permissionManager->getGroupsWithPermission( 'bot' );
108 
109  if ( count( $groupsWithBotPermission ) ) {
110  $tables[] = 'user_groups';
111  $conds[] = 'ug_group IS NULL';
112  $jconds['user_groups'] = [
113  'LEFT JOIN',
114  [
115  'ug_group' => $groupsWithBotPermission,
116  'ug_user = ' . $actorQuery['fields']['img_user'],
117  'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
118  ]
119  ];
120  }
121  }
122 
123  if ( $opts->getValue( 'hidepatrolled' ) ) {
124  $tables[] = 'recentchanges';
125  $conds['rc_type'] = RC_LOG;
126  $conds['rc_log_type'] = 'upload';
127  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
128  $conds['rc_namespace'] = NS_FILE;
129 
130  $jconds['recentchanges'] = [
131  'JOIN',
132  [
133  'rc_title = img_name',
134  'rc_actor = ' . $actorQuery['fields']['img_actor'],
135  'rc_timestamp = img_timestamp'
136  ]
137  ];
138  }
139 
140  if ( $opts->getValue( 'mediatype' ) ) {
141  $conds['img_media_type'] = $opts->getValue( 'mediatype' );
142  }
143 
144  $likeVal = $opts->getValue( 'like' );
145  if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
146  $likeObj = Title::newFromText( $likeVal );
147  if ( $likeObj instanceof Title ) {
148  $like = $dbr->buildLike(
149  $dbr->anyString(),
150  strtolower( $likeObj->getDBkey() ),
151  $dbr->anyString()
152  );
153  $conds[] = "LOWER(img_name) $like";
154  }
155  }
156 
157  // We're ordering by img_timestamp, but MariaDB sometimes likes to query other tables first
158  // and filesort the result set later.
159  // See T124205 / https://mariadb.atlassian.net/browse/MDEV-8880, and T244533
160  $options[] = 'STRAIGHT_JOIN';
161 
162  $query = [
163  'tables' => $tables,
164  'fields' => $fields,
165  'join_conds' => $jconds,
166  'conds' => $conds,
167  'options' => $options,
168  ];
169 
170  return $query;
171  }
172 
173  public function getIndexField() {
174  return 'img_timestamp';
175  }
176 
177  protected function getStartBody() {
178  if ( !$this->gallery ) {
179  // Note that null for mode is taken to mean use default.
180  $mode = $this->getRequest()->getVal( 'gallerymode', null );
181  try {
182  $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
183  } catch ( Exception $e ) {
184  // User specified something invalid, fallback to default.
185  $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
186  }
187  }
188 
189  return '';
190  }
191 
192  protected function getEndBody() {
193  return $this->gallery->toHTML();
194  }
195 
196  protected function doBatchLookups() {
197  $userIds = [];
198  $this->mResult->seek( 0 );
199  foreach ( $this->mResult as $row ) {
200  $userIds[] = $row->img_user;
201  }
202  // Do a link batch query for names and userpages
203  $this->userCache->doQuery( $userIds, [ 'userpage' ], __METHOD__ );
204  }
205 
206  public function formatRow( $row ) {
207  $username = $this->userCache->getUserName( $row->img_user, $row->img_user_text );
208 
209  if ( ExternalUserNames::isExternal( $username ) ) {
210  $ul = htmlspecialchars( $username );
211  } else {
212  $ul = $this->getLinkRenderer()->makeLink(
213  new TitleValue( NS_USER, $username ),
214  $username
215  );
216  }
217  $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
218 
219  $this->gallery->add(
220  Title::makeTitle( NS_FILE, $row->img_name ),
221  "$ul<br />\n<i>"
222  . htmlspecialchars( $time )
223  . "</i><br />\n",
224  '',
225  '',
226  [],
228  );
229 
230  return '';
231  }
232 }
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:37
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:70
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:328
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:45
UserCache
Definition: UserCache.php:32
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 links for pages.
Definition: LinkRenderer.php:41
RC_LOG
const RC_LOG
Definition: Defines.php:133
ImageGalleryBase\LOADING_LAZY
const LOADING_LAZY
Definition: ImageGalleryBase.php:34
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:244
NS_FILE
const NS_FILE
Definition: Defines.php:75
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:545
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
NewFilesPager\$gallery
ImageGalleryBase $gallery
Definition: NewFilesPager.php:34
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:79
ActorMigration
This class handles the logic for the actor table migration and should always be used in lieu of direc...
Definition: ActorMigration.php:41
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a real or virtual (DB_MASTER/DB_REPLICA) server index.
$dbr
$dbr
Definition: testCompression.php:54
NewFilesPager\formatRow
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition: NewFilesPager.php:206
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
IndexPager\setLimit
setLimit( $limit)
Set the limit from an other source than the request.
Definition: IndexPager.php:325
RangeChronologicalPager\getDateRangeCond
getDateRangeCond( $startStamp, $endStamp)
Set and return a date range condition using timestamps provided by the user.
Definition: RangeChronologicalPager.php:46
NewFilesPager\getStartBody
getStartBody()
Hook into getBody(), allows text to be inserted at the start.
Definition: NewFilesPager.php:177
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:591
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
RangeChronologicalPager
Pager for filtering by a range of dates.
Definition: RangeChronologicalPager.php:28
NewFilesPager\$actorMigration
ActorMigration $actorMigration
Definition: NewFilesPager.php:45
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:50
NewFilesPager\$userCache
UserCache $userCache
Definition: NewFilesPager.php:48
$ul
$ul
Definition: upgradeLogging.php:222
NewFilesPager\doBatchLookups
doBatchLookups()
Called from getBody(), before getStartBody() is called and after doQuery() was called.
Definition: NewFilesPager.php:196
NewFilesPager\$permissionManager
PermissionManager $permissionManager
Definition: NewFilesPager.php:42
NewFilesPager\$opts
FormOptions $opts
Definition: NewFilesPager.php:39
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:976
NewFilesPager\__construct
__construct(IContextSource $context, FormOptions $opts, LinkRenderer $linkRenderer, PermissionManager $permissionManager, ActorMigration $actorMigration, ILoadBalancer $loadBalancer, UserCache $userCache)
Definition: NewFilesPager.php:59
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
NewFilesPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: NewFilesPager.php:90
Title
Represents a title within MediaWiki.
Definition: Title.php:41
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:82
NewFilesPager\getEndBody
getEndBody()
Hook into getBody() for the end of the list.
Definition: NewFilesPager.php:192
NS_USER
const NS_USER
Definition: Defines.php:71
NewFilesPager\getIndexField
getIndexField()
Returns the name of the index field.
Definition: NewFilesPager.php:173
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
ExternalUserNames\isExternal
static isExternal( $username)
Tells whether the username is external or not.
Definition: ExternalUserNames.php:137
NewFilesPager
Definition: NewFilesPager.php:29
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:39