3 use MediaWiki\MediaWikiServices
;
5 class SpecialChangeContentModel
extends FormSpecialPage
{
7 public function __construct() {
8 parent
::__construct( 'ChangeContentModel', 'editcontentmodel' );
11 public function doesWrites() {
21 * @var Revision|bool|null
23 * A Revision object, false if no revision exists, null if not loaded yet
27 protected function setParameter( $par ) {
28 $par = $this->getRequest()->getVal( 'pagetitle', $par );
29 $title = Title
::newFromText( $par );
31 $this->title
= $title;
32 $this->par
= $title->getPrefixedText();
38 protected function postText() {
41 $contentModelLogPage = new LogPage( 'contentmodel' );
42 $text = Xml
::element( 'h2', null, $contentModelLogPage->getName()->text() );
44 LogEventsList
::showLogExtract( $out, 'contentmodel', $this->title
);
50 protected function getDisplayFormat() {
54 protected function alterForm( HTMLForm
$form ) {
55 if ( !$this->title
) {
56 $form->setMethod( 'GET' );
59 $this->addHelpLink( 'Help:ChangeContentModel' );
62 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
65 public function validateTitle( $title ) {
71 // Already validated by HTMLForm, but if not, throw
72 // and exception instead of a fatal
73 $titleObj = Title
::newFromTextThrow( $title );
75 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
77 if ( $this->oldRevision
) {
78 $oldContent = $this->oldRevision
->getContent();
79 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
80 return $this->msg( 'changecontentmodel-nodirectediting' )
81 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
89 protected function getFormFields() {
94 'name' => 'pagetitle',
95 'default' => $this->par
,
96 'label-message' => 'changecontentmodel-title-label',
97 'validation-callback' => [ $this, 'validateTitle' ],
100 if ( $this->title
) {
101 $options = $this->getOptionsForTitle( $this->title
);
102 if ( empty( $options ) ) {
103 throw new ErrorPageError(
104 'changecontentmodel-emptymodels-title',
105 'changecontentmodel-emptymodels-text',
106 [ $this->title
->getPrefixedText() ]
109 $fields['pagetitle']['readonly'] = true;
113 'name' => 'currentcontentmodel',
114 'default' => $this->title
->getContentModel(),
115 'label-message' => 'changecontentmodel-current-label',
121 'options' => $options,
122 'label-message' => 'changecontentmodel-model-label'
127 'validation-callback' => function ( $reason ) {
128 $match = EditPage
::matchSummarySpamRegex( $reason );
130 return $this->msg( 'spamprotectionmatch', $match )->parse();
135 'label-message' => 'changecontentmodel-reason-label',
143 private function getOptionsForTitle( Title
$title = null ) {
144 $models = ContentHandler
::getContentModels();
146 foreach ( $models as $model ) {
147 $handler = ContentHandler
::getForModelID( $model );
148 if ( !$handler->supportsDirectEditing() ) {
152 if ( $title->getContentModel() === $model ) {
155 if ( !$handler->canBeUsedOn( $title ) ) {
159 $options[ContentHandler
::getLocalizedName( $model )] = $model;
165 public function onSubmit( array $data ) {
166 if ( $data['pagetitle'] === '' ) {
167 // Initial form view of special page, pass
171 // At this point, it has to be a POST request. This is enforced by HTMLForm,
172 // but lets be safe verify that.
173 if ( !$this->getRequest()->wasPosted() ) {
174 throw new RuntimeException( "Form submission was not POSTed" );
177 $this->title
= Title
::newFromText( $data['pagetitle'] );
178 $titleWithNewContentModel = clone $this->title
;
179 $titleWithNewContentModel->setContentModel( $data['model'] );
180 $user = $this->getUser();
181 // Check permissions and make sure the user has permission to:
182 $errors = wfMergeErrorArrays(
183 // edit the contentmodel of the page
184 $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user ),
185 // edit the page under the old content model
186 $this->title
->getUserPermissionsErrors( 'edit', $user ),
187 // edit the contentmodel under the new content model
188 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
189 // edit the page under the new content model
190 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
193 $out = $this->getOutput();
194 $wikitext = $out->formatPermissionsErrorMessage( $errors );
195 // Hack to get our wikitext parsed
196 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
199 $page = WikiPage
::factory( $this->title
);
200 if ( $this->oldRevision
=== null ) {
201 $this->oldRevision
= $page->getRevision() ?
: false;
203 $oldModel = $this->title
->getContentModel();
204 if ( $this->oldRevision
) {
205 $oldContent = $this->oldRevision
->getContent();
207 $newContent = ContentHandler
::makeContent(
208 $oldContent->serialize(), $this->title
, $data['model']
210 } catch ( MWException
$e ) {
211 return Status
::newFatal(
212 $this->msg( 'changecontentmodel-cannot-convert' )
214 $this->title
->getPrefixedText(),
215 ContentHandler
::getLocalizedName( $data['model'] )
220 // Page doesn't exist, create an empty content object
221 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
224 // All other checks have passed, let's check rate limits
225 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
226 throw new ThrottledError();
229 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
230 $flags |
= EDIT_INTERNAL
;
231 if ( MediaWikiServices
::getInstance()
232 ->getPermissionManager()
233 ->userHasRight( $user, 'bot' )
235 $flags |
= EDIT_FORCE_BOT
;
238 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ?
'change' : 'new' );
239 $log->setPerformer( $user );
240 $log->setTarget( $this->title
);
241 $log->setComment( $data['reason'] );
242 $log->setParameters( [
243 '4::oldmodel' => $oldModel,
244 '5::newmodel' => $data['model']
247 $formatter = LogFormatter
::newFromEntry( $log );
248 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
249 $reason = $formatter->getPlainActionText();
250 if ( $data['reason'] !== '' ) {
251 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
255 $derivativeContext = new DerivativeContext( $this->getContext() );
256 $derivativeContext->setTitle( $this->title
);
257 $derivativeContext->setWikiPage( $page );
258 $status = new Status();
259 if ( !Hooks
::run( 'EditFilterMergedContent',
260 [ $derivativeContext, $newContent, $status, $reason,
263 if ( $status->isGood() ) {
264 // TODO: extensions should really specify an error message
265 $status->fatal( 'hookaborted' );
270 $status = $page->doEditContent(
274 $this->oldRevision ?
$this->oldRevision
->getId() : false,
277 if ( !$status->isOK() ) {
281 $logid = $log->insert();
282 $log->publish( $logid );
287 public function onSuccess() {
288 $out = $this->getOutput();
289 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
290 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
294 * Return an array of subpages beginning with $search that this special page will accept.
296 * @param string $search Prefix to search for
297 * @param int $limit Maximum number of results to return (usually 10)
298 * @param int $offset Number of results to skip (usually 0)
299 * @return string[] Matching subpages
301 public function prefixSearchSubpages( $search, $limit, $offset ) {
302 return $this->prefixSearchString( $search, $limit, $offset );
305 protected function getGroupName() {