Merge "Added a separate error message for mkdir failures"
[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 public function doesWrites() {
10 return true;
11 }
12
13 /**
14 * @var Title|null
15 */
16 private $title;
17
18 /**
19 * @var Revision|bool|null
20 *
21 * A Revision object, false if no revision exists, null if not loaded yet
22 */
23 private $oldRevision;
24
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 = '';
33 }
34 }
35
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;
44 }
45 return $text;
46 }
47
48 protected function getDisplayFormat() {
49 return 'ooui';
50 }
51
52 protected function alterForm( HTMLForm $form ) {
53 if ( !$this->title ) {
54 $form->setMethod( 'GET' );
55 }
56
57 $this->addHelpLink( 'Help:ChangeContentModel' );
58
59 // T120576
60 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
61 }
62
63 public function validateTitle( $title ) {
64 if ( !$title ) {
65 // No form input yet
66 return true;
67 }
68
69 // Already validated by HTMLForm, but if not, throw
70 // and exception instead of a fatal
71 $titleObj = Title::newFromTextThrow( $title );
72
73 $this->oldRevision = Revision::newFromTitle( $titleObj ) ?: false;
74
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();
81 }
82 }
83
84 return true;
85 }
86
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' ],
96 ],
97 ];
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()
105 );
106 }
107 $fields['pagetitle']['readonly'] = true;
108 $fields += [
109 'model' => [
110 'type' => 'select',
111 'name' => 'model',
112 'options' => $options,
113 'label-message' => 'changecontentmodel-model-label'
114 ],
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();
122 }
123
124 return true;
125 },
126 'label-message' => 'changecontentmodel-reason-label',
127 ],
128 ];
129 }
130
131 return $fields;
132 }
133
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;
141 }
142 if ( $title ) {
143 if ( $title->getContentModel() === $model ) {
144 continue;
145 }
146 if ( !$handler->canBeUsedOn( $title ) ) {
147 continue;
148 }
149 }
150 $options[ContentHandler::getLocalizedName( $model )] = $model;
151 }
152
153 return $options;
154 }
155
156 public function onSubmit( array $data ) {
157 global $wgContLang;
158
159 if ( $data['pagetitle'] === '' ) {
160 // Initial form view of special page, pass
161 return false;
162 }
163
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" );
168 }
169
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 )
184 );
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 ] ) );
190 }
191
192 $page = WikiPage::factory( $this->title );
193 if ( $this->oldRevision === null ) {
194 $this->oldRevision = $page->getRevision() ?: false;
195 }
196 $oldModel = $this->title->getContentModel();
197 if ( $this->oldRevision ) {
198 $oldContent = $this->oldRevision->getContent();
199 try {
200 $newContent = ContentHandler::makeContent(
201 $oldContent->serialize(), $this->title, $data['model']
202 );
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'] )
209 )
210 );
211 }
212 } else {
213 // Page doesn't exist, create an empty content object
214 $newContent = ContentHandler::getForModelID( $data['model'] )->makeEmptyContent();
215 }
216
217 // All other checks have passed, let's check rate limits
218 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
219 throw new ThrottledError();
220 }
221
222 $flags = $this->oldRevision ? EDIT_UPDATE : EDIT_NEW;
223 $flags |= EDIT_INTERNAL;
224 if ( $user->isAllowed( 'bot' ) ) {
225 $flags |= EDIT_FORCE_BOT;
226 }
227
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 ] );
236
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'];
242 }
243 # Truncate for whole multibyte characters.
244 $reason = $wgContLang->truncate( $reason, 255 );
245
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 ] )
254 ) {
255 if ( $status->isGood() ) {
256 // TODO: extensions should really specify an error message
257 $status->fatal( 'hookaborted' );
258 }
259 return $status;
260 }
261
262 $status = $page->doEditContent(
263 $newContent,
264 $reason,
265 $flags,
266 $this->oldRevision ? $this->oldRevision->getId() : false,
267 $user
268 );
269 if ( !$status->isOK() ) {
270 return $status;
271 }
272
273 $logid = $log->insert();
274 $log->publish( $logid );
275
276 return $status;
277 }
278
279 public function onSuccess() {
280 $out = $this->getOutput();
281 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
282 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
283 }
284
285 /**
286 * Return an array of subpages beginning with $search that this special page will accept.
287 *
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
292 */
293 public function prefixSearchSubpages( $search, $limit, $offset ) {
294 return $this->prefixSearchString( $search, $limit, $offset );
295 }
296
297 protected function getGroupName() {
298 return 'pagetools';
299 }
300 }