Merge "New maintenance script to clean up rows with invalid DB keys"
[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 ReverseChronologicalPager {
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 $this->opts = $opts;
45
46 $this->setLimit( $opts->getValue( 'limit' ) );
47
48 parent::__construct( $context );
49 }
50
51 function getQueryInfo() {
52 $opts = $this->opts;
53 $conds = $jconds = [];
54 $tables = [ 'image' ];
55 $fields = [ 'img_name', 'img_user', 'img_timestamp' ];
56 $options = [];
57
58 $user = $opts->getValue( 'user' );
59 if ( $user !== '' ) {
60 $userId = User::idFromName( $user );
61 if ( $userId ) {
62 $conds['img_user'] = $userId;
63 } else {
64 $conds['img_user_text'] = $user;
65 }
66 }
67
68 if ( !$opts->getValue( 'showbots' ) ) {
69 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
70
71 if ( count( $groupsWithBotPermission ) ) {
72 $dbr = wfGetDB( DB_REPLICA );
73 $tables[] = 'user_groups';
74 $conds[] = 'ug_group IS NULL';
75 $jconds['user_groups'] = [
76 'LEFT JOIN',
77 [
78 'ug_group' => $groupsWithBotPermission,
79 'ug_user = img_user',
80 $this->getConfig()->get( 'DisableUserGroupExpiry' ) ?
81 '1' :
82 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
83 ]
84 ];
85 }
86 }
87
88 if ( $opts->getValue( 'hidepatrolled' ) ) {
89 $tables[] = 'recentchanges';
90 $conds['rc_type'] = RC_LOG;
91 $conds['rc_log_type'] = 'upload';
92 $conds['rc_patrolled'] = 0;
93 $conds['rc_namespace'] = NS_FILE;
94 $jconds['recentchanges'] = [
95 'INNER JOIN',
96 [
97 'rc_title = img_name',
98 'rc_user = img_user',
99 'rc_timestamp = img_timestamp'
100 ]
101 ];
102 // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
103 // It sometimes decides to query `recentchanges` first and filesort the result set later
104 // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
105 $options[] = 'STRAIGHT_JOIN';
106 }
107
108 $likeVal = $opts->getValue( 'like' );
109 if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
110 $dbr = wfGetDB( DB_REPLICA );
111 $likeObj = Title::newFromText( $likeVal );
112 if ( $likeObj instanceof Title ) {
113 $like = $dbr->buildLike(
114 $dbr->anyString(),
115 strtolower( $likeObj->getDBkey() ),
116 $dbr->anyString()
117 );
118 $conds[] = "LOWER(img_name) $like";
119 }
120 }
121
122 $query = [
123 'tables' => $tables,
124 'fields' => $fields,
125 'join_conds' => $jconds,
126 'conds' => $conds,
127 'options' => $options,
128 ];
129
130 return $query;
131 }
132
133 function getIndexField() {
134 return 'img_timestamp';
135 }
136
137 function getStartBody() {
138 if ( !$this->gallery ) {
139 // Note that null for mode is taken to mean use default.
140 $mode = $this->getRequest()->getVal( 'gallerymode', null );
141 try {
142 $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
143 } catch ( Exception $e ) {
144 // User specified something invalid, fallback to default.
145 $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
146 }
147 }
148
149 return '';
150 }
151
152 function getEndBody() {
153 return $this->gallery->toHTML();
154 }
155
156 function formatRow( $row ) {
157 $name = $row->img_name;
158 $user = User::newFromId( $row->img_user );
159
160 $title = Title::makeTitle( NS_FILE, $name );
161 $ul = MediaWikiServices::getInstance()->getLinkRenderer()->makeLink(
162 $user->getUserPage(),
163 $user->getName()
164 );
165 $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
166
167 $this->gallery->add(
168 $title,
169 "$ul<br />\n<i>"
170 . htmlspecialchars( $time )
171 . "</i><br />\n"
172 );
173 }
174 }