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