7647999813fbd2f929c3835a147a292f44917a78
[lhc/web/wiklou.git] / includes / specials / SpecialChangeContentModel.php
1 <?php
2
3 class SpecialChangeContentModel extends FormSpecialPage {
4
5 public function __construct() {
6 parent::__construct( 'ChangeContentModel', 'editcontentmodel' );
7 }
8
9 /**
10 * @var Title|null
11 */
12 private $title;
13
14 /**
15 * @var Revision|bool|null
16 *
17 * A Revision object, false if no revision exists, null if not loaded yet
18 */
19 private $oldRevision;
20
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 = '';
29 }
30 }
31
32 protected function getDisplayFormat() {
33 return 'ooui';
34 }
35
36 protected function alterForm( HTMLForm $form ) {
37 if ( !$this->title ) {
38 $form->setMethod( 'GET' );
39 }
40 }
41
42 public function validateTitle( $title ) {
43 if ( !$title ) {
44 // No form input yet
45 return true;
46 }
47 try {
48 $titleObj = Title::newFromTextThrow( $title );
49 } catch ( MalformedTitleException $e ) {
50 $msg = $this->msg( $e->getErrorMessage() );
51 $params = $e->getErrorMessageParameters();
52 if ( $params ) {
53 $msg->params( $params );
54 }
55 return $msg->parse();
56 }
57 if ( !$titleObj->canExist() ) {
58 return $this->msg(
59 'changecontentmodel-title-cantexist',
60 $titleObj->getPrefixedText()
61 )->escaped();
62 }
63
64 $this->oldRevision = Revision::newFromTitle( $titleObj ) ?: false;
65
66 if ( $this->oldRevision ) {
67 $oldContent = $this->oldRevision->getContent();
68 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
69 return $this->msg( 'changecontentmodel-nodirectediting' )
70 ->params( ContentHandler::getLocalizedName( $oldContent->getModel() ) )
71 ->escaped();
72 }
73 }
74
75 return true;
76 }
77
78 protected function getFormFields() {
79 $that = $this;
80 $fields = array(
81 'pagetitle' => array(
82 'type' => 'text',
83 'name' => 'pagetitle',
84 'default' => $this->par,
85 'label-message' => 'changecontentmodel-title-label',
86 'validation-callback' => array( $this, 'validateTitle' ),
87 ),
88 );
89 if ( $this->title ) {
90 $fields['pagetitle']['readonly'] = true;
91 $fields += array(
92 'model' => array(
93 'type' => 'select',
94 'name' => 'model',
95 'options' => $this->getOptionsForTitle( $this->title ),
96 'label-message' => 'changecontentmodel-model-label'
97 ),
98 'reason' => array(
99 'type' => 'text',
100 'name' => 'reason',
101 'validation-callback' => function( $reason ) use ( $that ) {
102 $match = EditPage::matchSummarySpamRegex( $reason );
103 if ( $match ) {
104 return $that->msg( 'spamprotectionmatch', $match )->parse();
105 }
106
107 return true;
108 },
109 'label-message' => 'changecontentmodel-reason-label',
110 ),
111 );
112 }
113
114 return $fields;
115 }
116
117 private function getOptionsForTitle( Title $title = null ) {
118 $models = ContentHandler::getContentModels();
119 $options = array();
120 foreach ( $models as $model ) {
121 $handler = ContentHandler::getForModelID( $model );
122 if ( !$handler->supportsDirectEditing() ) {
123 continue;
124 }
125 if ( $title ) {
126 if ( $title->getContentModel() === $model ) {
127 continue;
128 }
129 if ( !$handler->canBeUsedOn( $title ) ) {
130 continue;
131 }
132 }
133 $options[ContentHandler::getLocalizedName( $model )] = $model;
134 }
135
136 return $options;
137 }
138
139 public function onSubmit( array $data ) {
140 global $wgContLang;
141
142 if ( $data['pagetitle'] === '' ) {
143 // Initial form view of special page, pass
144 return false;
145 }
146
147 // At this point, it has to be a POST request. This is enforced by HTMLForm,
148 // but lets be safe verify that.
149 if ( !$this->getRequest()->wasPosted() ) {
150 throw new RuntimeException( "Form submission was not POSTed" );
151 }
152
153 $this->title = Title::newFromText( $data['pagetitle' ] );
154 $user = $this->getUser();
155 // Check permissions and make sure the user has permission to edit the specific page
156 $errors = $this->title->getUserPermissionsErrors( 'editcontentmodel', $user );
157 $errors = wfMergeErrorArrays( $errors, $this->title->getUserPermissionsErrors( 'edit', $user ) );
158 if ( $errors ) {
159 $out = $this->getOutput();
160 $wikitext = $out->formatPermissionsErrorMessage( $errors );
161 // Hack to get our wikitext parsed
162 return Status::newFatal( new RawMessage( '$1', array( $wikitext ) ) );
163 }
164
165 $page = WikiPage::factory( $this->title );
166 if ( $this->oldRevision === null ) {
167 $this->oldRevision = $page->getRevision() ?: false;
168 }
169 $oldModel = $this->title->getContentModel();
170 if ( $this->oldRevision ) {
171 $oldContent = $this->oldRevision->getContent();
172 try {
173 $newContent = ContentHandler::makeContent(
174 $oldContent->getNativeData(), $this->title, $data['model']
175 );
176 } catch ( MWException $e ) {
177 return Status::newFatal(
178 $this->msg( 'changecontentmodel-cannot-convert' )
179 ->params(
180 $this->title->getPrefixedText(),
181 ContentHandler::getLocalizedName( $data['model'] )
182 )
183 );
184 }
185 } else {
186 // Page doesn't exist, create an empty content object
187 $newContent = ContentHandler::getForModelID( $data['model'] )->makeEmptyContent();
188 }
189 $flags = $this->oldRevision ? EDIT_UPDATE : EDIT_NEW;
190 if ( $user->isAllowed( 'bot' ) ) {
191 $flags |= EDIT_FORCE_BOT;
192 }
193
194 $log = new ManualLogEntry( 'contentmodel', 'change' );
195 $log->setPerformer( $user );
196 $log->setTarget( $this->title );
197 $log->setComment( $data['reason'] );
198 $log->setParameters( array(
199 '4::oldmodel' => $oldModel,
200 '5::newmodel' => $data['model']
201 ) );
202
203 $formatter = LogFormatter::newFromEntry( $log );
204 $formatter->setContext( RequestContext::newExtraneousContext( $this->title ) );
205 $reason = $formatter->getPlainActionText();
206 if ( $data['reason'] !== '' ) {
207 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
208 }
209 # Truncate for whole multibyte characters.
210 $reason = $wgContLang->truncate( $reason, 255 );
211
212 $status = $page->doEditContent(
213 $newContent,
214 $reason,
215 $flags,
216 $this->oldRevision ? $this->oldRevision->getId() : false,
217 $user
218 );
219 if ( !$status->isOK() ) {
220 return $status;
221 }
222
223 $logid = $log->insert();
224 $log->publish( $logid );
225
226 return $status;
227 }
228
229 public function onSuccess() {
230 $out = $this->getOutput();
231 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
232 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
233 }
234 }