Merge "OutputPage: Use PageViewLanguage instead of PageLanguage for mw.config"
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 * Implements Special:DeletedContributions
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 * Implements Special:DeletedContributions to display archived revisions
26 * @ingroup SpecialPage
27 */
28 class DeletedContributionsPage extends SpecialPage {
29 function __construct() {
30 parent::__construct( 'DeletedContributions', 'deletedhistory',
31 /*listed*/true, /*function*/false, /*file*/false );
32 }
33
34 /**
35 * Special page "deleted user contributions".
36 * Shows a list of the deleted contributions of a user.
37 *
38 * @param string $par (optional) user name of the user for which to show the contributions
39 */
40 function execute( $par ) {
41 $this->setHeaders();
42 $this->outputHeader();
43
44 $user = $this->getUser();
45
46 if ( !$this->userCanExecute( $user ) ) {
47 $this->displayRestrictionError();
48
49 return;
50 }
51
52 $request = $this->getRequest();
53 $out = $this->getOutput();
54 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
55
56 $options = [];
57
58 if ( $par !== null ) {
59 $target = $par;
60 } else {
61 $target = $request->getVal( 'target' );
62 }
63
64 if ( !strlen( $target ) ) {
65 $out->addHTML( $this->getForm( '' ) );
66
67 return;
68 }
69
70 $options['limit'] = $request->getInt( 'limit',
71 $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
72 $options['target'] = $target;
73
74 $userObj = User::newFromName( $target, false );
75 if ( !$userObj ) {
76 $out->addHTML( $this->getForm( '' ) );
77
78 return;
79 }
80 $this->getSkin()->setRelevantUser( $userObj );
81
82 $target = $userObj->getName();
83 $out->addSubtitle( $this->getSubTitle( $userObj ) );
84
85 $ns = $request->getVal( 'namespace', null );
86 if ( $ns !== null && $ns !== '' ) {
87 $options['namespace'] = intval( $ns );
88 } else {
89 $options['namespace'] = '';
90 }
91
92 $out->addHTML( $this->getForm( $options ) );
93
94 $pager = new DeletedContribsPager( $this->getContext(), $target, $options['namespace'] );
95 if ( !$pager->getNumRows() ) {
96 $out->addWikiMsg( 'nocontribs' );
97
98 return;
99 }
100
101 # Show a message about slave lag, if applicable
102 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
103 if ( $lag > 0 ) {
104 $out->showLagWarning( $lag );
105 }
106
107 $out->addHTML(
108 '<p>' . $pager->getNavigationBar() . '</p>' .
109 $pager->getBody() .
110 '<p>' . $pager->getNavigationBar() . '</p>' );
111
112 # If there were contributions, and it was a valid user or IP, show
113 # the appropriate "footer" message - WHOIS tools, etc.
114 if ( $target != 'newbies' ) {
115 $message = IP::isIPAddress( $target ) ?
116 'sp-contributions-footer-anon' :
117 'sp-contributions-footer';
118
119 if ( !$this->msg( $message )->isDisabled() ) {
120 $out->wrapWikiMsg(
121 "<div class='mw-contributions-footer'>\n$1\n</div>",
122 [ $message, $target ]
123 );
124 }
125 }
126 }
127
128 /**
129 * Generates the subheading with links
130 * @param User $userObj User object for the target
131 * @return string Appropriately-escaped HTML to be output literally
132 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
133 */
134 function getSubTitle( $userObj ) {
135 if ( $userObj->isAnon() ) {
136 $user = htmlspecialchars( $userObj->getName() );
137 } else {
138 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
139 }
140 $links = '';
141 $nt = $userObj->getUserPage();
142 $id = $userObj->getId();
143 $talk = $nt->getTalkPage();
144 if ( $talk ) {
145 # Talk page link
146 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
147 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
148 # Block / Change block / Unblock links
149 if ( $this->getUser()->isAllowed( 'block' ) ) {
150 if ( $userObj->isBlocked() && $userObj->getBlock()->getType() !== Block::TYPE_AUTO ) {
151 $tools[] = Linker::linkKnown( # Change block link
152 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
153 $this->msg( 'change-blocklink' )->escaped()
154 );
155 $tools[] = Linker::linkKnown( # Unblock link
156 SpecialPage::getTitleFor( 'BlockList' ),
157 $this->msg( 'unblocklink' )->escaped(),
158 [],
159 [
160 'action' => 'unblock',
161 'ip' => $nt->getDBkey()
162 ]
163 );
164 } else {
165 # User is not blocked
166 $tools[] = Linker::linkKnown( # Block link
167 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
168 $this->msg( 'blocklink' )->escaped()
169 );
170 }
171 }
172 # Block log link
173 $tools[] = Linker::linkKnown(
174 SpecialPage::getTitleFor( 'Log' ),
175 $this->msg( 'sp-contributions-blocklog' )->escaped(),
176 [],
177 [
178 'type' => 'block',
179 'page' => $nt->getPrefixedText()
180 ]
181 );
182 # Suppression log link (bug 59120)
183 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
184 $tools[] = Linker::linkKnown(
185 SpecialPage::getTitleFor( 'Log', 'suppress' ),
186 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
187 [],
188 [ 'offender' => $userObj->getName() ]
189 );
190 }
191 }
192
193 # Uploads
194 $tools[] = Linker::linkKnown(
195 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
196 $this->msg( 'sp-contributions-uploads' )->escaped()
197 );
198
199 # Other logs link
200 $tools[] = Linker::linkKnown(
201 SpecialPage::getTitleFor( 'Log' ),
202 $this->msg( 'sp-contributions-logs' )->escaped(),
203 [],
204 [ 'user' => $nt->getText() ]
205 );
206 # Link to contributions
207 $tools[] = Linker::linkKnown(
208 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
209 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
210 );
211
212 # Add a link to change user rights for privileged users
213 $userrightsPage = new UserrightsPage();
214 $userrightsPage->setContext( $this->getContext() );
215 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
216 $tools[] = Linker::linkKnown(
217 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
218 $this->msg( 'sp-contributions-userrights' )->escaped()
219 );
220 }
221
222 Hooks::run( 'ContributionsToolLinks', [ $id, $nt, &$tools ] );
223
224 $links = $this->getLanguage()->pipeList( $tools );
225
226 // Show a note if the user is blocked and display the last block log entry.
227 $block = Block::newFromTarget( $userObj, $userObj );
228 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
229 if ( $block->getType() == Block::TYPE_RANGE ) {
230 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
231 }
232
233 // LogEventsList::showLogExtract() wants the first parameter by ref
234 $out = $this->getOutput();
235 LogEventsList::showLogExtract(
236 $out,
237 'block',
238 $nt,
239 '',
240 [
241 'lim' => 1,
242 'showIfEmpty' => false,
243 'msgKey' => [
244 'sp-contributions-blocked-notice',
245 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
246 ],
247 'offset' => '' # don't use $this->getRequest() parameter offset
248 ]
249 );
250 }
251 }
252
253 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
254 }
255
256 /**
257 * Generates the namespace selector form with hidden attributes.
258 * @param array $options The options to be included.
259 * @return string
260 */
261 function getForm( $options ) {
262 $options['title'] = $this->getPageTitle()->getPrefixedText();
263 if ( !isset( $options['target'] ) ) {
264 $options['target'] = '';
265 } else {
266 $options['target'] = str_replace( '_', ' ', $options['target'] );
267 }
268
269 if ( !isset( $options['namespace'] ) ) {
270 $options['namespace'] = '';
271 }
272
273 if ( !isset( $options['contribs'] ) ) {
274 $options['contribs'] = 'user';
275 }
276
277 if ( $options['contribs'] == 'newbie' ) {
278 $options['target'] = '';
279 }
280
281 $f = Xml::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] );
282
283 foreach ( $options as $name => $value ) {
284 if ( in_array( $name, [ 'namespace', 'target', 'contribs' ] ) ) {
285 continue;
286 }
287 $f .= "\t" . Html::hidden( $name, $value ) . "\n";
288 }
289
290 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
291
292 $f .= Xml::openElement( 'fieldset' );
293 $f .= Xml::element( 'legend', [], $this->msg( 'sp-contributions-search' )->text() );
294 $f .= Xml::tags(
295 'label',
296 [ 'for' => 'target' ],
297 $this->msg( 'sp-contributions-username' )->parse()
298 ) . ' ';
299 $f .= Html::input(
300 'target',
301 $options['target'],
302 'text',
303 [
304 'size' => '20',
305 'required' => '',
306 'class' => [
307 'mw-autocomplete-user', // used by mediawiki.userSuggest
308 ],
309 ] + ( $options['target'] ? [] : [ 'autofocus' ] )
310 ) . ' ';
311 $f .= Html::namespaceSelector(
312 [
313 'selected' => $options['namespace'],
314 'all' => '',
315 'label' => $this->msg( 'namespace' )->text()
316 ],
317 [
318 'name' => 'namespace',
319 'id' => 'namespace',
320 'class' => 'namespaceselector',
321 ]
322 ) . ' ';
323 $f .= Xml::submitButton( $this->msg( 'sp-contributions-submit' )->text() );
324 $f .= Xml::closeElement( 'fieldset' );
325 $f .= Xml::closeElement( 'form' );
326
327 return $f;
328 }
329
330 /**
331 * Return an array of subpages beginning with $search that this special page will accept.
332 *
333 * @param string $search Prefix to search for
334 * @param int $limit Maximum number of results to return (usually 10)
335 * @param int $offset Number of results to skip (usually 0)
336 * @return string[] Matching subpages
337 */
338 public function prefixSearchSubpages( $search, $limit, $offset ) {
339 $user = User::newFromName( $search );
340 if ( !$user ) {
341 // No prefix suggestion for invalid user
342 return [];
343 }
344 // Autocomplete subpage as user list - public to allow caching
345 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
346 }
347
348 protected function getGroupName() {
349 return 'users';
350 }
351 }