MediaWiki REL1_35
ApiChangeContentModel.php
Go to the documentation of this file.
1<?php
2
4
15
19 public function execute() {
20 $params = $this->extractRequestParams();
21 $wikiPage = $this->getTitleOrPageId( $params );
22 $title = $wikiPage->getTitle();
23
24 if ( !$title->exists() ) {
25 $this->dieWithError( 'apierror-changecontentmodel-missingtitle' );
26 }
27 $plainTitle = Message::plaintextParam( $title->getPrefixedText() );
28
29 $newModel = $params['model'];
30 $user = $this->getUser();
31
32 $this->checkUserRightsAny( 'editcontentmodel' );
33 $changer = MediaWikiServices::getInstance()
34 ->getContentModelChangeFactory()
35 ->newContentModelChange(
36 $user,
37 $wikiPage,
38 $newModel
39 );
40 // Status messages should be apierror-*
41 $changer->setMessagePrefix( 'apierror-' );
42 $errors = $changer->checkPermissions();
43 if ( $errors !== [] ) {
44 $this->dieStatus( $this->errorArrayToStatus( $errors, $user ) );
45 }
46
47 if ( $params['tags'] ) {
48 $tagStatus = $changer->setTags( $params['tags'] );
49 if ( !$tagStatus->isGood() ) {
50 $this->dieStatus( $tagStatus );
51 }
52 }
53
54 // Everything passed, make the conversion
55 try {
56 $status = $changer->doContentModelChange(
57 $this->getContext(),
58 $params['summary'],
59 $params['bot']
60 );
61 } catch ( ThrottledError $te ) {
62 $this->dieWithError( 'apierror-ratelimited' );
63 }
64
65 if ( !$status->isGood() ) {
66 // Failed
67 $this->dieStatus( $status );
68 }
69 $logid = $status->getValue()['logid'];
70
71 $result = [
72 'result' => 'Success',
73 'title' => $title->getPrefixedText(),
74 'pageid' => $title->getArticleID(),
75 'contentmodel' => $title->getContentModel( Title::READ_LATEST ),
76 'logid' => $logid,
77 'revid' => $title->getLatestRevID( Title::READ_LATEST ),
78 ];
79
80 $this->getResult()->addValue( null, $this->getModuleName(), $result );
81 }
82
83 public function getAllowedParams() {
84 $models = ContentHandler::getContentModels();
85 $modelOptions = [];
86 foreach ( $models as $model ) {
87 $handler = ContentHandler::getForModelID( $model );
88 if ( !$handler->supportsDirectEditing() ) {
89 continue;
90 }
91 $modelOptions[] = $model;
92 }
93
94 return [
95 'title' => [
96 ApiBase::PARAM_TYPE => 'string',
97 ],
98 'pageid' => [
99 ApiBase::PARAM_TYPE => 'integer',
100 ],
101 'summary' => null,
102 'tags' => [
103 ApiBase::PARAM_TYPE => 'tags',
105 ],
106 'model' => [
107 ApiBase::PARAM_TYPE => $modelOptions,
109 ],
110 'bot' => false,
111 ];
112 }
113
114 public function mustBePosted() {
115 return true;
116 }
117
118 public function isWriteMode() {
119 return true;
120 }
121
122 public function needsToken() {
123 return 'csrf';
124 }
125
126 public function getHelpUrls() {
127 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:ChangeContentModel';
128 }
129
130 protected function getExamplesMessages() {
131 return [
132 'action=changecontentmodel&title=Main Page&model=text&token=123ABC'
133 => 'apihelp-changecontentmodel-example'
134 ];
135 }
136}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_REQUIRED
Definition ApiBase.php:102
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:1539
const PARAM_TYPE
Definition ApiBase.php:78
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition ApiBase.php:1248
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition ApiBase.php:1041
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
const PARAM_ISMULTI
Definition ApiBase.php:74
Api module to change the content model of existing pages.
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 Stable to override.
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.
getUser()
Stable to override.
getContext()
Get the base IContextSource object.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static plaintextParam( $plaintext)
Definition Message.php:1130
Show an error when the user hits a rate limit.