Merge "Update the way captions show up in packed-overlay and packed-hover"
[lhc/web/wiklou.git] / includes / specials / SpecialMergeHistory.php
1 <?php
2 /**
3 * Implements Special:MergeHistory
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 * Special page allowing users with the appropriate permissions to
26 * merge article histories, with some restrictions
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialMergeHistory extends SpecialPage {
31 /** @var FormOptions */
32 protected $mOpts;
33
34 /** @var Status */
35 protected $mStatus;
36
37 /** @var Title|null */
38 protected $mTargetObj, $mDestObj;
39
40 /** @var int[] */
41 public $prevId;
42
43 public function __construct() {
44 parent::__construct( 'MergeHistory', 'mergehistory' );
45 }
46
47 public function doesWrites() {
48 return true;
49 }
50
51 public function execute( $par ) {
52 $this->useTransactionalTimeLimit();
53
54 $this->checkPermissions();
55 $this->checkReadOnly();
56
57 $this->setHeaders();
58 $this->outputHeader();
59
60 $this->addHelpLink( 'Help:Merge history' );
61
62 $opts = new FormOptions();
63
64 $opts->add( 'target', '' );
65 $opts->add( 'dest', '' );
66 $opts->add( 'target', '' );
67 $opts->add( 'mergepoint', '' );
68 $opts->add( 'reason', '' );
69 $opts->add( 'merge', false );
70
71 $opts->fetchValuesFromRequest( $this->getRequest() );
72
73 $target = $opts->getValue( 'target' );
74 $dest = $opts->getValue( 'dest' );
75 $targetObj = Title::newFromText( $target );
76 $destObj = Title::newFromText( $dest );
77 $status = Status::newGood();
78
79 $this->mOpts = $opts;
80 $this->mTargetObj = $targetObj;
81 $this->mDestObj = $destObj;
82
83 if ( $opts->getValue( 'merge' ) && $targetObj && $destObj ) {
84 $this->merge();
85
86 return;
87 }
88
89 if ( $target === '' && $dest === '' ) {
90 $this->showMergeForm();
91
92 return;
93 }
94
95 if ( !$targetObj instanceof Title ) {
96 $status->merge( Status::newFatal( 'mergehistory-invalid-source' ) );
97 } elseif ( !$targetObj->exists() ) {
98 $status->merge( Status::newFatal( 'mergehistory-no-source',
99 wfEscapeWikiText( $targetObj->getPrefixedText() )
100 ) );
101 }
102
103 if ( !$destObj instanceof Title ) {
104 $status->merge( Status::newFatal( 'mergehistory-invalid-destination' ) );
105 } elseif ( !$destObj->exists() ) {
106 $status->merge( Status::newFatal( 'mergehistory-no-destination',
107 wfEscapeWikiText( $destObj->getPrefixedText() )
108 ) );
109 }
110
111 if ( $targetObj && $destObj && $targetObj->equals( $destObj ) ) {
112 $status->merge( Status::newFatal( 'mergehistory-same-destination' ) );
113 }
114
115 $this->mStatus = $status;
116
117 $this->showMergeForm();
118
119 if ( $status->isOK() ) {
120 $this->showHistory();
121 }
122 }
123
124 function showMergeForm() {
125 $formDescriptor = [
126 'target' => [
127 'type' => 'title',
128 'name' => 'target',
129 'label-message' => 'mergehistory-from',
130 'required' => true,
131 ],
132
133 'dest' => [
134 'type' => 'title',
135 'name' => 'dest',
136 'label-message' => 'mergehistory-into',
137 'required' => true,
138 ],
139 ];
140
141 $form = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
142 ->setIntro( $this->msg( 'mergehistory-header' ) )
143 ->setWrapperLegendMsg( 'mergehistory-box' )
144 ->setSubmitTextMsg( 'mergehistory-go' )
145 ->setMethod( 'post' )
146 ->prepareForm()
147 ->displayForm( $this->mStatus );
148 }
149
150 private function showHistory() {
151 # List all stored revisions
152 $revisions = new MergeHistoryPager(
153 $this, [], $this->mTargetObj, $this->mDestObj
154 );
155 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
156
157 $out = $this->getOutput();
158 $header = '<h2 id="mw-mergehistory">' .
159 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n";
160
161 if ( $haveRevisions ) {
162 $hiddenFields = [
163 'merge' => true,
164 'target' => $this->mOpts->getValue( 'target' ),
165 'dest' => $this->mOpts->getValue( 'dest' ),
166 ];
167
168 $formDescriptor = [
169 'reason' => [
170 'type' => 'text',
171 'name' => 'reason',
172 'label-message' => 'mergehistory-reason',
173 ],
174 ];
175
176 $mergeText = $this->msg( 'mergehistory-merge',
177 $this->mTargetObj->getPrefixedText(),
178 $this->mDestObj->getPrefixedText()
179 )->parse();
180
181 $history = $header .
182 $revisions->getNavigationBar() .
183 '<ul>' .
184 $revisions->getBody() .
185 '</ul>' .
186 $revisions->getNavigationBar();
187
188 $form = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
189 ->addHiddenFields( $hiddenFields )
190 ->setPreText( $mergeText )
191 ->setFooterText( $history )
192 ->setSubmitTextMsg( 'mergehistory-submit' )
193 ->setMethod( 'post' )
194 ->prepareForm()
195 ->displayForm( false );
196 } else {
197 $out->addHTML( $header );
198 $out->addWikiMsg( 'mergehistory-empty' );
199 }
200
201 # Show relevant lines from the merge log:
202 $mergeLogPage = new LogPage( 'merge' );
203 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
204 LogEventsList::showLogExtract( $out, 'merge', $this->mTargetObj );
205 }
206
207 function formatRevisionRow( $row ) {
208 $rev = new Revision( $row );
209
210 $stxt = '';
211 $last = $this->msg( 'last' )->escaped();
212
213 $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
214 $checkBox = Xml::radio( 'mergepoint', $ts, ( $this->mOpts->getValue( 'mergepoint' ) === $ts ) );
215
216 $user = $this->getUser();
217
218 $pageLink = Linker::linkKnown(
219 $rev->getTitle(),
220 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
221 [],
222 [ 'oldid' => $rev->getId() ]
223 );
224 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
225 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
226 }
227
228 # Last link
229 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
230 $last = $this->msg( 'last' )->escaped();
231 } elseif ( isset( $this->prevId[$row->rev_id] ) ) {
232 $last = Linker::linkKnown(
233 $rev->getTitle(),
234 $this->msg( 'last' )->escaped(),
235 [],
236 [
237 'diff' => $row->rev_id,
238 'oldid' => $this->prevId[$row->rev_id]
239 ]
240 );
241 }
242
243 $userLink = Linker::revUserTools( $rev );
244
245 $size = $row->rev_len;
246 if ( !is_null( $size ) ) {
247 $stxt = Linker::formatRevisionSize( $size );
248 }
249 $comment = Linker::revComment( $rev );
250
251 return Html::rawElement( 'li', [],
252 $this->msg( 'mergehistory-revisionrow' )
253 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
254 }
255
256 /**
257 * Actually attempt the history move
258 *
259 * @todo if all versions of page A are moved to B and then a user
260 * tries to do a reverse-merge via the "unmerge" log link, then page
261 * A will still be a redirect (as it was after the original merge),
262 * though it will have the old revisions back from before (as expected).
263 * The user may have to "undo" the redirect manually to finish the "unmerge".
264 * Maybe this should delete redirects at the target page of merges?
265 *
266 * @return bool Success
267 */
268 function merge() {
269 $opts = $this->mOpts;
270
271 # Get the titles directly from the IDs, in case the target page params
272 # were spoofed. The queries are done based on the IDs, so it's best to
273 # keep it consistent...
274 $targetObj = $this->mTargetObj;
275 $destObj = $this->mDestObj;
276
277 if ( is_null( $targetObj ) || is_null( $destObj ) ||
278 $targetObj->getArticleID() == $destObj->getArticleID() ) {
279 return false;
280 }
281
282 // MergeHistory object
283 $mh = new MergeHistory( $targetObj, $destObj, $opts->getValue( 'mergepoint' ) );
284
285 // Merge!
286 $mergeStatus = $mh->merge( $this->getUser(), $opts->getValue( 'reason' ) );
287 if ( !$mergeStatus->isOK() ) {
288 // Failed merge
289 $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
290 return false;
291 }
292
293 $targetLink = Linker::link(
294 $targetObj,
295 null,
296 [],
297 [ 'redirect' => 'no' ]
298 );
299
300 $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
301 ->rawParams( $targetLink )
302 ->params( $destObj->getPrefixedText() )
303 ->numParams( $mh->getMergedRevisionCount() )
304 );
305
306 return true;
307 }
308
309 protected function getGroupName() {
310 return 'pagetools';
311 }
312 }