Merge "Allow filtering on newbies in Special:NewFiles"
[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 = $jconds = [];
63 $tables = [ 'image' ];
64 $fields = [ 'img_name', 'img_user', 'img_timestamp' ];
65 $options = [];
66
67 $user = $opts->getValue( 'user' );
68 if ( $user !== '' ) {
69 $userId = User::idFromName( $user );
70 if ( $userId ) {
71 $conds['img_user'] = $userId;
72 } else {
73 $conds['img_user_text'] = $user;
74 }
75 }
76
77 if ( $opts->getValue( 'newbies' ) ) {
78 // newbie = most recent 1% of users
79 $dbr = wfGetDB( DB_REPLICA );
80 $max = $dbr->selectField( 'user', 'max(user_id)', false, __METHOD__ );
81 $conds[] = 'img_user >' . (int)( $max - $max / 100 );
82
83 // there's no point in looking for new user activity in a far past;
84 // beyond a certain point, we'd just end up scanning the rest of the
85 // table even though the users we're looking for didn't yet exist...
86 // see T140537, (for ContribsPages, but similar to this)
87 $conds[] = 'img_timestamp > ' .
88 $dbr->addQuotes( $dbr->timestamp( wfTimestamp() - 30 * 24 * 60 * 60 ) );
89 }
90
91 if ( !$opts->getValue( 'showbots' ) ) {
92 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
93
94 if ( count( $groupsWithBotPermission ) ) {
95 $dbr = wfGetDB( DB_REPLICA );
96 $tables[] = 'user_groups';
97 $conds[] = 'ug_group IS NULL';
98 $jconds['user_groups'] = [
99 'LEFT JOIN',
100 [
101 'ug_group' => $groupsWithBotPermission,
102 'ug_user = img_user',
103 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
104 ]
105 ];
106 }
107 }
108
109 if ( $opts->getValue( 'hidepatrolled' ) ) {
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 $jconds['recentchanges'] = [
116 'INNER JOIN',
117 [
118 'rc_title = img_name',
119 'rc_user = img_user',
120 'rc_timestamp = img_timestamp'
121 ]
122 ];
123 // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
124 // It sometimes decides to query `recentchanges` first and filesort the result set later
125 // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
126 $options[] = 'STRAIGHT_JOIN';
127 }
128
129 $likeVal = $opts->getValue( 'like' );
130 if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
131 $dbr = wfGetDB( DB_REPLICA );
132 $likeObj = Title::newFromText( $likeVal );
133 if ( $likeObj instanceof Title ) {
134 $like = $dbr->buildLike(
135 $dbr->anyString(),
136 strtolower( $likeObj->getDBkey() ),
137 $dbr->anyString()
138 );
139 $conds[] = "LOWER(img_name) $like";
140 }
141 }
142
143 $query = [
144 'tables' => $tables,
145 'fields' => $fields,
146 'join_conds' => $jconds,
147 'conds' => $conds,
148 'options' => $options,
149 ];
150
151 return $query;
152 }
153
154 function getIndexField() {
155 return 'img_timestamp';
156 }
157
158 function getStartBody() {
159 if ( !$this->gallery ) {
160 // Note that null for mode is taken to mean use default.
161 $mode = $this->getRequest()->getVal( 'gallerymode', null );
162 try {
163 $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
164 } catch ( Exception $e ) {
165 // User specified something invalid, fallback to default.
166 $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
167 }
168 }
169
170 return '';
171 }
172
173 function getEndBody() {
174 return $this->gallery->toHTML();
175 }
176
177 function formatRow( $row ) {
178 $name = $row->img_name;
179 $user = User::newFromId( $row->img_user );
180
181 $title = Title::makeTitle( NS_FILE, $name );
182 $ul = MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
183 $user->getUserPage(),
184 $user->getName()
185 );
186 $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
187
188 $this->gallery->add(
189 $title,
190 "$ul<br />\n<i>"
191 . htmlspecialchars( $time )
192 . "</i><br />\n"
193 );
194 }
195 }