3 * Implements Special:Log
5 * Copyright © 2008 Aaron Schulz
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
23 * @ingroup SpecialPage
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 ) {
38 $this->outputHeader();
39 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
41 $opts = new FormOptions
;
42 $opts->add( 'type', '' );
43 $opts->add( 'user', '' );
44 $opts->add( 'page', '' );
45 $opts->add( 'pattern', false );
46 $opts->add( 'year', null, FormOptions
::INTNULL
);
47 $opts->add( 'month', null, FormOptions
::INTNULL
);
48 $opts->add( 'tagfilter', '' );
49 $opts->add( 'offset', '' );
50 $opts->add( 'dir', '' );
51 $opts->add( 'offender', '' );
52 $opts->add( 'subtype', '' );
55 $opts->fetchValuesFromRequest( $this->getRequest() );
56 if ( $par !== null ) {
57 $this->parseParams( $opts, (string)$par );
60 # Don't let the user get stuck with a certain date
61 if ( $opts->getValue( 'offset' ) ||
$opts->getValue( 'dir' ) == 'prev' ) {
62 $opts->setValue( 'year', '' );
63 $opts->setValue( 'month', '' );
66 // If the user doesn't have the right permission to view the specific
67 // log type, throw a PermissionsError
68 // If the log type is invalid, just show all public logs
69 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
70 $type = $opts->getValue( 'type' );
71 if ( !LogPage
::isLogType( $type ) ) {
72 $opts->setValue( 'type', '' );
73 } elseif ( isset( $logRestrictions[$type] )
74 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
76 throw new PermissionsError( $logRestrictions[$type] );
79 # Handle type-specific inputs
81 if ( $opts->getValue( 'type' ) == 'suppress' ) {
82 $offender = User
::newFromName( $opts->getValue( 'offender' ), false );
83 if ( $offender && $offender->getId() > 0 ) {
84 $qc = [ 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() ];
85 } elseif ( $offender && IP
::isIPAddress( $offender->getName() ) ) {
86 $qc = [ 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() ];
89 // Allow extensions to add relations to their search types
91 'SpecialLogAddLogSearchRelations',
92 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
96 # Some log types are only for a 'User:' title but we might have been given
97 # only the username instead of the full title 'User:username'. This part try
98 # to lookup for a user by that name and eventually fix user input. See bug 1697.
99 if ( in_array( $opts->getValue( 'type' ), self
::getLogTypesOnUser() ) ) {
100 # ok we have a type of log which expect a user title.
101 $target = Title
::newFromText( $opts->getValue( 'page' ) );
102 if ( $target && $target->getNamespace() === NS_MAIN
) {
103 # User forgot to add 'User:', we are adding it for him
104 $opts->setValue( 'page',
105 Title
::makeTitleSafe( NS_USER
, $opts->getValue( 'page' ) )
110 $this->show( $opts, $qc );
114 * List log type for which the target is a user
115 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
116 * Title user instead.
121 public static function getLogTypesOnUser() {
122 static $types = null;
123 if ( $types !== null ) {
132 Hooks
::run( 'GetLogTypesOnUser', [ &$types ] );
137 * Return an array of subpages that this special page will accept.
139 * @return string[] subpages
141 public function getSubpagesForPrefixSearch() {
142 $subpages = $this->getConfig()->get( 'LogTypes' );
148 private function parseParams( FormOptions
$opts, $par ) {
150 $par = $par !== null ?
$par : '';
151 $parms = explode( '/', $par );
152 $symsForAll = [ '*', 'all' ];
153 if ( $parms[0] != '' &&
154 ( in_array( $par, $this->getConfig()->get( 'LogTypes' ) ) ||
in_array( $par, $symsForAll ) )
156 $opts->setValue( 'type', $par );
157 } elseif ( count( $parms ) == 2 ) {
158 $opts->setValue( 'type', $parms[0] );
159 $opts->setValue( 'user', $parms[1] );
160 } elseif ( $par != '' ) {
161 $opts->setValue( 'user', $par );
165 private function show( FormOptions
$opts, array $extraConds ) {
166 # Create a LogPager item to get the results and a LogEventsList item to format them...
167 $loglist = new LogEventsList(
170 LogEventsList
::USE_CHECKBOXES
173 $pager = new LogPager(
175 $opts->getValue( 'type' ),
176 $opts->getValue( 'user' ),
177 $opts->getValue( 'page' ),
178 $opts->getValue( 'pattern' ),
180 $opts->getValue( 'year' ),
181 $opts->getValue( 'month' ),
182 $opts->getValue( 'tagfilter' ),
183 $opts->getValue( 'subtype' )
186 $this->addHeader( $opts->getValue( 'type' ) );
189 if ( $pager->getPerformer() ) {
190 $performerUser = User
::newFromName( $pager->getPerformer(), false );
191 $this->getSkin()->setRelevantUser( $performerUser );
195 $loglist->showOptions(
197 $pager->getPerformer(),
199 $pager->getPattern(),
202 $pager->getFilterParams(),
203 $pager->getTagFilter(),
208 $logBody = $pager->getBody();
210 $this->getOutput()->addHTML(
211 $pager->getNavigationBar() .
212 $this->getActionButtons(
213 $loglist->beginLogEventsList() .
215 $loglist->endLogEventsList()
217 $pager->getNavigationBar()
220 $this->getOutput()->addWikiMsg( 'logempty' );
224 private function getActionButtons( $formcontents ) {
225 $user = $this->getUser();
226 $canRevDelete = $user->isAllowedAll( 'deletedhistory', 'deletelogentry' );
227 $showTagEditUI = ChangeTags
::showTagEditingUI( $user );
228 # If the user doesn't have the ability to delete log entries nor edit tags,
229 # don't bother showing them the button(s).
230 if ( !$canRevDelete && !$showTagEditUI ) {
231 return $formcontents;
234 # Show button to hide log entries and/or edit change tags
235 $s = Html
::openElement(
237 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
239 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
240 $s .= Html
::hidden( 'type', 'logging' ) . "\n";
243 if ( $canRevDelete ) {
244 $buttons .= Html
::element(
248 'name' => 'revisiondelete',
250 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
252 $this->msg( 'showhideselectedlogentries' )->text()
255 if ( $showTagEditUI ) {
256 $buttons .= Html
::element(
260 'name' => 'editchangetags',
262 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
264 $this->msg( 'log-edit-tags' )->text()
268 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
270 $s .= $buttons . $formcontents . $buttons;
271 $s .= Html
::closeElement( 'form' );
277 * Set page title and show header for this log type
278 * @param string $type
281 protected function addHeader( $type ) {
282 $page = new LogPage( $type );
283 $this->getOutput()->setPageTitle( $page->getName() );
284 $this->getOutput()->addHTML( $page->getDescription()
285 ->setContext( $this->getContext() )->parseAsBlock() );
288 protected function getGroupName() {