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
{
41 protected $mTimestamp;
52 /** @var bool Was posted? */
55 /** @var bool Was submitted? */
56 protected $mSubmitted;
59 protected $mTargetObj;
67 public function __construct() {
68 parent
::__construct( 'MergeHistory', 'mergehistory' );
71 public function doesWrites() {
78 private function loadRequestParams() {
79 $request = $this->getRequest();
80 $this->mAction
= $request->getVal( 'action' );
81 $this->mTarget
= $request->getVal( 'target' );
82 $this->mDest
= $request->getVal( 'dest' );
83 $this->mSubmitted
= $request->getBool( 'submitted' );
85 $this->mTargetID
= intval( $request->getVal( 'targetID' ) );
86 $this->mDestID
= intval( $request->getVal( 'destID' ) );
87 $this->mTimestamp
= $request->getVal( 'mergepoint' );
88 if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp
) ) {
89 $this->mTimestamp
= '';
91 $this->mComment
= $request->getText( 'wpComment' );
93 $this->mMerge
= $request->wasPosted()
94 && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
97 if ( $this->mSubmitted
) {
98 $this->mTargetObj
= Title
::newFromText( $this->mTarget
);
99 $this->mDestObj
= Title
::newFromText( $this->mDest
);
101 $this->mTargetObj
= null;
102 $this->mDestObj
= null;
106 public function execute( $par ) {
107 $this->useTransactionalTimeLimit();
109 $this->checkPermissions();
110 $this->checkReadOnly();
112 $this->loadRequestParams();
115 $this->outputHeader();
117 if ( $this->mTargetID
&& $this->mDestID
&& $this->mAction
== 'submit' && $this->mMerge
) {
123 if ( !$this->mSubmitted
) {
124 $this->showMergeForm();
130 if ( !$this->mTargetObj
instanceof Title
) {
131 $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
132 } elseif ( !$this->mTargetObj
->exists() ) {
133 $errors[] = $this->msg( 'mergehistory-no-source',
134 wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() )
138 if ( !$this->mDestObj
instanceof Title
) {
139 $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
140 } elseif ( !$this->mDestObj
->exists() ) {
141 $errors[] = $this->msg( 'mergehistory-no-destination',
142 wfEscapeWikiText( $this->mDestObj
->getPrefixedText() )
146 if ( $this->mTargetObj
&& $this->mDestObj
&& $this->mTargetObj
->equals( $this->mDestObj
) ) {
147 $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
150 if ( count( $errors ) ) {
151 $this->showMergeForm();
152 $this->getOutput()->addHTML( implode( "\n", $errors ) );
154 $this->showHistory();
158 function showMergeForm() {
159 $out = $this->getOutput();
160 $out->addWikiMsg( 'mergehistory-header' );
163 Xml
::openElement( 'form', [
165 'action' => wfScript() ] ) .
167 Xml
::element( 'legend', [],
168 $this->msg( 'mergehistory-box' )->text() ) .
169 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
170 Html
::hidden( 'submitted', '1' ) .
171 Html
::hidden( 'mergepoint', $this->mTimestamp
) .
172 Xml
::openElement( 'table' ) .
174 <td>' . Xml
::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
175 <td>' . Xml
::input( 'target', 30, $this->mTarget
, [ 'id' => 'target' ] ) . '</td>
177 <td>' . Xml
::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
178 <td>' . Xml
::input( 'dest', 30, $this->mDest
, [ 'id' => 'dest' ] ) . '</td>
180 Xml
::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
182 Xml
::closeElement( 'table' ) .
187 $this->addHelpLink( 'Help:Merge history' );
190 private function showHistory() {
191 $this->showMergeForm();
193 # List all stored revisions
194 $revisions = new MergeHistoryPager(
195 $this, [], $this->mTargetObj
, $this->mDestObj
197 $haveRevisions = $revisions && $revisions->getNumRows() > 0;
199 $out = $this->getOutput();
200 $titleObj = $this->getPageTitle();
201 $action = $titleObj->getLocalURL( [ 'action' => 'submit' ] );
202 # Start the form here
203 $top = Xml
::openElement(
211 $out->addHTML( $top );
213 if ( $haveRevisions ) {
214 # Format the user-visible controls (comment field, submission button)
215 # in a nice little table
217 Xml
::openElement( 'fieldset' ) .
218 $this->msg( 'mergehistory-merge', $this->mTargetObj
->getPrefixedText(),
219 $this->mDestObj
->getPrefixedText() )->parse() .
220 Xml
::openElement( 'table', [ 'id' => 'mw-mergehistory-table' ] ) .
222 <td class="mw-label">' .
223 Xml
::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
225 <td class="mw-input">' .
226 Xml
::input( 'wpComment', 50, $this->mComment
, [ 'id' => 'wpComment' ] ) .
231 <td class="mw-submit">' .
233 $this->msg( 'mergehistory-submit' )->text(),
234 [ 'name' => 'merge', 'id' => 'mw-merge-submit' ]
238 Xml
::closeElement( 'table' ) .
239 Xml
::closeElement( 'fieldset' );
241 $out->addHTML( $table );
245 '<h2 id="mw-mergehistory">' .
246 $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
249 if ( $haveRevisions ) {
250 $out->addHTML( $revisions->getNavigationBar() );
251 $out->addHTML( '<ul>' );
252 $out->addHTML( $revisions->getBody() );
253 $out->addHTML( '</ul>' );
254 $out->addHTML( $revisions->getNavigationBar() );
256 $out->addWikiMsg( 'mergehistory-empty' );
259 # Show relevant lines from the merge log:
260 $mergeLogPage = new LogPage( 'merge' );
261 $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
262 LogEventsList
::showLogExtract( $out, 'merge', $this->mTargetObj
);
264 # When we submit, go by page ID to avoid some nasty but unlikely collisions.
265 # Such would happen if a page was renamed after the form loaded, but before submit
266 $misc = Html
::hidden( 'targetID', $this->mTargetObj
->getArticleID() );
267 $misc .= Html
::hidden( 'destID', $this->mDestObj
->getArticleID() );
268 $misc .= Html
::hidden( 'target', $this->mTarget
);
269 $misc .= Html
::hidden( 'dest', $this->mDest
);
270 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
271 $misc .= Xml
::closeElement( 'form' );
272 $out->addHTML( $misc );
277 function formatRevisionRow( $row ) {
278 $rev = new Revision( $row );
280 $linkRenderer = $this->getLinkRenderer();
283 $last = $this->msg( 'last' )->escaped();
285 $ts = wfTimestamp( TS_MW
, $row->rev_timestamp
);
286 $checkBox = Xml
::radio( 'mergepoint', $ts, ( $this->mTimestamp
=== $ts ) );
288 $user = $this->getUser();
290 $pageLink = $linkRenderer->makeKnownLink(
292 $this->getLanguage()->userTimeAndDate( $ts, $user ),
294 [ 'oldid' => $rev->getId() ]
296 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
297 $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
301 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
302 $last = $this->msg( 'last' )->escaped();
303 } elseif ( isset( $this->prevId
[$row->rev_id
] ) ) {
304 $last = $linkRenderer->makeKnownLink(
306 $this->msg( 'last' )->text(),
309 'diff' => $row->rev_id
,
310 'oldid' => $this->prevId
[$row->rev_id
]
315 $userLink = Linker
::revUserTools( $rev );
317 $size = $row->rev_len
;
318 if ( !is_null( $size ) ) {
319 $stxt = Linker
::formatRevisionSize( $size );
321 $comment = Linker
::revComment( $rev );
323 return Html
::rawElement( 'li', [],
324 $this->msg( 'mergehistory-revisionrow' )
325 ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
329 * Actually attempt the history move
331 * @todo if all versions of page A are moved to B and then a user
332 * tries to do a reverse-merge via the "unmerge" log link, then page
333 * A will still be a redirect (as it was after the original merge),
334 * though it will have the old revisions back from before (as expected).
335 * The user may have to "undo" the redirect manually to finish the "unmerge".
336 * Maybe this should delete redirects at the target page of merges?
338 * @return bool Success
341 # Get the titles directly from the IDs, in case the target page params
342 # were spoofed. The queries are done based on the IDs, so it's best to
343 # keep it consistent...
344 $targetTitle = Title
::newFromID( $this->mTargetID
);
345 $destTitle = Title
::newFromID( $this->mDestID
);
346 if ( is_null( $targetTitle ) ||
is_null( $destTitle ) ) {
347 return false; // validate these
349 if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
353 // MergeHistory object
354 $mh = new MergeHistory( $targetTitle, $destTitle, $this->mTimestamp
);
357 $mergeStatus = $mh->merge( $this->getUser(), $this->mComment
);
358 if ( !$mergeStatus->isOK() ) {
360 $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
364 $linkRenderer = $this->getLinkRenderer();
366 $targetLink = $linkRenderer->makeLink(
370 [ 'redirect' => 'no' ]
373 $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
374 ->rawParams( $targetLink )
375 ->params( $destTitle->getPrefixedText() )
376 ->numParams( $mh->getMergedRevisionCount() )
382 protected function getGroupName() {