SpecialMergeHistory.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. use MediaWiki\Revision\RevisionRecord;
  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 string */
  32. protected $mAction;
  33. /** @var string */
  34. protected $mTarget;
  35. /** @var string */
  36. protected $mDest;
  37. /** @var string */
  38. protected $mTimestamp;
  39. /** @var int */
  40. protected $mTargetID;
  41. /** @var int */
  42. protected $mDestID;
  43. /** @var string */
  44. protected $mComment;
  45. /** @var bool Was posted? */
  46. protected $mMerge;
  47. /** @var bool Was submitted? */
  48. protected $mSubmitted;
  49. /** @var Title */
  50. protected $mTargetObj;
  51. /** @var Title */
  52. protected $mDestObj;
  53. /** @var int[] */
  54. public $prevId;
  55. public function __construct() {
  56. parent::__construct( 'MergeHistory', 'mergehistory' );
  57. }
  58. public function doesWrites() {
  59. return true;
  60. }
  61. /**
  62. * @return void
  63. */
  64. private function loadRequestParams() {
  65. $request = $this->getRequest();
  66. $this->mAction = $request->getVal( 'action' );
  67. $this->mTarget = $request->getVal( 'target' );
  68. $this->mDest = $request->getVal( 'dest' );
  69. $this->mSubmitted = $request->getBool( 'submitted' );
  70. $this->mTargetID = intval( $request->getVal( 'targetID' ) );
  71. $this->mDestID = intval( $request->getVal( 'destID' ) );
  72. $this->mTimestamp = $request->getVal( 'mergepoint' );
  73. if ( !preg_match( '/[0-9]{14}/', $this->mTimestamp ) ) {
  74. $this->mTimestamp = '';
  75. }
  76. $this->mComment = $request->getText( 'wpComment' );
  77. $this->mMerge = $request->wasPosted()
  78. && $this->getUser()->matchEditToken( $request->getVal( 'wpEditToken' ) );
  79. // target page
  80. if ( $this->mSubmitted ) {
  81. $this->mTargetObj = Title::newFromText( $this->mTarget );
  82. $this->mDestObj = Title::newFromText( $this->mDest );
  83. } else {
  84. $this->mTargetObj = null;
  85. $this->mDestObj = null;
  86. }
  87. }
  88. public function execute( $par ) {
  89. $this->useTransactionalTimeLimit();
  90. $this->checkPermissions();
  91. $this->checkReadOnly();
  92. $this->loadRequestParams();
  93. $this->setHeaders();
  94. $this->outputHeader();
  95. if ( $this->mTargetID && $this->mDestID && $this->mAction == 'submit' && $this->mMerge ) {
  96. $this->merge();
  97. return;
  98. }
  99. if ( !$this->mSubmitted ) {
  100. $this->showMergeForm();
  101. return;
  102. }
  103. $errors = [];
  104. if ( !$this->mTargetObj instanceof Title ) {
  105. $errors[] = $this->msg( 'mergehistory-invalid-source' )->parseAsBlock();
  106. } elseif ( !$this->mTargetObj->exists() ) {
  107. $errors[] = $this->msg( 'mergehistory-no-source',
  108. wfEscapeWikiText( $this->mTargetObj->getPrefixedText() )
  109. )->parseAsBlock();
  110. }
  111. if ( !$this->mDestObj instanceof Title ) {
  112. $errors[] = $this->msg( 'mergehistory-invalid-destination' )->parseAsBlock();
  113. } elseif ( !$this->mDestObj->exists() ) {
  114. $errors[] = $this->msg( 'mergehistory-no-destination',
  115. wfEscapeWikiText( $this->mDestObj->getPrefixedText() )
  116. )->parseAsBlock();
  117. }
  118. if ( $this->mTargetObj && $this->mDestObj && $this->mTargetObj->equals( $this->mDestObj ) ) {
  119. $errors[] = $this->msg( 'mergehistory-same-destination' )->parseAsBlock();
  120. }
  121. if ( count( $errors ) ) {
  122. $this->showMergeForm();
  123. $this->getOutput()->addHTML( implode( "\n", $errors ) );
  124. } else {
  125. $this->showHistory();
  126. }
  127. }
  128. function showMergeForm() {
  129. $out = $this->getOutput();
  130. $out->addWikiMsg( 'mergehistory-header' );
  131. $out->addHTML(
  132. Xml::openElement( 'form', [
  133. 'method' => 'get',
  134. 'action' => wfScript() ] ) .
  135. '<fieldset>' .
  136. Xml::element( 'legend', [],
  137. $this->msg( 'mergehistory-box' )->text() ) .
  138. Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
  139. Html::hidden( 'submitted', '1' ) .
  140. Html::hidden( 'mergepoint', $this->mTimestamp ) .
  141. Xml::openElement( 'table' ) .
  142. '<tr>
  143. <td>' . Xml::label( $this->msg( 'mergehistory-from' )->text(), 'target' ) . '</td>
  144. <td>' . Xml::input( 'target', 30, $this->mTarget, [ 'id' => 'target' ] ) . '</td>
  145. </tr><tr>
  146. <td>' . Xml::label( $this->msg( 'mergehistory-into' )->text(), 'dest' ) . '</td>
  147. <td>' . Xml::input( 'dest', 30, $this->mDest, [ 'id' => 'dest' ] ) . '</td>
  148. </tr><tr><td>' .
  149. Xml::submitButton( $this->msg( 'mergehistory-go' )->text() ) .
  150. '</td></tr>' .
  151. Xml::closeElement( 'table' ) .
  152. '</fieldset>' .
  153. '</form>'
  154. );
  155. $this->addHelpLink( 'Help:Merge history' );
  156. }
  157. private function showHistory() {
  158. $this->showMergeForm();
  159. # List all stored revisions
  160. $revisions = new MergeHistoryPager(
  161. $this, [], $this->mTargetObj, $this->mDestObj
  162. );
  163. $haveRevisions = $revisions && $revisions->getNumRows() > 0;
  164. $out = $this->getOutput();
  165. $titleObj = $this->getPageTitle();
  166. $action = $titleObj->getLocalURL( [ 'action' => 'submit' ] );
  167. # Start the form here
  168. $top = Xml::openElement(
  169. 'form',
  170. [
  171. 'method' => 'post',
  172. 'action' => $action,
  173. 'id' => 'merge'
  174. ]
  175. );
  176. $out->addHTML( $top );
  177. if ( $haveRevisions ) {
  178. # Format the user-visible controls (comment field, submission button)
  179. # in a nice little table
  180. $table =
  181. Xml::openElement( 'fieldset' ) .
  182. $this->msg( 'mergehistory-merge', $this->mTargetObj->getPrefixedText(),
  183. $this->mDestObj->getPrefixedText() )->parse() .
  184. Xml::openElement( 'table', [ 'id' => 'mw-mergehistory-table' ] ) .
  185. '<tr>
  186. <td class="mw-label">' .
  187. Xml::label( $this->msg( 'mergehistory-reason' )->text(), 'wpComment' ) .
  188. '</td>
  189. <td class="mw-input">' .
  190. Xml::input( 'wpComment', 50, $this->mComment, [ 'id' => 'wpComment' ] ) .
  191. "</td>
  192. </tr>
  193. <tr>
  194. <td>\u{00A0}</td>
  195. <td class=\"mw-submit\">" .
  196. Xml::submitButton(
  197. $this->msg( 'mergehistory-submit' )->text(),
  198. [ 'name' => 'merge', 'id' => 'mw-merge-submit' ]
  199. ) .
  200. '</td>
  201. </tr>' .
  202. Xml::closeElement( 'table' ) .
  203. Xml::closeElement( 'fieldset' );
  204. $out->addHTML( $table );
  205. }
  206. $out->addHTML(
  207. '<h2 id="mw-mergehistory">' .
  208. $this->msg( 'mergehistory-list' )->escaped() . "</h2>\n"
  209. );
  210. if ( $haveRevisions ) {
  211. $out->addHTML( $revisions->getNavigationBar() );
  212. $out->addHTML( '<ul>' );
  213. $out->addHTML( $revisions->getBody() );
  214. $out->addHTML( '</ul>' );
  215. $out->addHTML( $revisions->getNavigationBar() );
  216. } else {
  217. $out->addWikiMsg( 'mergehistory-empty' );
  218. }
  219. # Show relevant lines from the merge log:
  220. $mergeLogPage = new LogPage( 'merge' );
  221. $out->addHTML( '<h2>' . $mergeLogPage->getName()->escaped() . "</h2>\n" );
  222. LogEventsList::showLogExtract( $out, 'merge', $this->mTargetObj );
  223. # When we submit, go by page ID to avoid some nasty but unlikely collisions.
  224. # Such would happen if a page was renamed after the form loaded, but before submit
  225. $misc = Html::hidden( 'targetID', $this->mTargetObj->getArticleID() );
  226. $misc .= Html::hidden( 'destID', $this->mDestObj->getArticleID() );
  227. $misc .= Html::hidden( 'target', $this->mTarget );
  228. $misc .= Html::hidden( 'dest', $this->mDest );
  229. $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
  230. $misc .= Xml::closeElement( 'form' );
  231. $out->addHTML( $misc );
  232. return true;
  233. }
  234. function formatRevisionRow( $row ) {
  235. $rev = new Revision( $row );
  236. $linkRenderer = $this->getLinkRenderer();
  237. $stxt = '';
  238. $last = $this->msg( 'last' )->escaped();
  239. $ts = wfTimestamp( TS_MW, $row->rev_timestamp );
  240. $checkBox = Xml::radio( 'mergepoint', $ts, ( $this->mTimestamp === $ts ) );
  241. $user = $this->getUser();
  242. $pageLink = $linkRenderer->makeKnownLink(
  243. $rev->getTitle(),
  244. $this->getLanguage()->userTimeAndDate( $ts, $user ),
  245. [],
  246. [ 'oldid' => $rev->getId() ]
  247. );
  248. if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
  249. $pageLink = '<span class="history-deleted">' . $pageLink . '</span>';
  250. }
  251. # Last link
  252. if ( !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
  253. $last = $this->msg( 'last' )->escaped();
  254. } elseif ( isset( $this->prevId[$row->rev_id] ) ) {
  255. $last = $linkRenderer->makeKnownLink(
  256. $rev->getTitle(),
  257. $this->msg( 'last' )->text(),
  258. [],
  259. [
  260. 'diff' => $row->rev_id,
  261. 'oldid' => $this->prevId[$row->rev_id]
  262. ]
  263. );
  264. }
  265. $userLink = Linker::revUserTools( $rev );
  266. $size = $row->rev_len;
  267. if ( !is_null( $size ) ) {
  268. $stxt = Linker::formatRevisionSize( $size );
  269. }
  270. $comment = Linker::revComment( $rev );
  271. return Html::rawElement( 'li', [],
  272. $this->msg( 'mergehistory-revisionrow' )
  273. ->rawParams( $checkBox, $last, $pageLink, $userLink, $stxt, $comment )->escaped() );
  274. }
  275. /**
  276. * Actually attempt the history move
  277. *
  278. * @todo if all versions of page A are moved to B and then a user
  279. * tries to do a reverse-merge via the "unmerge" log link, then page
  280. * A will still be a redirect (as it was after the original merge),
  281. * though it will have the old revisions back from before (as expected).
  282. * The user may have to "undo" the redirect manually to finish the "unmerge".
  283. * Maybe this should delete redirects at the target page of merges?
  284. *
  285. * @return bool Success
  286. */
  287. function merge() {
  288. # Get the titles directly from the IDs, in case the target page params
  289. # were spoofed. The queries are done based on the IDs, so it's best to
  290. # keep it consistent...
  291. $targetTitle = Title::newFromID( $this->mTargetID );
  292. $destTitle = Title::newFromID( $this->mDestID );
  293. if ( is_null( $targetTitle ) || is_null( $destTitle ) ) {
  294. return false; // validate these
  295. }
  296. if ( $targetTitle->getArticleID() == $destTitle->getArticleID() ) {
  297. return false;
  298. }
  299. // MergeHistory object
  300. $mh = new MergeHistory( $targetTitle, $destTitle, $this->mTimestamp );
  301. // Merge!
  302. $mergeStatus = $mh->merge( $this->getUser(), $this->mComment );
  303. if ( !$mergeStatus->isOK() ) {
  304. // Failed merge
  305. $this->getOutput()->addWikiMsg( $mergeStatus->getMessage() );
  306. return false;
  307. }
  308. $linkRenderer = $this->getLinkRenderer();
  309. $targetLink = $linkRenderer->makeLink(
  310. $targetTitle,
  311. null,
  312. [],
  313. [ 'redirect' => 'no' ]
  314. );
  315. $this->getOutput()->addWikiMsg( $this->msg( 'mergehistory-done' )
  316. ->rawParams( $targetLink )
  317. ->params( $destTitle->getPrefixedText() )
  318. ->numParams( $mh->getMergedRevisionCount() )
  319. );
  320. return true;
  321. }
  322. protected function getGroupName() {
  323. return 'pagetools';
  324. }
  325. }