Special case opus mime detction
[mediawiki.git] / includes / specials / SpecialChangeContentModel.php
bloba36b4148bc70e93506b575c0ddc7a24503b8bc71
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 postText() {
37 $text = '';
38 if ( $this->title ) {
39 $contentModelLogPage = new LogPage( 'contentmodel' );
40 $text = Xml::element( 'h2', null, $contentModelLogPage->getName()->text() );
41 $out = '';
42 LogEventsList::showLogExtract( $out, 'contentmodel', $this->title );
43 $text .= $out;
45 return $text;
48 protected function getDisplayFormat() {
49 return 'ooui';
52 protected function alterForm( HTMLForm $form ) {
53 if ( !$this->title ) {
54 $form->setMethod( 'GET' );
57 $this->addHelpLink( 'Help:ChangeContentModel' );
59 // T120576
60 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
63 public function validateTitle( $title ) {
64 if ( !$title ) {
65 // No form input yet
66 return true;
69 // Already validated by HTMLForm, but if not, throw
70 // and exception instead of a fatal
71 $titleObj = Title::newFromTextThrow( $title );
73 $this->oldRevision = Revision::newFromTitle( $titleObj ) ?: false;
75 if ( $this->oldRevision ) {
76 $oldContent = $this->oldRevision->getContent();
77 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
78 return $this->msg( 'changecontentmodel-nodirectediting' )
79 ->params( ContentHandler::getLocalizedName( $oldContent->getModel() ) )
80 ->escaped();
84 return true;
87 protected function getFormFields() {
88 $fields = [
89 'pagetitle' => [
90 'type' => 'title',
91 'creatable' => true,
92 'name' => 'pagetitle',
93 'default' => $this->par,
94 'label-message' => 'changecontentmodel-title-label',
95 'validation-callback' => [ $this, 'validateTitle' ],
98 if ( $this->title ) {
99 $options = $this->getOptionsForTitle( $this->title );
100 if ( empty( $options ) ) {
101 throw new ErrorPageError(
102 'changecontentmodel-emptymodels-title',
103 'changecontentmodel-emptymodels-text',
104 $this->title->getPrefixedText()
107 $fields['pagetitle']['readonly'] = true;
108 $fields += [
109 'model' => [
110 'type' => 'select',
111 'name' => 'model',
112 'options' => $options,
113 'label-message' => 'changecontentmodel-model-label'
115 'reason' => [
116 'type' => 'text',
117 'name' => 'reason',
118 'validation-callback' => function( $reason ) {
119 $match = EditPage::matchSummarySpamRegex( $reason );
120 if ( $match ) {
121 return $this->msg( 'spamprotectionmatch', $match )->parse();
124 return true;
126 'label-message' => 'changecontentmodel-reason-label',
131 return $fields;
134 private function getOptionsForTitle( Title $title = null ) {
135 $models = ContentHandler::getContentModels();
136 $options = [];
137 foreach ( $models as $model ) {
138 $handler = ContentHandler::getForModelID( $model );
139 if ( !$handler->supportsDirectEditing() ) {
140 continue;
142 if ( $title ) {
143 if ( $title->getContentModel() === $model ) {
144 continue;
146 if ( !$handler->canBeUsedOn( $title ) ) {
147 continue;
150 $options[ContentHandler::getLocalizedName( $model )] = $model;
153 return $options;
156 public function onSubmit( array $data ) {
157 global $wgContLang;
159 if ( $data['pagetitle'] === '' ) {
160 // Initial form view of special page, pass
161 return false;
164 // At this point, it has to be a POST request. This is enforced by HTMLForm,
165 // but lets be safe verify that.
166 if ( !$this->getRequest()->wasPosted() ) {
167 throw new RuntimeException( "Form submission was not POSTed" );
170 $this->title = Title::newFromText( $data['pagetitle'] );
171 $titleWithNewContentModel = clone $this->title;
172 $titleWithNewContentModel->setContentModel( $data['model'] );
173 $user = $this->getUser();
174 // Check permissions and make sure the user has permission to:
175 $errors = wfMergeErrorArrays(
176 // edit the contentmodel of the page
177 $this->title->getUserPermissionsErrors( 'editcontentmodel', $user ),
178 // edit the page under the old content model
179 $this->title->getUserPermissionsErrors( 'edit', $user ),
180 // edit the contentmodel under the new content model
181 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
182 // edit the page under the new content model
183 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
185 if ( $errors ) {
186 $out = $this->getOutput();
187 $wikitext = $out->formatPermissionsErrorMessage( $errors );
188 // Hack to get our wikitext parsed
189 return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
192 $page = WikiPage::factory( $this->title );
193 if ( $this->oldRevision === null ) {
194 $this->oldRevision = $page->getRevision() ?: false;
196 $oldModel = $this->title->getContentModel();
197 if ( $this->oldRevision ) {
198 $oldContent = $this->oldRevision->getContent();
199 try {
200 $newContent = ContentHandler::makeContent(
201 $oldContent->getNativeData(), $this->title, $data['model']
203 } catch ( MWException $e ) {
204 return Status::newFatal(
205 $this->msg( 'changecontentmodel-cannot-convert' )
206 ->params(
207 $this->title->getPrefixedText(),
208 ContentHandler::getLocalizedName( $data['model'] )
212 } else {
213 // Page doesn't exist, create an empty content object
214 $newContent = ContentHandler::getForModelID( $data['model'] )->makeEmptyContent();
217 // All other checks have passed, let's check rate limits
218 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
219 throw new ThrottledError();
222 $flags = $this->oldRevision ? EDIT_UPDATE : EDIT_NEW;
223 $flags |= EDIT_INTERNAL;
224 if ( $user->isAllowed( 'bot' ) ) {
225 $flags |= EDIT_FORCE_BOT;
228 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ? 'change' : 'new' );
229 $log->setPerformer( $user );
230 $log->setTarget( $this->title );
231 $log->setComment( $data['reason'] );
232 $log->setParameters( [
233 '4::oldmodel' => $oldModel,
234 '5::newmodel' => $data['model']
235 ] );
237 $formatter = LogFormatter::newFromEntry( $log );
238 $formatter->setContext( RequestContext::newExtraneousContext( $this->title ) );
239 $reason = $formatter->getPlainActionText();
240 if ( $data['reason'] !== '' ) {
241 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
243 # Truncate for whole multibyte characters.
244 $reason = $wgContLang->truncate( $reason, 255 );
246 // Run edit filters
247 $derivativeContext = new DerivativeContext( $this->getContext() );
248 $derivativeContext->setTitle( $this->title );
249 $derivativeContext->setWikiPage( $page );
250 $status = new Status();
251 if ( !Hooks::run( 'EditFilterMergedContent',
252 [ $derivativeContext, $newContent, $status, $reason,
253 $user, false ] )
255 if ( $status->isGood() ) {
256 // TODO: extensions should really specify an error message
257 $status->fatal( 'hookaborted' );
259 return $status;
262 $status = $page->doEditContent(
263 $newContent,
264 $reason,
265 $flags,
266 $this->oldRevision ? $this->oldRevision->getId() : false,
267 $user
269 if ( !$status->isOK() ) {
270 return $status;
273 $logid = $log->insert();
274 $log->publish( $logid );
276 return $status;
279 public function onSuccess() {
280 $out = $this->getOutput();
281 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
282 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
286 * Return an array of subpages beginning with $search that this special page will accept.
288 * @param string $search Prefix to search for
289 * @param int $limit Maximum number of results to return (usually 10)
290 * @param int $offset Number of results to skip (usually 0)
291 * @return string[] Matching subpages
293 public function prefixSearchSubpages( $search, $limit, $offset ) {
294 return $this->prefixSearchString( $search, $limit, $offset );
297 protected function getGroupName() {
298 return 'pagetools';