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' );
45 $this->addHelpLink( 'Help:ChangeContentModel' );
48 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
51 public function validateTitle( $title ) {
57 // Already validated by HTMLForm, but if not, throw
58 // and exception instead of a fatal
59 $titleObj = Title
::newFromTextThrow( $title );
61 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
63 if ( $this->oldRevision
) {
64 $oldContent = $this->oldRevision
->getContent();
65 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
66 return $this->msg( 'changecontentmodel-nodirectediting' )
67 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
75 protected function getFormFields() {
80 'name' => 'pagetitle',
81 'default' => $this->par
,
82 'label-message' => 'changecontentmodel-title-label',
83 'validation-callback' => [ $this, 'validateTitle' ],
87 $fields['pagetitle']['readonly'] = true;
92 'options' => $this->getOptionsForTitle( $this->title
),
93 'label-message' => 'changecontentmodel-model-label'
98 'validation-callback' => function( $reason ) {
99 $match = EditPage
::matchSummarySpamRegex( $reason );
101 return $this->msg( 'spamprotectionmatch', $match )->parse();
106 'label-message' => 'changecontentmodel-reason-label',
114 private function getOptionsForTitle( Title
$title = null ) {
115 $models = ContentHandler
::getContentModels();
117 foreach ( $models as $model ) {
118 $handler = ContentHandler
::getForModelID( $model );
119 if ( !$handler->supportsDirectEditing() ) {
123 if ( $title->getContentModel() === $model ) {
126 if ( !$handler->canBeUsedOn( $title ) ) {
130 $options[ContentHandler
::getLocalizedName( $model )] = $model;
136 public function onSubmit( array $data ) {
139 if ( $data['pagetitle'] === '' ) {
140 // Initial form view of special page, pass
144 // At this point, it has to be a POST request. This is enforced by HTMLForm,
145 // but lets be safe verify that.
146 if ( !$this->getRequest()->wasPosted() ) {
147 throw new RuntimeException( "Form submission was not POSTed" );
150 $this->title
= Title
::newFromText( $data['pagetitle'] );
151 $user = $this->getUser();
152 // Check permissions and make sure the user has permission to edit the specific page
153 $errors = $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user );
154 $errors = wfMergeErrorArrays( $errors, $this->title
->getUserPermissionsErrors( 'edit', $user ) );
156 $out = $this->getOutput();
157 $wikitext = $out->formatPermissionsErrorMessage( $errors );
158 // Hack to get our wikitext parsed
159 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
162 $page = WikiPage
::factory( $this->title
);
163 if ( $this->oldRevision
=== null ) {
164 $this->oldRevision
= $page->getRevision() ?
: false;
166 $oldModel = $this->title
->getContentModel();
167 if ( $this->oldRevision
) {
168 $oldContent = $this->oldRevision
->getContent();
170 $newContent = ContentHandler
::makeContent(
171 $oldContent->getNativeData(), $this->title
, $data['model']
173 } catch ( MWException
$e ) {
174 return Status
::newFatal(
175 $this->msg( 'changecontentmodel-cannot-convert' )
177 $this->title
->getPrefixedText(),
178 ContentHandler
::getLocalizedName( $data['model'] )
183 // Page doesn't exist, create an empty content object
184 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
186 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
187 if ( $user->isAllowed( 'bot' ) ) {
188 $flags |
= EDIT_FORCE_BOT
;
191 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
192 $log->setPerformer( $user );
193 $log->setTarget( $this->title
);
194 $log->setComment( $data['reason'] );
195 $log->setParameters( [
196 '4::oldmodel' => $oldModel,
197 '5::newmodel' => $data['model']
200 $formatter = LogFormatter
::newFromEntry( $log );
201 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
202 $reason = $formatter->getPlainActionText();
203 if ( $data['reason'] !== '' ) {
204 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
206 # Truncate for whole multibyte characters.
207 $reason = $wgContLang->truncate( $reason, 255 );
209 $status = $page->doEditContent(
213 $this->oldRevision ?
$this->oldRevision
->getId() : false,
216 if ( !$status->isOK() ) {
220 $logid = $log->insert();
221 $log->publish( $logid );
226 public function onSuccess() {
227 $out = $this->getOutput();
228 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
229 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
233 * Return an array of subpages beginning with $search that this special page will accept.
235 * @param string $search Prefix to search for
236 * @param int $limit Maximum number of results to return (usually 10)
237 * @param int $offset Number of results to skip (usually 0)
238 * @return string[] Matching subpages
240 public function prefixSearchSubpages( $search, $limit, $offset ) {
241 return $this->prefixSearchString( $search, $limit, $offset );
244 protected function getGroupName() {