Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / specials / SpecialNewimages.php
1 <?php
2 /**
3 * Implements Special:Newimages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 class SpecialNewFiles extends IncludableSpecialPage {
25 public function __construct() {
26 parent::__construct( 'Newimages' );
27 }
28
29 public function execute( $par ) {
30 $this->setHeaders();
31 $this->outputHeader();
32
33 $out = $this->getOutput();
34 $this->addHelpLink( 'Help:New images' );
35
36 $pager = new NewFilesPager( $this->getContext(), $par );
37
38 if ( !$this->including() ) {
39 $this->setTopText();
40 $form = $pager->getForm();
41 $form->prepareForm();
42 $form->displayForm( '' );
43 }
44
45 $out->addHTML( $pager->getBody() );
46 if ( !$this->including() ) {
47 $out->addHTML( $pager->getNavigationBar() );
48 }
49 }
50
51 protected function getGroupName() {
52 return 'changes';
53 }
54
55 /**
56 * Send the text to be displayed above the options
57 */
58 function setTopText() {
59 global $wgContLang;
60
61 $message = $this->msg( 'newimagestext' )->inContentLanguage();
62 if ( !$message->isDisabled() ) {
63 $this->getOutput()->addWikiText(
64 Html::rawElement( 'p',
65 [ 'lang' => $wgContLang->getHtmlCode(), 'dir' => $wgContLang->getDir() ],
66 "\n" . $message->plain() . "\n"
67 ),
68 /* $lineStart */ false,
69 /* $interface */ false
70 );
71 }
72 }
73 }
74
75 /**
76 * @ingroup SpecialPage Pager
77 */
78 class NewFilesPager extends ReverseChronologicalPager {
79 /**
80 * @var ImageGallery
81 */
82 protected $gallery;
83
84 /**
85 * @var bool
86 */
87 protected $showBots;
88
89 /**
90 * @var bool
91 */
92 protected $hidePatrolled;
93
94 function __construct( IContextSource $context, $par = null ) {
95 $this->like = $context->getRequest()->getText( 'like' );
96 $this->showBots = $context->getRequest()->getBool( 'showbots', 0 );
97 $this->hidePatrolled = $context->getRequest()->getBool( 'hidepatrolled', 0 );
98 if ( is_numeric( $par ) ) {
99 $this->setLimit( $par );
100 }
101
102 parent::__construct( $context );
103 }
104
105 function getQueryInfo() {
106 $conds = $jconds = [];
107 $tables = [ 'image' ];
108 $fields = [ 'img_name', 'img_user', 'img_timestamp' ];
109 $options = [];
110
111 if ( !$this->showBots ) {
112 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
113
114 if ( count( $groupsWithBotPermission ) ) {
115 $tables[] = 'user_groups';
116 $conds[] = 'ug_group IS NULL';
117 $jconds['user_groups'] = [
118 'LEFT JOIN',
119 [
120 'ug_group' => $groupsWithBotPermission,
121 'ug_user = img_user'
122 ]
123 ];
124 }
125 }
126
127 if ( $this->hidePatrolled ) {
128 $tables[] = 'recentchanges';
129 $conds['rc_type'] = RC_LOG;
130 $conds['rc_log_type'] = 'upload';
131 $conds['rc_patrolled'] = 0;
132 $conds['rc_namespace'] = NS_FILE;
133 $jconds['recentchanges'] = [
134 'INNER JOIN',
135 [
136 'rc_title = img_name',
137 'rc_user = img_user',
138 'rc_timestamp = img_timestamp'
139 ]
140 ];
141 // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
142 // It sometimes decides to query `recentchanges` first and filesort the result set later
143 // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
144 $options[] = 'STRAIGHT_JOIN';
145 }
146
147 if ( !$this->getConfig()->get( 'MiserMode' ) && $this->like !== null ) {
148 $dbr = wfGetDB( DB_SLAVE );
149 $likeObj = Title::newFromText( $this->like );
150 if ( $likeObj instanceof Title ) {
151 $like = $dbr->buildLike(
152 $dbr->anyString(),
153 strtolower( $likeObj->getDBkey() ),
154 $dbr->anyString()
155 );
156 $conds[] = "LOWER(img_name) $like";
157 }
158 }
159
160 $query = [
161 'tables' => $tables,
162 'fields' => $fields,
163 'join_conds' => $jconds,
164 'conds' => $conds,
165 'options' => $options,
166 ];
167
168 return $query;
169 }
170
171 function getIndexField() {
172 return 'img_timestamp';
173 }
174
175 function getStartBody() {
176 if ( !$this->gallery ) {
177 // Note that null for mode is taken to mean use default.
178 $mode = $this->getRequest()->getVal( 'gallerymode', null );
179 try {
180 $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
181 } catch ( Exception $e ) {
182 // User specified something invalid, fallback to default.
183 $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
184 }
185 }
186
187 return '';
188 }
189
190 function getEndBody() {
191 return $this->gallery->toHTML();
192 }
193
194 function formatRow( $row ) {
195 $name = $row->img_name;
196 $user = User::newFromId( $row->img_user );
197
198 $title = Title::makeTitle( NS_FILE, $name );
199 $ul = Linker::link( $user->getUserPage(), $user->getName() );
200 $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
201
202 $this->gallery->add(
203 $title,
204 "$ul<br />\n<i>"
205 . htmlspecialchars( $time )
206 . "</i><br />\n"
207 );
208 }
209
210 function getForm() {
211 $fields = [
212 'like' => [
213 'type' => 'text',
214 'label-message' => 'newimages-label',
215 'name' => 'like',
216 ],
217 'showbots' => [
218 'type' => 'check',
219 'label-message' => 'newimages-showbots',
220 'name' => 'showbots',
221 ],
222 'hidepatrolled' => [
223 'type' => 'check',
224 'label-message' => 'newimages-hidepatrolled',
225 'name' => 'hidepatrolled',
226 ],
227 'limit' => [
228 'type' => 'hidden',
229 'default' => $this->mLimit,
230 'name' => 'limit',
231 ],
232 'offset' => [
233 'type' => 'hidden',
234 'default' => $this->getRequest()->getText( 'offset' ),
235 'name' => 'offset',
236 ],
237 ];
238
239 if ( $this->getConfig()->get( 'MiserMode' ) ) {
240 unset( $fields['like'] );
241 }
242
243 if ( !$this->getUser()->useFilePatrol() ) {
244 unset( $fields['hidepatrolled'] );
245 }
246
247 $context = new DerivativeContext( $this->getContext() );
248 $context->setTitle( $this->getTitle() ); // Remove subpage
249 $form = new HTMLForm( $fields, $context );
250
251 $form->setSubmitTextMsg( 'ilsubmit' );
252 $form->setSubmitProgressive();
253
254 $form->setMethod( 'get' );
255 $form->setWrapperLegendMsg( 'newimages-legend' );
256
257 return $form;
258 }
259 }