3 class SpecialChangeContentModel
extends FormSpecialPage
{
5 public function __construct() {
6 parent
::__construct( 'ChangeContentModel', 'editcontentmodel' );
15 * @var Revision|bool|null
17 * A Revision object, false if no revision exists, null if not loaded yet
21 protected function setParameter( $par ) {
22 $par = $this->getRequest()->getVal( 'pagetitle', $par );
23 $title = Title
::newFromText( $par );
25 $this->title
= $title;
26 $this->par
= $title->getPrefixedText();
32 protected function getDisplayFormat() {
36 protected function alterForm( HTMLForm
$form ) {
37 if ( !$this->title
) {
38 $form->setMethod( 'GET' );
42 public function validateTitle( $title ) {
48 $titleObj = Title
::newFromTextThrow( $title );
49 } catch ( MalformedTitleException
$e ) {
50 $msg = $this->msg( $e->getErrorMessage() );
51 $params = $e->getErrorMessageParameters();
53 $msg->params( $params );
57 if ( !$titleObj->canExist() ) {
59 'changecontentmodel-title-cantexist',
60 $titleObj->getPrefixedText()
64 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
66 if ( $this->oldRevision
) {
67 $oldContent = $this->oldRevision
->getContent();
68 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
69 return $this->msg( 'changecontentmodel-nodirectediting' )
70 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
78 protected function getFormFields() {
83 'name' => 'pagetitle',
84 'default' => $this->par
,
85 'label-message' => 'changecontentmodel-title-label',
86 'validation-callback' => array( $this, 'validateTitle' ),
90 $fields['pagetitle']['readonly'] = true;
95 'options' => $this->getOptionsForTitle( $this->title
),
96 'label-message' => 'changecontentmodel-model-label'
101 'validation-callback' => function( $reason ) use ( $that ) {
102 $match = EditPage
::matchSummarySpamRegex( $reason );
104 return $that->msg( 'spamprotectionmatch', $match )->parse();
109 'label-message' => 'changecontentmodel-reason-label',
117 private function getOptionsForTitle( Title
$title = null ) {
118 $models = ContentHandler
::getContentModels();
120 foreach ( $models as $model ) {
121 $handler = ContentHandler
::getForModelID( $model );
122 if ( !$handler->supportsDirectEditing() ) {
126 if ( $title->getContentModel() === $model ) {
129 if ( !$handler->canBeUsedOn( $title ) ) {
133 $options[ContentHandler
::getLocalizedName( $model )] = $model;
139 public function onSubmit( array $data ) {
142 if ( $data['pagetitle'] === '' ) {
143 // Initial form view of special page, pass
147 // At this point, it has to be a POST request. This is enforced by HTMLForm,
148 // but lets be safe verify that.
149 if ( !$this->getRequest()->wasPosted() ) {
150 throw new RuntimeException( "Form submission was not POSTed" );
153 $this->title
= Title
::newFromText( $data['pagetitle' ] );
154 $user = $this->getUser();
155 // Check permissions and make sure the user has permission to edit the specific page
156 $errors = $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user );
157 $errors = wfMergeErrorArrays( $errors, $this->title
->getUserPermissionsErrors( 'edit', $user ) );
159 $out = $this->getOutput();
160 $wikitext = $out->formatPermissionsErrorMessage( $errors );
161 // Hack to get our wikitext parsed
162 return Status
::newFatal( new RawMessage( '$1', array( $wikitext ) ) );
165 $page = WikiPage
::factory( $this->title
);
166 if ( $this->oldRevision
=== null ) {
167 $this->oldRevision
= $page->getRevision() ?
: false;
169 $oldModel = $this->title
->getContentModel();
170 if ( $this->oldRevision
) {
171 $oldContent = $this->oldRevision
->getContent();
173 $newContent = ContentHandler
::makeContent(
174 $oldContent->getNativeData(), $this->title
, $data['model']
176 } catch ( MWException
$e ) {
177 return Status
::newFatal(
178 $this->msg( 'changecontentmodel-cannot-convert' )
180 $this->title
->getPrefixedText(),
181 ContentHandler
::getLocalizedName( $data['model'] )
186 // Page doesn't exist, create an empty content object
187 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
189 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
190 if ( $user->isAllowed( 'bot' ) ) {
191 $flags |
= EDIT_FORCE_BOT
;
194 $log = new ManualLogEntry( 'contentmodel', 'change' );
195 $log->setPerformer( $user );
196 $log->setTarget( $this->title
);
197 $log->setComment( $data['reason'] );
198 $log->setParameters( array(
199 '4::oldmodel' => $oldModel,
200 '5::newmodel' => $data['model']
203 $formatter = LogFormatter
::newFromEntry( $log );
204 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
205 $reason = $formatter->getPlainActionText();
206 if ( $data['reason'] !== '' ) {
207 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
209 # Truncate for whole multibyte characters.
210 $reason = $wgContLang->truncate( $reason, 255 );
212 $status = $page->doEditContent(
216 $this->oldRevision ?
$this->oldRevision
->getId() : false,
219 if ( !$status->isOK() ) {
223 $logid = $log->insert();
224 $log->publish( $logid );
229 public function onSuccess() {
230 $out = $this->getOutput();
231 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
232 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);