MediaWiki  master
ApiChangeContentModel.php
Go to the documentation of this file.
1 <?php
2 
5 
16 
19 
22 
29  public function __construct(
30  ApiMain $main,
31  $action,
34  ) {
35  parent::__construct( $main, $action );
36  $this->contentHandlerFactory = $contentHandlerFactory;
37  $this->contentModelChangeFactory = $contentModelChangeFactory;
38  }
39 
43  public function execute() {
44  $params = $this->extractRequestParams();
45  $wikiPage = $this->getTitleOrPageId( $params );
46  $title = $wikiPage->getTitle();
47  $this->getErrorFormatter()->setContextTitle( $title );
48 
49  if ( !$title->exists() ) {
50  $this->dieWithError( 'apierror-changecontentmodel-missingtitle' );
51  }
52 
53  $newModel = $params['model'];
54 
55  $this->checkUserRightsAny( 'editcontentmodel' );
56  $changer = $this->contentModelChangeFactory->newContentModelChange(
57  $this->getAuthority(),
58  $wikiPage,
59  $newModel
60  );
61  // Status messages should be apierror-*
62  $changer->setMessagePrefix( 'apierror-' );
63  $permissionStatus = $changer->authorizeChange();
64  if ( !$permissionStatus->isGood() ) {
65  $this->dieStatus( $permissionStatus );
66  }
67 
68  if ( $params['tags'] ) {
69  $tagStatus = $changer->setTags( $params['tags'] );
70  if ( !$tagStatus->isGood() ) {
71  $this->dieStatus( $tagStatus );
72  }
73  }
74 
75  // Everything passed, make the conversion
76  try {
77  $status = $changer->doContentModelChange(
78  $this->getContext(),
79  $params['summary'],
80  $params['bot']
81  );
82  } catch ( ThrottledError $te ) {
83  $this->dieWithError( 'apierror-ratelimited' );
84  }
85 
86  if ( !$status->isGood() ) {
87  // Failed
88  $this->dieStatus( $status );
89  }
90  $logid = $status->getValue()['logid'];
91 
92  $result = [
93  'result' => 'Success',
94  'title' => $title->getPrefixedText(),
95  'pageid' => $title->getArticleID(),
96  'contentmodel' => $title->getContentModel( Title::READ_LATEST ),
97  'logid' => $logid,
98  'revid' => $title->getLatestRevID( Title::READ_LATEST ),
99  ];
100 
101  $this->getResult()->addValue( null, $this->getModuleName(), $result );
102  }
103 
104  public function getAllowedParams() {
105  $models = $this->contentHandlerFactory->getContentModels();
106  $modelOptions = [];
107  foreach ( $models as $model ) {
108  $handler = $this->contentHandlerFactory->getContentHandler( $model );
109  if ( !$handler->supportsDirectEditing() ) {
110  continue;
111  }
112  $modelOptions[] = $model;
113  }
114 
115  return [
116  'title' => [
117  ApiBase::PARAM_TYPE => 'string',
118  ],
119  'pageid' => [
120  ApiBase::PARAM_TYPE => 'integer',
121  ],
122  'summary' => null,
123  'tags' => [
124  ApiBase::PARAM_TYPE => 'tags',
125  ApiBase::PARAM_ISMULTI => true,
126  ],
127  'model' => [
128  ApiBase::PARAM_TYPE => $modelOptions,
129  ApiBase::PARAM_REQUIRED => true,
130  ],
131  'bot' => false,
132  ];
133  }
134 
135  public function mustBePosted() {
136  return true;
137  }
138 
139  public function isWriteMode() {
140  return true;
141  }
142 
143  public function needsToken() {
144  return 'csrf';
145  }
146 
147  public function getHelpUrls() {
148  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Help:ChangeContentModel';
149  }
150 
151  protected function getExamplesMessages() {
152  return [
153  'action=changecontentmodel&title=Main Page&model=text&token=123ABC'
154  => 'apihelp-changecontentmodel-example'
155  ];
156  }
157 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:47
ApiChangeContentModel\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiChangeContentModel.php:139
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:105
ApiChangeContentModel\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: ApiChangeContentModel.php:18
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1436
ApiBase\getTitleOrPageId
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:1033
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:1545
ApiChangeContentModel\__construct
__construct(ApiMain $main, $action, IContentHandlerFactory $contentHandlerFactory, ContentModelChangeFactory $contentModelChangeFactory)
Definition: ApiChangeContentModel.php:29
ApiChangeContentModel\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiChangeContentModel.php:151
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
ApiChangeContentModel\execute
execute()
A lot of this code is based on SpecialChangeContentModel.
Definition: ApiChangeContentModel.php:43
ApiChangeContentModel\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiChangeContentModel.php:143
ApiChangeContentModel\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiChangeContentModel.php:135
ApiChangeContentModel
Api module to change the content model of existing pages.
Definition: ApiChangeContentModel.php:15
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:764
$title
$title
Definition: testCompression.php:38
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiChangeContentModel\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiChangeContentModel.php:147
ApiChangeContentModel\$contentModelChangeFactory
ContentModelChangeFactory $contentModelChangeFactory
Definition: ApiChangeContentModel.php:21
ApiChangeContentModel\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiChangeContentModel.php:104
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1499
Page\ContentModelChangeFactory
Definition: ContentModelChangeFactory.php:32
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:77
ApiBase\getErrorFormatter
getErrorFormatter()
Definition: ApiBase.php:639