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