aboutsummaryrefslogtreecommitdiffstats
path: root/includes/api/ApiChangeContentModel.php
blob: 48efc81d3c752e0d9e834b0035389e481ae57cb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Page\ContentModelChangeFactory;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * Api module to change the content model of existing pages
 *
 * For new pages, use the edit api and specify the desired content model and format.
 *
 * @since 1.35
 * @ingroup API
 * @author DannyS712
 */
class ApiChangeContentModel extends ApiBase {

	private IContentHandlerFactory $contentHandlerFactory;
	private ContentModelChangeFactory $contentModelChangeFactory;

	/**
	 * @param ApiMain $main
	 * @param string $action
	 * @param IContentHandlerFactory $contentHandlerFactory
	 * @param ContentModelChangeFactory $contentModelChangeFactory
	 */
	public function __construct(
		ApiMain $main,
		$action,
		IContentHandlerFactory $contentHandlerFactory,
		ContentModelChangeFactory $contentModelChangeFactory
	) {
		parent::__construct( $main, $action );
		$this->contentHandlerFactory = $contentHandlerFactory;
		$this->contentModelChangeFactory = $contentModelChangeFactory;
	}

	/**
	 * A lot of this code is based on SpecialChangeContentModel
	 */
	public function execute() {
		$params = $this->extractRequestParams();
		$wikiPage = $this->getTitleOrPageId( $params );
		$title = $wikiPage->getTitle();
		$this->getErrorFormatter()->setContextTitle( $title );

		if ( !$title->exists() ) {
			$this->dieWithError( 'apierror-changecontentmodel-missingtitle' );
		}

		$newModel = $params['model'];

		$this->checkUserRightsAny( 'editcontentmodel' );
		$changer = $this->contentModelChangeFactory->newContentModelChange(
			$this->getAuthority(),
			$wikiPage,
			$newModel
		);
		// Status messages should be apierror-*
		$changer->setMessagePrefix( 'apierror-' );
		$permissionStatus = $changer->authorizeChange();
		if ( !$permissionStatus->isGood() ) {
			$this->dieStatus( $permissionStatus );
		}

		if ( $params['tags'] ) {
			$tagStatus = $changer->setTags( $params['tags'] );
			if ( !$tagStatus->isGood() ) {
				$this->dieStatus( $tagStatus );
			}
		}

		// Everything passed, make the conversion
		$status = $changer->doContentModelChange(
			$this->getContext(),
			$params['summary'] ?? '',
			$params['bot']
		);

		if ( !$status->isGood() ) {
			// Failed
			$this->dieStatus( $status );
		}
		$logid = $status->getValue()['logid'];

		$result = [
			'result' => 'Success',
			'title' => $title->getPrefixedText(),
			'pageid' => $title->getArticleID(),
			'contentmodel' => $title->getContentModel( IDBAccessObject::READ_LATEST ),
			'logid' => $logid,
			'revid' => $title->getLatestRevID( IDBAccessObject::READ_LATEST ),
		];

		$this->getResult()->addValue( null, $this->getModuleName(), $result );
	}

	public function getAllowedParams() {
		$models = $this->contentHandlerFactory->getContentModels();
		$modelOptions = [];
		foreach ( $models as $model ) {
			$handler = $this->contentHandlerFactory->getContentHandler( $model );
			if ( !$handler->supportsDirectEditing() ) {
				continue;
			}
			$modelOptions[] = $model;
		}

		return [
			'title' => [
				ParamValidator::PARAM_TYPE => 'string',
			],
			'pageid' => [
				ParamValidator::PARAM_TYPE => 'integer',
			],
			'summary' => [
				ParamValidator::PARAM_TYPE => 'string',
			],
			'tags' => [
				ParamValidator::PARAM_TYPE => 'tags',
				ParamValidator::PARAM_ISMULTI => true,
			],
			'model' => [
				ParamValidator::PARAM_TYPE => $modelOptions,
				ParamValidator::PARAM_REQUIRED => true,
			],
			'bot' => false,
		];
	}

	public function mustBePosted() {
		return true;
	}

	public function isWriteMode() {
		return true;
	}

	public function needsToken() {
		return 'csrf';
	}

	public function getHelpUrls() {
		return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:ChangeContentModel';
	}

	protected function getExamplesMessages() {
		return [
			'action=changecontentmodel&title=Main Page&model=text&token=123ABC'
				=> 'apihelp-changecontentmodel-example'
		];
	}
}