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