Merge "Add CollationFa"
[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 replica DB 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 */
131 function getSubTitle( $userObj ) {
132 $linkRenderer = $this->getLinkRenderer();
133 if ( $userObj->isAnon() ) {
134 $user = htmlspecialchars( $userObj->getName() );
135 } else {
136 $user = $linkRenderer->makeLink( $userObj->getUserPage(), $userObj->getName() );
137 }
138 $links = '';
139 $nt = $userObj->getUserPage();
140 $talk = $nt->getTalkPage();
141 if ( $talk ) {
142 $tools = SpecialContributions::getUserLinks( $this, $userObj );
143
144 # Link to contributions
145 $insert['contribs'] = $linkRenderer->makeKnownLink(
146 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
147 $this->msg( 'sp-deletedcontributions-contribs' )->text()
148 );
149
150 // Swap out the deletedcontribs link for our contribs one
151 $tools = wfArrayInsertAfter( $tools, $insert, 'deletedcontribs' );
152 unset( $tools['deletedcontribs'] );
153
154 $links = $this->getLanguage()->pipeList( $tools );
155
156 // Show a note if the user is blocked and display the last block log entry.
157 $block = Block::newFromTarget( $userObj, $userObj );
158 if ( !is_null( $block ) && $block->getType() != Block::TYPE_AUTO ) {
159 if ( $block->getType() == Block::TYPE_RANGE ) {
160 $nt = MWNamespace::getCanonicalName( NS_USER ) . ':' . $block->getTarget();
161 }
162
163 // LogEventsList::showLogExtract() wants the first parameter by ref
164 $out = $this->getOutput();
165 LogEventsList::showLogExtract(
166 $out,
167 'block',
168 $nt,
169 '',
170 [
171 'lim' => 1,
172 'showIfEmpty' => false,
173 'msgKey' => [
174 'sp-contributions-blocked-notice',
175 $userObj->getName() # Support GENDER in 'sp-contributions-blocked-notice'
176 ],
177 'offset' => '' # don't use $this->getRequest() parameter offset
178 ]
179 );
180 }
181 }
182
183 return $this->msg( 'contribsub2' )->rawParams( $user, $links )->params( $userObj->getName() );
184 }
185
186 /**
187 * Generates the namespace selector form with hidden attributes.
188 */
189 function getForm() {
190 $opts = $this->mOpts;
191
192 $formDescriptor = [
193 'target' => [
194 'type' => 'user',
195 'name' => 'target',
196 'label-message' => 'sp-contributions-username',
197 'default' => $opts->getValue( 'target' ),
198 'ipallowed' => true,
199 ],
200
201 'namespace' => [
202 'type' => 'namespaceselect',
203 'name' => 'namespace',
204 'label-message' => 'namespace',
205 'all' => '',
206 ],
207 ];
208
209 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
210 ->setWrapperLegendMsg( 'sp-contributions-search' )
211 ->setSubmitTextMsg( 'sp-contributions-submit' )
212 // prevent setting subpage and 'target' parameter at the same time
213 ->setAction( $this->getPageTitle()->getLocalURL() )
214 ->setMethod( 'get' )
215 ->prepareForm()
216 ->displayForm( false );
217 }
218
219 /**
220 * Return an array of subpages beginning with $search that this special page will accept.
221 *
222 * @param string $search Prefix to search for
223 * @param int $limit Maximum number of results to return (usually 10)
224 * @param int $offset Number of results to skip (usually 0)
225 * @return string[] Matching subpages
226 */
227 public function prefixSearchSubpages( $search, $limit, $offset ) {
228 $user = User::newFromName( $search );
229 if ( !$user ) {
230 // No prefix suggestion for invalid user
231 return [];
232 }
233 // Autocomplete subpage as user list - public to allow caching
234 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
235 }
236
237 protected function getGroupName() {
238 return 'users';
239 }
240 }