Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / specials / SpecialLog.php
1 <?php
2 /**
3 * Implements Special:Log
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 use Wikimedia\Timestamp\TimestampException;
25
26 /**
27 * A special page that lists log entries
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialLog extends SpecialPage {
32 public function __construct() {
33 parent::__construct( 'Log' );
34 }
35
36 public function execute( $par ) {
37 global $wgActorTableSchemaMigrationStage;
38
39 $this->setHeaders();
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->addModules( 'mediawiki.userSuggest' );
43 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
44 $this->addHelpLink( 'Help:Log' );
45
46 $opts = new FormOptions;
47 $opts->add( 'type', '' );
48 $opts->add( 'user', '' );
49 $opts->add( 'page', '' );
50 $opts->add( 'pattern', false );
51 $opts->add( 'year', null, FormOptions::INTNULL );
52 $opts->add( 'month', null, FormOptions::INTNULL );
53 $opts->add( 'day', null, FormOptions::INTNULL );
54 $opts->add( 'tagfilter', '' );
55 $opts->add( 'offset', '' );
56 $opts->add( 'dir', '' );
57 $opts->add( 'offender', '' );
58 $opts->add( 'subtype', '' );
59 $opts->add( 'logid', '' );
60
61 // Set values
62 $opts->fetchValuesFromRequest( $this->getRequest() );
63 if ( $par !== null ) {
64 $this->parseParams( $opts, (string)$par );
65 }
66
67 // Set date values
68 $dateString = $this->getRequest()->getVal( 'wpdate' );
69 if ( !empty( $dateString ) ) {
70 try {
71 $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
72 } catch ( TimestampException $e ) {
73 // If users provide an invalid date, silently ignore it
74 // instead of letting an exception bubble up (T201411)
75 $dateStamp = false;
76 }
77 if ( $dateStamp ) {
78 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
79 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
80 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
81 }
82 }
83
84 # Don't let the user get stuck with a certain date
85 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
86 $opts->setValue( 'year', '' );
87 $opts->setValue( 'month', '' );
88 }
89
90 // If the user doesn't have the right permission to view the specific
91 // log type, throw a PermissionsError
92 // If the log type is invalid, just show all public logs
93 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
94 $type = $opts->getValue( 'type' );
95 if ( !LogPage::isLogType( $type ) ) {
96 $opts->setValue( 'type', '' );
97 } elseif ( isset( $logRestrictions[$type] )
98 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
99 ) {
100 throw new PermissionsError( $logRestrictions[$type] );
101 }
102
103 # Handle type-specific inputs
104 $qc = [];
105 if ( $opts->getValue( 'type' ) == 'suppress' ) {
106 $offenderName = $opts->getValue( 'offender' );
107 $offender = empty( $offenderName ) ? null : User::newFromName( $offenderName, false );
108 if ( $offender ) {
109 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
110 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
111 } elseif ( $offender->getId() > 0 ) {
112 $qc = [ 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() ];
113 } else {
114 $qc = [ 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() ];
115 }
116 }
117 } else {
118 // Allow extensions to add relations to their search types
119 Hooks::run(
120 'SpecialLogAddLogSearchRelations',
121 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
122 );
123 }
124
125 # Some log types are only for a 'User:' title but we might have been given
126 # only the username instead of the full title 'User:username'. This part try
127 # to lookup for a user by that name and eventually fix user input. See T3697.
128 if ( in_array( $opts->getValue( 'type' ), self::getLogTypesOnUser() ) ) {
129 # ok we have a type of log which expect a user title.
130 $target = Title::newFromText( $opts->getValue( 'page' ) );
131 if ( $target && $target->getNamespace() === NS_MAIN ) {
132 # User forgot to add 'User:', we are adding it for him
133 $opts->setValue( 'page',
134 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
135 );
136 }
137 }
138
139 $this->show( $opts, $qc );
140 }
141
142 /**
143 * List log type for which the target is a user
144 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
145 * Title user instead.
146 *
147 * @since 1.25
148 * @return array
149 */
150 public static function getLogTypesOnUser() {
151 static $types = null;
152 if ( $types !== null ) {
153 return $types;
154 }
155 $types = [
156 'block',
157 'newusers',
158 'rights',
159 ];
160
161 Hooks::run( 'GetLogTypesOnUser', [ &$types ] );
162 return $types;
163 }
164
165 /**
166 * Return an array of subpages that this special page will accept.
167 *
168 * @return string[] subpages
169 */
170 public function getSubpagesForPrefixSearch() {
171 $subpages = LogPage::validTypes();
172 $subpages[] = 'all';
173 sort( $subpages );
174 return $subpages;
175 }
176
177 /**
178 * Set options based on the subpage title parts:
179 * - One part that is a valid log type: Special:Log/logtype
180 * - Two parts: Special:Log/logtype/username
181 * - Otherwise, assume the whole subpage is a username.
182 *
183 * @param FormOptions $opts
184 * @param string $par
185 */
186 private function parseParams( FormOptions $opts, $par ) {
187 # Get parameters
188 $par = $par ?? '';
189 $parms = explode( '/', $par );
190 $symsForAll = [ '*', 'all' ];
191 if ( $parms[0] != '' &&
192 ( in_array( $par, LogPage::validTypes() ) || in_array( $par, $symsForAll ) )
193 ) {
194 $opts->setValue( 'type', $par );
195 } elseif ( count( $parms ) == 2 ) {
196 $opts->setValue( 'type', $parms[0] );
197 $opts->setValue( 'user', $parms[1] );
198 } elseif ( $par != '' ) {
199 $opts->setValue( 'user', $par );
200 }
201 }
202
203 private function show( FormOptions $opts, array $extraConds ) {
204 # Create a LogPager item to get the results and a LogEventsList item to format them...
205 $loglist = new LogEventsList(
206 $this->getContext(),
207 $this->getLinkRenderer(),
208 LogEventsList::USE_CHECKBOXES
209 );
210
211 $pager = new LogPager(
212 $loglist,
213 $opts->getValue( 'type' ),
214 $opts->getValue( 'user' ),
215 $opts->getValue( 'page' ),
216 $opts->getValue( 'pattern' ),
217 $extraConds,
218 $opts->getValue( 'year' ),
219 $opts->getValue( 'month' ),
220 $opts->getValue( 'day' ),
221 $opts->getValue( 'tagfilter' ),
222 $opts->getValue( 'subtype' ),
223 $opts->getValue( 'logid' )
224 );
225
226 $this->addHeader( $opts->getValue( 'type' ) );
227
228 # Set relevant user
229 if ( $pager->getPerformer() ) {
230 $performerUser = User::newFromName( $pager->getPerformer(), false );
231 $this->getSkin()->setRelevantUser( $performerUser );
232 }
233
234 # Show form options
235 $loglist->showOptions(
236 $pager->getType(),
237 $pager->getPerformer(),
238 $pager->getPage(),
239 $pager->getPattern(),
240 $pager->getYear(),
241 $pager->getMonth(),
242 $pager->getDay(),
243 $pager->getFilterParams(),
244 $pager->getTagFilter(),
245 $pager->getAction()
246 );
247
248 # Insert list
249 $logBody = $pager->getBody();
250 if ( $logBody ) {
251 $this->getOutput()->addHTML(
252 $pager->getNavigationBar() .
253 $this->getActionButtons(
254 $loglist->beginLogEventsList() .
255 $logBody .
256 $loglist->endLogEventsList()
257 ) .
258 $pager->getNavigationBar()
259 );
260 } else {
261 $this->getOutput()->addWikiMsg( 'logempty' );
262 }
263 }
264
265 private function getActionButtons( $formcontents ) {
266 $user = $this->getUser();
267 $canRevDelete = $user->isAllowedAll( 'deletedhistory', 'deletelogentry' );
268 $showTagEditUI = ChangeTags::showTagEditingUI( $user );
269 # If the user doesn't have the ability to delete log entries nor edit tags,
270 # don't bother showing them the button(s).
271 if ( !$canRevDelete && !$showTagEditUI ) {
272 return $formcontents;
273 }
274
275 # Show button to hide log entries and/or edit change tags
276 $s = Html::openElement(
277 'form',
278 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
279 ) . "\n";
280 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
281 $s .= Html::hidden( 'type', 'logging' ) . "\n";
282
283 $buttons = '';
284 if ( $canRevDelete ) {
285 $buttons .= Html::element(
286 'button',
287 [
288 'type' => 'submit',
289 'name' => 'revisiondelete',
290 'value' => '1',
291 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
292 ],
293 $this->msg( 'showhideselectedlogentries' )->text()
294 ) . "\n";
295 }
296 if ( $showTagEditUI ) {
297 $buttons .= Html::element(
298 'button',
299 [
300 'type' => 'submit',
301 'name' => 'editchangetags',
302 'value' => '1',
303 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
304 ],
305 $this->msg( 'log-edit-tags' )->text()
306 ) . "\n";
307 }
308
309 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
310
311 $s .= $buttons . $formcontents . $buttons;
312 $s .= Html::closeElement( 'form' );
313
314 return $s;
315 }
316
317 /**
318 * Set page title and show header for this log type
319 * @param string $type
320 * @since 1.19
321 */
322 protected function addHeader( $type ) {
323 $page = new LogPage( $type );
324 $this->getOutput()->setPageTitle( $page->getName() );
325 $this->getOutput()->addHTML( $page->getDescription()
326 ->setContext( $this->getContext() )->parseAsBlock() );
327 }
328
329 protected function getGroupName() {
330 return 'changes';
331 }
332 }