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