Special:NewFiles: Make 'hidepatrolled' query less slow
[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 $options = [];
109
110 if ( !$this->showBots ) {
111 $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
112
113 if ( count( $groupsWithBotPermission ) ) {
114 $tables[] = 'user_groups';
115 $conds[] = 'ug_group IS NULL';
116 $jconds['user_groups'] = [
117 'LEFT JOIN',
118 [
119 'ug_group' => $groupsWithBotPermission,
120 'ug_user = img_user'
121 ]
122 ];
123 }
124 }
125
126 if ( $this->hidePatrolled ) {
127 $tables[] = 'recentchanges';
128 $conds['rc_type'] = RC_LOG;
129 $conds['rc_log_type'] = 'upload';
130 $conds['rc_patrolled'] = 0;
131 $conds['rc_namespace'] = NS_FILE;
132 $jconds['recentchanges'] = [
133 'INNER JOIN',
134 [
135 'rc_title = img_name',
136 'rc_user = img_user',
137 'rc_timestamp = img_timestamp'
138 ]
139 ];
140 // We're ordering by img_timestamp, so we have to make sure MariaDB queries `image` first.
141 // It sometimes decides to query `recentchanges` first and filesort the result set later
142 // to get the right ordering. T124205 / https://mariadb.atlassian.net/browse/MDEV-8880
143 $options[] = 'STRAIGHT_JOIN';
144 }
145
146 if ( !$this->getConfig()->get( 'MiserMode' ) && $this->like !== null ) {
147 $dbr = wfGetDB( DB_SLAVE );
148 $likeObj = Title::newFromText( $this->like );
149 if ( $likeObj instanceof Title ) {
150 $like = $dbr->buildLike(
151 $dbr->anyString(),
152 strtolower( $likeObj->getDBkey() ),
153 $dbr->anyString()
154 );
155 $conds[] = "LOWER(img_name) $like";
156 }
157 }
158
159 $query = [
160 'tables' => $tables,
161 'fields' => '*',
162 'join_conds' => $jconds,
163 'conds' => $conds,
164 'options' => $options,
165 ];
166
167 return $query;
168 }
169
170 function getIndexField() {
171 return 'img_timestamp';
172 }
173
174 function getStartBody() {
175 if ( !$this->gallery ) {
176 // Note that null for mode is taken to mean use default.
177 $mode = $this->getRequest()->getVal( 'gallerymode', null );
178 try {
179 $this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() );
180 } catch ( Exception $e ) {
181 // User specified something invalid, fallback to default.
182 $this->gallery = ImageGalleryBase::factory( false, $this->getContext() );
183 }
184 }
185
186 return '';
187 }
188
189 function getEndBody() {
190 return $this->gallery->toHTML();
191 }
192
193 function formatRow( $row ) {
194 $name = $row->img_name;
195 $user = User::newFromId( $row->img_user );
196
197 $title = Title::makeTitle( NS_FILE, $name );
198 $ul = Linker::link( $user->getUserpage(), $user->getName() );
199 $time = $this->getLanguage()->userTimeAndDate( $row->img_timestamp, $this->getUser() );
200
201 $this->gallery->add(
202 $title,
203 "$ul<br />\n<i>"
204 . htmlspecialchars( $time )
205 . "</i><br />\n"
206 );
207 }
208
209 function getForm() {
210 $fields = [
211 'like' => [
212 'type' => 'text',
213 'label-message' => 'newimages-label',
214 'name' => 'like',
215 ],
216 'showbots' => [
217 'type' => 'check',
218 'label-message' => 'newimages-showbots',
219 'name' => 'showbots',
220 ],
221 'hidepatrolled' => [
222 'type' => 'check',
223 'label-message' => 'newimages-hidepatrolled',
224 'name' => 'hidepatrolled',
225 ],
226 'limit' => [
227 'type' => 'hidden',
228 'default' => $this->mLimit,
229 'name' => 'limit',
230 ],
231 'offset' => [
232 'type' => 'hidden',
233 'default' => $this->getRequest()->getText( 'offset' ),
234 'name' => 'offset',
235 ],
236 ];
237
238 if ( $this->getConfig()->get( 'MiserMode' ) ) {
239 unset( $fields['like'] );
240 }
241
242 if ( !$this->getUser()->useFilePatrol() ) {
243 unset( $fields['hidepatrolled'] );
244 }
245
246 $context = new DerivativeContext( $this->getContext() );
247 $context->setTitle( $this->getTitle() ); // Remove subpage
248 $form = new HTMLForm( $fields, $context );
249
250 $form->setSubmitTextMsg( 'ilsubmit' );
251 $form->setSubmitProgressive();
252
253 $form->setMethod( 'get' );
254 $form->setWrapperLegendMsg( 'newimages-legend' );
255
256 return $form;
257 }
258 }