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