Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiMergeHistory
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 8
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
156
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2015 Geoffrey Mon <geofbot@gmail.com>
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Page\MergeHistoryFactory;
12use MediaWiki\Title\Title;
13use Wikimedia\ParamValidator\ParamValidator;
14
15/**
16 * API Module to merge page histories
17 * @ingroup API
18 */
19class ApiMergeHistory extends ApiBase {
20
21    public function __construct(
22        ApiMain $mainModule,
23        string $moduleName,
24        private readonly MergeHistoryFactory $mergeHistoryFactory,
25    ) {
26        parent::__construct( $mainModule, $moduleName );
27    }
28
29    public function execute() {
30        $this->useTransactionalTimeLimit();
31
32        $params = $this->extractRequestParams();
33
34        $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
35        $this->requireOnlyOneParameter( $params, 'to', 'toid' );
36
37        // Get page objects (nonexistent pages get caught in MergeHistory::isValidMerge())
38        if ( isset( $params['from'] ) ) {
39            $fromTitle = Title::newFromText( $params['from'] );
40            if ( !$fromTitle || $fromTitle->isExternal() ) {
41                $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
42            }
43        } elseif ( isset( $params['fromid'] ) ) {
44            $fromTitle = Title::newFromID( $params['fromid'] );
45            if ( !$fromTitle ) {
46                $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
47            }
48        } else {
49            self::dieDebug( __METHOD__, 'unknown from parameter' );
50        }
51
52        if ( isset( $params['to'] ) ) {
53            $toTitle = Title::newFromText( $params['to'] );
54            if ( !$toTitle || $toTitle->isExternal() ) {
55                $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
56            }
57        } elseif ( isset( $params['toid'] ) ) {
58            $toTitle = Title::newFromID( $params['toid'] );
59            if ( !$toTitle ) {
60                $this->dieWithError( [ 'apierror-nosuchpageid', $params['toid'] ] );
61            }
62        } else {
63            self::dieDebug( __METHOD__, 'unknown to parameter' );
64        }
65
66        $status = $this->mergeHistoryFactory->newMergeHistory(
67            $fromTitle,
68            $toTitle,
69            $params['timestamp'],
70            $params['starttimestamp']
71        )->merge( $this->getAuthority(), $params['reason'] );
72        if ( !$status->isOK() ) {
73            $this->dieStatus( $status );
74        }
75
76        $r = [
77            'from' => $fromTitle->getPrefixedText(),
78            'to' => $toTitle->getPrefixedText(),
79            'timestamp' => $params['timestamp'],
80            'reason' => $params['reason']
81        ];
82        $result = $this->getResult();
83
84        $result->addValue( null, $this->getModuleName(), $r );
85    }
86
87    /** @inheritDoc */
88    public function mustBePosted() {
89        return true;
90    }
91
92    /** @inheritDoc */
93    public function isWriteMode() {
94        return true;
95    }
96
97    /** @inheritDoc */
98    public function getAllowedParams() {
99        return [
100            'from' => null,
101            'fromid' => [
102                ParamValidator::PARAM_TYPE => 'integer'
103            ],
104            'to' => null,
105            'toid' => [
106                ParamValidator::PARAM_TYPE => 'integer'
107            ],
108            // This can either be a timestamp or a timestamp-with-ID pair; don't reject the latter in validation
109            'timestamp' => null,
110            'reason' => '',
111            'starttimestamp' => null
112        ];
113    }
114
115    /** @inheritDoc */
116    public function needsToken() {
117        return 'csrf';
118    }
119
120    /** @inheritDoc */
121    protected function getExamplesMessages() {
122        return [
123            'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
124            'reason=Reason'
125            => 'apihelp-mergehistory-example-merge',
126            'action=mergehistory&from=Oldpage&to=Newpage&token=123ABC&' .
127            'reason=Reason&timestamp=2015-12-31T04%3A37%3A41Z' // TODO
128            => 'apihelp-mergehistory-example-merge-timestamp',
129        ];
130    }
131
132    /** @inheritDoc */
133    public function getHelpUrls() {
134        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Mergehistory';
135    }
136}
137
138/** @deprecated class alias since 1.43 */
139class_alias( ApiMergeHistory::class, 'ApiMergeHistory' );