MediaWiki  master
SpecialChangeContentModel.php
Go to the documentation of this file.
1 <?php
2 
7 
9 
12 
18  parent::__construct( 'ChangeContentModel', 'editcontentmodel' );
19 
20  if ( !$contentHandlerFactory ) {
21  wfDeprecated( __METHOD__ . ' without $contentHandlerFactory parameter', '1.35' );
22  $contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory();
23  }
24  $this->contentHandlerFactory = $contentHandlerFactory;
25  }
26 
27  public function doesWrites() {
28  return true;
29  }
30 
34  private $title;
35 
41  private $oldRevision;
42 
43  protected function setParameter( $par ) {
44  $par = $this->getRequest()->getVal( 'pagetitle', $par );
46  if ( $title ) {
47  $this->title = $title;
48  $this->par = $title->getPrefixedText();
49  } else {
50  $this->par = '';
51  }
52  }
53 
54  protected function postText() {
55  $text = '';
56  if ( $this->title ) {
57  $contentModelLogPage = new LogPage( 'contentmodel' );
58  $text = Xml::element( 'h2', null, $contentModelLogPage->getName()->text() );
59  $out = '';
60  LogEventsList::showLogExtract( $out, 'contentmodel', $this->title );
61  $text .= $out;
62  }
63  return $text;
64  }
65 
66  protected function getDisplayFormat() {
67  return 'ooui';
68  }
69 
70  protected function alterForm( HTMLForm $form ) {
71  $this->addHelpLink( 'Help:ChangeContentModel' );
72 
73  if ( $this->title ) {
74  $form->setFormIdentifier( 'modelform' );
75  } else {
76  $form->setFormIdentifier( 'titleform' );
77  }
78 
79  // T120576
80  $form->setSubmitTextMsg( 'changecontentmodel-submit' );
81 
82  if ( $this->title ) {
83  $this->getOutput()->addBacklinkSubtitle( $this->title );
84  }
85  }
86 
87  public function validateTitle( $title ) {
88  // Already validated by HTMLForm, but if not, throw
89  // an exception instead of a fatal
90  $titleObj = Title::newFromTextThrow( $title );
91 
92  $this->oldRevision = MediaWikiServices::getInstance()
93  ->getRevisionLookup()
94  ->getRevisionByTitle( $titleObj ) ?: false;
95 
96  if ( $this->oldRevision ) {
97  $oldContent = $this->oldRevision->getContent( SlotRecord::MAIN );
98  if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
99  return $this->msg( 'changecontentmodel-nodirectediting' )
100  ->params( ContentHandler::getLocalizedName( $oldContent->getModel() ) )
101  ->escaped();
102  }
103  }
104 
105  return true;
106  }
107 
108  protected function getFormFields() {
109  $fields = [
110  'pagetitle' => [
111  'type' => 'title',
112  'creatable' => true,
113  'name' => 'pagetitle',
114  'default' => $this->par,
115  'label-message' => 'changecontentmodel-title-label',
116  'validation-callback' => [ $this, 'validateTitle' ],
117  ],
118  ];
119  if ( $this->title ) {
120  $spamChecker = MediaWikiServices::getInstance()->getSpamChecker();
121 
122  $options = $this->getOptionsForTitle( $this->title );
123  if ( empty( $options ) ) {
124  throw new ErrorPageError(
125  'changecontentmodel-emptymodels-title',
126  'changecontentmodel-emptymodels-text',
127  [ $this->title->getPrefixedText() ]
128  );
129  }
130  $fields['pagetitle']['readonly'] = true;
131  $fields += [
132  'currentmodel' => [
133  'type' => 'text',
134  'name' => 'currentcontentmodel',
135  'default' => $this->title->getContentModel(),
136  'label-message' => 'changecontentmodel-current-label',
137  'readonly' => true
138  ],
139  'model' => [
140  'type' => 'select',
141  'name' => 'model',
142  'options' => $options,
143  'label-message' => 'changecontentmodel-model-label'
144  ],
145  'reason' => [
146  'type' => 'text',
148  'name' => 'reason',
149  'validation-callback' => function ( $reason ) use ( $spamChecker ) {
150  if ( $reason === null || $reason === '' ) {
151  // Null on form display, or no reason given
152  return true;
153  }
154 
155  $match = $spamChecker->checkSummary( $reason );
156 
157  if ( $match ) {
158  return $this->msg( 'spamprotectionmatch', $match )->parse();
159  }
160 
161  return true;
162  },
163  'label-message' => 'changecontentmodel-reason-label',
164  ],
165  ];
166  }
167 
168  return $fields;
169  }
170 
171  private function getOptionsForTitle( Title $title = null ) {
172  $models = $this->contentHandlerFactory->getContentModels();
173  $options = [];
174  foreach ( $models as $model ) {
175  $handler = $this->contentHandlerFactory->getContentHandler( $model );
176  if ( !$handler->supportsDirectEditing() ) {
177  continue;
178  }
179  if ( $title ) {
180  if ( $title->getContentModel() === $model ) {
181  continue;
182  }
183  if ( !$handler->canBeUsedOn( $title ) ) {
184  continue;
185  }
186  }
187  $options[ContentHandler::getLocalizedName( $model )] = $model;
188  }
189 
190  return $options;
191  }
192 
193  public function onSubmit( array $data ) {
194  $user = $this->getUser();
195  $this->title = Title::newFromText( $data['pagetitle'] );
196  $page = WikiPage::factory( $this->title );
197 
198  $changer = MediaWikiServices::getInstance()
199  ->getContentModelChangeFactory()
200  ->newContentModelChange(
201  $user,
202  $page,
203  $data['model']
204  );
205 
206  $errors = $changer->checkPermissions();
207  if ( $errors ) {
208  $out = $this->getOutput();
209  $wikitext = $out->formatPermissionsErrorMessage( $errors );
210  // Hack to get our wikitext parsed
211  return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
212  }
213 
214  // Can also throw a ThrottledError, don't catch it
215  $status = $changer->doContentModelChange(
216  $this->getContext(),
217  $data['reason'],
218  true
219  );
220 
221  return $status;
222  }
223 
224  public function onSuccess() {
225  $out = $this->getOutput();
226  $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
227  $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
228  }
229 
238  public function prefixSearchSubpages( $search, $limit, $offset ) {
239  return $this->prefixSearchString( $search, $limit, $offset );
240  }
241 
242  protected function getGroupName() {
243  return 'pagetools';
244  }
245 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:328
SpecialChangeContentModel\getOptionsForTitle
getOptionsForTitle(Title $title=null)
Definition: SpecialChangeContentModel.php:171
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
SpecialChangeContentModel\$title
Title null $title
Definition: SpecialChangeContentModel.php:34
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:158
true
return true
Definition: router.php:90
SpecialChangeContentModel\postText
postText()
Add post-text to the form.
Definition: SpecialChangeContentModel.php:54
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1852
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
SpecialChangeContentModel\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialChangeContentModel.php:27
SpecialChangeContentModel\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialChangeContentModel.php:70
SpecialChangeContentModel\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialChangeContentModel.php:238
SpecialChangeContentModel
Definition: SpecialChangeContentModel.php:8
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:864
SpecialPage\prefixSearchString
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
Definition: SpecialPage.php:534
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:157
HTMLForm\setFormIdentifier
setFormIdentifier( $ident)
Set an internal identifier for this form.
Definition: HTMLForm.php:1479
SpecialChangeContentModel\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialChangeContentModel.php:108
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1027
SpecialChangeContentModel\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialChangeContentModel.php:242
SpecialChangeContentModel\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialChangeContentModel.php:224
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:37
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:41
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:754
SpecialChangeContentModel\__construct
__construct(?IContentHandlerFactory $contentHandlerFactory=null)
Definition: SpecialChangeContentModel.php:17
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Definition: LogEventsList.php:614
SpecialChangeContentModel\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: SpecialChangeContentModel.php:11
SpecialChangeContentModel\validateTitle
validateTitle( $title)
Definition: SpecialChangeContentModel.php:87
SpecialChangeContentModel\onSubmit
onSubmit(array $data)
Process the form on POST submission.
Definition: SpecialChangeContentModel.php:193
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:717
FormSpecialPage\$par
string null $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
Title\newFromTextThrow
static newFromTextThrow( $text, $defaultNamespace=NS_MAIN)
Like Title::newFromText(), but throws MalformedTitleException when the title is invalid,...
Definition: Title.php:363
ContentHandler\getLocalizedName
static getLocalizedName( $name, Language $lang=null)
Returns the localized name for a given content model.
Definition: ContentHandler.php:299
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:734
Title\getContentModel
getContentModel( $flags=0)
Get the page's content model id, see the CONTENT_MODEL_XXX constants.
Definition: Title.php:1046
SpecialChangeContentModel\getDisplayFormat
getDisplayFormat()
Get display format for the form.
Definition: SpecialChangeContentModel.php:66
CommentStore\COMMENT_CHARACTER_LIMIT
const COMMENT_CHARACTER_LIMIT
Maximum length of a comment in UTF-8 characters.
Definition: CommentStore.php:48
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1411
SpecialChangeContentModel\setParameter
setParameter( $par)
Maybe do something interesting with the subpage parameter.
Definition: SpecialChangeContentModel.php:43
Title
Represents a title within MediaWiki.
Definition: Title.php:41
SpecialChangeContentModel\$oldRevision
RevisionRecord bool null $oldRevision
A RevisionRecord object, false if no revision exists, null if not loaded yet.
Definition: SpecialChangeContentModel.php:41
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135