3 * Implements Special:Log
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Timestamp\TimestampException
;
28 * A special page that lists log entries
30 * @ingroup SpecialPage
32 class SpecialLog
extends SpecialPage
{
33 public function __construct() {
34 parent
::__construct( 'Log' );
37 public function execute( $par ) {
38 global $wgActorTableSchemaMigrationStage;
41 $this->outputHeader();
42 $out = $this->getOutput();
43 $out->addModules( 'mediawiki.userSuggest' );
44 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
45 $this->addHelpLink( 'Help:Log' );
47 $opts = new FormOptions
;
48 $opts->add( 'type', '' );
49 $opts->add( 'user', '' );
50 $opts->add( 'page', '' );
51 $opts->add( 'pattern', false );
52 $opts->add( 'year', null, FormOptions
::INTNULL
);
53 $opts->add( 'month', null, FormOptions
::INTNULL
);
54 $opts->add( 'day', null, FormOptions
::INTNULL
);
55 $opts->add( 'tagfilter', '' );
56 $opts->add( 'offset', '' );
57 $opts->add( 'dir', '' );
58 $opts->add( 'offender', '' );
59 $opts->add( 'subtype', '' );
60 $opts->add( 'logid', '' );
63 $opts->fetchValuesFromRequest( $this->getRequest() );
64 if ( $par !== null ) {
65 $this->parseParams( $opts, (string)$par );
69 $dateString = $this->getRequest()->getVal( 'wpdate' );
70 if ( !empty( $dateString ) ) {
72 $dateStamp = MWTimestamp
::getInstance( $dateString . ' 00:00:00' );
73 } catch ( TimestampException
$e ) {
74 // If users provide an invalid date, silently ignore it
75 // instead of letting an exception bubble up (T201411)
79 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
80 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
81 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
85 # Don't let the user get stuck with a certain date
86 if ( $opts->getValue( 'offset' ) ||
$opts->getValue( 'dir' ) == 'prev' ) {
87 $opts->setValue( 'year', '' );
88 $opts->setValue( 'month', '' );
91 // If the user doesn't have the right permission to view the specific
92 // log type, throw a PermissionsError
93 // If the log type is invalid, just show all public logs
94 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
95 $type = $opts->getValue( 'type' );
96 if ( !LogPage
::isLogType( $type ) ) {
97 $opts->setValue( 'type', '' );
98 } elseif ( isset( $logRestrictions[$type] )
99 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
101 throw new PermissionsError( $logRestrictions[$type] );
104 # Handle type-specific inputs
106 if ( $opts->getValue( 'type' ) == 'suppress' ) {
107 $offenderName = $opts->getValue( 'offender' );
108 $offender = empty( $offenderName ) ?
null : User
::newFromName( $offenderName, false );
110 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
111 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
112 } elseif ( $offender->getId() > 0 ) {
113 $qc = [ 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() ];
115 $qc = [ 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() ];
119 // Allow extensions to add relations to their search types
121 'SpecialLogAddLogSearchRelations',
122 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
126 # Some log types are only for a 'User:' title but we might have been given
127 # only the username instead of the full title 'User:username'. This part try
128 # to lookup for a user by that name and eventually fix user input. See T3697.
129 if ( in_array( $opts->getValue( 'type' ), self
::getLogTypesOnUser() ) ) {
130 # ok we have a type of log which expect a user title.
131 $target = Title
::newFromText( $opts->getValue( 'page' ) );
132 if ( $target && $target->getNamespace() === NS_MAIN
) {
133 # User forgot to add 'User:', we are adding it for him
134 $opts->setValue( 'page',
135 Title
::makeTitleSafe( NS_USER
, $opts->getValue( 'page' ) )
140 $this->show( $opts, $qc );
144 * List log type for which the target is a user
145 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
146 * Title user instead.
151 public static function getLogTypesOnUser() {
152 static $types = null;
153 if ( $types !== null ) {
162 Hooks
::run( 'GetLogTypesOnUser', [ &$types ] );
167 * Return an array of subpages that this special page will accept.
169 * @return string[] subpages
171 public function getSubpagesForPrefixSearch() {
172 $subpages = LogPage
::validTypes();
179 * Set options based on the subpage title parts:
180 * - One part that is a valid log type: Special:Log/logtype
181 * - Two parts: Special:Log/logtype/username
182 * - Otherwise, assume the whole subpage is a username.
184 * @param FormOptions $opts
187 private function parseParams( FormOptions
$opts, $par ) {
190 $parms = explode( '/', $par );
191 $symsForAll = [ '*', 'all' ];
192 if ( $parms[0] != '' &&
193 ( in_array( $par, LogPage
::validTypes() ) ||
in_array( $par, $symsForAll ) )
195 $opts->setValue( 'type', $par );
196 } elseif ( count( $parms ) == 2 ) {
197 $opts->setValue( 'type', $parms[0] );
198 $opts->setValue( 'user', $parms[1] );
199 } elseif ( $par != '' ) {
200 $opts->setValue( 'user', $par );
204 private function show( FormOptions
$opts, array $extraConds ) {
205 # Create a LogPager item to get the results and a LogEventsList item to format them...
206 $loglist = new LogEventsList(
208 $this->getLinkRenderer(),
209 LogEventsList
::USE_CHECKBOXES
212 $pager = new LogPager(
214 $opts->getValue( 'type' ),
215 $opts->getValue( 'user' ),
216 $opts->getValue( 'page' ),
217 $opts->getValue( 'pattern' ),
219 $opts->getValue( 'year' ),
220 $opts->getValue( 'month' ),
221 $opts->getValue( 'day' ),
222 $opts->getValue( 'tagfilter' ),
223 $opts->getValue( 'subtype' ),
224 $opts->getValue( 'logid' )
227 $this->addHeader( $opts->getValue( 'type' ) );
230 if ( $pager->getPerformer() ) {
231 $performerUser = User
::newFromName( $pager->getPerformer(), false );
232 $this->getSkin()->setRelevantUser( $performerUser );
236 $loglist->showOptions(
238 $pager->getPerformer(),
240 $pager->getPattern(),
244 $pager->getFilterParams(),
245 $pager->getTagFilter(),
250 $logBody = $pager->getBody();
252 $this->getOutput()->addHTML(
253 $pager->getNavigationBar() .
254 $this->getActionButtons(
255 $loglist->beginLogEventsList() .
257 $loglist->endLogEventsList()
259 $pager->getNavigationBar()
262 $this->getOutput()->addWikiMsg( 'logempty' );
266 private function getActionButtons( $formcontents ) {
267 $user = $this->getUser();
268 $canRevDelete = MediaWikiServices
::getInstance()
269 ->getPermissionManager()
270 ->userHasAllRights( $user, 'deletedhistory', 'deletelogentry' );
271 $showTagEditUI = ChangeTags
::showTagEditingUI( $user );
272 # If the user doesn't have the ability to delete log entries nor edit tags,
273 # don't bother showing them the button(s).
274 if ( !$canRevDelete && !$showTagEditUI ) {
275 return $formcontents;
278 # Show button to hide log entries and/or edit change tags
279 $s = Html
::openElement(
281 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
283 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
284 $s .= Html
::hidden( 'type', 'logging' ) . "\n";
287 if ( $canRevDelete ) {
288 $buttons .= Html
::element(
292 'name' => 'revisiondelete',
294 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
296 $this->msg( 'showhideselectedlogentries' )->text()
299 if ( $showTagEditUI ) {
300 $buttons .= Html
::element(
304 'name' => 'editchangetags',
306 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
308 $this->msg( 'log-edit-tags' )->text()
312 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
314 $s .= $buttons . $formcontents . $buttons;
315 $s .= Html
::closeElement( 'form' );
321 * Set page title and show header for this log type
322 * @param string $type
325 protected function addHeader( $type ) {
326 $page = new LogPage( $type );
327 $this->getOutput()->setPageTitle( $page->getName() );
328 $this->getOutput()->addHTML( $page->getDescription()
329 ->setContext( $this->getContext() )->parseAsBlock() );
332 protected function getGroupName() {