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 public function validateTitle( $title ) {
54 // Already validated by HTMLForm, but if not, throw
55 // and exception instead of a fatal
56 $titleObj = Title
::newFromTextThrow( $title );
58 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
60 if ( $this->oldRevision
) {
61 $oldContent = $this->oldRevision
->getContent();
62 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
63 return $this->msg( 'changecontentmodel-nodirectediting' )
64 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
72 protected function getFormFields() {
77 'name' => 'pagetitle',
78 'default' => $this->par
,
79 'label-message' => 'changecontentmodel-title-label',
80 'validation-callback' => [ $this, 'validateTitle' ],
84 $fields['pagetitle']['readonly'] = true;
89 'options' => $this->getOptionsForTitle( $this->title
),
90 'label-message' => 'changecontentmodel-model-label'
95 'validation-callback' => function( $reason ) {
96 $match = EditPage
::matchSummarySpamRegex( $reason );
98 return $this->msg( 'spamprotectionmatch', $match )->parse();
103 'label-message' => 'changecontentmodel-reason-label',
111 private function getOptionsForTitle( Title
$title = null ) {
112 $models = ContentHandler
::getContentModels();
114 foreach ( $models as $model ) {
115 $handler = ContentHandler
::getForModelID( $model );
116 if ( !$handler->supportsDirectEditing() ) {
120 if ( $title->getContentModel() === $model ) {
123 if ( !$handler->canBeUsedOn( $title ) ) {
127 $options[ContentHandler
::getLocalizedName( $model )] = $model;
133 public function onSubmit( array $data ) {
136 if ( $data['pagetitle'] === '' ) {
137 // Initial form view of special page, pass
141 // At this point, it has to be a POST request. This is enforced by HTMLForm,
142 // but lets be safe verify that.
143 if ( !$this->getRequest()->wasPosted() ) {
144 throw new RuntimeException( "Form submission was not POSTed" );
147 $this->title
= Title
::newFromText( $data['pagetitle'] );
148 $user = $this->getUser();
149 // Check permissions and make sure the user has permission to edit the specific page
150 $errors = $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user );
151 $errors = wfMergeErrorArrays( $errors, $this->title
->getUserPermissionsErrors( 'edit', $user ) );
153 $out = $this->getOutput();
154 $wikitext = $out->formatPermissionsErrorMessage( $errors );
155 // Hack to get our wikitext parsed
156 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
159 $page = WikiPage
::factory( $this->title
);
160 if ( $this->oldRevision
=== null ) {
161 $this->oldRevision
= $page->getRevision() ?
: false;
163 $oldModel = $this->title
->getContentModel();
164 if ( $this->oldRevision
) {
165 $oldContent = $this->oldRevision
->getContent();
167 $newContent = ContentHandler
::makeContent(
168 $oldContent->getNativeData(), $this->title
, $data['model']
170 } catch ( MWException
$e ) {
171 return Status
::newFatal(
172 $this->msg( 'changecontentmodel-cannot-convert' )
174 $this->title
->getPrefixedText(),
175 ContentHandler
::getLocalizedName( $data['model'] )
180 // Page doesn't exist, create an empty content object
181 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
183 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
184 if ( $user->isAllowed( 'bot' ) ) {
185 $flags |
= EDIT_FORCE_BOT
;
188 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
189 $log->setPerformer( $user );
190 $log->setTarget( $this->title
);
191 $log->setComment( $data['reason'] );
192 $log->setParameters( [
193 '4::oldmodel' => $oldModel,
194 '5::newmodel' => $data['model']
197 $formatter = LogFormatter
::newFromEntry( $log );
198 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
199 $reason = $formatter->getPlainActionText();
200 if ( $data['reason'] !== '' ) {
201 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
203 # Truncate for whole multibyte characters.
204 $reason = $wgContLang->truncate( $reason, 255 );
206 $status = $page->doEditContent(
210 $this->oldRevision ?
$this->oldRevision
->getId() : false,
213 if ( !$status->isOK() ) {
217 $logid = $log->insert();
218 $log->publish( $logid );
223 public function onSuccess() {
224 $out = $this->getOutput();
225 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
226 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
230 * Return an array of subpages beginning with $search that this special page will accept.
232 * @param string $search Prefix to search for
233 * @param int $limit Maximum number of results to return (usually 10)
234 * @param int $offset Number of results to skip (usually 0)
235 * @return string[] Matching subpages
237 public function prefixSearchSubpages( $search, $limit, $offset ) {
238 return $this->prefixSearchString( $search, $limit, $offset );
241 protected function getGroupName() {