3 class SpecialChangeContentModel
extends FormSpecialPage
{
5 public function __construct() {
6 parent
::__construct( 'ChangeContentModel', 'editcontentmodel' );
9 public function doesWrites() {
19 * @var Revision|bool|null
21 * A Revision object, false if no revision exists, null if not loaded yet
25 protected function setParameter( $par ) {
26 $par = $this->getRequest()->getVal( 'pagetitle', $par );
27 $title = Title
::newFromText( $par );
29 $this->title
= $title;
30 $this->par
= $title->getPrefixedText();
36 protected function postText() {
39 $contentModelLogPage = new LogPage( 'contentmodel' );
40 $text = Xml
::element( 'h2', null, $contentModelLogPage->getName()->text() );
42 LogEventsList
::showLogExtract( $out, 'contentmodel', $this->title
);
48 protected function getDisplayFormat() {
52 protected function alterForm( HTMLForm
$form ) {
53 if ( !$this->title
) {
54 $form->setMethod( 'GET' );
57 $this->addHelpLink( 'Help:ChangeContentModel' );
60 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
63 public function validateTitle( $title ) {
69 // Already validated by HTMLForm, but if not, throw
70 // and exception instead of a fatal
71 $titleObj = Title
::newFromTextThrow( $title );
73 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
75 if ( $this->oldRevision
) {
76 $oldContent = $this->oldRevision
->getContent();
77 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
78 return $this->msg( 'changecontentmodel-nodirectediting' )
79 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
87 protected function getFormFields() {
92 'name' => 'pagetitle',
93 'default' => $this->par
,
94 'label-message' => 'changecontentmodel-title-label',
95 'validation-callback' => [ $this, 'validateTitle' ],
99 $options = $this->getOptionsForTitle( $this->title
);
100 if ( empty( $options ) ) {
101 throw new ErrorPageError(
102 'changecontentmodel-emptymodels-title',
103 'changecontentmodel-emptymodels-text',
104 $this->title
->getPrefixedText()
107 $fields['pagetitle']['readonly'] = true;
112 'options' => $options,
113 'label-message' => 'changecontentmodel-model-label'
118 'validation-callback' => function ( $reason ) {
119 $match = EditPage
::matchSummarySpamRegex( $reason );
121 return $this->msg( 'spamprotectionmatch', $match )->parse();
126 'label-message' => 'changecontentmodel-reason-label',
134 private function getOptionsForTitle( Title
$title = null ) {
135 $models = ContentHandler
::getContentModels();
137 foreach ( $models as $model ) {
138 $handler = ContentHandler
::getForModelID( $model );
139 if ( !$handler->supportsDirectEditing() ) {
143 if ( $title->getContentModel() === $model ) {
146 if ( !$handler->canBeUsedOn( $title ) ) {
150 $options[ContentHandler
::getLocalizedName( $model )] = $model;
156 public function onSubmit( array $data ) {
157 if ( $data['pagetitle'] === '' ) {
158 // Initial form view of special page, pass
162 // At this point, it has to be a POST request. This is enforced by HTMLForm,
163 // but lets be safe verify that.
164 if ( !$this->getRequest()->wasPosted() ) {
165 throw new RuntimeException( "Form submission was not POSTed" );
168 $this->title
= Title
::newFromText( $data['pagetitle'] );
169 $titleWithNewContentModel = clone $this->title
;
170 $titleWithNewContentModel->setContentModel( $data['model'] );
171 $user = $this->getUser();
172 // Check permissions and make sure the user has permission to:
173 $errors = wfMergeErrorArrays(
174 // edit the contentmodel of the page
175 $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user ),
176 // edit the page under the old content model
177 $this->title
->getUserPermissionsErrors( 'edit', $user ),
178 // edit the contentmodel under the new content model
179 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
180 // edit the page under the new content model
181 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
184 $out = $this->getOutput();
185 $wikitext = $out->formatPermissionsErrorMessage( $errors );
186 // Hack to get our wikitext parsed
187 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
190 $page = WikiPage
::factory( $this->title
);
191 if ( $this->oldRevision
=== null ) {
192 $this->oldRevision
= $page->getRevision() ?
: false;
194 $oldModel = $this->title
->getContentModel();
195 if ( $this->oldRevision
) {
196 $oldContent = $this->oldRevision
->getContent();
198 $newContent = ContentHandler
::makeContent(
199 $oldContent->serialize(), $this->title
, $data['model']
201 } catch ( MWException
$e ) {
202 return Status
::newFatal(
203 $this->msg( 'changecontentmodel-cannot-convert' )
205 $this->title
->getPrefixedText(),
206 ContentHandler
::getLocalizedName( $data['model'] )
211 // Page doesn't exist, create an empty content object
212 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
215 // All other checks have passed, let's check rate limits
216 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
217 throw new ThrottledError();
220 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
221 $flags |
= EDIT_INTERNAL
;
222 if ( $user->isAllowed( 'bot' ) ) {
223 $flags |
= EDIT_FORCE_BOT
;
226 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
227 $log->setPerformer( $user );
228 $log->setTarget( $this->title
);
229 $log->setComment( $data['reason'] );
230 $log->setParameters( [
231 '4::oldmodel' => $oldModel,
232 '5::newmodel' => $data['model']
235 $formatter = LogFormatter
::newFromEntry( $log );
236 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
237 $reason = $formatter->getPlainActionText();
238 if ( $data['reason'] !== '' ) {
239 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
243 $derivativeContext = new DerivativeContext( $this->getContext() );
244 $derivativeContext->setTitle( $this->title
);
245 $derivativeContext->setWikiPage( $page );
246 $status = new Status();
247 if ( !Hooks
::run( 'EditFilterMergedContent',
248 [ $derivativeContext, $newContent, $status, $reason,
251 if ( $status->isGood() ) {
252 // TODO: extensions should really specify an error message
253 $status->fatal( 'hookaborted' );
258 $status = $page->doEditContent(
262 $this->oldRevision ?
$this->oldRevision
->getId() : false,
265 if ( !$status->isOK() ) {
269 $logid = $log->insert();
270 $log->publish( $logid );
275 public function onSuccess() {
276 $out = $this->getOutput();
277 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
278 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
282 * Return an array of subpages beginning with $search that this special page will accept.
284 * @param string $search Prefix to search for
285 * @param int $limit Maximum number of results to return (usually 10)
286 * @param int $offset Number of results to skip (usually 0)
287 * @return string[] Matching subpages
289 public function prefixSearchSubpages( $search, $limit, $offset ) {
290 return $this->prefixSearchString( $search, $limit, $offset );
293 protected function getGroupName() {