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