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