Implement extension registration from an extension.json file
[mediawiki.git] / includes / specials / SpecialLog.php
blob923283eb2e545d25f07506d7d57c493b07b0eea7
1 <?php
2 /**
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
22 * @file
23 * @ingroup SpecialPage
26 /**
27 * A special page that lists log entries
29 * @ingroup SpecialPage
31 class SpecialLog extends SpecialPage {
32 /**
33 * List log type for which the target is a user
34 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
35 * Title user instead.
37 private $typeOnUser = array(
38 'block',
39 'newusers',
40 'rights',
43 public function __construct() {
44 parent::__construct( 'Log' );
47 public function execute( $par ) {
48 $this->setHeaders();
49 $this->outputHeader();
50 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
52 $opts = new FormOptions;
53 $opts->add( 'type', '' );
54 $opts->add( 'user', '' );
55 $opts->add( 'page', '' );
56 $opts->add( 'pattern', false );
57 $opts->add( 'year', null, FormOptions::INTNULL );
58 $opts->add( 'month', null, FormOptions::INTNULL );
59 $opts->add( 'tagfilter', '' );
60 $opts->add( 'offset', '' );
61 $opts->add( 'dir', '' );
62 $opts->add( 'offender', '' );
64 // Set values
65 $opts->fetchValuesFromRequest( $this->getRequest() );
66 if ( $par !== null ) {
67 $this->parseParams( $opts, (string)$par );
70 # Don't let the user get stuck with a certain date
71 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
72 $opts->setValue( 'year', '' );
73 $opts->setValue( 'month', '' );
76 // If the user doesn't have the right permission to view the specific
77 // log type, throw a PermissionsError
78 // If the log type is invalid, just show all public logs
79 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
80 $type = $opts->getValue( 'type' );
81 if ( !LogPage::isLogType( $type ) ) {
82 $opts->setValue( 'type', '' );
83 } elseif ( isset( $logRestrictions[$type] )
84 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
85 ) {
86 throw new PermissionsError( $logRestrictions[$type] );
89 # Handle type-specific inputs
90 $qc = array();
91 if ( $opts->getValue( 'type' ) == 'suppress' ) {
92 $offender = User::newFromName( $opts->getValue( 'offender' ), false );
93 if ( $offender && $offender->getId() > 0 ) {
94 $qc = array( 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() );
95 } elseif ( $offender && IP::isIPAddress( $offender->getName() ) ) {
96 $qc = array( 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() );
98 } else {
99 // Allow extensions to add relations to their search types
100 Hooks::run( 'SpecialLogAddLogSearchRelations', array( $opts->getValue( 'type' ), $this->getRequest(), &$qc ) );
103 # Some log types are only for a 'User:' title but we might have been given
104 # only the username instead of the full title 'User:username'. This part try
105 # to lookup for a user by that name and eventually fix user input. See bug 1697.
106 Hooks::run( 'GetLogTypesOnUser', array( &$this->typeOnUser ) );
107 if ( in_array( $opts->getValue( 'type' ), $this->typeOnUser ) ) {
108 # ok we have a type of log which expect a user title.
109 $target = Title::newFromText( $opts->getValue( 'page' ) );
110 if ( $target && $target->getNamespace() === NS_MAIN ) {
111 # User forgot to add 'User:', we are adding it for him
112 $opts->setValue( 'page',
113 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
118 $this->show( $opts, $qc );
122 * Return an array of subpages that this special page will accept.
124 * @return string[] subpages
126 public function getSubpagesForPrefixSearch() {
127 $subpages = $this->getConfig()->get( 'LogTypes' );
128 $subpages[] = 'all';
129 sort( $subpages );
130 return $subpages;
133 private function parseParams( FormOptions $opts, $par ) {
134 # Get parameters
135 $parms = explode( '/', ( $par = ( $par !== null ) ? $par : '' ) );
136 $symsForAll = array( '*', 'all' );
137 if ( $parms[0] != '' &&
138 ( in_array( $par, $this->getConfig()->get( 'LogTypes' ) ) || in_array( $par, $symsForAll ) )
140 $opts->setValue( 'type', $par );
141 } elseif ( count( $parms ) == 2 ) {
142 $opts->setValue( 'type', $parms[0] );
143 $opts->setValue( 'user', $parms[1] );
144 } elseif ( $par != '' ) {
145 $opts->setValue( 'user', $par );
149 private function show( FormOptions $opts, array $extraConds ) {
150 # Create a LogPager item to get the results and a LogEventsList item to format them...
151 $loglist = new LogEventsList(
152 $this->getContext(),
153 null,
154 LogEventsList::USE_REVDEL_CHECKBOXES
156 $pager = new LogPager(
157 $loglist,
158 $opts->getValue( 'type' ),
159 $opts->getValue( 'user' ),
160 $opts->getValue( 'page' ),
161 $opts->getValue( 'pattern' ),
162 $extraConds,
163 $opts->getValue( 'year' ),
164 $opts->getValue( 'month' ),
165 $opts->getValue( 'tagfilter' )
168 $this->addHeader( $opts->getValue( 'type' ) );
170 # Set relevant user
171 if ( $pager->getPerformer() ) {
172 $this->getSkin()->setRelevantUser( User::newFromName( $pager->getPerformer() ) );
175 # Show form options
176 $loglist->showOptions(
177 $pager->getType(),
178 $opts->getValue( 'user' ),
179 $pager->getPage(),
180 $pager->getPattern(),
181 $pager->getYear(),
182 $pager->getMonth(),
183 $pager->getFilterParams(),
184 $opts->getValue( 'tagfilter' )
187 # Insert list
188 $logBody = $pager->getBody();
189 if ( $logBody ) {
190 $this->getOutput()->addHTML(
191 $pager->getNavigationBar() .
192 $this->getRevisionButton(
193 $loglist->beginLogEventsList() .
194 $logBody .
195 $loglist->endLogEventsList()
197 $pager->getNavigationBar()
199 } else {
200 $this->getOutput()->addWikiMsg( 'logempty' );
204 private function getRevisionButton( $formcontents ) {
205 # If the user doesn't have the ability to delete log entries,
206 # don't bother showing them the button.
207 if ( !$this->getUser()->isAllowedAll( 'deletedhistory', 'deletelogentry' ) ) {
208 return $formcontents;
211 # Show button to hide log entries
212 $s = Html::openElement(
213 'form',
214 array( 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' )
215 ) . "\n";
216 $s .= Html::hidden( 'title', SpecialPage::getTitleFor( 'Revisiondelete' ) ) . "\n";
217 $s .= Html::hidden( 'target', SpecialPage::getTitleFor( 'Log' ) ) . "\n";
218 $s .= Html::hidden( 'type', 'logging' ) . "\n";
219 $button = Html::element(
220 'button',
221 array(
222 'type' => 'submit',
223 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
225 $this->msg( 'showhideselectedlogentries' )->text()
226 ) . "\n";
227 $s .= $button . $formcontents . $button;
228 $s .= Html::closeElement( 'form' );
230 return $s;
234 * Set page title and show header for this log type
235 * @param string $type
236 * @since 1.19
238 protected function addHeader( $type ) {
239 $page = new LogPage( $type );
240 $this->getOutput()->setPageTitle( $page->getName()->text() );
241 $this->getOutput()->addHTML( $page->getDescription()->parseAsBlock() );
244 protected function getGroupName() {
245 return 'changes';