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