MediaWiki  master
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() {
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',
104  ApiBase::PARAM_ISMULTI => true,
105  ],
106  'model' => [
107  ApiBase::PARAM_TYPE => $modelOptions,
108  ApiBase::PARAM_REQUIRED => true,
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 }
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:269
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:42
ApiChangeContentModel\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiChangeContentModel.php:118
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:77
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:986
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1484
ApiChangeContentModel\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiChangeContentModel.php:130
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiChangeContentModel\execute
execute()
A lot of this code is based on SpecialChangeContentModel.
Definition: ApiChangeContentModel.php:19
ApiChangeContentModel\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiChangeContentModel.php:122
ApiChangeContentModel\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiChangeContentModel.php:114
ContentHandler\getContentModels
static getContentModels()
Definition: ContentHandler.php:320
ApiChangeContentModel
Api module to change the content model of existing pages.
Definition: ApiChangeContentModel.php:14
ThrottledError
Show an error when the user hits a rate limit.
Definition: ThrottledError.php:28
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
$title
$title
Definition: testCompression.php:38
Message\plaintextParam
static plaintextParam( $plaintext)
Definition: Message.php:1114
ApiChangeContentModel\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiChangeContentModel.php:126
ApiChangeContentModel\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiChangeContentModel.php:83
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1440
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1193