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