MediaWiki 1.41.2
ApiChangeContentModel.php
Go to the documentation of this file.
1<?php
2
7
18
19 private IContentHandlerFactory $contentHandlerFactory;
20 private ContentModelChangeFactory $contentModelChangeFactory;
21
28 public function __construct(
29 ApiMain $main,
30 $action,
31 IContentHandlerFactory $contentHandlerFactory,
32 ContentModelChangeFactory $contentModelChangeFactory
33 ) {
34 parent::__construct( $main, $action );
35 $this->contentHandlerFactory = $contentHandlerFactory;
36 $this->contentModelChangeFactory = $contentModelChangeFactory;
37 }
38
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $wikiPage = $this->getTitleOrPageId( $params );
45 $title = $wikiPage->getTitle();
46 $this->getErrorFormatter()->setContextTitle( $title );
47
48 if ( !$title->exists() ) {
49 $this->dieWithError( 'apierror-changecontentmodel-missingtitle' );
50 }
51
52 $newModel = $params['model'];
53
54 $this->checkUserRightsAny( 'editcontentmodel' );
55 $changer = $this->contentModelChangeFactory->newContentModelChange(
56 $this->getAuthority(),
57 $wikiPage,
58 $newModel
59 );
60 // Status messages should be apierror-*
61 $changer->setMessagePrefix( 'apierror-' );
62 $permissionStatus = $changer->authorizeChange();
63 if ( !$permissionStatus->isGood() ) {
64 $this->dieStatus( $permissionStatus );
65 }
66
67 if ( $params['tags'] ) {
68 $tagStatus = $changer->setTags( $params['tags'] );
69 if ( !$tagStatus->isGood() ) {
70 $this->dieStatus( $tagStatus );
71 }
72 }
73
74 // Everything passed, make the conversion
75 $status = $changer->doContentModelChange(
76 $this->getContext(),
77 $params['summary'] ?? '',
78 $params['bot']
79 );
80
81 if ( !$status->isGood() ) {
82 // Failed
83 $this->dieStatus( $status );
84 }
85 $logid = $status->getValue()['logid'];
86
87 $result = [
88 'result' => 'Success',
89 'title' => $title->getPrefixedText(),
90 'pageid' => $title->getArticleID(),
91 'contentmodel' => $title->getContentModel( Title::READ_LATEST ),
92 'logid' => $logid,
93 'revid' => $title->getLatestRevID( Title::READ_LATEST ),
94 ];
95
96 $this->getResult()->addValue( null, $this->getModuleName(), $result );
97 }
98
99 public function getAllowedParams() {
100 $models = $this->contentHandlerFactory->getContentModels();
101 $modelOptions = [];
102 foreach ( $models as $model ) {
103 $handler = $this->contentHandlerFactory->getContentHandler( $model );
104 if ( !$handler->supportsDirectEditing() ) {
105 continue;
106 }
107 $modelOptions[] = $model;
108 }
109
110 return [
111 'title' => [
112 ParamValidator::PARAM_TYPE => 'string',
113 ],
114 'pageid' => [
115 ParamValidator::PARAM_TYPE => 'integer',
116 ],
117 'summary' => [
118 ParamValidator::PARAM_TYPE => 'string',
119 ],
120 'tags' => [
121 ParamValidator::PARAM_TYPE => 'tags',
122 ParamValidator::PARAM_ISMULTI => true,
123 ],
124 'model' => [
125 ParamValidator::PARAM_TYPE => $modelOptions,
126 ParamValidator::PARAM_REQUIRED => true,
127 ],
128 'bot' => false,
129 ];
130 }
131
132 public function mustBePosted() {
133 return true;
134 }
135
136 public function isWriteMode() {
137 return true;
138 }
139
140 public function needsToken() {
141 return 'csrf';
142 }
143
144 public function getHelpUrls() {
145 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:ChangeContentModel';
146 }
147
148 protected function getExamplesMessages() {
149 return [
150 'action=changecontentmodel&title=Main Page&model=text&token=123ABC'
151 => 'apihelp-changecontentmodel-example'
152 ];
153 }
154}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:62
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1515
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1630
getErrorFormatter()
Definition ApiBase.php:678
getResult()
Get the result object.
Definition ApiBase.php:667
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:807
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:528
getTitleOrPageId( $params, $load=false)
Attempts to load a WikiPage object from a title or pageid parameter, if possible.
Definition ApiBase.php:1080
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1570
Api module to change the content model of existing pages.
__construct(ApiMain $main, $action, IContentHandlerFactory $contentHandlerFactory, ContentModelChangeFactory $contentModelChangeFactory)
execute()
A lot of this code is based on SpecialChangeContentModel.
needsToken()
Returns the token type this module requires in order to execute.
getHelpUrls()
Return links to more detailed help pages about the module.
mustBePosted()
Indicates whether this module must be called with a POST request.
isWriteMode()
Indicates whether this module requires write mode.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:64
getContext()
Get the base IContextSource object.
Represents a title within MediaWiki.
Definition Title.php:76
Service for formatting and validating API parameters.
Service for changing the content model of wiki pages.