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;
112 'options' => $options,
113 'label-message' => 'changecontentmodel-model-label'
118 'validation-callback' => function( $reason ) {
119 $match = EditPage
::matchSummarySpamRegex( $reason );
121 return $this->msg( 'spamprotectionmatch', $match )->parse();
126 'label-message' => 'changecontentmodel-reason-label',
134 private function getOptionsForTitle( Title
$title = null ) {
135 $models = ContentHandler
::getContentModels();
137 foreach ( $models as $model ) {
138 $handler = ContentHandler
::getForModelID( $model );
139 if ( !$handler->supportsDirectEditing() ) {
143 if ( $title->getContentModel() === $model ) {
146 if ( !$handler->canBeUsedOn( $title ) ) {
150 $options[ContentHandler
::getLocalizedName( $model )] = $model;
156 public function onSubmit( array $data ) {
159 if ( $data['pagetitle'] === '' ) {
160 // Initial form view of special page, pass
164 // At this point, it has to be a POST request. This is enforced by HTMLForm,
165 // but lets be safe verify that.
166 if ( !$this->getRequest()->wasPosted() ) {
167 throw new RuntimeException( "Form submission was not POSTed" );
170 $this->title
= Title
::newFromText( $data['pagetitle'] );
171 $titleWithNewContentModel = clone $this->title
;
172 $titleWithNewContentModel->setContentModel( $data['model'] );
173 $user = $this->getUser();
174 // Check permissions and make sure the user has permission to:
175 $errors = wfMergeErrorArrays(
176 // edit the contentmodel of the page
177 $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user ),
178 // edit the page under the old content model
179 $this->title
->getUserPermissionsErrors( 'edit', $user ),
180 // edit the contentmodel under the new content model
181 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
182 // edit the page under the new content model
183 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
186 $out = $this->getOutput();
187 $wikitext = $out->formatPermissionsErrorMessage( $errors );
188 // Hack to get our wikitext parsed
189 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
192 $page = WikiPage
::factory( $this->title
);
193 if ( $this->oldRevision
=== null ) {
194 $this->oldRevision
= $page->getRevision() ?
: false;
196 $oldModel = $this->title
->getContentModel();
197 if ( $this->oldRevision
) {
198 $oldContent = $this->oldRevision
->getContent();
200 $newContent = ContentHandler
::makeContent(
201 $oldContent->getNativeData(), $this->title
, $data['model']
203 } catch ( MWException
$e ) {
204 return Status
::newFatal(
205 $this->msg( 'changecontentmodel-cannot-convert' )
207 $this->title
->getPrefixedText(),
208 ContentHandler
::getLocalizedName( $data['model'] )
213 // Page doesn't exist, create an empty content object
214 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
217 // All other checks have passed, let's check rate limits
218 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
219 throw new ThrottledError();
222 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
223 $flags |
= EDIT_INTERNAL
;
224 if ( $user->isAllowed( 'bot' ) ) {
225 $flags |
= EDIT_FORCE_BOT
;
228 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
229 $log->setPerformer( $user );
230 $log->setTarget( $this->title
);
231 $log->setComment( $data['reason'] );
232 $log->setParameters( [
233 '4::oldmodel' => $oldModel,
234 '5::newmodel' => $data['model']
237 $formatter = LogFormatter
::newFromEntry( $log );
238 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
239 $reason = $formatter->getPlainActionText();
240 if ( $data['reason'] !== '' ) {
241 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
243 # Truncate for whole multibyte characters.
244 $reason = $wgContLang->truncate( $reason, 255 );
247 $derivativeContext = new DerivativeContext( $this->getContext() );
248 $derivativeContext->setTitle( $this->title
);
249 $derivativeContext->setWikiPage( $page );
250 $status = new Status();
251 if ( !Hooks
::run( 'EditFilterMergedContent',
252 [ $derivativeContext, $newContent, $status, $reason,
255 if ( $status->isGood() ) {
256 // TODO: extensions should really specify an error message
257 $status->fatal( 'hookaborted' );
262 $status = $page->doEditContent(
266 $this->oldRevision ?
$this->oldRevision
->getId() : false,
269 if ( !$status->isOK() ) {
273 $logid = $log->insert();
274 $log->publish( $logid );
279 public function onSuccess() {
280 $out = $this->getOutput();
281 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
282 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
286 * Return an array of subpages beginning with $search that this special page will accept.
288 * @param string $search Prefix to search for
289 * @param int $limit Maximum number of results to return (usually 10)
290 * @param int $offset Number of results to skip (usually 0)
291 * @return string[] Matching subpages
293 public function prefixSearchSubpages( $search, $limit, $offset ) {
294 return $this->prefixSearchString( $search, $limit, $offset );
297 protected function getGroupName() {