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