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
25 * A special page that lists log entries
27 * @ingroup SpecialPage
29 class SpecialLog
extends SpecialPage
{
30 public function __construct() {
31 parent
::__construct( 'Log' );
34 public function execute( $par ) {
35 global $wgActorTableSchemaMigrationStage;
38 $this->outputHeader();
39 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
40 $this->addHelpLink( 'Help:Log' );
42 $opts = new FormOptions
;
43 $opts->add( 'type', '' );
44 $opts->add( 'user', '' );
45 $opts->add( 'page', '' );
46 $opts->add( 'pattern', false );
47 $opts->add( 'year', null, FormOptions
::INTNULL
);
48 $opts->add( 'month', null, FormOptions
::INTNULL
);
49 $opts->add( 'day', null, FormOptions
::INTNULL
);
50 $opts->add( 'tagfilter', '' );
51 $opts->add( 'offset', '' );
52 $opts->add( 'dir', '' );
53 $opts->add( 'offender', '' );
54 $opts->add( 'subtype', '' );
55 $opts->add( 'logid', '' );
58 $opts->fetchValuesFromRequest( $this->getRequest() );
59 if ( $par !== null ) {
60 $this->parseParams( $opts, (string)$par );
64 $dateString = $this->getRequest()->getVal( 'wpdate' );
65 if ( !empty( $dateString ) ) {
66 $dateStamp = MWTimestamp
::getInstance( $dateString . ' 00:00:00' );
67 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
68 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
69 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
72 # Don't let the user get stuck with a certain date
73 if ( $opts->getValue( 'offset' ) ||
$opts->getValue( 'dir' ) == 'prev' ) {
74 $opts->setValue( 'year', '' );
75 $opts->setValue( 'month', '' );
78 // If the user doesn't have the right permission to view the specific
79 // log type, throw a PermissionsError
80 // If the log type is invalid, just show all public logs
81 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
82 $type = $opts->getValue( 'type' );
83 if ( !LogPage
::isLogType( $type ) ) {
84 $opts->setValue( 'type', '' );
85 } elseif ( isset( $logRestrictions[$type] )
86 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
88 throw new PermissionsError( $logRestrictions[$type] );
91 # Handle type-specific inputs
93 if ( $opts->getValue( 'type' ) == 'suppress' ) {
94 $offenderName = $opts->getValue( 'offender' );
95 $offender = empty( $offenderName ) ?
null : User
::newFromName( $offenderName, false );
97 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW
) {
98 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
100 if ( $offender->getId() > 0 ) {
101 $field = 'target_author_id';
102 $value = $offender->getId();
104 $field = 'target_author_ip';
105 $value = $offender->getName();
107 if ( !$offender->getActorId() ) {
108 $qc = [ 'ls_field' => $field, 'ls_value' => $value ];
110 $db = wfGetDB( DB_REPLICA
);
112 'ls_field' => [ 'target_author_actor', $field ], // So LogPager::getQueryInfo() works right
115 [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ], LIST_AND
117 $db->makeList( [ 'ls_field' => $field, 'ls_value' => $value ], LIST_AND
),
124 // Allow extensions to add relations to their search types
126 'SpecialLogAddLogSearchRelations',
127 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
131 # Some log types are only for a 'User:' title but we might have been given
132 # only the username instead of the full title 'User:username'. This part try
133 # to lookup for a user by that name and eventually fix user input. See T3697.
134 if ( in_array( $opts->getValue( 'type' ), self
::getLogTypesOnUser() ) ) {
135 # ok we have a type of log which expect a user title.
136 $target = Title
::newFromText( $opts->getValue( 'page' ) );
137 if ( $target && $target->getNamespace() === NS_MAIN
) {
138 # User forgot to add 'User:', we are adding it for him
139 $opts->setValue( 'page',
140 Title
::makeTitleSafe( NS_USER
, $opts->getValue( 'page' ) )
145 $this->show( $opts, $qc );
149 * List log type for which the target is a user
150 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
151 * Title user instead.
156 public static function getLogTypesOnUser() {
157 static $types = null;
158 if ( $types !== null ) {
167 Hooks
::run( 'GetLogTypesOnUser', [ &$types ] );
172 * Return an array of subpages that this special page will accept.
174 * @return string[] subpages
176 public function getSubpagesForPrefixSearch() {
177 $subpages = LogPage
::validTypes();
184 * Set options based on the subpage title parts:
185 * - One part that is a valid log type: Special:Log/logtype
186 * - Two parts: Special:Log/logtype/username
187 * - Otherwise, assume the whole subpage is a username.
189 * @param FormOptions $opts
191 * @throws ConfigException
193 private function parseParams( FormOptions
$opts, $par ) {
195 $par = $par !== null ?
$par : '';
196 $parms = explode( '/', $par );
197 $symsForAll = [ '*', 'all' ];
198 if ( $parms[0] != '' &&
199 ( in_array( $par, LogPage
::validTypes() ) ||
in_array( $par, $symsForAll ) )
201 $opts->setValue( 'type', $par );
202 } elseif ( count( $parms ) == 2 ) {
203 $opts->setValue( 'type', $parms[0] );
204 $opts->setValue( 'user', $parms[1] );
205 } elseif ( $par != '' ) {
206 $opts->setValue( 'user', $par );
210 private function show( FormOptions
$opts, array $extraConds ) {
211 # Create a LogPager item to get the results and a LogEventsList item to format them...
212 $loglist = new LogEventsList(
214 $this->getLinkRenderer(),
215 LogEventsList
::USE_CHECKBOXES
218 $pager = new LogPager(
220 $opts->getValue( 'type' ),
221 $opts->getValue( 'user' ),
222 $opts->getValue( 'page' ),
223 $opts->getValue( 'pattern' ),
225 $opts->getValue( 'year' ),
226 $opts->getValue( 'month' ),
227 $opts->getValue( 'day' ),
228 $opts->getValue( 'tagfilter' ),
229 $opts->getValue( 'subtype' ),
230 $opts->getValue( 'logid' )
233 $this->addHeader( $opts->getValue( 'type' ) );
236 if ( $pager->getPerformer() ) {
237 $performerUser = User
::newFromName( $pager->getPerformer(), false );
238 $this->getSkin()->setRelevantUser( $performerUser );
242 $loglist->showOptions(
244 $pager->getPerformer(),
246 $pager->getPattern(),
250 $pager->getFilterParams(),
251 $pager->getTagFilter(),
256 $logBody = $pager->getBody();
258 $this->getOutput()->addHTML(
259 $pager->getNavigationBar() .
260 $this->getActionButtons(
261 $loglist->beginLogEventsList() .
263 $loglist->endLogEventsList()
265 $pager->getNavigationBar()
268 $this->getOutput()->addWikiMsg( 'logempty' );
272 private function getActionButtons( $formcontents ) {
273 $user = $this->getUser();
274 $canRevDelete = $user->isAllowedAll( 'deletedhistory', 'deletelogentry' );
275 $showTagEditUI = ChangeTags
::showTagEditingUI( $user );
276 # If the user doesn't have the ability to delete log entries nor edit tags,
277 # don't bother showing them the button(s).
278 if ( !$canRevDelete && !$showTagEditUI ) {
279 return $formcontents;
282 # Show button to hide log entries and/or edit change tags
283 $s = Html
::openElement(
285 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
287 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
288 $s .= Html
::hidden( 'type', 'logging' ) . "\n";
291 if ( $canRevDelete ) {
292 $buttons .= Html
::element(
296 'name' => 'revisiondelete',
298 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
300 $this->msg( 'showhideselectedlogentries' )->text()
303 if ( $showTagEditUI ) {
304 $buttons .= Html
::element(
308 'name' => 'editchangetags',
310 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
312 $this->msg( 'log-edit-tags' )->text()
316 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
318 $s .= $buttons . $formcontents . $buttons;
319 $s .= Html
::closeElement( 'form' );
325 * Set page title and show header for this log type
326 * @param string $type
329 protected function addHeader( $type ) {
330 $page = new LogPage( $type );
331 $this->getOutput()->setPageTitle( $page->getName() );
332 $this->getOutput()->addHTML( $page->getDescription()
333 ->setContext( $this->getContext() )->parseAsBlock() );
336 protected function getGroupName() {