41819fc431b0f2431fd2d1e4e4e03baaf89f890a
[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 class NewFilesPager extends ReverseChronologicalPager {
26
27 /**
28 * @var ImageGallery
29 */
30 protected $gallery;
31
32 /**
33 * @var FormOptions
34 */
35 protected $opts;
36
37 /**
38 * @param IContextSource $context
39 * @param FormOptions $opts
40 */
41 function __construct( IContextSource $context, FormOptions $opts ) {
42 $this->opts = $opts;
43
44 $this->setLimit( $opts->getValue( 'limit' ) );
45
46 parent::__construct( $context );
47 }
48
49 function getQueryInfo() {
50 $opts = $this->opts;
51 $conds = $jconds = [];
52 $tables = [ 'image' ];
53 $fields = [ 'img_name', 'img_user', 'img_timestamp' ];
54 $options = [];
55
56 if ( !$opts->getValue( 'showbots' ) ) {
57 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
58
59 if ( count( $groupsWithBotPermission ) ) {
60 $tables[] = 'user_groups';
61 $conds[] = 'ug_group IS NULL';
62 $jconds['user_groups'] = [
63 'LEFT JOIN',
64 [
65 'ug_group' => $groupsWithBotPermission,
66 'ug_user = img_user'
67 ]
68 ];
69 }
70 }
71
72 if ( $opts->getValue( 'hidepatrolled' ) ) {
73 $tables[] = 'recentchanges';
74 $conds['rc_type'] = RC_LOG;
75 $conds['rc_log_type'] = 'upload';
76 $conds['rc_patrolled'] = 0;
77 $conds['rc_namespace'] = NS_FILE;
78 $jconds['recentchanges'] = [
79 'INNER JOIN',
80 [
81 'rc_title = img_name',
82 'rc_user = img_user',
83 'rc_timestamp = img_timestamp'
84 ]
85 ];
86 // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
87 // It sometimes decides to query `recentchanges` first and filesort the result set later
88 // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
89 $options[] = 'STRAIGHT_JOIN';
90 }
91
92 $likeVal = $opts->getValue( 'like' );
93 if ( !$this->getConfig()->get( 'MiserMode' ) && $likeVal !== '' ) {
94 $dbr = wfGetDB( DB_REPLICA );
95 $likeObj = Title::newFromText( $likeVal );
96 if ( $likeObj instanceof Title ) {
97 $like = $dbr->buildLike(
98 $dbr->anyString(),
99 strtolower( $likeObj->getDBkey() ),
100 $dbr->anyString()
101 );
102 $conds[] = "LOWER(img_name) $like";
103 }
104 }
105
106 $query = [
107 'tables' => $tables,
108 'fields' => $fields,
109 'join_conds' => $jconds,
110 'conds' => $conds,
111 'options' => $options,
112 ];
113
114 return $query;
115 }
116
117 function getIndexField() {
118 return 'img_timestamp';
119 }
120
121 function getStartBody() {
122 if ( !$this->gallery ) {
123 // Note that null for mode is taken to mean use default.
124 $mode = $this->getRequest()->getVal( 'gallerymode', null );
125 try {
126 $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
127 } catch ( Exception $e ) {
128 // User specified something invalid, fallback to default.
129 $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
130 }
131 }
132
133 return '';
134 }
135
136 function getEndBody() {
137 return $this->gallery->toHTML();
138 }
139
140 function formatRow( $row ) {
141 $name = $row->img_name;
142 $user = User::newFromId( $row->img_user );
143
144 $title = Title::makeTitle( NS_FILE, $name );
145 $ul = Linker::link( $user->getUserPage(), $user->getName() );
146 $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
147
148 $this->gallery->add(
149 $title,
150 "$ul<br />\n<i>"
151 . htmlspecialchars( $time )
152 . "</i><br />\n"
153 );
154 }
155 }