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