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 $options = $this->getOptionsForTitle( $this->title
);
88 if ( empty( $options ) ) {
89 throw new ErrorPageError(
90 'changecontentmodel-emptymodels-title',
91 'changecontentmodel-emptymodels-text',
92 $this->title
->getPrefixedText()
95 $fields['pagetitle']['readonly'] = true;
100 'options' => $options,
101 'label-message' => 'changecontentmodel-model-label'
106 'validation-callback' => function( $reason ) {
107 $match = EditPage
::matchSummarySpamRegex( $reason );
109 return $this->msg( 'spamprotectionmatch', $match )->parse();
114 'label-message' => 'changecontentmodel-reason-label',
122 private function getOptionsForTitle( Title
$title = null ) {
123 $models = ContentHandler
::getContentModels();
125 foreach ( $models as $model ) {
126 $handler = ContentHandler
::getForModelID( $model );
127 if ( !$handler->supportsDirectEditing() ) {
131 if ( $title->getContentModel() === $model ) {
134 if ( !$handler->canBeUsedOn( $title ) ) {
138 $options[ContentHandler
::getLocalizedName( $model )] = $model;
144 public function onSubmit( array $data ) {
147 if ( $data['pagetitle'] === '' ) {
148 // Initial form view of special page, pass
152 // At this point, it has to be a POST request. This is enforced by HTMLForm,
153 // but lets be safe verify that.
154 if ( !$this->getRequest()->wasPosted() ) {
155 throw new RuntimeException( "Form submission was not POSTed" );
158 $this->title
= Title
::newFromText( $data['pagetitle'] );
159 $titleWithNewContentModel = clone $this->title
;
160 $titleWithNewContentModel->setContentModel( $data['model'] );
161 $user = $this->getUser();
162 // Check permissions and make sure the user has permission to:
163 $errors = wfMergeErrorArrays(
164 // edit the contentmodel of the page
165 $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user ),
166 // edit the page under the old content model
167 $this->title
->getUserPermissionsErrors( 'edit', $user ),
168 // edit the contentmodel under the new content model
169 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
170 // edit the page under the new content model
171 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
174 $out = $this->getOutput();
175 $wikitext = $out->formatPermissionsErrorMessage( $errors );
176 // Hack to get our wikitext parsed
177 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
180 $page = WikiPage
::factory( $this->title
);
181 if ( $this->oldRevision
=== null ) {
182 $this->oldRevision
= $page->getRevision() ?
: false;
184 $oldModel = $this->title
->getContentModel();
185 if ( $this->oldRevision
) {
186 $oldContent = $this->oldRevision
->getContent();
188 $newContent = ContentHandler
::makeContent(
189 $oldContent->getNativeData(), $this->title
, $data['model']
191 } catch ( MWException
$e ) {
192 return Status
::newFatal(
193 $this->msg( 'changecontentmodel-cannot-convert' )
195 $this->title
->getPrefixedText(),
196 ContentHandler
::getLocalizedName( $data['model'] )
201 // Page doesn't exist, create an empty content object
202 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
205 // All other checks have passed, let's check rate limits
206 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
207 throw new ThrottledError();
210 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
211 $flags |
= EDIT_INTERNAL
;
212 if ( $user->isAllowed( 'bot' ) ) {
213 $flags |
= EDIT_FORCE_BOT
;
216 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
217 $log->setPerformer( $user );
218 $log->setTarget( $this->title
);
219 $log->setComment( $data['reason'] );
220 $log->setParameters( [
221 '4::oldmodel' => $oldModel,
222 '5::newmodel' => $data['model']
225 $formatter = LogFormatter
::newFromEntry( $log );
226 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
227 $reason = $formatter->getPlainActionText();
228 if ( $data['reason'] !== '' ) {
229 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
231 # Truncate for whole multibyte characters.
232 $reason = $wgContLang->truncate( $reason, 255 );
235 $derivativeContext = new DerivativeContext( $this->getContext() );
236 $derivativeContext->setTitle( $this->title
);
237 $derivativeContext->setWikiPage( $page );
238 $status = new Status();
239 if ( !Hooks
::run( 'EditFilterMergedContent',
240 [ $derivativeContext, $newContent, $status, $reason,
243 if ( $status->isGood() ) {
244 // TODO: extensions should really specify an error message
245 $status->fatal( 'hookaborted' );
250 $status = $page->doEditContent(
254 $this->oldRevision ?
$this->oldRevision
->getId() : false,
257 if ( !$status->isOK() ) {
261 $logid = $log->insert();
262 $log->publish( $logid );
267 public function onSuccess() {
268 $out = $this->getOutput();
269 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
270 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
274 * Return an array of subpages beginning with $search that this special page will accept.
276 * @param string $search Prefix to search for
277 * @param int $limit Maximum number of results to return (usually 10)
278 * @param int $offset Number of results to skip (usually 0)
279 * @return string[] Matching subpages
281 public function prefixSearchSubpages( $search, $limit, $offset ) {
282 return $this->prefixSearchString( $search, $limit, $offset );
285 protected function getGroupName() {