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 postText() {
39 $contentModelLogPage = new LogPage( 'contentmodel' );
40 $text = Xml
::element( 'h2', null, $contentModelLogPage->getName()->text() );
42 LogEventsList
::showLogExtract( $out, 'contentmodel', $this->title
);
48 protected function getDisplayFormat() {
52 protected function alterForm( HTMLForm
$form ) {
53 if ( !$this->title
) {
54 $form->setMethod( 'GET' );
57 $this->addHelpLink( 'Help:ChangeContentModel' );
60 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
63 public function validateTitle( $title ) {
69 // Already validated by HTMLForm, but if not, throw
70 // and exception instead of a fatal
71 $titleObj = Title
::newFromTextThrow( $title );
73 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
75 if ( $this->oldRevision
) {
76 $oldContent = $this->oldRevision
->getContent();
77 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
78 return $this->msg( 'changecontentmodel-nodirectediting' )
79 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
87 protected function getFormFields() {
92 'name' => 'pagetitle',
93 'default' => $this->par
,
94 'label-message' => 'changecontentmodel-title-label',
95 'validation-callback' => [ $this, 'validateTitle' ],
99 $options = $this->getOptionsForTitle( $this->title
);
100 if ( empty( $options ) ) {
101 throw new ErrorPageError(
102 'changecontentmodel-emptymodels-title',
103 'changecontentmodel-emptymodels-text',
104 [ $this->title
->getPrefixedText() ]
107 $fields['pagetitle']['readonly'] = true;
111 'name' => 'currentcontentmodel',
112 'default' => $this->title
->getContentModel(),
113 'label-message' => 'changecontentmodel-current-label',
119 'options' => $options,
120 'label-message' => 'changecontentmodel-model-label'
125 'validation-callback' => function ( $reason ) {
126 $match = EditPage
::matchSummarySpamRegex( $reason );
128 return $this->msg( 'spamprotectionmatch', $match )->parse();
133 'label-message' => 'changecontentmodel-reason-label',
141 private function getOptionsForTitle( Title
$title = null ) {
142 $models = ContentHandler
::getContentModels();
144 foreach ( $models as $model ) {
145 $handler = ContentHandler
::getForModelID( $model );
146 if ( !$handler->supportsDirectEditing() ) {
150 if ( $title->getContentModel() === $model ) {
153 if ( !$handler->canBeUsedOn( $title ) ) {
157 $options[ContentHandler
::getLocalizedName( $model )] = $model;
163 public function onSubmit( array $data ) {
164 if ( $data['pagetitle'] === '' ) {
165 // Initial form view of special page, pass
169 // At this point, it has to be a POST request. This is enforced by HTMLForm,
170 // but lets be safe verify that.
171 if ( !$this->getRequest()->wasPosted() ) {
172 throw new RuntimeException( "Form submission was not POSTed" );
175 $this->title
= Title
::newFromText( $data['pagetitle'] );
176 $titleWithNewContentModel = clone $this->title
;
177 $titleWithNewContentModel->setContentModel( $data['model'] );
178 $user = $this->getUser();
179 // Check permissions and make sure the user has permission to:
180 $errors = wfMergeErrorArrays(
181 // edit the contentmodel of the page
182 $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user ),
183 // edit the page under the old content model
184 $this->title
->getUserPermissionsErrors( 'edit', $user ),
185 // edit the contentmodel under the new content model
186 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
187 // edit the page under the new content model
188 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
191 $out = $this->getOutput();
192 $wikitext = $out->formatPermissionsErrorMessage( $errors );
193 // Hack to get our wikitext parsed
194 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
197 $page = WikiPage
::factory( $this->title
);
198 if ( $this->oldRevision
=== null ) {
199 $this->oldRevision
= $page->getRevision() ?
: false;
201 $oldModel = $this->title
->getContentModel();
202 if ( $this->oldRevision
) {
203 $oldContent = $this->oldRevision
->getContent();
205 $newContent = ContentHandler
::makeContent(
206 $oldContent->serialize(), $this->title
, $data['model']
208 } catch ( MWException
$e ) {
209 return Status
::newFatal(
210 $this->msg( 'changecontentmodel-cannot-convert' )
212 $this->title
->getPrefixedText(),
213 ContentHandler
::getLocalizedName( $data['model'] )
218 // Page doesn't exist, create an empty content object
219 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
222 // All other checks have passed, let's check rate limits
223 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
224 throw new ThrottledError();
227 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
228 $flags |
= EDIT_INTERNAL
;
229 if ( $user->isAllowed( 'bot' ) ) {
230 $flags |
= EDIT_FORCE_BOT
;
233 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
234 $log->setPerformer( $user );
235 $log->setTarget( $this->title
);
236 $log->setComment( $data['reason'] );
237 $log->setParameters( [
238 '4::oldmodel' => $oldModel,
239 '5::newmodel' => $data['model']
242 $formatter = LogFormatter
::newFromEntry( $log );
243 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
244 $reason = $formatter->getPlainActionText();
245 if ( $data['reason'] !== '' ) {
246 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
250 $derivativeContext = new DerivativeContext( $this->getContext() );
251 $derivativeContext->setTitle( $this->title
);
252 $derivativeContext->setWikiPage( $page );
253 $status = new Status();
254 if ( !Hooks
::run( 'EditFilterMergedContent',
255 [ $derivativeContext, $newContent, $status, $reason,
258 if ( $status->isGood() ) {
259 // TODO: extensions should really specify an error message
260 $status->fatal( 'hookaborted' );
265 $status = $page->doEditContent(
269 $this->oldRevision ?
$this->oldRevision
->getId() : false,
272 if ( !$status->isOK() ) {
276 $logid = $log->insert();
277 $log->publish( $logid );
282 public function onSuccess() {
283 $out = $this->getOutput();
284 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
285 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
289 * Return an array of subpages beginning with $search that this special page will accept.
291 * @param string $search Prefix to search for
292 * @param int $limit Maximum number of results to return (usually 10)
293 * @param int $offset Number of results to skip (usually 0)
294 * @return string[] Matching subpages
296 public function prefixSearchSubpages( $search, $limit, $offset ) {
297 return $this->prefixSearchString( $search, $limit, $offset );
300 protected function getGroupName() {