3 namespace MediaWiki\Specials
;
8 use MediaWiki\Collation\CollationFactory
;
9 use MediaWiki\CommentStore\CommentStore
;
10 use MediaWiki\Content\ContentHandler
;
11 use MediaWiki\Content\IContentHandlerFactory
;
12 use MediaWiki\EditPage\SpamChecker
;
13 use MediaWiki\HTMLForm\HTMLForm
;
14 use MediaWiki\Language\RawMessage
;
15 use MediaWiki\Page\ContentModelChangeFactory
;
16 use MediaWiki\Page\WikiPageFactory
;
17 use MediaWiki\Revision\RevisionLookup
;
18 use MediaWiki\Revision\RevisionRecord
;
19 use MediaWiki\Revision\SlotRecord
;
20 use MediaWiki\SpecialPage\FormSpecialPage
;
21 use MediaWiki\Status\Status
;
22 use MediaWiki\Title\Title
;
23 use MediaWiki\Xml\Xml
;
24 use SearchEngineFactory
;
27 * @ingroup SpecialPage
29 class SpecialChangeContentModel
extends FormSpecialPage
{
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;
40 * @param IContentHandlerFactory $contentHandlerFactory
41 * @param ContentModelChangeFactory $contentModelChangeFactory
42 * @param SpamChecker $spamChecker
43 * @param RevisionLookup $revisionLookup
44 * @param WikiPageFactory $wikiPageFactory
45 * @param SearchEngineFactory $searchEngineFactory
46 * @param CollationFactory $collationFactory
48 public function __construct(
49 IContentHandlerFactory
$contentHandlerFactory,
50 ContentModelChangeFactory
$contentModelChangeFactory,
51 SpamChecker
$spamChecker,
52 RevisionLookup
$revisionLookup,
53 WikiPageFactory
$wikiPageFactory,
54 SearchEngineFactory
$searchEngineFactory,
55 CollationFactory
$collationFactory
57 parent
::__construct( 'ChangeContentModel', 'editcontentmodel' );
59 $this->contentHandlerFactory
= $contentHandlerFactory;
60 $this->contentModelChangeFactory
= $contentModelChangeFactory;
61 $this->spamChecker
= $spamChecker;
62 $this->revisionLookup
= $revisionLookup;
63 $this->wikiPageFactory
= $wikiPageFactory;
64 $this->searchEngineFactory
= $searchEngineFactory;
65 $this->collationFactory
= $collationFactory;
68 public function doesWrites() {
78 * @var RevisionRecord|bool|null
80 * A RevisionRecord object, false if no revision exists, null if not loaded yet
84 protected function setParameter( $par ) {
85 $par = $this->getRequest()->getVal( 'pagetitle', $par );
86 $title = Title
::newFromText( $par );
88 $this->title
= $title;
89 $this->par
= $title->getPrefixedText();
95 protected function postHtml() {
98 $contentModelLogPage = new LogPage( 'contentmodel' );
99 $text = Xml
::element( 'h2', null, $contentModelLogPage->getName()->text() );
101 LogEventsList
::showLogExtract( $out, 'contentmodel', $this->title
);
107 protected function getDisplayFormat() {
111 protected function alterForm( HTMLForm
$form ) {
112 $this->addHelpLink( 'Help:ChangeContentModel' );
114 if ( $this->title
) {
115 $form->setFormIdentifier( 'modelform' );
117 $form->setFormIdentifier( 'titleform' );
121 $form->setSubmitTextMsg( 'changecontentmodel-submit' );
123 if ( $this->title
) {
124 $this->getOutput()->addBacklinkSubtitle( $this->title
);
128 public function validateTitle( $title ) {
129 // Already validated by HTMLForm, but if not, throw
130 // an exception instead of a fatal
131 $titleObj = Title
::newFromTextThrow( $title );
133 $this->oldRevision
= $this->revisionLookup
->getRevisionByTitle( $titleObj ) ?
: false;
135 if ( $this->oldRevision
) {
136 $oldContent = $this->oldRevision
->getContent( SlotRecord
::MAIN
);
137 if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
138 return $this->msg( 'changecontentmodel-nodirectediting' )
139 ->params( ContentHandler
::getLocalizedName( $oldContent->getModel() ) )
147 protected function getFormFields() {
152 'name' => 'pagetitle',
153 'default' => $this->par
,
154 'label-message' => 'changecontentmodel-title-label',
155 'validation-callback' => [ $this, 'validateTitle' ],
158 if ( $this->title
) {
159 $options = $this->getOptionsForTitle( $this->title
);
161 throw new ErrorPageError(
162 'changecontentmodel-emptymodels-title',
163 'changecontentmodel-emptymodels-text',
164 [ $this->title
->getPrefixedText() ]
167 $fields['pagetitle']['readonly'] = true;
172 'default' => $this->title
->getContentModel(),
173 'options' => $options,
174 'label-message' => 'changecontentmodel-model-label'
178 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
,
180 'validation-callback' => function ( $reason ) {
181 if ( $reason === null ||
$reason === '' ) {
182 // Null on form display, or no reason given
186 $match = $this->spamChecker
->checkSummary( $reason );
189 return $this->msg( 'spamprotectionmatch', $match )->parse();
194 'label-message' => 'changecontentmodel-reason-label',
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="">.
207 private function getOptionsForTitle( ?Title
$title = null ) {
208 $models = $this->contentHandlerFactory
->getContentModels();
210 foreach ( $models as $model ) {
211 $handler = $this->contentHandlerFactory
->getContentHandler( $model );
212 if ( !$handler->supportsDirectEditing() ) {
216 if ( !$handler->canBeUsedOn( $title ) ) {
220 $options[ContentHandler
::getLocalizedName( $model )] = $model;
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 );
235 public function onSubmit( array $data ) {
236 $this->title
= Title
::newFromText( $data['pagetitle'] );
237 $page = $this->wikiPageFactory
->newFromTitle( $this->title
);
239 $changer = $this->contentModelChangeFactory
->newContentModelChange(
240 $this->getContext()->getAuthority(),
245 $permissionStatus = $changer->authorizeChange();
246 if ( !$permissionStatus->isGood() ) {
247 $out = $this->getOutput();
248 $wikitext = $out->formatPermissionStatus( $permissionStatus );
249 // Hack to get our wikitext parsed
250 return Status
::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
253 $status = $changer->doContentModelChange(
262 public function onSuccess() {
263 $out = $this->getOutput();
264 $out->setPageTitleMsg( $this->msg( 'changecontentmodel-success-title' ) );
265 $out->addWikiMsg( 'changecontentmodel-success-text', $this->title
);
269 * Return an array of subpages beginning with $search that this special page will accept.
271 * @param string $search Prefix to search for
272 * @param int $limit Maximum number of results to return (usually 10)
273 * @param int $offset Number of results to skip (usually 0)
274 * @return string[] Matching subpages
276 public function prefixSearchSubpages( $search, $limit, $offset ) {
277 return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory
);
280 protected function getGroupName() {
285 /** @deprecated class alias since 1.41 */
286 class_alias( SpecialChangeContentModel
::class, 'SpecialChangeContentModel' );