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