Merge "Exclude redirects from Special:Fewestrevisions"
[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 use MediaWiki\Block\DatabaseBlock;
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * Implements Special:DeletedContributions to display archived revisions
29 * @ingroup SpecialPage
30 */
31 class DeletedContributionsPage extends SpecialPage {
32 /** @var FormOptions */
33 protected $mOpts;
34
35 function __construct() {
36 parent::__construct( 'DeletedContributions', 'deletedhistory' );
37 }
38
39 /**
40 * Special page "deleted user contributions".
41 * Shows a list of the deleted contributions of a user.
42 *
43 * @param string $par (optional) user name of the user for which to show the contributions
44 */
45 function execute( $par ) {
46 $this->setHeaders();
47 $this->outputHeader();
48 $this->checkPermissions();
49 $this->addHelpLink( 'Help:User contributions' );
50
51 $out = $this->getOutput();
52 $out->setPageTitle( $this->msg( 'deletedcontributions-title' ) );
53
54 $opts = new FormOptions();
55
56 $opts->add( 'target', '' );
57 $opts->add( 'namespace', '' );
58 $opts->add( 'limit', 20 );
59
60 $opts->fetchValuesFromRequest( $this->getRequest() );
61 $opts->validateIntBounds( 'limit', 0, $this->getConfig()->get( 'QueryPageDefaultLimit' ) );
62
63 if ( $par !== null ) {
64 // Beautify the username
65 $par = User::getCanonicalName( $par, false );
66 $opts->setValue( 'target', (string)$par );
67 }
68
69 $ns = $opts->getValue( 'namespace' );
70 if ( $ns !== null && $ns !== '' ) {
71 $opts->setValue( 'namespace', intval( $ns ) );
72 }
73
74 $this->mOpts = $opts;
75
76 $target = trim( $opts->getValue( 'target' ) );
77 if ( !strlen( $target ) ) {
78 $this->getForm();
79
80 return;
81 }
82
83 $userObj = User::newFromName( $target, false );
84 if ( !$userObj ) {
85 $this->getForm();
86
87 return;
88 }
89 $this->getSkin()->setRelevantUser( $userObj );
90
91 $target = $userObj->getName();
92 $out->addSubtitle( $this->getSubTitle( $userObj ) );
93
94 $this->getForm();
95
96 $pager = new DeletedContribsPager( $this->getContext(), $target, $opts->getValue( 'namespace' ) );
97 if ( !$pager->getNumRows() ) {
98 $out->addWikiMsg( 'nocontribs' );
99
100 return;
101 }
102
103 # Show a message about replica DB lag, if applicable
104 $lag = $pager->getDatabase()->getSessionLagStatus()['lag'];
105 if ( $lag > 0 ) {
106 $out->showLagWarning( $lag );
107 }
108
109 $out->addHTML(
110 '<p>' . $pager->getNavigationBar() . '</p>' .
111 $pager->getBody() .
112 '<p>' . $pager->getNavigationBar() . '</p>' );
113
114 # If there were contributions, and it was a valid user or IP, show
115 # the appropriate "footer" message - WHOIS tools, etc.
116 if ( $target != 'newbies' ) {
117 $message = IP::isIPAddress( $target ) ?
118 'sp-contributions-footer-anon' :
119 'sp-contributions-footer';
120
121 if ( !$this->msg( $message )->isDisabled() ) {
122 $out->wrapWikiMsg(
123 "<div class='mw-contributions-footer'>\n$1\n</div>",
124 [ $message, $target ]
125 );
126 }
127 }
128 }
129
130 /**
131 * Generates the subheading with links
132 * @param User $userObj User object for the target
133 * @return string Appropriately-escaped HTML to be output literally
134 */
135 function getSubTitle( $userObj ) {
136 $linkRenderer = $this->getLinkRenderer();
137 if ( $userObj->isAnon() ) {
138 $user = htmlspecialchars( $userObj->getName() );
139 } else {
140 $user = $linkRenderer->makeLink( $userObj->getUserPage(), $userObj->getName() );
141 }
142 $links = '';
143 $nt = $userObj->getUserPage();
144 $talk = $nt->getTalkPage();
145 if ( $talk ) {
146 $tools = SpecialContributions::getUserLinks( $this, $userObj );
147
148 $contributionsLink = $linkRenderer->makeKnownLink(
149 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
150 $this->msg( 'sp-deletedcontributions-contribs' )->text()
151 );
152 if ( isset( $tools['deletedcontribs'] ) ) {
153 // Swap out the deletedcontribs link for our contribs one
154 $tools = wfArrayInsertAfter(
155 $tools, [ 'contribs' => $contributionsLink ], 'deletedcontribs' );
156 unset( $tools['deletedcontribs'] );
157 } else {
158 $tools['contribs'] = $contributionsLink;
159 }
160
161 $links = $this->getLanguage()->pipeList( $tools );
162
163 // Show a note if the user is blocked and display the last block log entry.
164 $block = DatabaseBlock::newFromTarget( $userObj, $userObj );
165 if ( !is_null( $block ) && $block->getType() != DatabaseBlock::TYPE_AUTO ) {
166 if ( $block->getType() == DatabaseBlock::TYPE_RANGE ) {
167 $nt = MediaWikiServices::getInstance()->getNamespaceInfo()->
168 getCanonicalName( NS_USER ) . ':' . $block->getTarget();
169 }
170
171 // LogEventsList::showLogExtract() wants the first parameter by ref
172 $out = $this->getOutput();
173 LogEventsList::showLogExtract(
174 $out,
175 'block',
176 $nt,
177 '',
178 [
179 'lim' => 1,
180 'showIfEmpty' => false,
181 'msgKey' => [
182 'sp-contributions-blocked-notice',
183 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
184 ],
185 'offset' => '' # don't use $this->getRequest() parameter offset
186 ]
187 );
188 }
189 }
190
191 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
192 }
193
194 /**
195 * Generates the namespace selector form with hidden attributes.
196 */
197 function getForm() {
198 $opts = $this->mOpts;
199
200 $formDescriptor = [
201 'target' => [
202 'type' => 'user',
203 'name' => 'target',
204 'label-message' => 'sp-contributions-username',
205 'default' => $opts->getValue( 'target' ),
206 'ipallowed' => true,
207 ],
208
209 'namespace' => [
210 'type' => 'namespaceselect',
211 'name' => 'namespace',
212 'label-message' => 'namespace',
213 'all' => '',
214 ],
215 ];
216
217 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
218 ->setWrapperLegendMsg( 'sp-contributions-search' )
219 ->setSubmitTextMsg( 'sp-contributions-submit' )
220 // prevent setting subpage and 'target' parameter at the same time
221 ->setAction( $this->getPageTitle()->getLocalURL() )
222 ->setMethod( 'get' )
223 ->prepareForm()
224 ->displayForm( false );
225 }
226
227 /**
228 * Return an array of subpages beginning with $search that this special page will accept.
229 *
230 * @param string $search Prefix to search for
231 * @param int $limit Maximum number of results to return (usually 10)
232 * @param int $offset Number of results to skip (usually 0)
233 * @return string[] Matching subpages
234 */
235 public function prefixSearchSubpages( $search, $limit, $offset ) {
236 $user = User::newFromName( $search );
237 if ( !$user ) {
238 // No prefix suggestion for invalid user
239 return [];
240 }
241 // Autocomplete subpage as user list - public to allow caching
242 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
243 }
244
245 protected function getGroupName() {
246 return 'users';
247 }
248 }