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