Add `actor` table and code to start using it
[lhc/web/wiklou.git] / includes / specials / pagers / NewFilesPager.php
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 /**
23 * @ingroup Pager
24 */
25 use MediaWiki\MediaWikiServices;
26
27 class NewFilesPager extends RangeChronologicalPager {
28
29 /**
30 * @var ImageGalleryBase
31 */
32 protected $gallery;
33
34 /**
35 * @var FormOptions
36 */
37 protected $opts;
38
39 /**
40 * @param IContextSource $context
41 * @param FormOptions $opts
42 */
43 function __construct( IContextSource $context, FormOptions $opts ) {
44 parent::__construct( $context );
45
46 $this->opts = $opts;
47 $this->setLimit( $opts->getValue( 'limit' ) );
48
49 $startTimestamp = '';
50 $endTimestamp = '';
51 if ( $opts->getValue( 'start' ) ) {
52 $startTimestamp = $opts->getValue( 'start' ) . ' 00:00:00';
53 }
54 if ( $opts->getValue( 'end' ) ) {
55 $endTimestamp = $opts->getValue( 'end' ) . ' 23:59:59';
56 }
57 $this->getDateRangeCond( $startTimestamp, $endTimestamp );
58 }
59
60 function getQueryInfo() {
61 $opts = $this->opts;
62 $conds = [];
63 $imgQuery = LocalFile::getQueryInfo();
64 $tables = $imgQuery['tables'];
65 $fields = [ 'img_name', 'img_timestamp' ] + $imgQuery['fields'];
66 $options = [];
67 $jconds = $imgQuery['joins'];
68
69 $user = $opts->getValue( 'user' );
70 if ( $user !== '' ) {
71 $conds[] = ActorMigration::newMigration()
72 ->getWhere( wfGetDB( DB_REPLICA ), 'img_user', User::newFromName( $user, false ) )['conds'];
73 }
74
75 if ( $opts->getValue( 'newbies' ) ) {
76 // newbie = most recent 1% of users
77 $dbr = wfGetDB( DB_REPLICA );
78 $max = $dbr->selectField( 'user', 'max(user_id)', false, __METHOD__ );
79 $conds[] = $imgQuery['fields']['img_user'] . ' >' . (int)( $max - $max / 100 );
80
81 // there's no point in looking for new user activity in a far past;
82 // beyond a certain point, we'd just end up scanning the rest of the
83 // table even though the users we're looking for didn't yet exist...
84 // see T140537, (for ContribsPages, but similar to this)
85 $conds[] = 'img_timestamp > ' .
86 $dbr->addQuotes( $dbr->timestamp( wfTimestamp() - 30 * 24 * 60 * 60 ) );
87 }
88
89 if ( !$opts->getValue( 'showbots' ) ) {
90 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
91
92 if ( count( $groupsWithBotPermission ) ) {
93 $dbr = wfGetDB( DB_REPLICA );
94 $tables[] = 'user_groups';
95 $conds[] = 'ug_group IS NULL';
96 $jconds['user_groups'] = [
97 'LEFT JOIN',
98 [
99 'ug_group' => $groupsWithBotPermission,
100 'ug_user = ' . $imgQuery['fields']['img_user'],
101 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
102 ]
103 ];
104 }
105 }
106
107 if ( $opts->getValue( 'hidepatrolled' ) ) {
108 global $wgActorTableSchemaMigrationStage;
109
110 $tables[] = 'recentchanges';
111 $conds['rc_type'] = RC_LOG;
112 $conds['rc_log_type'] = 'upload';
113 $conds['rc_patrolled'] = 0;
114 $conds['rc_namespace'] = NS_FILE;
115
116 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
117 $jcond = 'rc_actor = ' . $imgQuery['fields']['img_actor'];
118 } else {
119 $rcQuery = ActorMigration::newMigration()->getJoin( 'rc_user' );
120 $tables += $rcQuery['tables'];
121 $joins += $rcQuery['joins'];
122 $jcond = $rcQuery['fields']['rc_user'] . ' = ' . $imgQuery['fields']['img_user'];
123 }
124 $jconds['recentchanges'] = [
125 'INNER JOIN',
126 [
127 'rc_title = img_name',
128 $jcond,
129 'rc_timestamp = img_timestamp'
130 ]
131 ];
132 // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
133 // It sometimes decides to query `recentchanges` first and filesort the result set later
134 // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
135 $options[] = 'STRAIGHT_JOIN';
136 }
137
138 if ( $opts->getValue( 'mediatype' ) ) {
139 $conds['img_media_type'] = $opts->getValue( 'mediatype' );
140 }
141
142 $likeVal = $opts->getValue( 'like' );
143 if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
144 $dbr = wfGetDB( DB_REPLICA );
145 $likeObj = Title::newFromText( $likeVal );
146 if ( $likeObj instanceof Title ) {
147 $like = $dbr->buildLike(
148 $dbr->anyString(),
149 strtolower( $likeObj->getDBkey() ),
150 $dbr->anyString()
151 );
152 $conds[] = "LOWER(img_name) $like";
153 }
154 }
155
156 $query = [
157 'tables' => $tables,
158 'fields' => $fields,
159 'join_conds' => $jconds,
160 'conds' => $conds,
161 'options' => $options,
162 ];
163
164 return $query;
165 }
166
167 function getIndexField() {
168 return 'img_timestamp';
169 }
170
171 function getStartBody() {
172 if ( !$this->gallery ) {
173 // Note that null for mode is taken to mean use default.
174 $mode = $this->getRequest()->getVal( 'gallerymode', null );
175 try {
176 $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
177 } catch ( Exception $e ) {
178 // User specified something invalid, fallback to default.
179 $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
180 }
181 }
182
183 return '';
184 }
185
186 function getEndBody() {
187 return $this->gallery->toHTML();
188 }
189
190 function formatRow( $row ) {
191 $name = $row->img_name;
192 $user = User::newFromId( $row->img_user );
193
194 $title = Title::makeTitle( NS_FILE, $name );
195 $ul = MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
196 $user->getUserPage(),
197 $user->getName()
198 );
199 $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
200
201 $this->gallery->add(
202 $title,
203 "$ul<br />\n<i>"
204 . htmlspecialchars( $time )
205 . "</i><br />\n"
206 );
207 }
208 }