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