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 $dateStamp->setTimezone( $this->getConfig()->get( 'Localtimezone' ) );
69 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
70 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
71 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
74 # Don't let the user get stuck with a certain date
75 if ( $opts->getValue( 'offset' ) ||
$opts->getValue( 'dir' ) == 'prev' ) {
76 $opts->setValue( 'year', '' );
77 $opts->setValue( 'month', '' );
80 // If the user doesn't have the right permission to view the specific
81 // log type, throw a PermissionsError
82 // If the log type is invalid, just show all public logs
83 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
84 $type = $opts->getValue( 'type' );
85 if ( !LogPage
::isLogType( $type ) ) {
86 $opts->setValue( 'type', '' );
87 } elseif ( isset( $logRestrictions[$type] )
88 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
90 throw new PermissionsError( $logRestrictions[$type] );
93 # Handle type-specific inputs
95 if ( $opts->getValue( 'type' ) == 'suppress' ) {
96 $offenderName = $opts->getValue( 'offender' );
97 $offender = empty( $offenderName ) ?
null : User
::newFromName( $offenderName, false );
99 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW
) {
100 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
102 if ( $offender->getId() > 0 ) {
103 $field = 'target_author_id';
104 $value = $offender->getId();
106 $field = 'target_author_ip';
107 $value = $offender->getName();
109 if ( !$offender->getActorId() ) {
110 $qc = [ 'ls_field' => $field, 'ls_value' => $value ];
112 $db = wfGetDB( DB_REPLICA
);
114 'ls_field' => [ 'target_author_actor', $field ], // So LogPager::getQueryInfo() works right
117 [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ], LIST_AND
119 $db->makeList( [ 'ls_field' => $field, 'ls_value' => $value ], LIST_AND
),
126 // Allow extensions to add relations to their search types
128 'SpecialLogAddLogSearchRelations',
129 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
133 # Some log types are only for a 'User:' title but we might have been given
134 # only the username instead of the full title 'User:username'. This part try
135 # to lookup for a user by that name and eventually fix user input. See T3697.
136 if ( in_array( $opts->getValue( 'type' ), self
::getLogTypesOnUser() ) ) {
137 # ok we have a type of log which expect a user title.
138 $target = Title
::newFromText( $opts->getValue( 'page' ) );
139 if ( $target && $target->getNamespace() === NS_MAIN
) {
140 # User forgot to add 'User:', we are adding it for him
141 $opts->setValue( 'page',
142 Title
::makeTitleSafe( NS_USER
, $opts->getValue( 'page' ) )
147 $this->show( $opts, $qc );
151 * List log type for which the target is a user
152 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
153 * Title user instead.
158 public static function getLogTypesOnUser() {
159 static $types = null;
160 if ( $types !== null ) {
169 Hooks
::run( 'GetLogTypesOnUser', [ &$types ] );
174 * Return an array of subpages that this special page will accept.
176 * @return string[] subpages
178 public function getSubpagesForPrefixSearch() {
179 $subpages = LogPage
::validTypes();
186 * Set options based on the subpage title parts:
187 * - One part that is a valid log type: Special:Log/logtype
188 * - Two parts: Special:Log/logtype/username
189 * - Otherwise, assume the whole subpage is a username.
191 * @param FormOptions $opts
193 * @throws ConfigException
195 private function parseParams( FormOptions
$opts, $par ) {
197 $par = $par !== null ?
$par : '';
198 $parms = explode( '/', $par );
199 $symsForAll = [ '*', 'all' ];
200 if ( $parms[0] != '' &&
201 ( in_array( $par, LogPage
::validTypes() ) ||
in_array( $par, $symsForAll ) )
203 $opts->setValue( 'type', $par );
204 } elseif ( count( $parms ) == 2 ) {
205 $opts->setValue( 'type', $parms[0] );
206 $opts->setValue( 'user', $parms[1] );
207 } elseif ( $par != '' ) {
208 $opts->setValue( 'user', $par );
212 private function show( FormOptions
$opts, array $extraConds ) {
213 # Create a LogPager item to get the results and a LogEventsList item to format them...
214 $loglist = new LogEventsList(
216 $this->getLinkRenderer(),
217 LogEventsList
::USE_CHECKBOXES
220 $pager = new LogPager(
222 $opts->getValue( 'type' ),
223 $opts->getValue( 'user' ),
224 $opts->getValue( 'page' ),
225 $opts->getValue( 'pattern' ),
227 $opts->getValue( 'year' ),
228 $opts->getValue( 'month' ),
229 $opts->getValue( 'day' ),
230 $opts->getValue( 'tagfilter' ),
231 $opts->getValue( 'subtype' ),
232 $opts->getValue( 'logid' )
235 $this->addHeader( $opts->getValue( 'type' ) );
238 if ( $pager->getPerformer() ) {
239 $performerUser = User
::newFromName( $pager->getPerformer(), false );
240 $this->getSkin()->setRelevantUser( $performerUser );
244 $loglist->showOptions(
246 $pager->getPerformer(),
248 $pager->getPattern(),
252 $pager->getFilterParams(),
253 $pager->getTagFilter(),
258 $logBody = $pager->getBody();
260 $this->getOutput()->addHTML(
261 $pager->getNavigationBar() .
262 $this->getActionButtons(
263 $loglist->beginLogEventsList() .
265 $loglist->endLogEventsList()
267 $pager->getNavigationBar()
270 $this->getOutput()->addWikiMsg( 'logempty' );
274 private function getActionButtons( $formcontents ) {
275 $user = $this->getUser();
276 $canRevDelete = $user->isAllowedAll( 'deletedhistory', 'deletelogentry' );
277 $showTagEditUI = ChangeTags
::showTagEditingUI( $user );
278 # If the user doesn't have the ability to delete log entries nor edit tags,
279 # don't bother showing them the button(s).
280 if ( !$canRevDelete && !$showTagEditUI ) {
281 return $formcontents;
284 # Show button to hide log entries and/or edit change tags
285 $s = Html
::openElement(
287 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
289 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
290 $s .= Html
::hidden( 'type', 'logging' ) . "\n";
293 if ( $canRevDelete ) {
294 $buttons .= Html
::element(
298 'name' => 'revisiondelete',
300 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
302 $this->msg( 'showhideselectedlogentries' )->text()
305 if ( $showTagEditUI ) {
306 $buttons .= Html
::element(
310 'name' => 'editchangetags',
312 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
314 $this->msg( 'log-edit-tags' )->text()
318 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
320 $s .= $buttons . $formcontents . $buttons;
321 $s .= Html
::closeElement( 'form' );
327 * Set page title and show header for this log type
328 * @param string $type
331 protected function addHeader( $type ) {
332 $page = new LogPage( $type );
333 $this->getOutput()->setPageTitle( $page->getName() );
334 $this->getOutput()->addHTML( $page->getDescription()
335 ->setContext( $this->getContext() )->parseAsBlock() );
338 protected function getGroupName() {