AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / SpecialChangeContentModel.php
blobccbb2752ba1f1a1f127e5c04bdbe232aedf380d8
1 <?php
3 class SpecialChangeContentModel extends FormSpecialPage {
5 public function __construct() {
6 parent::__construct( 'ChangeContentModel', 'editcontentmodel' );
9 public function doesWrites() {
10 return true;
13 /**
14 * @var Title|null
16 private $title;
18 /**
19 * @var Revision|bool|null
21 * A Revision object, false if no revision exists, null if not loaded yet
23 private $oldRevision;
25 protected function setParameter( $par ) {
26 $par = $this->getRequest()->getVal( 'pagetitle', $par );
27 $title = Title::newFromText( $par );
28 if ( $title ) {
29 $this->title = $title;
30 $this->par = $title->getPrefixedText();
31 } else {
32 $this->par = '';
36 protected function getDisplayFormat() {
37 return 'ooui';
40 protected function alterForm( HTMLForm $form ) {
41 if ( !$this->title ) {
42 $form->setMethod( 'GET' );
45 $this->addHelpLink( 'Help:ChangeContentModel' );
47 // T120576
48 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
51 public function validateTitle( $title ) {
52 if ( !$title ) {
53 // No form input yet
54 return true;
57 // Already validated by HTMLForm, but if not, throw
58 // and exception instead of a fatal
59 $titleObj = Title::newFromTextThrow( $title );
61 $this->oldRevision = Revision::newFromTitle( $titleObj ) ?: false;
63 if ( $this->oldRevision ) {
64 $oldContent = $this->oldRevision->getContent();
65 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
66 return $this->msg( 'changecontentmodel-nodirectediting' )
67 ->params( ContentHandler::getLocalizedName( $oldContent->getModel() ) )
68 ->escaped();
72 return true;
75 protected function getFormFields() {
76 $fields = [
77 'pagetitle' => [
78 'type' => 'title',
79 'creatable' => true,
80 'name' => 'pagetitle',
81 'default' => $this->par,
82 'label-message' => 'changecontentmodel-title-label',
83 'validation-callback' => [ $this, 'validateTitle' ],
86 if ( $this->title ) {
87 $options = $this->getOptionsForTitle( $this->title );
88 if ( empty( $options ) ) {
89 throw new ErrorPageError(
90 'changecontentmodel-emptymodels-title',
91 'changecontentmodel-emptymodels-text',
92 $this->title->getPrefixedText()
95 $fields['pagetitle']['readonly'] = true;
96 $fields += [
97 'model' => [
98 'type' => 'select',
99 'name' => 'model',
100 'options' => $options,
101 'label-message' => 'changecontentmodel-model-label'
103 'reason' => [
104 'type' => 'text',
105 'name' => 'reason',
106 'validation-callback' => function( $reason ) {
107 $match = EditPage::matchSummarySpamRegex( $reason );
108 if ( $match ) {
109 return $this->msg( 'spamprotectionmatch', $match )->parse();
112 return true;
114 'label-message' => 'changecontentmodel-reason-label',
119 return $fields;
122 private function getOptionsForTitle( Title $title = null ) {
123 $models = ContentHandler::getContentModels();
124 $options = [];
125 foreach ( $models as $model ) {
126 $handler = ContentHandler::getForModelID( $model );
127 if ( !$handler->supportsDirectEditing() ) {
128 continue;
130 if ( $title ) {
131 if ( $title->getContentModel() === $model ) {
132 continue;
134 if ( !$handler->canBeUsedOn( $title ) ) {
135 continue;
138 $options[ContentHandler::getLocalizedName( $model )] = $model;
141 return $options;
144 public function onSubmit( array $data ) {
145 global $wgContLang;
147 if ( $data['pagetitle'] === '' ) {
148 // Initial form view of special page, pass
149 return false;
152 // At this point, it has to be a POST request. This is enforced by HTMLForm,
153 // but lets be safe verify that.
154 if ( !$this->getRequest()->wasPosted() ) {
155 throw new RuntimeException( "Form submission was not POSTed" );
158 $this->title = Title::newFromText( $data['pagetitle'] );
159 $user = $this->getUser();
160 // Check permissions and make sure the user has permission to edit the specific page
161 $errors = $this->title->getUserPermissionsErrors( 'editcontentmodel', $user );
162 $errors = wfMergeErrorArrays( $errors, $this->title->getUserPermissionsErrors( 'edit', $user ) );
163 if ( $errors ) {
164 $out = $this->getOutput();
165 $wikitext = $out->formatPermissionsErrorMessage( $errors );
166 // Hack to get our wikitext parsed
167 return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
170 $page = WikiPage::factory( $this->title );
171 if ( $this->oldRevision === null ) {
172 $this->oldRevision = $page->getRevision() ?: false;
174 $oldModel = $this->title->getContentModel();
175 if ( $this->oldRevision ) {
176 $oldContent = $this->oldRevision->getContent();
177 try {
178 $newContent = ContentHandler::makeContent(
179 $oldContent->getNativeData(), $this->title, $data['model']
181 } catch ( MWException $e ) {
182 return Status::newFatal(
183 $this->msg( 'changecontentmodel-cannot-convert' )
184 ->params(
185 $this->title->getPrefixedText(),
186 ContentHandler::getLocalizedName( $data['model'] )
190 } else {
191 // Page doesn't exist, create an empty content object
192 $newContent = ContentHandler::getForModelID( $data['model'] )->makeEmptyContent();
194 $flags = $this->oldRevision ? EDIT_UPDATE : EDIT_NEW;
195 $flags |= EDIT_INTERNAL;
196 if ( $user->isAllowed( 'bot' ) ) {
197 $flags |= EDIT_FORCE_BOT;
200 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ? 'change' : 'new' );
201 $log->setPerformer( $user );
202 $log->setTarget( $this->title );
203 $log->setComment( $data['reason'] );
204 $log->setParameters( [
205 '4::oldmodel' => $oldModel,
206 '5::newmodel' => $data['model']
207 ] );
209 $formatter = LogFormatter::newFromEntry( $log );
210 $formatter->setContext( RequestContext::newExtraneousContext( $this->title ) );
211 $reason = $formatter->getPlainActionText();
212 if ( $data['reason'] !== '' ) {
213 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
215 # Truncate for whole multibyte characters.
216 $reason = $wgContLang->truncate( $reason, 255 );
218 $status = $page->doEditContent(
219 $newContent,
220 $reason,
221 $flags,
222 $this->oldRevision ? $this->oldRevision->getId() : false,
223 $user
225 if ( !$status->isOK() ) {
226 return $status;
229 $logid = $log->insert();
230 $log->publish( $logid );
232 return $status;
235 public function onSuccess() {
236 $out = $this->getOutput();
237 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
238 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
242 * Return an array of subpages beginning with $search that this special page will accept.
244 * @param string $search Prefix to search for
245 * @param int $limit Maximum number of results to return (usually 10)
246 * @param int $offset Number of results to skip (usually 0)
247 * @return string[] Matching subpages
249 public function prefixSearchSubpages( $search, $limit, $offset ) {
250 return $this->prefixSearchString( $search, $limit, $offset );
253 protected function getGroupName() {
254 return 'pagetools';