3 * Implements Special:MergeHistory
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\Storage\RevisionRecord
;
27 * Special page allowing users with the appropriate permissions to
28 * merge article histories, with some restrictions
30 * @ingroup SpecialPage
32 class SpecialMergeHistory
extends SpecialPage
{
43 protected $mTimestamp;
54 /** @var bool Was posted? */
57 /** @var bool Was submitted? */
58 protected $mSubmitted;
61 protected $mTargetObj;
69 public function __construct() {
70 parent
::__construct( 'MergeHistory', 'mergehistory' );
73 public function doesWrites() {
80 private function loadRequestParams() {
81 $request = $this->getRequest();
82 $this->mAction
= $request->getVal( 'action' );
83 $this->mTarget
= $request->getVal( 'target' );
84 $this->mDest
= $request->getVal( 'dest' );
85 $this->mSubmitted
= $request->getBool( 'submitted' );
87 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
88 $this->mDestID
= intval( $request->getVal( 'destID' ) );
89 $this->mTimestamp
= $request->getVal( 'mergepoint' );
90 if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
91 $this->mTimestamp
= '';
93 $this->mComment
= $request->getText( 'wpComment' );
95 $this->mMerge
= $request->wasPosted()
96 && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
99 if ( $this->mSubmitted
) {
100 $this->mTargetObj
= Title
::newFromText( $this->mTarget
);
101 $this->mDestObj
= Title
::newFromText( $this->mDest
);
103 $this->mTargetObj
= null;
104 $this->mDestObj
= null;
108 public function execute( $par ) {
109 $this->useTransactionalTimeLimit();
111 $this->checkPermissions();
112 $this->checkReadOnly();
114 $this->loadRequestParams();
117 $this->outputHeader();
119 if ( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
125 if ( !$this->mSubmitted
) {
126 $this->showMergeForm();
132 if ( !$this->mTargetObj
instanceof Title
) {
133 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
134 } elseif ( !$this->mTargetObj
->exists() ) {
135 $errors[] = $this->msg( 'mergehistory-no-source',
136 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
140 if ( !$this->mDestObj
instanceof Title
) {
141 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
142 } elseif ( !$this->mDestObj
->exists() ) {
143 $errors[] = $this->msg( 'mergehistory-no-destination',
144 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
148 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
149 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
152 if ( count( $errors ) ) {
153 $this->showMergeForm();
154 $this->getOutput()->addHTML( implode( "\n", $errors ) );
156 $this->showHistory();
160 function showMergeForm() {
161 $out = $this->getOutput();
162 $out->addWikiMsg( 'mergehistory-header' );
165 Xml
::openElement( 'form', [
167 'action' => wfScript() ] ) .
169 Xml
::element( 'legend', [],
170 $this->msg( 'mergehistory-box' )->text() ) .
171 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
172 Html
::hidden( 'submitted', '1' ) .
173 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
174 Xml
::openElement( 'table' ) .
176 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
177 <td>' . Xml
::input( 'target', 30, $this->mTarget
, [ 'id' => 'target' ] ) . '</td>
179 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
180 <td>' . Xml
::input( 'dest', 30, $this->mDest
, [ 'id' => 'dest' ] ) . '</td>
182 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
184 Xml
::closeElement( 'table' ) .
189 $this->addHelpLink( 'Help:Merge history' );
192 private function showHistory() {
193 $this->showMergeForm();
195 # List all stored revisions
196 $revisions = new MergeHistoryPager(
197 $this, [], $this->mTargetObj
, $this->mDestObj
199 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
201 $out = $this->getOutput();
202 $titleObj = $this->getPageTitle();
203 $action = $titleObj->getLocalURL( [ 'action' => 'submit' ] );
204 # Start the form here
205 $top = Xml
::openElement(
213 $out->addHTML( $top );
215 if ( $haveRevisions ) {
216 # Format the user-visible controls (comment field, submission button)
217 # in a nice little table
219 Xml
::openElement( 'fieldset' ) .
220 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
221 $this->mDestObj
->getPrefixedText() )->parse() .
222 Xml
::openElement( 'table', [ 'id' => 'mw-mergehistory-table' ] ) .
224 <td class="mw-label">' .
225 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
227 <td class="mw-input">' .
228 Xml
::input( 'wpComment', 50, $this->mComment
, [ 'id' => 'wpComment' ] ) .
233 <td class=\"mw-submit\">" .
235 $this->msg( 'mergehistory-submit' )->text(),
236 [ 'name' => 'merge', 'id' => 'mw-merge-submit' ]
240 Xml
::closeElement( 'table' ) .
241 Xml
::closeElement( 'fieldset' );
243 $out->addHTML( $table );
247 '<h2 id="mw-mergehistory">' .
248 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
251 if ( $haveRevisions ) {
252 $out->addHTML( $revisions->getNavigationBar() );
253 $out->addHTML( '<ul>' );
254 $out->addHTML( $revisions->getBody() );
255 $out->addHTML( '</ul>' );
256 $out->addHTML( $revisions->getNavigationBar() );
258 $out->addWikiMsg( 'mergehistory-empty' );
261 # Show relevant lines from the merge log:
262 $mergeLogPage = new LogPage( 'merge' );
263 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
264 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
266 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
267 # Such would happen if a page was renamed after the form loaded, but before submit
268 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
269 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
270 $misc .= Html
::hidden( 'target', $this->mTarget
);
271 $misc .= Html
::hidden( 'dest', $this->mDest
);
272 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
273 $misc .= Xml
::closeElement( 'form' );
274 $out->addHTML( $misc );
279 function formatRevisionRow( $row ) {
280 $rev = new Revision( $row );
282 $linkRenderer = $this->getLinkRenderer();
285 $last = $this->msg( 'last' )->escaped();
287 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
288 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mTimestamp
=== $ts ) );
290 $user = $this->getUser();
292 $pageLink = $linkRenderer->makeKnownLink(
294 $this->getLanguage()->userTimeAndDate( $ts, $user ),
296 [ 'oldid' => $rev->getId() ]
298 if ( $rev->isDeleted( RevisionRecord
::DELETED_TEXT
) ) {
299 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
303 if ( !$rev->userCan( RevisionRecord
::DELETED_TEXT
, $user ) ) {
304 $last = $this->msg( 'last' )->escaped();
305 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
306 $last = $linkRenderer->makeKnownLink(
308 $this->msg( 'last' )->text(),
311 'diff' => $row->rev_id
,
312 'oldid' => $this->prevId
[$row->rev_id
]
317 $userLink = Linker
::revUserTools( $rev );
319 $size = $row->rev_len
;
320 if ( !is_null( $size ) ) {
321 $stxt = Linker
::formatRevisionSize( $size );
323 $comment = Linker
::revComment( $rev );
325 return Html
::rawElement( 'li', [],
326 $this->msg( 'mergehistory-revisionrow' )
327 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
331 * Actually attempt the history move
333 * @todo if all versions of page A are moved to B and then a user
334 * tries to do a reverse-merge via the "unmerge" log link, then page
335 * A will still be a redirect (as it was after the original merge),
336 * though it will have the old revisions back from before (as expected).
337 * The user may have to "undo" the redirect manually to finish the "unmerge".
338 * Maybe this should delete redirects at the target page of merges?
340 * @return bool Success
343 # Get the titles directly from the IDs, in case the target page params
344 # were spoofed. The queries are done based on the IDs, so it's best to
345 # keep it consistent...
346 $targetTitle = Title
::newFromID( $this->mTargetID
);
347 $destTitle = Title
::newFromID( $this->mDestID
);
348 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
349 return false; // validate these
351 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
355 // MergeHistory object
356 $mh = new MergeHistory( $targetTitle, $destTitle, $this->mTimestamp
);
359 $mergeStatus = $mh->merge( $this->getUser(), $this->mComment
);
360 if ( !$mergeStatus->isOK() ) {
362 $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
366 $linkRenderer = $this->getLinkRenderer();
368 $targetLink = $linkRenderer->makeLink(
372 [ 'redirect' => 'no' ]
375 $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
376 ->rawParams( $targetLink )
377 ->params( $destTitle->getPrefixedText() )
378 ->numParams( $mh->getMergedRevisionCount() )
384 protected function getGroupName() {