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