3 namespace MediaWiki\Api
;
5 use MediaWiki\Content\IContentHandlerFactory
;
6 use MediaWiki\Page\ContentModelChangeFactory
;
7 use Wikimedia\ParamValidator\ParamValidator
;
8 use Wikimedia\Rdbms\IDBAccessObject
;
11 * Api module to change the content model of existing pages
13 * For new pages, use the edit api and specify the desired content model and format.
19 class ApiChangeContentModel
extends ApiBase
{
21 private IContentHandlerFactory
$contentHandlerFactory;
22 private ContentModelChangeFactory
$contentModelChangeFactory;
24 public function __construct(
27 IContentHandlerFactory
$contentHandlerFactory,
28 ContentModelChangeFactory
$contentModelChangeFactory
30 parent
::__construct( $main, $action );
31 $this->contentHandlerFactory
= $contentHandlerFactory;
32 $this->contentModelChangeFactory
= $contentModelChangeFactory;
36 * A lot of this code is based on SpecialChangeContentModel
38 public function execute() {
39 $params = $this->extractRequestParams();
40 $wikiPage = $this->getTitleOrPageId( $params );
41 $title = $wikiPage->getTitle();
42 $this->getErrorFormatter()->setContextTitle( $title );
44 if ( !$title->exists() ) {
45 $this->dieWithError( 'apierror-changecontentmodel-missingtitle' );
48 $newModel = $params['model'];
50 $this->checkUserRightsAny( 'editcontentmodel' );
51 $changer = $this->contentModelChangeFactory
->newContentModelChange(
52 $this->getAuthority(),
56 // Status messages should be apierror-*
57 $changer->setMessagePrefix( 'apierror-' );
58 $permissionStatus = $changer->authorizeChange();
59 if ( !$permissionStatus->isGood() ) {
60 $this->dieStatus( $permissionStatus );
63 if ( $params['tags'] ) {
64 $tagStatus = $changer->setTags( $params['tags'] );
65 if ( !$tagStatus->isGood() ) {
66 $this->dieStatus( $tagStatus );
70 // Everything passed, make the conversion
71 $status = $changer->doContentModelChange(
73 $params['summary'] ??
'',
77 if ( !$status->isGood() ) {
79 $this->dieStatus( $status );
81 $logid = $status->getValue()['logid'];
84 'result' => 'Success',
85 'title' => $title->getPrefixedText(),
86 'pageid' => $title->getArticleID(),
87 'contentmodel' => $title->getContentModel( IDBAccessObject
::READ_LATEST
),
89 'revid' => $title->getLatestRevID( IDBAccessObject
::READ_LATEST
),
92 $this->getResult()->addValue( null, $this->getModuleName(), $result );
95 public function getAllowedParams() {
96 $models = $this->contentHandlerFactory
->getContentModels();
98 foreach ( $models as $model ) {
99 $handler = $this->contentHandlerFactory
->getContentHandler( $model );
100 if ( !$handler->supportsDirectEditing() ) {
103 $modelOptions[] = $model;
108 ParamValidator
::PARAM_TYPE
=> 'string',
111 ParamValidator
::PARAM_TYPE
=> 'integer',
114 ParamValidator
::PARAM_TYPE
=> 'string',
117 ParamValidator
::PARAM_TYPE
=> 'tags',
118 ParamValidator
::PARAM_ISMULTI
=> true,
121 ParamValidator
::PARAM_TYPE
=> $modelOptions,
122 ParamValidator
::PARAM_REQUIRED
=> true,
128 public function mustBePosted() {
132 public function isWriteMode() {
136 public function needsToken() {
140 public function getHelpUrls() {
141 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:ChangeContentModel';
144 protected function getExamplesMessages() {
146 'action=changecontentmodel&title=Main Page&model=text&token=123ABC'
147 => 'apihelp-changecontentmodel-example'
152 /** @deprecated class alias since 1.43 */
153 class_alias( ApiChangeContentModel
::class, 'ApiChangeContentModel' );