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 &&
84 $destObj && $opts->getValue( 'mergepoint' ) !== '' ) {
90 if ( $target === '' && $dest === '' ) {
91 $this->showMergeForm();
96 if ( !$targetObj instanceof Title
) {
97 $status->merge( Status
::newFatal( 'mergehistory-invalid-source' ) );
98 } elseif ( !$targetObj->exists() ) {
99 $status->merge( Status
::newFatal( 'mergehistory-no-source',
100 wfEscapeWikiText( $targetObj->getPrefixedText() )
104 if ( !$destObj instanceof Title
) {
105 $status->merge( Status
::newFatal( 'mergehistory-invalid-destination' ) );
106 } elseif ( !$destObj->exists() ) {
107 $status->merge( Status
::newFatal( 'mergehistory-no-destination',
108 wfEscapeWikiText( $destObj->getPrefixedText() )
112 if ( $targetObj && $destObj && $targetObj->equals( $destObj ) ) {
113 $status->merge( Status
::newFatal( 'mergehistory-same-destination' ) );
116 $this->mStatus
= $status;
118 $this->showMergeForm();
120 if ( $status->isOK() ) {
121 $this->showHistory();
125 function showMergeForm() {
130 'label-message' => 'mergehistory-from',
137 'label-message' => 'mergehistory-into',
142 $form = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() )
143 ->setIntro( $this->msg( 'mergehistory-header' ) )
144 ->setWrapperLegendMsg( 'mergehistory-box' )
145 ->setSubmitTextMsg( 'mergehistory-go' )
146 ->setMethod( 'post' )
148 ->displayForm( $this->mStatus
);
151 private function showHistory() {
152 # List all stored revisions
153 $revisions = new MergeHistoryPager(
154 $this, [], $this->mTargetObj
, $this->mDestObj
156 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
158 $out = $this->getOutput();
159 $header = '<h2 id="mw-mergehistory">' .
160 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n";
162 if ( $haveRevisions ) {
165 'target' => $this->mOpts
->getValue( 'target' ),
166 'dest' => $this->mOpts
->getValue( 'dest' ),
173 'label-message' => 'mergehistory-reason',
177 $mergeText = $this->msg( 'mergehistory-merge',
178 $this->mTargetObj
->getPrefixedText(),
179 $this->mDestObj
->getPrefixedText()
183 $revisions->getNavigationBar() .
185 $revisions->getBody() .
187 $revisions->getNavigationBar();
189 $form = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() )
190 ->addHiddenFields( $hiddenFields )
191 ->setPreText( $mergeText )
192 ->setFooterText( $history )
193 ->setSubmitTextMsg( 'mergehistory-submit' )
194 ->setMethod( 'post' )
196 ->displayForm( false );
198 $out->addHTML( $header );
199 $out->addWikiMsg( 'mergehistory-empty' );
202 # Show relevant lines from the merge log:
203 $mergeLogPage = new LogPage( 'merge' );
204 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
205 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
208 function formatRevisionRow( $row ) {
209 $rev = new Revision( $row );
212 $last = $this->msg( 'last' )->escaped();
214 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
215 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mOpts
->getValue( 'mergepoint' ) === $ts ) );
217 $user = $this->getUser();
219 $pageLink = Linker
::linkKnown(
221 htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) ),
223 [ 'oldid' => $rev->getId() ]
225 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
226 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
230 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
231 $last = $this->msg( 'last' )->escaped();
232 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
233 $last = Linker
::linkKnown(
235 $this->msg( 'last' )->escaped(),
238 'diff' => $row->rev_id
,
239 'oldid' => $this->prevId
[$row->rev_id
]
244 $userLink = Linker
::revUserTools( $rev );
246 $size = $row->rev_len
;
247 if ( !is_null( $size ) ) {
248 $stxt = Linker
::formatRevisionSize( $size );
250 $comment = Linker
::revComment( $rev );
252 return Html
::rawElement( 'li', [],
253 $this->msg( 'mergehistory-revisionrow' )
254 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
258 * Actually attempt the history move
260 * @todo if all versions of page A are moved to B and then a user
261 * tries to do a reverse-merge via the "unmerge" log link, then page
262 * A will still be a redirect (as it was after the original merge),
263 * though it will have the old revisions back from before (as expected).
264 * The user may have to "undo" the redirect manually to finish the "unmerge".
265 * Maybe this should delete redirects at the target page of merges?
267 * @return bool Success
270 $opts = $this->mOpts
;
272 # Get the titles directly from the IDs, in case the target page params
273 # were spoofed. The queries are done based on the IDs, so it's best to
274 # keep it consistent...
275 $targetObj = $this->mTargetObj
;
276 $destObj = $this->mDestObj
;
278 if ( is_null( $targetObj ) ||
is_null( $destObj ) ||
279 $targetObj->getArticleID() == $destObj->getArticleID() ) {
283 // MergeHistory object
284 $mh = new MergeHistory( $targetObj, $destObj, $opts->getValue( 'mergepoint' ) );
287 $mergeStatus = $mh->merge( $this->getUser(), $opts->getValue( 'reason' ) );
288 if ( !$mergeStatus->isOK() ) {
290 $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
294 $targetLink = Linker
::link(
298 [ 'redirect' => 'no' ]
301 $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
302 ->rawParams( $targetLink )
303 ->params( $destObj->getPrefixedText() )
304 ->numParams( $mh->getMergedRevisionCount() )
310 protected function getGroupName() {