ApiMergeHistory.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
  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. */
  22. /**
  23. * API Module to merge page histories
  24. * @ingroup API
  25. */
  26. class ApiMergeHistory extends ApiBase {
  27. public function execute() {
  28. $this->useTransactionalTimeLimit();
  29. $params = $this->extractRequestParams();
  30. $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
  31. $this->requireOnlyOneParameter( $params, 'to', 'toid' );
  32. // Get page objects (nonexistant pages get caught in MergeHistory::isValidMerge())
  33. if ( isset( $params['from'] ) ) {
  34. $fromTitle = Title::newFromText( $params['from'] );
  35. if ( !$fromTitle || $fromTitle->isExternal() ) {
  36. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
  37. }
  38. } elseif ( isset( $params['fromid'] ) ) {
  39. $fromTitle = Title::newFromID( $params['fromid'] );
  40. if ( !$fromTitle ) {
  41. $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
  42. }
  43. }
  44. if ( isset( $params['to'] ) ) {
  45. $toTitle = Title::newFromText( $params['to'] );
  46. if ( !$toTitle || $toTitle->isExternal() ) {
  47. $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
  48. }
  49. } elseif ( isset( $params['toid'] ) ) {
  50. $toTitle = Title::newFromID( $params['toid'] );
  51. if ( !$toTitle ) {
  52. $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
  53. }
  54. }
  55. $reason = $params['reason'];
  56. $timestamp = $params['timestamp'];
  57. // Merge!
  58. $status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason );
  59. if ( !$status->isOK() ) {
  60. $this->dieStatus( $status );
  61. }
  62. $r = [
  63. 'from' => $fromTitle->getPrefixedText(),
  64. 'to' => $toTitle->getPrefixedText(),
  65. 'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ),
  66. 'reason' => $params['reason']
  67. ];
  68. $result = $this->getResult();
  69. $result->addValue( null, $this->getModuleName(), $r );
  70. }
  71. /**
  72. * @param Title $from
  73. * @param Title $to
  74. * @param string $timestamp
  75. * @param string $reason
  76. * @return Status
  77. */
  78. protected function merge( Title $from, Title $to, $timestamp, $reason ) {
  79. $mh = new MergeHistory( $from, $to, $timestamp );
  80. return $mh->merge( $this->getUser(), $reason );
  81. }
  82. public function mustBePosted() {
  83. return true;
  84. }
  85. public function isWriteMode() {
  86. return true;
  87. }
  88. public function getAllowedParams() {
  89. return [
  90. 'from' => null,
  91. 'fromid' => [
  92. ApiBase::PARAM_TYPE => 'integer'
  93. ],
  94. 'to' => null,
  95. 'toid' => [
  96. ApiBase::PARAM_TYPE => 'integer'
  97. ],
  98. 'timestamp' => [
  99. ApiBase::PARAM_TYPE => 'timestamp'
  100. ],
  101. 'reason' => '',
  102. ];
  103. }
  104. public function needsToken() {
  105. return 'csrf';
  106. }
  107. protected function getExamplesMessages() {
  108. return [
  109. 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
  110. 'reason=Reason'
  111. => 'apihelp-mergehistory-example-merge',
  112. 'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
  113. 'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
  114. => 'apihelp-mergehistory-example-merge-timestamp',
  115. ];
  116. }
  117. public function getHelpUrls() {
  118. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
  119. }
  120. }