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' );
46 public function validateTitle( $title ) {
52 // Already validated by HTMLForm, but if not, throw
53 // and exception instead of a fatal
54 $titleObj = Title
::newFromTextThrow( $title );
56 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
58 if ( $this->oldRevision
) {
59 $oldContent = $this->oldRevision
->getContent();
60 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
61 return $this->msg( 'changecontentmodel-nodirectediting' )
62 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
70 protected function getFormFields() {
76 'name' => 'pagetitle',
77 'default' => $this->par
,
78 'label-message' => 'changecontentmodel-title-label',
79 'validation-callback' => array( $this, 'validateTitle' ),
83 $fields['pagetitle']['readonly'] = true;
88 'options' => $this->getOptionsForTitle( $this->title
),
89 'label-message' => 'changecontentmodel-model-label'
94 'validation-callback' => function( $reason ) use ( $that ) {
95 $match = EditPage
::matchSummarySpamRegex( $reason );
97 return $that->msg( 'spamprotectionmatch', $match )->parse();
102 'label-message' => 'changecontentmodel-reason-label',
110 private function getOptionsForTitle( Title
$title = null ) {
111 $models = ContentHandler
::getContentModels();
113 foreach ( $models as $model ) {
114 $handler = ContentHandler
::getForModelID( $model );
115 if ( !$handler->supportsDirectEditing() ) {
119 if ( $title->getContentModel() === $model ) {
122 if ( !$handler->canBeUsedOn( $title ) ) {
126 $options[ContentHandler
::getLocalizedName( $model )] = $model;
132 public function onSubmit( array $data ) {
135 if ( $data['pagetitle'] === '' ) {
136 // Initial form view of special page, pass
140 // At this point, it has to be a POST request. This is enforced by HTMLForm,
141 // but lets be safe verify that.
142 if ( !$this->getRequest()->wasPosted() ) {
143 throw new RuntimeException( "Form submission was not POSTed" );
146 $this->title
= Title
::newFromText( $data['pagetitle'] );
147 $user = $this->getUser();
148 // Check permissions and make sure the user has permission to edit the specific page
149 $errors = $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user );
150 $errors = wfMergeErrorArrays( $errors, $this->title
->getUserPermissionsErrors( 'edit', $user ) );
152 $out = $this->getOutput();
153 $wikitext = $out->formatPermissionsErrorMessage( $errors );
154 // Hack to get our wikitext parsed
155 return Status
::newFatal( new RawMessage( '$1', array( $wikitext ) ) );
158 $page = WikiPage
::factory( $this->title
);
159 if ( $this->oldRevision
=== null ) {
160 $this->oldRevision
= $page->getRevision() ?
: false;
162 $oldModel = $this->title
->getContentModel();
163 if ( $this->oldRevision
) {
164 $oldContent = $this->oldRevision
->getContent();
166 $newContent = ContentHandler
::makeContent(
167 $oldContent->getNativeData(), $this->title
, $data['model']
169 } catch ( MWException
$e ) {
170 return Status
::newFatal(
171 $this->msg( 'changecontentmodel-cannot-convert' )
173 $this->title
->getPrefixedText(),
174 ContentHandler
::getLocalizedName( $data['model'] )
179 // Page doesn't exist, create an empty content object
180 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
182 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
183 if ( $user->isAllowed( 'bot' ) ) {
184 $flags |
= EDIT_FORCE_BOT
;
187 $log = new ManualLogEntry( 'contentmodel', 'change' );
188 $log->setPerformer( $user );
189 $log->setTarget( $this->title
);
190 $log->setComment( $data['reason'] );
191 $log->setParameters( array(
192 '4::oldmodel' => $oldModel,
193 '5::newmodel' => $data['model']
196 $formatter = LogFormatter
::newFromEntry( $log );
197 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
198 $reason = $formatter->getPlainActionText();
199 if ( $data['reason'] !== '' ) {
200 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
202 # Truncate for whole multibyte characters.
203 $reason = $wgContLang->truncate( $reason, 255 );
205 $status = $page->doEditContent(
209 $this->oldRevision ?
$this->oldRevision
->getId() : false,
212 if ( !$status->isOK() ) {
216 $logid = $log->insert();
217 $log->publish( $logid );
222 public function onSuccess() {
223 $out = $this->getOutput();
224 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
225 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
229 * Return an array of subpages beginning with $search that this special page will accept.
231 * @param string $search Prefix to search for
232 * @param int $limit Maximum number of results to return (usually 10)
233 * @param int $offset Number of results to skip (usually 0)
234 * @return string[] Matching subpages
236 public function prefixSearchSubpages( $search, $limit, $offset ) {
237 $title = Title
::newFromText( $search );
238 if ( !$title ||
!$title->canExist() ) {
239 // No prefix suggestion in special and media namespace
242 // Autocomplete subpage the same as a normal search
243 $prefixSearcher = new StringPrefixSearch
;
244 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
248 protected function getGroupName() {