Merge "Add @since tags to MediaWikiServices"
[lhc/web/wiklou.git] / includes / specials / SpecialLog.php
1 <?php
2 /**
3 * Implements Special:Log
4 *
5 * Copyright © 2008 Aaron Schulz
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup SpecialPage
24 */
25
26 /**
27 * A special page that lists log entries
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialLog extends SpecialPage {
32 public function __construct() {
33 parent::__construct( 'Log' );
34 }
35
36 public function execute( $par ) {
37 $this->setHeaders();
38 $this->outputHeader();
39 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
40
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', '' );
53
54 // Set values
55 $opts->fetchValuesFromRequest( $this->getRequest() );
56 if ( $par !== null ) {
57 $this->parseParams( $opts, (string)$par );
58 }
59
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', '' );
64 }
65
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] )
75 ) {
76 throw new PermissionsError( $logRestrictions[$type] );
77 }
78
79 # Handle type-specific inputs
80 $qc = [];
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() ];
87 }
88 } else {
89 // Allow extensions to add relations to their search types
90 Hooks::run(
91 'SpecialLogAddLogSearchRelations',
92 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
93 );
94 }
95
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' ) )
106 );
107 }
108 }
109
110 $this->show( $opts, $qc );
111 }
112
113 /**
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.
117 *
118 * @since 1.25
119 * @return array
120 */
121 public static function getLogTypesOnUser() {
122 static $types = null;
123 if ( $types !== null ) {
124 return $types;
125 }
126 $types = [
127 'block',
128 'newusers',
129 'rights',
130 ];
131
132 Hooks::run( 'GetLogTypesOnUser', [ &$types ] );
133 return $types;
134 }
135
136 /**
137 * Return an array of subpages that this special page will accept.
138 *
139 * @return string[] subpages
140 */
141 public function getSubpagesForPrefixSearch() {
142 $subpages = $this->getConfig()->get( 'LogTypes' );
143 $subpages[] = 'all';
144 sort( $subpages );
145 return $subpages;
146 }
147
148 private function parseParams( FormOptions $opts, $par ) {
149 # Get parameters
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 ) )
155 ) {
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 );
162 }
163 }
164
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(
168 $this->getContext(),
169 null,
170 LogEventsList::USE_CHECKBOXES
171 );
172
173 $pager = new LogPager(
174 $loglist,
175 $opts->getValue( 'type' ),
176 $opts->getValue( 'user' ),
177 $opts->getValue( 'page' ),
178 $opts->getValue( 'pattern' ),
179 $extraConds,
180 $opts->getValue( 'year' ),
181 $opts->getValue( 'month' ),
182 $opts->getValue( 'tagfilter' ),
183 $opts->getValue( 'subtype' )
184 );
185
186 $this->addHeader( $opts->getValue( 'type' ) );
187
188 # Set relevant user
189 if ( $pager->getPerformer() ) {
190 $performerUser = User::newFromName( $pager->getPerformer(), false );
191 $this->getSkin()->setRelevantUser( $performerUser );
192 }
193
194 # Show form options
195 $loglist->showOptions(
196 $pager->getType(),
197 $pager->getPerformer(),
198 $pager->getPage(),
199 $pager->getPattern(),
200 $pager->getYear(),
201 $pager->getMonth(),
202 $pager->getFilterParams(),
203 $pager->getTagFilter(),
204 $pager->getAction()
205 );
206
207 # Insert list
208 $logBody = $pager->getBody();
209 if ( $logBody ) {
210 $this->getOutput()->addHTML(
211 $pager->getNavigationBar() .
212 $this->getActionButtons(
213 $loglist->beginLogEventsList() .
214 $logBody .
215 $loglist->endLogEventsList()
216 ) .
217 $pager->getNavigationBar()
218 );
219 } else {
220 $this->getOutput()->addWikiMsg( 'logempty' );
221 }
222 }
223
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;
232 }
233
234 # Show button to hide log entries and/or edit change tags
235 $s = Html::openElement(
236 'form',
237 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
238 ) . "\n";
239 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
240 $s .= Html::hidden( 'type', 'logging' ) . "\n";
241
242 $buttons = '';
243 if ( $canRevDelete ) {
244 $buttons .= Html::element(
245 'button',
246 [
247 'type' => 'submit',
248 'name' => 'revisiondelete',
249 'value' => '1',
250 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
251 ],
252 $this->msg( 'showhideselectedlogentries' )->text()
253 ) . "\n";
254 }
255 if ( $showTagEditUI ) {
256 $buttons .= Html::element(
257 'button',
258 [
259 'type' => 'submit',
260 'name' => 'editchangetags',
261 'value' => '1',
262 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
263 ],
264 $this->msg( 'log-edit-tags' )->text()
265 ) . "\n";
266 }
267
268 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
269
270 $s .= $buttons . $formcontents . $buttons;
271 $s .= Html::closeElement( 'form' );
272
273 return $s;
274 }
275
276 /**
277 * Set page title and show header for this log type
278 * @param string $type
279 * @since 1.19
280 */
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() );
286 }
287
288 protected function getGroupName() {
289 return 'changes';
290 }
291 }