MediaWiki REL1_39
ApiChangeContentModel.php
Go to the documentation of this file.
1<?php
2
6
17
19 private $contentHandlerFactory;
20
22 private $contentModelChangeFactory;
23
30 public function __construct(
31 ApiMain $main,
32 $action,
33 IContentHandlerFactory $contentHandlerFactory,
34 ContentModelChangeFactory $contentModelChangeFactory
35 ) {
36 parent::__construct( $main, $action );
37 $this->contentHandlerFactory = $contentHandlerFactory;
38 $this->contentModelChangeFactory = $contentModelChangeFactory;
39 }
40
44 public function execute() {
45 $params = $this->extractRequestParams();
46 $wikiPage = $this->getTitleOrPageId( $params );
47 $title = $wikiPage->getTitle();
48 $this->getErrorFormatter()->setContextTitle( $title );
49
50 if ( !$title->exists() ) {
51 $this->dieWithError( 'apierror-changecontentmodel-missingtitle' );
52 }
53
54 $newModel = $params['model'];
55
56 $this->checkUserRightsAny( 'editcontentmodel' );
57 $changer = $this->contentModelChangeFactory->newContentModelChange(
58 $this->getAuthority(),
59 $wikiPage,
60 $newModel
61 );
62 // Status messages should be apierror-*
63 $changer->setMessagePrefix( 'apierror-' );
64 $permissionStatus = $changer->authorizeChange();
65 if ( !$permissionStatus->isGood() ) {
66 $this->dieStatus( $permissionStatus );
67 }
68
69 if ( $params['tags'] ) {
70 $tagStatus = $changer->setTags( $params['tags'] );
71 if ( !$tagStatus->isGood() ) {
72 $this->dieStatus( $tagStatus );
73 }
74 }
75
76 // Everything passed, make the conversion
77 try {
78 $status = $changer->doContentModelChange(
79 $this->getContext(),
80 $params['summary'],
81 $params['bot']
82 );
83 } catch ( ThrottledError $te ) {
84 $this->dieWithError( 'apierror-ratelimited' );
85 }
86
87 if ( !$status->isGood() ) {
88 // Failed
89 $this->dieStatus( $status );
90 }
91 $logid = $status->getValue()['logid'];
92
93 $result = [
94 'result' => 'Success',
95 'title' => $title->getPrefixedText(),
96 'pageid' => $title->getArticleID(),
97 'contentmodel' => $title->getContentModel( Title::READ_LATEST ),
98 'logid' => $logid,
99 'revid' => $title->getLatestRevID( Title::READ_LATEST ),
100 ];
101
102 $this->getResult()->addValue( null, $this->getModuleName(), $result );
103 }
104
105 public function getAllowedParams() {
106 $models = $this->contentHandlerFactory->getContentModels();
107 $modelOptions = [];
108 foreach ( $models as $model ) {
109 $handler = $this->contentHandlerFactory->getContentHandler( $model );
110 if ( !$handler->supportsDirectEditing() ) {
111 continue;
112 }
113 $modelOptions[] = $model;
114 }
115
116 return [
117 'title' => [
118 ParamValidator::PARAM_TYPE => 'string',
119 ],
120 'pageid' => [
121 ParamValidator::PARAM_TYPE => 'integer',
122 ],
123 'summary' => [
124 ParamValidator::PARAM_TYPE => 'string',
125 ],
126 'tags' => [
127 ParamValidator::PARAM_TYPE => 'tags',
128 ParamValidator::PARAM_ISMULTI => true,
129 ],
130 'model' => [
131 ParamValidator::PARAM_TYPE => $modelOptions,
132 ParamValidator::PARAM_REQUIRED => true,
133 ],
134 'bot' => false,
135 ];
136 }
137
138 public function mustBePosted() {
139 return true;
140 }
141
142 public function isWriteMode() {
143 return true;
144 }
145
146 public function needsToken() {
147 return 'csrf';
148 }
149
150 public function getHelpUrls() {
151 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:ChangeContentModel';
152 }
153
154 protected function getExamplesMessages() {
155 return [
156 'action=changecontentmodel&title=Main Page&model=text&token=123ABC'
157 => 'apihelp-changecontentmodel-example'
158 ];
159 }
160}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1454
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1560
getErrorFormatter()
Definition ApiBase.php:640
getResult()
Get the result object.
Definition ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:498
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:1036
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1515
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:52
getContext()
Get the base IContextSource object.
Show an error when the user hits a rate limit.
Service for formatting and validating API parameters.
Service for changing the content model of wiki pages.