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
25 * Special page allowing users with the appropriate permissions to
26 * merge article histories, with some restrictions
28 * @ingroup SpecialPage
30 class SpecialMergeHistory
extends SpecialPage
{
31 /** @var FormOptions */
37 /** @var Title|null */
38 protected $mTargetObj, $mDestObj;
43 public function __construct() {
44 parent
::__construct( 'MergeHistory', 'mergehistory' );
47 public function doesWrites() {
51 public function execute( $par ) {
52 $this->useTransactionalTimeLimit();
54 $this->checkPermissions();
55 $this->checkReadOnly();
58 $this->outputHeader();
60 $this->addHelpLink( 'Help:Merge history' );
62 $opts = new FormOptions();
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 );
71 $opts->fetchValuesFromRequest( $this->getRequest() );
73 $target = $opts->getValue( 'target' );
74 $dest = $opts->getValue( 'dest' );
75 $targetObj = Title
::newFromText( $target );
76 $destObj = Title
::newFromText( $dest );
77 $status = Status
::newGood();
80 $this->mTargetObj
= $targetObj;
81 $this->mDestObj
= $destObj;
83 if ( $opts->getValue( 'merge' ) && $targetObj && $destObj ) {
89 if ( $target === '' && $dest === '' ) {
90 $this->showMergeForm();
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() )
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() )
111 if ( $targetObj && $destObj && $targetObj->equals( $destObj ) ) {
112 $status->merge( Status
::newFatal( 'mergehistory-same-destination' ) );
115 $this->mStatus
= $status;
117 $this->showMergeForm();
119 if ( $status->isOK() ) {
120 $this->showHistory();
124 function showMergeForm() {
129 'label-message' => 'mergehistory-from',
136 'label-message' => 'mergehistory-into',
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' )
147 ->displayForm( $this->mStatus
);
150 private function showHistory() {
151 # List all stored revisions
152 $revisions = new MergeHistoryPager(
153 $this, [], $this->mTargetObj
, $this->mDestObj
155 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
157 $out = $this->getOutput();
158 $header = '<h2 id="mw-mergehistory">' .
159 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n";
161 if ( $haveRevisions ) {
164 'target' => $this->mOpts
->getValue( 'target' ),
165 'dest' => $this->mOpts
->getValue( 'dest' ),
172 'label-message' => 'mergehistory-reason',
176 $mergeText = $this->msg( 'mergehistory-merge',
177 $this->mTargetObj
->getPrefixedText(),
178 $this->mDestObj
->getPrefixedText()
182 $revisions->getNavigationBar() .
184 $revisions->getBody() .
186 $revisions->getNavigationBar();
188 $form = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() )
189 ->addHiddenFields( $hiddenFields )
190 ->setPreText( $mergeText )
191 ->setFooterText( $history )
192 ->setSubmitTextMsg( 'mergehistory-submit' )
193 ->setMethod( 'post' )
195 ->displayForm( false );
197 $out->addHTML( $header );
198 $out->addWikiMsg( 'mergehistory-empty' );
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
);
207 function formatRevisionRow( $row ) {
208 $rev = new Revision( $row );
211 $last = $this->msg( 'last' )->escaped();
213 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
214 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mOpts
->getValue( 'mergepoint' ) === $ts ) );
216 $user = $this->getUser();
218 $pageLink = Linker
::linkKnown(
220 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
222 [ 'oldid' => $rev->getId() ]
224 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
225 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
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(
234 $this->msg( 'last' )->escaped(),
237 'diff' => $row->rev_id
,
238 'oldid' => $this->prevId
[$row->rev_id
]
243 $userLink = Linker
::revUserTools( $rev );
245 $size = $row->rev_len
;
246 if ( !is_null( $size ) ) {
247 $stxt = Linker
::formatRevisionSize( $size );
249 $comment = Linker
::revComment( $rev );
251 return Html
::rawElement( 'li', [],
252 $this->msg( 'mergehistory-revisionrow' )
253 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
257 * Actually attempt the history move
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?
266 * @return bool Success
269 $opts = $this->mOpts
;
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
;
277 if ( is_null( $targetObj ) ||
is_null( $destObj ) ||
278 $targetObj->getArticleID() == $destObj->getArticleID() ) {
282 // MergeHistory object
283 $mh = new MergeHistory( $targetObj, $destObj, $opts->getValue( 'mergepoint' ) );
286 $mergeStatus = $mh->merge( $this->getUser(), $opts->getValue( 'reason' ) );
287 if ( !$mergeStatus->isOK() ) {
289 $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
293 $targetLink = Linker
::link(
297 [ 'redirect' => 'no' ]
300 $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
301 ->rawParams( $targetLink )
302 ->params( $destObj->getPrefixedText() )
303 ->numParams( $mh->getMergedRevisionCount() )
309 protected function getGroupName() {