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 getDisplayFormat() {
40 protected function alterForm( HTMLForm
$form ) {
41 if ( !$this->title
) {
42 $form->setMethod( 'GET' );
46 public function validateTitle( $title ) {
52 // Already validated by HTMLForm, but if not, throw
53 // and exception instead of a fatal
54 $titleObj = Title
::newFromTextThrow( $title );
56 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
58 if ( $this->oldRevision
) {
59 $oldContent = $this->oldRevision
->getContent();
60 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
61 return $this->msg( 'changecontentmodel-nodirectediting' )
62 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
70 protected function getFormFields() {
75 'name' => 'pagetitle',
76 'default' => $this->par
,
77 'label-message' => 'changecontentmodel-title-label',
78 'validation-callback' => [ $this, 'validateTitle' ],
82 $fields['pagetitle']['readonly'] = true;
87 'options' => $this->getOptionsForTitle( $this->title
),
88 'label-message' => 'changecontentmodel-model-label'
93 'validation-callback' => function( $reason ) {
94 $match = EditPage
::matchSummarySpamRegex( $reason );
96 return $this->msg( 'spamprotectionmatch', $match )->parse();
101 'label-message' => 'changecontentmodel-reason-label',
109 private function getOptionsForTitle( Title
$title = null ) {
110 $models = ContentHandler
::getContentModels();
112 foreach ( $models as $model ) {
113 $handler = ContentHandler
::getForModelID( $model );
114 if ( !$handler->supportsDirectEditing() ) {
118 if ( $title->getContentModel() === $model ) {
121 if ( !$handler->canBeUsedOn( $title ) ) {
125 $options[ContentHandler
::getLocalizedName( $model )] = $model;
131 public function onSubmit( array $data ) {
134 if ( $data['pagetitle'] === '' ) {
135 // Initial form view of special page, pass
139 // At this point, it has to be a POST request. This is enforced by HTMLForm,
140 // but lets be safe verify that.
141 if ( !$this->getRequest()->wasPosted() ) {
142 throw new RuntimeException( "Form submission was not POSTed" );
145 $this->title
= Title
::newFromText( $data['pagetitle'] );
146 $user = $this->getUser();
147 // Check permissions and make sure the user has permission to edit the specific page
148 $errors = $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user );
149 $errors = wfMergeErrorArrays( $errors, $this->title
->getUserPermissionsErrors( 'edit', $user ) );
151 $out = $this->getOutput();
152 $wikitext = $out->formatPermissionsErrorMessage( $errors );
153 // Hack to get our wikitext parsed
154 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
157 $page = WikiPage
::factory( $this->title
);
158 if ( $this->oldRevision
=== null ) {
159 $this->oldRevision
= $page->getRevision() ?
: false;
161 $oldModel = $this->title
->getContentModel();
162 if ( $this->oldRevision
) {
163 $oldContent = $this->oldRevision
->getContent();
165 $newContent = ContentHandler
::makeContent(
166 $oldContent->getNativeData(), $this->title
, $data['model']
168 } catch ( MWException
$e ) {
169 return Status
::newFatal(
170 $this->msg( 'changecontentmodel-cannot-convert' )
172 $this->title
->getPrefixedText(),
173 ContentHandler
::getLocalizedName( $data['model'] )
178 // Page doesn't exist, create an empty content object
179 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
181 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
182 if ( $user->isAllowed( 'bot' ) ) {
183 $flags |
= EDIT_FORCE_BOT
;
186 $log = new ManualLogEntry( 'contentmodel', 'change' );
187 $log->setPerformer( $user );
188 $log->setTarget( $this->title
);
189 $log->setComment( $data['reason'] );
190 $log->setParameters( [
191 '4::oldmodel' => $oldModel,
192 '5::newmodel' => $data['model']
195 $formatter = LogFormatter
::newFromEntry( $log );
196 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
197 $reason = $formatter->getPlainActionText();
198 if ( $data['reason'] !== '' ) {
199 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
201 # Truncate for whole multibyte characters.
202 $reason = $wgContLang->truncate( $reason, 255 );
204 $status = $page->doEditContent(
208 $this->oldRevision ?
$this->oldRevision
->getId() : false,
211 if ( !$status->isOK() ) {
215 $logid = $log->insert();
216 $log->publish( $logid );
221 public function onSuccess() {
222 $out = $this->getOutput();
223 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
224 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
228 * Return an array of subpages beginning with $search that this special page will accept.
230 * @param string $search Prefix to search for
231 * @param int $limit Maximum number of results to return (usually 10)
232 * @param int $offset Number of results to skip (usually 0)
233 * @return string[] Matching subpages
235 public function prefixSearchSubpages( $search, $limit, $offset ) {
236 return $this->prefixSearchString( $search, $limit, $offset );
239 protected function getGroupName() {