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