Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / specials / SpecialLog.php
1 <?php
2 /**
3 * Implements Special:Log
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\MediaWikiServices;
25 use Wikimedia\Timestamp\TimestampException;
26
27 /**
28 * A special page that lists log entries
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialLog extends SpecialPage {
33 public function __construct() {
34 parent::__construct( 'Log' );
35 }
36
37 public function execute( $par ) {
38 global $wgActorTableSchemaMigrationStage;
39
40 $this->setHeaders();
41 $this->outputHeader();
42 $out = $this->getOutput();
43 $out->addModules( 'mediawiki.userSuggest' );
44 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
45 $this->addHelpLink( 'Help:Log' );
46
47 $opts = new FormOptions;
48 $opts->add( 'type', '' );
49 $opts->add( 'user', '' );
50 $opts->add( 'page', '' );
51 $opts->add( 'pattern', false );
52 $opts->add( 'year', null, FormOptions::INTNULL );
53 $opts->add( 'month', null, FormOptions::INTNULL );
54 $opts->add( 'day', null, FormOptions::INTNULL );
55 $opts->add( 'tagfilter', '' );
56 $opts->add( 'offset', '' );
57 $opts->add( 'dir', '' );
58 $opts->add( 'offender', '' );
59 $opts->add( 'subtype', '' );
60 $opts->add( 'logid', '' );
61
62 // Set values
63 $opts->fetchValuesFromRequest( $this->getRequest() );
64 if ( $par !== null ) {
65 $this->parseParams( $opts, (string)$par );
66 }
67
68 // Set date values
69 $dateString = $this->getRequest()->getVal( 'wpdate' );
70 if ( !empty( $dateString ) ) {
71 try {
72 $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
73 } catch ( TimestampException $e ) {
74 // If users provide an invalid date, silently ignore it
75 // instead of letting an exception bubble up (T201411)
76 $dateStamp = false;
77 }
78 if ( $dateStamp ) {
79 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
80 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
81 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
82 }
83 }
84
85 # Don't let the user get stuck with a certain date
86 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
87 $opts->setValue( 'year', '' );
88 $opts->setValue( 'month', '' );
89 }
90
91 // If the user doesn't have the right permission to view the specific
92 // log type, throw a PermissionsError
93 // If the log type is invalid, just show all public logs
94 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
95 $type = $opts->getValue( 'type' );
96 if ( !LogPage::isLogType( $type ) ) {
97 $opts->setValue( 'type', '' );
98 } elseif ( isset( $logRestrictions[$type] )
99 && !$this->getUser()->isAllowed( $logRestrictions[$type] )
100 ) {
101 throw new PermissionsError( $logRestrictions[$type] );
102 }
103
104 # Handle type-specific inputs
105 $qc = [];
106 if ( $opts->getValue( 'type' ) == 'suppress' ) {
107 $offenderName = $opts->getValue( 'offender' );
108 $offender = empty( $offenderName ) ? null : User::newFromName( $offenderName, false );
109 if ( $offender ) {
110 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
111 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
112 } elseif ( $offender->getId() > 0 ) {
113 $qc = [ 'ls_field' => 'target_author_id', 'ls_value' => $offender->getId() ];
114 } else {
115 $qc = [ 'ls_field' => 'target_author_ip', 'ls_value' => $offender->getName() ];
116 }
117 }
118 } else {
119 // Allow extensions to add relations to their search types
120 Hooks::run(
121 'SpecialLogAddLogSearchRelations',
122 [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
123 );
124 }
125
126 # Some log types are only for a 'User:' title but we might have been given
127 # only the username instead of the full title 'User:username'. This part try
128 # to lookup for a user by that name and eventually fix user input. See T3697.
129 if ( in_array( $opts->getValue( 'type' ), self::getLogTypesOnUser() ) ) {
130 # ok we have a type of log which expect a user title.
131 $target = Title::newFromText( $opts->getValue( 'page' ) );
132 if ( $target && $target->getNamespace() === NS_MAIN ) {
133 # User forgot to add 'User:', we are adding it for him
134 $opts->setValue( 'page',
135 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
136 );
137 }
138 }
139
140 $this->show( $opts, $qc );
141 }
142
143 /**
144 * List log type for which the target is a user
145 * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER
146 * Title user instead.
147 *
148 * @since 1.25
149 * @return array
150 */
151 public static function getLogTypesOnUser() {
152 static $types = null;
153 if ( $types !== null ) {
154 return $types;
155 }
156 $types = [
157 'block',
158 'newusers',
159 'rights',
160 ];
161
162 Hooks::run( 'GetLogTypesOnUser', [ &$types ] );
163 return $types;
164 }
165
166 /**
167 * Return an array of subpages that this special page will accept.
168 *
169 * @return string[] subpages
170 */
171 public function getSubpagesForPrefixSearch() {
172 $subpages = LogPage::validTypes();
173 $subpages[] = 'all';
174 sort( $subpages );
175 return $subpages;
176 }
177
178 /**
179 * Set options based on the subpage title parts:
180 * - One part that is a valid log type: Special:Log/logtype
181 * - Two parts: Special:Log/logtype/username
182 * - Otherwise, assume the whole subpage is a username.
183 *
184 * @param FormOptions $opts
185 * @param string $par
186 */
187 private function parseParams( FormOptions $opts, $par ) {
188 # Get parameters
189 $par = $par ?? '';
190 $parms = explode( '/', $par );
191 $symsForAll = [ '*', 'all' ];
192 if ( $parms[0] != '' &&
193 ( in_array( $par, LogPage::validTypes() ) || in_array( $par, $symsForAll ) )
194 ) {
195 $opts->setValue( 'type', $par );
196 } elseif ( count( $parms ) == 2 ) {
197 $opts->setValue( 'type', $parms[0] );
198 $opts->setValue( 'user', $parms[1] );
199 } elseif ( $par != '' ) {
200 $opts->setValue( 'user', $par );
201 }
202 }
203
204 private function show( FormOptions $opts, array $extraConds ) {
205 # Create a LogPager item to get the results and a LogEventsList item to format them...
206 $loglist = new LogEventsList(
207 $this->getContext(),
208 $this->getLinkRenderer(),
209 LogEventsList::USE_CHECKBOXES
210 );
211
212 $pager = new LogPager(
213 $loglist,
214 $opts->getValue( 'type' ),
215 $opts->getValue( 'user' ),
216 $opts->getValue( 'page' ),
217 $opts->getValue( 'pattern' ),
218 $extraConds,
219 $opts->getValue( 'year' ),
220 $opts->getValue( 'month' ),
221 $opts->getValue( 'day' ),
222 $opts->getValue( 'tagfilter' ),
223 $opts->getValue( 'subtype' ),
224 $opts->getValue( 'logid' )
225 );
226
227 $this->addHeader( $opts->getValue( 'type' ) );
228
229 # Set relevant user
230 if ( $pager->getPerformer() ) {
231 $performerUser = User::newFromName( $pager->getPerformer(), false );
232 $this->getSkin()->setRelevantUser( $performerUser );
233 }
234
235 # Show form options
236 $loglist->showOptions(
237 $pager->getType(),
238 $pager->getPerformer(),
239 $pager->getPage(),
240 $pager->getPattern(),
241 $pager->getYear(),
242 $pager->getMonth(),
243 $pager->getDay(),
244 $pager->getFilterParams(),
245 $pager->getTagFilter(),
246 $pager->getAction()
247 );
248
249 # Insert list
250 $logBody = $pager->getBody();
251 if ( $logBody ) {
252 $this->getOutput()->addHTML(
253 $pager->getNavigationBar() .
254 $this->getActionButtons(
255 $loglist->beginLogEventsList() .
256 $logBody .
257 $loglist->endLogEventsList()
258 ) .
259 $pager->getNavigationBar()
260 );
261 } else {
262 $this->getOutput()->addWikiMsg( 'logempty' );
263 }
264 }
265
266 private function getActionButtons( $formcontents ) {
267 $user = $this->getUser();
268 $canRevDelete = MediaWikiServices::getInstance()
269 ->getPermissionManager()
270 ->userHasAllRights( $user, 'deletedhistory', 'deletelogentry' );
271 $showTagEditUI = ChangeTags::showTagEditingUI( $user );
272 # If the user doesn't have the ability to delete log entries nor edit tags,
273 # don't bother showing them the button(s).
274 if ( !$canRevDelete && !$showTagEditUI ) {
275 return $formcontents;
276 }
277
278 # Show button to hide log entries and/or edit change tags
279 $s = Html::openElement(
280 'form',
281 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
282 ) . "\n";
283 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
284 $s .= Html::hidden( 'type', 'logging' ) . "\n";
285
286 $buttons = '';
287 if ( $canRevDelete ) {
288 $buttons .= Html::element(
289 'button',
290 [
291 'type' => 'submit',
292 'name' => 'revisiondelete',
293 'value' => '1',
294 'class' => "deleterevision-log-submit mw-log-deleterevision-button"
295 ],
296 $this->msg( 'showhideselectedlogentries' )->text()
297 ) . "\n";
298 }
299 if ( $showTagEditUI ) {
300 $buttons .= Html::element(
301 'button',
302 [
303 'type' => 'submit',
304 'name' => 'editchangetags',
305 'value' => '1',
306 'class' => "editchangetags-log-submit mw-log-editchangetags-button"
307 ],
308 $this->msg( 'log-edit-tags' )->text()
309 ) . "\n";
310 }
311
312 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
313
314 $s .= $buttons . $formcontents . $buttons;
315 $s .= Html::closeElement( 'form' );
316
317 return $s;
318 }
319
320 /**
321 * Set page title and show header for this log type
322 * @param string $type
323 * @since 1.19
324 */
325 protected function addHeader( $type ) {
326 $page = new LogPage( $type );
327 $this->getOutput()->setPageTitle( $page->getName() );
328 $this->getOutput()->addHTML( $page->getDescription()
329 ->setContext( $this->getContext() )->parseAsBlock() );
330 }
331
332 protected function getGroupName() {
333 return 'changes';
334 }
335 }