3 class SpecialChangeContentModel
extends FormSpecialPage
{
5 public function __construct() {
6 parent
::__construct( 'ChangeContentModel', 'editcontentmodel' );
15 * @var Revision|bool|null
17 * A Revision object, false if no revision exists, null if not loaded yet
21 protected function setParameter( $par ) {
22 $par = $this->getRequest()->getVal( 'pagetitle', $par );
23 $title = Title
::newFromText( $par );
25 $this->title
= $title;
26 $this->par
= $title->getPrefixedText();
32 protected function getDisplayFormat() {
36 protected function alterForm( HTMLForm
$form ) {
37 if ( !$this->title
) {
38 $form->setMethod( 'GET' );
42 public function validateTitle( $title ) {
48 // Already validated by HTMLForm, but if not, throw
49 // and exception instead of a fatal
50 $titleObj = Title
::newFromTextThrow( $title );
52 $this->oldRevision
= Revision
::newFromTitle( $titleObj ) ?
: false;
54 if ( $this->oldRevision
) {
55 $oldContent = $this->oldRevision
->getContent();
56 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
57 return $this->msg( 'changecontentmodel-nodirectediting' )
58 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
66 protected function getFormFields() {
72 'name' => 'pagetitle',
73 'default' => $this->par
,
74 'label-message' => 'changecontentmodel-title-label',
75 'validation-callback' => array( $this, 'validateTitle' ),
79 $fields['pagetitle']['readonly'] = true;
84 'options' => $this->getOptionsForTitle( $this->title
),
85 'label-message' => 'changecontentmodel-model-label'
90 'validation-callback' => function( $reason ) use ( $that ) {
91 $match = EditPage
::matchSummarySpamRegex( $reason );
93 return $that->msg( 'spamprotectionmatch', $match )->parse();
98 'label-message' => 'changecontentmodel-reason-label',
106 private function getOptionsForTitle( Title
$title = null ) {
107 $models = ContentHandler
::getContentModels();
109 foreach ( $models as $model ) {
110 $handler = ContentHandler
::getForModelID( $model );
111 if ( !$handler->supportsDirectEditing() ) {
115 if ( $title->getContentModel() === $model ) {
118 if ( !$handler->canBeUsedOn( $title ) ) {
122 $options[ContentHandler
::getLocalizedName( $model )] = $model;
128 public function onSubmit( array $data ) {
131 if ( $data['pagetitle'] === '' ) {
132 // Initial form view of special page, pass
136 // At this point, it has to be a POST request. This is enforced by HTMLForm,
137 // but lets be safe verify that.
138 if ( !$this->getRequest()->wasPosted() ) {
139 throw new RuntimeException( "Form submission was not POSTed" );
142 $this->title
= Title
::newFromText( $data['pagetitle'] );
143 $user = $this->getUser();
144 // Check permissions and make sure the user has permission to edit the specific page
145 $errors = $this->title
->getUserPermissionsErrors( 'editcontentmodel', $user );
146 $errors = wfMergeErrorArrays( $errors, $this->title
->getUserPermissionsErrors( 'edit', $user ) );
148 $out = $this->getOutput();
149 $wikitext = $out->formatPermissionsErrorMessage( $errors );
150 // Hack to get our wikitext parsed
151 return Status
::newFatal( new RawMessage( '$1', array( $wikitext ) ) );
154 $page = WikiPage
::factory( $this->title
);
155 if ( $this->oldRevision
=== null ) {
156 $this->oldRevision
= $page->getRevision() ?
: false;
158 $oldModel = $this->title
->getContentModel();
159 if ( $this->oldRevision
) {
160 $oldContent = $this->oldRevision
->getContent();
162 $newContent = ContentHandler
::makeContent(
163 $oldContent->getNativeData(), $this->title
, $data['model']
165 } catch ( MWException
$e ) {
166 return Status
::newFatal(
167 $this->msg( 'changecontentmodel-cannot-convert' )
169 $this->title
->getPrefixedText(),
170 ContentHandler
::getLocalizedName( $data['model'] )
175 // Page doesn't exist, create an empty content object
176 $newContent = ContentHandler
::getForModelID( $data['model'] )->makeEmptyContent();
178 $flags = $this->oldRevision ? EDIT_UPDATE
: EDIT_NEW
;
179 if ( $user->isAllowed( 'bot' ) ) {
180 $flags |
= EDIT_FORCE_BOT
;
183 $log = new ManualLogEntry( 'contentmodel', 'change' );
184 $log->setPerformer( $user );
185 $log->setTarget( $this->title
);
186 $log->setComment( $data['reason'] );
187 $log->setParameters( array(
188 '4::oldmodel' => $oldModel,
189 '5::newmodel' => $data['model']
192 $formatter = LogFormatter
::newFromEntry( $log );
193 $formatter->setContext( RequestContext
::newExtraneousContext( $this->title
) );
194 $reason = $formatter->getPlainActionText();
195 if ( $data['reason'] !== '' ) {
196 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
198 # Truncate for whole multibyte characters.
199 $reason = $wgContLang->truncate( $reason, 255 );
201 $status = $page->doEditContent(
205 $this->oldRevision ?
$this->oldRevision
->getId() : false,
208 if ( !$status->isOK() ) {
212 $logid = $log->insert();
213 $log->publish( $logid );
218 public function onSuccess() {
219 $out = $this->getOutput();
220 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
221 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
225 * Return an array of subpages beginning with $search that this special page will accept.
227 * @param string $search Prefix to search for
228 * @param int $limit Maximum number of results to return (usually 10)
229 * @param int $offset Number of results to skip (usually 0)
230 * @return string[] Matching subpages
232 public function prefixSearchSubpages( $search, $limit, $offset ) {
233 $title = Title
::newFromText( $search );
234 if ( !$title ||
!$title->canExist() ) {
235 // No prefix suggestion in special and media namespace
238 // Autocomplete subpage the same as a normal search
239 $prefixSearcher = new StringPrefixSearch
;
240 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
244 protected function getGroupName() {