Declare dynamic properties
[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 'currentmodel' => [
110 'type' => 'text',
111 'name' => 'currentcontentmodel',
112 'default' => $this->title->getContentModel(),
113 'label-message' => 'changecontentmodel-current-label',
114 'readonly' => true
115 ],
116 'model' => [
117 'type' => 'select',
118 'name' => 'model',
119 'options' => $options,
120 'label-message' => 'changecontentmodel-model-label'
121 ],
122 'reason' => [
123 'type' => 'text',
124 'name' => 'reason',
125 'validation-callback' => function ( $reason ) {
126 $match = EditPage::matchSummarySpamRegex( $reason );
127 if ( $match ) {
128 return $this->msg( 'spamprotectionmatch', $match )->parse();
129 }
130
131 return true;
132 },
133 'label-message' => 'changecontentmodel-reason-label',
134 ],
135 ];
136 }
137
138 return $fields;
139 }
140
141 private function getOptionsForTitle( Title $title = null ) {
142 $models = ContentHandler::getContentModels();
143 $options = [];
144 foreach ( $models as $model ) {
145 $handler = ContentHandler::getForModelID( $model );
146 if ( !$handler->supportsDirectEditing() ) {
147 continue;
148 }
149 if ( $title ) {
150 if ( $title->getContentModel() === $model ) {
151 continue;
152 }
153 if ( !$handler->canBeUsedOn( $title ) ) {
154 continue;
155 }
156 }
157 $options[ContentHandler::getLocalizedName( $model )] = $model;
158 }
159
160 return $options;
161 }
162
163 public function onSubmit( array $data ) {
164 if ( $data['pagetitle'] === '' ) {
165 // Initial form view of special page, pass
166 return false;
167 }
168
169 // At this point, it has to be a POST request. This is enforced by HTMLForm,
170 // but lets be safe verify that.
171 if ( !$this->getRequest()->wasPosted() ) {
172 throw new RuntimeException( "Form submission was not POSTed" );
173 }
174
175 $this->title = Title::newFromText( $data['pagetitle'] );
176 $titleWithNewContentModel = clone $this->title;
177 $titleWithNewContentModel->setContentModel( $data['model'] );
178 $user = $this->getUser();
179 // Check permissions and make sure the user has permission to:
180 $errors = wfMergeErrorArrays(
181 // edit the contentmodel of the page
182 $this->title->getUserPermissionsErrors( 'editcontentmodel', $user ),
183 // edit the page under the old content model
184 $this->title->getUserPermissionsErrors( 'edit', $user ),
185 // edit the contentmodel under the new content model
186 $titleWithNewContentModel->getUserPermissionsErrors( 'editcontentmodel', $user ),
187 // edit the page under the new content model
188 $titleWithNewContentModel->getUserPermissionsErrors( 'edit', $user )
189 );
190 if ( $errors ) {
191 $out = $this->getOutput();
192 $wikitext = $out->formatPermissionsErrorMessage( $errors );
193 // Hack to get our wikitext parsed
194 return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
195 }
196
197 $page = WikiPage::factory( $this->title );
198 if ( $this->oldRevision === null ) {
199 $this->oldRevision = $page->getRevision() ?: false;
200 }
201 $oldModel = $this->title->getContentModel();
202 if ( $this->oldRevision ) {
203 $oldContent = $this->oldRevision->getContent();
204 try {
205 $newContent = ContentHandler::makeContent(
206 $oldContent->serialize(), $this->title, $data['model']
207 );
208 } catch ( MWException $e ) {
209 return Status::newFatal(
210 $this->msg( 'changecontentmodel-cannot-convert' )
211 ->params(
212 $this->title->getPrefixedText(),
213 ContentHandler::getLocalizedName( $data['model'] )
214 )
215 );
216 }
217 } else {
218 // Page doesn't exist, create an empty content object
219 $newContent = ContentHandler::getForModelID( $data['model'] )->makeEmptyContent();
220 }
221
222 // All other checks have passed, let's check rate limits
223 if ( $user->pingLimiter( 'editcontentmodel' ) ) {
224 throw new ThrottledError();
225 }
226
227 $flags = $this->oldRevision ? EDIT_UPDATE : EDIT_NEW;
228 $flags |= EDIT_INTERNAL;
229 if ( $user->isAllowed( 'bot' ) ) {
230 $flags |= EDIT_FORCE_BOT;
231 }
232
233 $log = new ManualLogEntry( 'contentmodel', $this->oldRevision ? 'change' : 'new' );
234 $log->setPerformer( $user );
235 $log->setTarget( $this->title );
236 $log->setComment( $data['reason'] );
237 $log->setParameters( [
238 '4::oldmodel' => $oldModel,
239 '5::newmodel' => $data['model']
240 ] );
241
242 $formatter = LogFormatter::newFromEntry( $log );
243 $formatter->setContext( RequestContext::newExtraneousContext( $this->title ) );
244 $reason = $formatter->getPlainActionText();
245 if ( $data['reason'] !== '' ) {
246 $reason .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $data['reason'];
247 }
248
249 // Run edit filters
250 $derivativeContext = new DerivativeContext( $this->getContext() );
251 $derivativeContext->setTitle( $this->title );
252 $derivativeContext->setWikiPage( $page );
253 $status = new Status();
254 if ( !Hooks::run( 'EditFilterMergedContent',
255 [ $derivativeContext, $newContent, $status, $reason,
256 $user, false ] )
257 ) {
258 if ( $status->isGood() ) {
259 // TODO: extensions should really specify an error message
260 $status->fatal( 'hookaborted' );
261 }
262 return $status;
263 }
264
265 $status = $page->doEditContent(
266 $newContent,
267 $reason,
268 $flags,
269 $this->oldRevision ? $this->oldRevision->getId() : false,
270 $user
271 );
272 if ( !$status->isOK() ) {
273 return $status;
274 }
275
276 $logid = $log->insert();
277 $log->publish( $logid );
278
279 return $status;
280 }
281
282 public function onSuccess() {
283 $out = $this->getOutput();
284 $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
285 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
286 }
287
288 /**
289 * Return an array of subpages beginning with $search that this special page will accept.
290 *
291 * @param string $search Prefix to search for
292 * @param int $limit Maximum number of results to return (usually 10)
293 * @param int $offset Number of results to skip (usually 0)
294 * @return string[] Matching subpages
295 */
296 public function prefixSearchSubpages( $search, $limit, $offset ) {
297 return $this->prefixSearchString( $search, $limit, $offset );
298 }
299
300 protected function getGroupName() {
301 return 'pagetools';
302 }
303 }