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