Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / specials / SpecialChangeContentModel.php
blob4812c9d6a0e626f42a72ba99cf222e0f175621fd
1 <?php
3 class SpecialChangeContentModel extends FormSpecialPage {
5 public function __construct() {
6 parent::__construct( 'ChangeContentModel', 'editcontentmodel' );
9 /**
10 * @var Title|null
12 private $title;
14 /**
15 * @var Revision|bool|null
17 * A Revision object, false if no revision exists, null if not loaded yet
19 private $oldRevision;
21 protected function setParameter( $par ) {
22 $par = $this->getRequest()->getVal( 'pagetitle', $par );
23 $title = Title::newFromText( $par );
24 if ( $title ) {
25 $this->title = $title;
26 $this->par = $title->getPrefixedText();
27 } else {
28 $this->par = '';
32 protected function getDisplayFormat() {
33 return 'ooui';
36 protected function alterForm( HTMLForm $form ) {
37 if ( !$this->title ) {
38 $form->setMethod( 'GET' );
42 public function validateTitle( $title ) {
43 if ( !$title ) {
44 // No form input yet
45 return true;
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() ) )
59 ->escaped();
63 return true;
66 protected function getFormFields() {
67 $that = $this;
68 $fields = array(
69 'pagetitle' => array(
70 'type' => 'title',
71 'creatable' => true,
72 'name' => 'pagetitle',
73 'default' => $this->par,
74 'label-message' => 'changecontentmodel-title-label',
75 'validation-callback' => array( $this, 'validateTitle' ),
78 if ( $this->title ) {
79 $fields['pagetitle']['readonly'] = true;
80 $fields += array(
81 'model' => array(
82 'type' => 'select',
83 'name' => 'model',
84 'options' => $this->getOptionsForTitle( $this->title ),
85 'label-message' => 'changecontentmodel-model-label'
87 'reason' => array(
88 'type' => 'text',
89 'name' => 'reason',
90 'validation-callback' => function( $reason ) use ( $that ) {
91 $match = EditPage::matchSummarySpamRegex( $reason );
92 if ( $match ) {
93 return $that->msg( 'spamprotectionmatch', $match )->parse();
96 return true;
98 'label-message' => 'changecontentmodel-reason-label',
103 return $fields;
106 private function getOptionsForTitle( Title $title = null ) {
107 $models = ContentHandler::getContentModels();
108 $options = array();
109 foreach ( $models as $model ) {
110 $handler = ContentHandler::getForModelID( $model );
111 if ( !$handler->supportsDirectEditing() ) {
112 continue;
114 if ( $title ) {
115 if ( $title->getContentModel() === $model ) {
116 continue;
118 if ( !$handler->canBeUsedOn( $title ) ) {
119 continue;
122 $options[ContentHandler::getLocalizedName( $model )] = $model;
125 return $options;
128 public function onSubmit( array $data ) {
129 global $wgContLang;
131 if ( $data['pagetitle'] === '' ) {
132 // Initial form view of special page, pass
133 return false;
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 ) );
147 if ( $errors ) {
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();
161 try {
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' )
168 ->params(
169 $this->title->getPrefixedText(),
170 ContentHandler::getLocalizedName( $data['model'] )
174 } else {
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']
190 ) );
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(
202 $newContent,
203 $reason,
204 $flags,
205 $this->oldRevision ? $this->oldRevision->getId() : false,
206 $user
208 if ( !$status->isOK() ) {
209 return $status;
212 $logid = $log->insert();
213 $log->publish( $logid );
215 return $status;
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
236 return array();
238 // Autocomplete subpage the same as a normal search
239 $prefixSearcher = new StringPrefixSearch;
240 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
241 return $result;
244 protected function getGroupName() {
245 return 'pagetools';