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