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