Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 124
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialChangeContentModel
0.00% covered (danger)
0.00%
0 / 123
0.00% covered (danger)
0.00%
0 / 13
930
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setParameter
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 postHtml
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getDisplayFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 validateTitle
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 getFormFields
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
42
 getOptionsForTitle
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 onSubmit
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 onSuccess
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 prefixSearchSubpages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Specials;
4
5use MediaWiki\Collation\CollationFactory;
6use MediaWiki\CommentStore\CommentStore;
7use MediaWiki\Content\ContentHandler;
8use MediaWiki\Content\IContentHandlerFactory;
9use MediaWiki\EditPage\SpamChecker;
10use MediaWiki\Exception\ErrorPageError;
11use MediaWiki\Html\Html;
12use MediaWiki\HTMLForm\HTMLForm;
13use MediaWiki\Language\RawMessage;
14use MediaWiki\Logging\LogEventsList;
15use MediaWiki\Logging\LogPage;
16use MediaWiki\Page\ContentModelChangeFactory;
17use MediaWiki\Page\WikiPageFactory;
18use MediaWiki\Revision\RevisionLookup;
19use MediaWiki\Revision\RevisionRecord;
20use MediaWiki\Revision\SlotRecord;
21use MediaWiki\SpecialPage\FormSpecialPage;
22use MediaWiki\Status\Status;
23use MediaWiki\Title\Title;
24use SearchEngineFactory;
25
26/**
27 * @ingroup SpecialPage
28 */
29class SpecialChangeContentModel extends FormSpecialPage {
30
31    private IContentHandlerFactory $contentHandlerFactory;
32    private ContentModelChangeFactory $contentModelChangeFactory;
33    private SpamChecker $spamChecker;
34    private RevisionLookup $revisionLookup;
35    private WikiPageFactory $wikiPageFactory;
36    private SearchEngineFactory $searchEngineFactory;
37    private CollationFactory $collationFactory;
38
39    public function __construct(
40        IContentHandlerFactory $contentHandlerFactory,
41        ContentModelChangeFactory $contentModelChangeFactory,
42        SpamChecker $spamChecker,
43        RevisionLookup $revisionLookup,
44        WikiPageFactory $wikiPageFactory,
45        SearchEngineFactory $searchEngineFactory,
46        CollationFactory $collationFactory
47    ) {
48        parent::__construct( 'ChangeContentModel', 'editcontentmodel' );
49
50        $this->contentHandlerFactory = $contentHandlerFactory;
51        $this->contentModelChangeFactory = $contentModelChangeFactory;
52        $this->spamChecker = $spamChecker;
53        $this->revisionLookup = $revisionLookup;
54        $this->wikiPageFactory = $wikiPageFactory;
55        $this->searchEngineFactory = $searchEngineFactory;
56        $this->collationFactory = $collationFactory;
57    }
58
59    /** @inheritDoc */
60    public function doesWrites() {
61        return true;
62    }
63
64    /**
65     * @var Title|null
66     */
67    private $title;
68
69    /**
70     * @var RevisionRecord|bool|null
71     *
72     * A RevisionRecord object, false if no revision exists, null if not loaded yet
73     */
74    private $oldRevision;
75
76    /** @inheritDoc */
77    protected function setParameter( $par ) {
78        $par = $this->getRequest()->getVal( 'pagetitle', $par );
79        $title = Title::newFromText( $par );
80        if ( $title ) {
81            $this->title = $title;
82            $this->par = $title->getPrefixedText();
83        } else {
84            $this->par = '';
85        }
86    }
87
88    /** @inheritDoc */
89    protected function postHtml() {
90        $text = '';
91        if ( $this->title ) {
92            $contentModelLogPage = new LogPage( 'contentmodel' );
93            $text = Html::element( 'h2', [], $contentModelLogPage->getName()->text() );
94            $out = '';
95            LogEventsList::showLogExtract( $out, 'contentmodel', $this->title );
96            $text .= $out;
97        }
98        return $text;
99    }
100
101    /** @inheritDoc */
102    protected function getDisplayFormat() {
103        return 'ooui';
104    }
105
106    protected function alterForm( HTMLForm $form ) {
107        $this->addHelpLink( 'Help:ChangeContentModel' );
108
109        if ( $this->title ) {
110            $form->setFormIdentifier( 'modelform' );
111        } else {
112            $form->setFormIdentifier( 'titleform' );
113        }
114
115        // T120576
116        $form->setSubmitTextMsg( 'changecontentmodel-submit' );
117
118        if ( $this->title ) {
119            $this->getOutput()->addBacklinkSubtitle( $this->title );
120        }
121    }
122
123    /**
124     * @param string $title
125     * @return string|bool
126     */
127    private function validateTitle( $title ) {
128        // Already validated by HTMLForm, but if not, throw
129        // an exception instead of a fatal
130        $titleObj = Title::newFromTextThrow( $title );
131
132        $this->oldRevision = $this->revisionLookup->getRevisionByTitle( $titleObj ) ?: false;
133
134        if ( $this->oldRevision ) {
135            $oldContent = $this->oldRevision->getContent( SlotRecord::MAIN );
136            if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
137                return $this->msg( 'changecontentmodel-nodirectediting' )
138                    ->params( ContentHandler::getLocalizedName( $oldContent->getModel() ) )
139                    ->escaped();
140            }
141        }
142
143        return true;
144    }
145
146    /** @inheritDoc */
147    protected function getFormFields() {
148        $fields = [
149            'pagetitle' => [
150                'type' => 'title',
151                'creatable' => true,
152                'name' => 'pagetitle',
153                'default' => $this->par,
154                'label-message' => 'changecontentmodel-title-label',
155                'validation-callback' => $this->validateTitle( ... ),
156            ],
157        ];
158        if ( $this->title ) {
159            $options = $this->getOptionsForTitle( $this->title );
160            if ( !$options ) {
161                throw new ErrorPageError(
162                    'changecontentmodel-emptymodels-title',
163                    'changecontentmodel-emptymodels-text',
164                    [ $this->title->getPrefixedText() ]
165                );
166            }
167            $fields['pagetitle']['readonly'] = true;
168            $fields += [
169                'model' => [
170                    'type' => 'select',
171                    'name' => 'model',
172                    'default' => $this->title->getContentModel(),
173                    'options' => $options,
174                    'label-message' => 'changecontentmodel-model-label'
175                ],
176                'reason' => [
177                    'type' => 'text',
178                    'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
179                    'name' => 'reason',
180                    'validation-callback' => function ( $reason ) {
181                        if ( $reason === null || $reason === '' ) {
182                            // Null on form display, or no reason given
183                            return true;
184                        }
185
186                        $match = $this->spamChecker->checkSummary( $reason );
187
188                        if ( $match ) {
189                            return $this->msg( 'spamprotectionmatch', $match )->parse();
190                        }
191
192                        return true;
193                    },
194                    'label-message' => 'changecontentmodel-reason-label',
195                ],
196            ];
197        }
198
199        return $fields;
200    }
201
202    /**
203     * @return array $options An array of data for an OOUI drop-down list. The array keys
204     * correspond to the human readable text in the drop-down list. The array values
205     * correspond to the <option value="">.
206     */
207    private function getOptionsForTitle( ?Title $title = null ) {
208        $models = $this->contentHandlerFactory->getContentModels();
209        $options = [];
210        foreach ( $models as $model ) {
211            $handler = $this->contentHandlerFactory->getContentHandler( $model );
212            if ( !$handler->supportsDirectEditing() ) {
213                continue;
214            }
215            if ( $title ) {
216                if ( !$handler->canBeUsedOn( $title ) ) {
217                    continue;
218                }
219            }
220            $options[ContentHandler::getLocalizedName( $model )] = $model;
221        }
222
223        // Put the options in the drop-down list in alphabetical order.
224        // Sort by array key, case insensitive.
225        $collation = $this->collationFactory->getCategoryCollation();
226        uksort( $options, static function ( $a, $b ) use ( $collation ) {
227            $a = $collation->getSortKey( $a );
228            $b = $collation->getSortKey( $b );
229            return strcmp( $a, $b );
230        } );
231
232        return $options;
233    }
234
235    /** @inheritDoc */
236    public function onSubmit( array $data ) {
237        $this->title = Title::newFromText( $data['pagetitle'] );
238        $page = $this->wikiPageFactory->newFromTitle( $this->title );
239
240        $changer = $this->contentModelChangeFactory->newContentModelChange(
241                $this->getContext()->getAuthority(),
242                $page,
243                $data['model']
244            );
245
246        $permissionStatus = $changer->authorizeChange();
247        if ( !$permissionStatus->isGood() ) {
248            $out = $this->getOutput();
249            $wikitext = $out->formatPermissionStatus( $permissionStatus );
250            // Hack to get our wikitext parsed
251            return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
252        }
253
254        $status = $changer->doContentModelChange(
255            $this->getContext(),
256            $data['reason'],
257            true
258        );
259
260        return $status;
261    }
262
263    public function onSuccess() {
264        $out = $this->getOutput();
265        $out->setPageTitleMsg( $this->msg( 'changecontentmodel-success-title' ) );
266        $out->addWikiMsg( 'changecontentmodel-success-text', $this->title->getPrefixedText() );
267    }
268
269    /**
270     * Return an array of subpages beginning with $search that this special page will accept.
271     *
272     * @param string $search Prefix to search for
273     * @param int $limit Maximum number of results to return (usually 10)
274     * @param int $offset Number of results to skip (usually 0)
275     * @return string[] Matching subpages
276     */
277    public function prefixSearchSubpages( $search, $limit, $offset ) {
278        return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
279    }
280
281    /** @inheritDoc */
282    protected function getGroupName() {
283        return 'pagetools';
284    }
285}
286
287/** @deprecated class alias since 1.41 */
288class_alias( SpecialChangeContentModel::class, 'SpecialChangeContentModel' );