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 Wikimedia\Timestamp\TimestampException
;
27 * A special page that lists log entries
29 * @ingroup SpecialPage
31 class SpecialLog
extends SpecialPage
{
32 public function __construct() {
33 parent
::__construct( 'Log' );
36 public function execute( $par ) {
37 global $wgActorTableSchemaMigrationStage;
40 $this->outputHeader();
41 $out = $this->getOutput();
42 $out->addModules( 'mediawiki.userSuggest' );
43 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
44 $this->addHelpLink( 'Help:Log' );
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', '' );
62 $opts->fetchValuesFromRequest( $this->getRequest() );
63 if ( $par !== null ) {
64 $this->parseParams( $opts, (string)$par );
68 $dateString = $this->getRequest()->getVal( 'wpdate' );
69 if ( !empty( $dateString ) ) {
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)
78 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
79 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
80 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
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', '' );
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] )
100 throw new PermissionsError( $logRestrictions[$type] );
103 # Handle type-specific inputs
105 if ( $opts->getValue( 'type' ) == 'suppress' ) {
106 $offenderName = $opts->getValue( 'offender' );
107 $offender = empty( $offenderName ) ?
null : User
::newFromName( $offenderName, false );
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() ];
114 $qc = [ 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() ];
118 // Allow extensions to add relations to their search types
120 'SpecialLogAddLogSearchRelations',
121 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
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' ) )
139 $this->show( $opts, $qc );
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.
150 public static function getLogTypesOnUser() {
151 static $types = null;
152 if ( $types !== null ) {
161 Hooks
::run( 'GetLogTypesOnUser', [ &$types ] );
166 * Return an array of subpages that this special page will accept.
168 * @return string[] subpages
170 public function getSubpagesForPrefixSearch() {
171 $subpages = LogPage
::validTypes();
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.
183 * @param FormOptions $opts
186 private function parseParams( FormOptions
$opts, $par ) {
188 $par = $par !== null ?
$par : '';
189 $parms = explode( '/', $par );
190 $symsForAll = [ '*', 'all' ];
191 if ( $parms[0] != '' &&
192 ( in_array( $par, LogPage
::validTypes() ) ||
in_array( $par, $symsForAll ) )
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 );
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(
207 $this->getLinkRenderer(),
208 LogEventsList
::USE_CHECKBOXES
211 $pager = new LogPager(
213 $opts->getValue( 'type' ),
214 $opts->getValue( 'user' ),
215 $opts->getValue( 'page' ),
216 $opts->getValue( 'pattern' ),
218 $opts->getValue( 'year' ),
219 $opts->getValue( 'month' ),
220 $opts->getValue( 'day' ),
221 $opts->getValue( 'tagfilter' ),
222 $opts->getValue( 'subtype' ),
223 $opts->getValue( 'logid' )
226 $this->addHeader( $opts->getValue( 'type' ) );
229 if ( $pager->getPerformer() ) {
230 $performerUser = User
::newFromName( $pager->getPerformer(), false );
231 $this->getSkin()->setRelevantUser( $performerUser );
235 $loglist->showOptions(
237 $pager->getPerformer(),
239 $pager->getPattern(),
243 $pager->getFilterParams(),
244 $pager->getTagFilter(),
249 $logBody = $pager->getBody();
251 $this->getOutput()->addHTML(
252 $pager->getNavigationBar() .
253 $this->getActionButtons(
254 $loglist->beginLogEventsList() .
256 $loglist->endLogEventsList()
258 $pager->getNavigationBar()
261 $this->getOutput()->addWikiMsg( 'logempty' );
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;
275 # Show button to hide log entries and/or edit change tags
276 $s = Html
::openElement(
278 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
280 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
281 $s .= Html
::hidden( 'type', 'logging' ) . "\n";
284 if ( $canRevDelete ) {
285 $buttons .= Html
::element(
289 'name' => 'revisiondelete',
291 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
293 $this->msg( 'showhideselectedlogentries' )->text()
296 if ( $showTagEditUI ) {
297 $buttons .= Html
::element(
301 'name' => 'editchangetags',
303 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
305 $this->msg( 'log-edit-tags' )->text()
309 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
311 $s .= $buttons . $formcontents . $buttons;
312 $s .= Html
::closeElement( 'form' );
318 * Set page title and show header for this log type
319 * @param string $type
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() );
329 protected function getGroupName() {