| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Page\MergeHistoryFactory; |
| 26 |
use MediaWiki\Page\PageIdentity; |
| 27 |
use MediaWiki\Status\Status; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\ParamValidator\ParamValidator; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ApiMergeHistory extends ApiBase { |
| 36 |
|
| 37 |
private MergeHistoryFactory $mergeHistoryFactory; |
| 38 |
|
| 39 |
public function __construct( |
| 40 |
ApiMain $mainModule, |
| 41 |
string $moduleName, |
| 42 |
MergeHistoryFactory $mergeHistoryFactory |
| 43 |
) { |
| 44 |
parent::__construct( $mainModule, $moduleName ); |
| 45 |
$this->mergeHistoryFactory = $mergeHistoryFactory; |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$this->useTransactionalTimeLimit(); |
| 50 |
|
| 51 |
$params = $this->extractRequestParams(); |
| 52 |
|
| 53 |
$this->requireOnlyOneParameter( $params, 'from', 'fromid' ); |
| 54 |
$this->requireOnlyOneParameter( $params, 'to', 'toid' ); |
| 55 |
|
| 56 |
|
| 57 |
if ( isset( $params['from'] ) ) { |
| 58 |
$fromTitle = Title::newFromText( $params['from'] ); |
| 59 |
if ( !$fromTitle || $fromTitle->isExternal() ) { |
| 60 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] ); |
| 61 |
} |
| 62 |
} elseif ( isset( $params['fromid'] ) ) { |
| 63 |
$fromTitle = Title::newFromID( $params['fromid'] ); |
| 64 |
if ( !$fromTitle ) { |
| 65 |
$this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if ( isset( $params['to'] ) ) { |
| 70 |
$toTitle = Title::newFromText( $params['to'] ); |
| 71 |
if ( !$toTitle || $toTitle->isExternal() ) { |
| 72 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] ); |
| 73 |
} |
| 74 |
} elseif ( isset( $params['toid'] ) ) { |
| 75 |
$toTitle = Title::newFromID( $params['toid'] ); |
| 76 |
if ( !$toTitle ) { |
| 77 |
$this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$reason = $params['reason']; |
| 82 |
$timestamp = $params['timestamp']; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$status = $this->merge( $fromTitle, $toTitle, $timestamp, $reason ); |
| 87 |
if ( !$status->isOK() ) { |
| 88 |
$this->dieStatus( $status ); |
| 89 |
} |
| 90 |
|
| 91 |
$r = [ |
| 92 |
|
| 93 |
'from' => $fromTitle->getPrefixedText(), |
| 94 |
|
| 95 |
'to' => $toTitle->getPrefixedText(), |
| 96 |
'timestamp' => wfTimestamp( TS_ISO_8601, $params['timestamp'] ), |
| 97 |
'reason' => $params['reason'] |
| 98 |
]; |
| 99 |
$result = $this->getResult(); |
| 100 |
|
| 101 |
$result->addValue( null, $this->getModuleName(), $r ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
protected function merge( PageIdentity $from, PageIdentity $to, $timestamp, $reason ) { |
| 112 |
$mh = $this->mergeHistoryFactory->newMergeHistory( $from, $to, $timestamp ); |
| 113 |
|
| 114 |
return $mh->merge( $this->getAuthority(), $reason ); |
| 115 |
} |
| 116 |
|
| 117 |
public function mustBePosted() { |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
public function isWriteMode() { |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
public function getAllowedParams() { |
| 126 |
return [ |
| 127 |
'from' => null, |
| 128 |
'fromid' => [ |
| 129 |
ParamValidator::PARAM_TYPE => 'integer' |
| 130 |
], |
| 131 |
'to' => null, |
| 132 |
'toid' => [ |
| 133 |
ParamValidator::PARAM_TYPE => 'integer' |
| 134 |
], |
| 135 |
'timestamp' => [ |
| 136 |
ParamValidator::PARAM_TYPE => 'timestamp' |
| 137 |
], |
| 138 |
'reason' => '', |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
public function needsToken() { |
| 143 |
return 'csrf'; |
| 144 |
} |
| 145 |
|
| 146 |
protected function getExamplesMessages() { |
| 147 |
return [ |
| 148 |
'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' . |
| 149 |
'reason=Reason' |
| 150 |
=> 'apihelp-mergehistory-example-merge', |
| 151 |
'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' . |
| 152 |
'reason=Reason×tamp=2015-12-31T04%3A37%3A41Z' |
| 153 |
=> 'apihelp-mergehistory-example-merge-timestamp', |
| 154 |
]; |
| 155 |
} |
| 156 |
|
| 157 |
public function getHelpUrls() { |
| 158 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory'; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
class_alias( ApiMergeHistory::class, 'ApiMergeHistory' ); |