Convert Special:DeletedContributions to use OOUI.
[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 /** @var FormOptions */
30 protected $mOpts;
31
32 function __construct() {
33 parent::__construct( 'DeletedContributions', 'deletedhistory' );
34 }
35
36 /**
37 * Special page "deleted user contributions".
38 * Shows a list of the deleted contributions of a user.
39 *
40 * @param string $par (optional) user name of the user for which to show the contributions
41 */
42 function execute( $par ) {
43 $this->setHeaders();
44 $this->outputHeader();
45 $this->checkPermissions();
46
47 $user = $this->getUser();
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 $opts->setValue( 'target', $par );
63 }
64
65 $ns = $opts->getValue( 'namespace' );
66 if ( $ns !== null && $ns !== '' ) {
67 $opts->setValue( 'namespace', intval( $ns ) );
68 }
69
70 $this->mOpts = $opts;
71
72 $target = $opts->getValue( 'target' );
73 if ( !strlen( $target ) ) {
74 $this->getForm();
75
76 return;
77 }
78
79 $userObj = User::newFromName( $target, false );
80 if ( !$userObj ) {
81 $this->getForm();
82
83 return;
84 }
85 $this->getSkin()->setRelevantUser( $userObj );
86
87 $target = $userObj->getName();
88 $out->addSubtitle( $this->getSubTitle( $userObj ) );
89
90 $this->getForm();
91
92 $pager = new DeletedContribsPager( $this->getContext(), $target, $opts->getValue( 'namespace' ) );
93 if ( !$pager->getNumRows() ) {
94 $out->addWikiMsg( 'nocontribs' );
95
96 return;
97 }
98
99 # Show a message about slave lag, if applicable
100 $lag = wfGetLB()->safeGetLag( $pager->getDatabase() );
101 if ( $lag > 0 ) {
102 $out->showLagWarning( $lag );
103 }
104
105 $out->addHTML(
106 '<p>' . $pager->getNavigationBar() . '</p>' .
107 $pager->getBody() .
108 '<p>' . $pager->getNavigationBar() . '</p>' );
109
110 # If there were contributions, and it was a valid user or IP, show
111 # the appropriate "footer" message - WHOIS tools, etc.
112 if ( $target != 'newbies' ) {
113 $message = IP::isIPAddress( $target ) ?
114 'sp-contributions-footer-anon' :
115 'sp-contributions-footer';
116
117 if ( !$this->msg( $message )->isDisabled() ) {
118 $out->wrapWikiMsg(
119 "<div class='mw-contributions-footer'>\n$1\n</div>",
120 [ $message, $target ]
121 );
122 }
123 }
124 }
125
126 /**
127 * Generates the subheading with links
128 * @param User $userObj User object for the target
129 * @return string Appropriately-escaped HTML to be output literally
130 * @todo FIXME: Almost the same as contributionsSub in SpecialContributions.php. Could be combined.
131 */
132 function getSubTitle( $userObj ) {
133 if ( $userObj->isAnon() ) {
134 $user = htmlspecialchars( $userObj->getName() );
135 } else {
136 $user = Linker::link( $userObj->getUserPage(), htmlspecialchars( $userObj->getName() ) );
137 }
138 $links = '';
139 $nt = $userObj->getUserPage();
140 $id = $userObj->getId();
141 $talk = $nt->getTalkPage();
142 if ( $talk ) {
143 # Talk page link
144 $tools[] = Linker::link( $talk, $this->msg( 'sp-contributions-talk' )->escaped() );
145 if ( ( $id !== null ) || ( $id === null && IP::isIPAddress( $nt->getText() ) ) ) {
146 # Block / Change block / Unblock links
147 if ( $this->getUser()->isAllowed( 'block' ) ) {
148 if ( $userObj->isBlocked() && $userObj->getBlock()->getType() !== Block::TYPE_AUTO ) {
149 $tools[] = Linker::linkKnown( # Change block link
150 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
151 $this->msg( 'change-blocklink' )->escaped()
152 );
153 $tools[] = Linker::linkKnown( # Unblock link
154 SpecialPage::getTitleFor( 'BlockList' ),
155 $this->msg( 'unblocklink' )->escaped(),
156 [],
157 [
158 'action' => 'unblock',
159 'ip' => $nt->getDBkey()
160 ]
161 );
162 } else {
163 # User is not blocked
164 $tools[] = Linker::linkKnown( # Block link
165 SpecialPage::getTitleFor( 'Block', $nt->getDBkey() ),
166 $this->msg( 'blocklink' )->escaped()
167 );
168 }
169 }
170 # Block log link
171 $tools[] = Linker::linkKnown(
172 SpecialPage::getTitleFor( 'Log' ),
173 $this->msg( 'sp-contributions-blocklog' )->escaped(),
174 [],
175 [
176 'type' => 'block',
177 'page' => $nt->getPrefixedText()
178 ]
179 );
180 # Suppression log link (bug 59120)
181 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
182 $tools[] = Linker::linkKnown(
183 SpecialPage::getTitleFor( 'Log', 'suppress' ),
184 $this->msg( 'sp-contributions-suppresslog' )->escaped(),
185 [],
186 [ 'offender' => $userObj->getName() ]
187 );
188 }
189 }
190
191 # Uploads
192 $tools[] = Linker::linkKnown(
193 SpecialPage::getTitleFor( 'Listfiles', $userObj->getName() ),
194 $this->msg( 'sp-contributions-uploads' )->escaped()
195 );
196
197 # Other logs link
198 $tools[] = Linker::linkKnown(
199 SpecialPage::getTitleFor( 'Log' ),
200 $this->msg( 'sp-contributions-logs' )->escaped(),
201 [],
202 [ 'user' => $nt->getText() ]
203 );
204 # Link to contributions
205 $tools[] = Linker::linkKnown(
206 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
207 $this->msg( 'sp-deletedcontributions-contribs' )->escaped()
208 );
209
210 # Add a link to change user rights for privileged users
211 $userrightsPage = new UserrightsPage();
212 $userrightsPage->setContext( $this->getContext() );
213 if ( $userrightsPage->userCanChangeRights( $userObj ) ) {
214 $tools[] = Linker::linkKnown(
215 SpecialPage::getTitleFor( 'Userrights', $nt->getDBkey() ),
216 $this->msg( 'sp-contributions-userrights' )->escaped()
217 );
218 }
219
220 Hooks::run( 'ContributionsToolLinks', [ $id, $nt, &$tools ] );
221
222 $links = $this->getLanguage()->pipeList( $tools );
223
224 // Show a note if the user is blocked and display the last block log entry.
225 $block = Block::newFromTarget( $userObj, $userObj );
226 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
227 if ( $block->getType() == Block::TYPE_RANGE ) {
228 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
229 }
230
231 // LogEventsList::showLogExtract() wants the first parameter by ref
232 $out = $this->getOutput();
233 LogEventsList::showLogExtract(
234 $out,
235 'block',
236 $nt,
237 '',
238 [
239 'lim' => 1,
240 'showIfEmpty' => false,
241 'msgKey' => [
242 'sp-contributions-blocked-notice',
243 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
244 ],
245 'offset' => '' # don't use $this->getRequest() parameter offset
246 ]
247 );
248 }
249 }
250
251 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
252 }
253
254 /**
255 * Generates the namespace selector form with hidden attributes.
256 */
257 function getForm() {
258 $opts = $this->mOpts;
259
260 $formDescriptor = [
261 'target' => [
262 'type' => 'user',
263 'name' => 'target',
264 'label-message' => 'sp-contributions-username',
265 'default' => $opts->getValue( 'target' ),
266 'ipallowed' => true,
267 ],
268
269 'namespace' => [
270 'type' => 'namespaceselect',
271 'name' => 'namespace',
272 'label-message' => 'namespace',
273 'all' => '',
274 ],
275 ];
276
277 $form = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
278 ->setWrapperLegendMsg( 'sp-contributions-search' )
279 ->setSubmitTextMsg( 'sp-contributions-submit' )
280 // prevent setting subpage and 'target' parameter at the same time
281 ->setAction( $this->getPageTitle()->getLocalURL() )
282 ->setMethod( 'get' )
283 ->prepareForm()
284 ->displayForm( false );
285 }
286
287 /**
288 * Return an array of subpages beginning with $search that this special page will accept.
289 *
290 * @param string $search Prefix to search for
291 * @param int $limit Maximum number of results to return (usually 10)
292 * @param int $offset Number of results to skip (usually 0)
293 * @return string[] Matching subpages
294 */
295 public function prefixSearchSubpages( $search, $limit, $offset ) {
296 $user = User::newFromName( $search );
297 if ( !$user ) {
298 // No prefix suggestion for invalid user
299 return [];
300 }
301 // Autocomplete subpage as user list - public to allow caching
302 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
303 }
304
305 protected function getGroupName() {
306 return 'users';
307 }
308 }