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( 'tagfilter', '' );
50 $opts->add( 'offset', '' );
51 $opts->add( 'dir', '' );
52 $opts->add( 'offender', '' );
53 $opts->add( 'subtype', '' );
56 $opts->fetchValuesFromRequest( $this->getRequest() );
57 if ( $par !== null ) {
58 $this->parseParams( $opts, (string)$par );
61 # Don't let the user get stuck with a certain date
62 if ( $opts->getValue( 'offset' ) ||
$opts->getValue( 'dir' ) == 'prev' ) {
63 $opts->setValue( 'year', '' );
64 $opts->setValue( 'month', '' );
67 // If the user doesn't have the right permission to view the specific
68 // log type, throw a PermissionsError
69 // If the log type is invalid, just show all public logs
70 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
71 $type = $opts->getValue( 'type' );
72 if ( !LogPage
::isLogType( $type ) ) {
73 $opts->setValue( 'type', '' );
74 } elseif ( isset( $logRestrictions[$type] )
75 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
77 throw new PermissionsError( $logRestrictions[$type] );
80 # Handle type-specific inputs
82 if ( $opts->getValue( 'type' ) == 'suppress' ) {
83 $offenderName = $opts->getValue( 'offender' );
84 $offender = empty( $offenderName ) ?
null : User
::newFromName( $offenderName, false );
86 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW
) {
87 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
89 if ( $offender->getId() > 0 ) {
90 $field = 'target_author_id';
91 $value = $offender->getId();
93 $field = 'target_author_ip';
94 $value = $offender->getName();
96 if ( !$offender->getActorId() ) {
97 $qc = [ 'ls_field' => $field, 'ls_value' => $value ];
99 $db = wfGetDB( DB_REPLICA
);
101 'ls_field' => [ 'target_author_actor', $field ], // So LogPager::getQueryInfo() works right
104 [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ], LIST_AND
106 $db->makeList( [ 'ls_field' => $field, 'ls_value' => $value ], LIST_AND
),
113 // Allow extensions to add relations to their search types
115 'SpecialLogAddLogSearchRelations',
116 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
120 # Some log types are only for a 'User:' title but we might have been given
121 # only the username instead of the full title 'User:username'. This part try
122 # to lookup for a user by that name and eventually fix user input. See T3697.
123 if ( in_array( $opts->getValue( 'type' ), self
::getLogTypesOnUser() ) ) {
124 # ok we have a type of log which expect a user title.
125 $target = Title
::newFromText( $opts->getValue( 'page' ) );
126 if ( $target && $target->getNamespace() === NS_MAIN
) {
127 # User forgot to add 'User:', we are adding it for him
128 $opts->setValue( 'page',
129 Title
::makeTitleSafe( NS_USER
, $opts->getValue( 'page' ) )
134 $this->show( $opts, $qc );
138 * List log type for which the target is a user
139 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
140 * Title user instead.
145 public static function getLogTypesOnUser() {
146 static $types = null;
147 if ( $types !== null ) {
156 Hooks
::run( 'GetLogTypesOnUser', [ &$types ] );
161 * Return an array of subpages that this special page will accept.
163 * @return string[] subpages
165 public function getSubpagesForPrefixSearch() {
166 $subpages = $this->getConfig()->get( 'LogTypes' );
172 private function parseParams( FormOptions
$opts, $par ) {
174 $par = $par !== null ?
$par : '';
175 $parms = explode( '/', $par );
176 $symsForAll = [ '*', 'all' ];
177 if ( $parms[0] != '' &&
178 ( in_array( $par, $this->getConfig()->get( 'LogTypes' ) ) ||
in_array( $par, $symsForAll ) )
180 $opts->setValue( 'type', $par );
181 } elseif ( count( $parms ) == 2 ) {
182 $opts->setValue( 'type', $parms[0] );
183 $opts->setValue( 'user', $parms[1] );
184 } elseif ( $par != '' ) {
185 $opts->setValue( 'user', $par );
189 private function show( FormOptions
$opts, array $extraConds ) {
190 # Create a LogPager item to get the results and a LogEventsList item to format them...
191 $loglist = new LogEventsList(
193 $this->getLinkRenderer(),
194 LogEventsList
::USE_CHECKBOXES
197 $pager = new LogPager(
199 $opts->getValue( 'type' ),
200 $opts->getValue( 'user' ),
201 $opts->getValue( 'page' ),
202 $opts->getValue( 'pattern' ),
204 $opts->getValue( 'year' ),
205 $opts->getValue( 'month' ),
206 $opts->getValue( 'tagfilter' ),
207 $opts->getValue( 'subtype' )
210 $this->addHeader( $opts->getValue( 'type' ) );
213 if ( $pager->getPerformer() ) {
214 $performerUser = User
::newFromName( $pager->getPerformer(), false );
215 $this->getSkin()->setRelevantUser( $performerUser );
219 $loglist->showOptions(
221 $pager->getPerformer(),
223 $pager->getPattern(),
226 $pager->getFilterParams(),
227 $pager->getTagFilter(),
232 $logBody = $pager->getBody();
234 $this->getOutput()->addHTML(
235 $pager->getNavigationBar() .
236 $this->getActionButtons(
237 $loglist->beginLogEventsList() .
239 $loglist->endLogEventsList()
241 $pager->getNavigationBar()
244 $this->getOutput()->addWikiMsg( 'logempty' );
248 private function getActionButtons( $formcontents ) {
249 $user = $this->getUser();
250 $canRevDelete = $user->isAllowedAll( 'deletedhistory', 'deletelogentry' );
251 $showTagEditUI = ChangeTags
::showTagEditingUI( $user );
252 # If the user doesn't have the ability to delete log entries nor edit tags,
253 # don't bother showing them the button(s).
254 if ( !$canRevDelete && !$showTagEditUI ) {
255 return $formcontents;
258 # Show button to hide log entries and/or edit change tags
259 $s = Html
::openElement(
261 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
263 $s .= Html
::hidden( 'action', 'historysubmit' ) . "\n";
264 $s .= Html
::hidden( 'type', 'logging' ) . "\n";
267 if ( $canRevDelete ) {
268 $buttons .= Html
::element(
272 'name' => 'revisiondelete',
274 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
276 $this->msg( 'showhideselectedlogentries' )->text()
279 if ( $showTagEditUI ) {
280 $buttons .= Html
::element(
284 'name' => 'editchangetags',
286 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
288 $this->msg( 'log-edit-tags' )->text()
292 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
294 $s .= $buttons . $formcontents . $buttons;
295 $s .= Html
::closeElement( 'form' );
301 * Set page title and show header for this log type
302 * @param string $type
305 protected function addHeader( $type ) {
306 $page = new LogPage( $type );
307 $this->getOutput()->setPageTitle( $page->getName() );
308 $this->getOutput()->addHTML( $page->getDescription()
309 ->setContext( $this->getContext() )->parseAsBlock() );
312 protected function getGroupName() {