MediaWiki  master
SpecialChangeContentModel.php
Go to the documentation of this file.
1 <?php
2 
5 
7 
10 
16  parent::__construct( 'ChangeContentModel', 'editcontentmodel' );
17  $this->contentHandlerFactory = DeprecationHelper::newArgumentWithDeprecation(
18  __METHOD__,
19  'contentHandlerFactory',
20  '1.35',
22  function () {
23  return MediaWikiServices::getInstance()->getContentHandlerFactory();
24  }
25  );
26  }
27 
28  public function doesWrites() {
29  return true;
30  }
31 
35  private $title;
36 
42  private $oldRevision;
43 
44  protected function setParameter( $par ) {
45  $par = $this->getRequest()->getVal( 'pagetitle', $par );
47  if ( $title ) {
48  $this->title = $title;
49  $this->par = $title->getPrefixedText();
50  } else {
51  $this->par = '';
52  }
53  }
54 
55  protected function postText() {
56  $text = '';
57  if ( $this->title ) {
58  $contentModelLogPage = new LogPage( 'contentmodel' );
59  $text = Xml::element( 'h2', null, $contentModelLogPage->getName()->text() );
60  $out = '';
61  LogEventsList::showLogExtract( $out, 'contentmodel', $this->title );
62  $text .= $out;
63  }
64  return $text;
65  }
66 
67  protected function getDisplayFormat() {
68  return 'ooui';
69  }
70 
71  protected function alterForm( HTMLForm $form ) {
72  if ( !$this->title ) {
73  $form->setMethod( 'GET' );
74  }
75 
76  $this->addHelpLink( 'Help:ChangeContentModel' );
77 
78  // T120576
79  $form->setSubmitTextMsg( 'changecontentmodel-submit' );
80 
81  if ( $this->title ) {
82  $this->getOutput()->addBacklinkSubtitle( $this->title );
83  }
84  }
85 
86  public function validateTitle( $title ) {
87  if ( !$title ) {
88  // No form input yet
89  return true;
90  }
91 
92  // Already validated by HTMLForm, but if not, throw
93  // an exception instead of a fatal
94  $titleObj = Title::newFromTextThrow( $title );
95 
96  $this->oldRevision = Revision::newFromTitle( $titleObj ) ?: false;
97 
98  if ( $this->oldRevision ) {
99  $oldContent = $this->oldRevision->getContent();
100  if ( !$oldContent->getContentHandler()->supportsDirectEditing() ) {
101  return $this->msg( 'changecontentmodel-nodirectediting' )
102  ->params( ContentHandler::getLocalizedName( $oldContent->getModel() ) )
103  ->escaped();
104  }
105  }
106 
107  return true;
108  }
109 
110  protected function getFormFields() {
111  $fields = [
112  'pagetitle' => [
113  'type' => 'title',
114  'creatable' => true,
115  'name' => 'pagetitle',
116  'default' => $this->par,
117  'label-message' => 'changecontentmodel-title-label',
118  'validation-callback' => [ $this, 'validateTitle' ],
119  ],
120  ];
121  if ( $this->title ) {
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',
147  'name' => 'reason',
148  'validation-callback' => function ( $reason ) {
149  $match = EditPage::matchSummarySpamRegex( $reason );
150  if ( $match ) {
151  return $this->msg( 'spamprotectionmatch', $match )->parse();
152  }
153 
154  return true;
155  },
156  'label-message' => 'changecontentmodel-reason-label',
157  ],
158  ];
159  }
160 
161  return $fields;
162  }
163 
164  private function getOptionsForTitle( Title $title = null ) {
165  $models = $this->contentHandlerFactory->getContentModels();
166  $options = [];
167  foreach ( $models as $model ) {
168  $handler = $this->contentHandlerFactory->getContentHandler( $model );
169  if ( !$handler->supportsDirectEditing() ) {
170  continue;
171  }
172  if ( $title ) {
173  if ( $title->getContentModel() === $model ) {
174  continue;
175  }
176  if ( !$handler->canBeUsedOn( $title ) ) {
177  continue;
178  }
179  }
180  $options[ContentHandler::getLocalizedName( $model )] = $model;
181  }
182 
183  return $options;
184  }
185 
186  public function onSubmit( array $data ) {
187  if ( $data['pagetitle'] === '' ) {
188  // Initial form view of special page, pass
189  return false;
190  }
191 
192  // At this point, it has to be a POST request. This is enforced by HTMLForm,
193  // but lets be safe verify that.
194  if ( !$this->getRequest()->wasPosted() ) {
195  throw new RuntimeException( "Form submission was not POSTed" );
196  }
197 
198  $user = $this->getUser();
199  $this->title = Title::newFromText( $data['pagetitle'] );
200  $page = WikiPage::factory( $this->title );
201 
202  $changer = new ContentModelChange(
203  $user,
204  MediaWikiServices::getInstance()->getPermissionManager(),
205  $page,
206  $data['model']
207  );
208 
209  $errors = $changer->checkPermissions();
210  if ( $errors ) {
211  $out = $this->getOutput();
212  $wikitext = $out->formatPermissionsErrorMessage( $errors );
213  // Hack to get our wikitext parsed
214  return Status::newFatal( new RawMessage( '$1', [ $wikitext ] ) );
215  }
216 
217  // Can also throw a ThrottledError, don't catch it
218  $status = $changer->doContentModelChange(
219  $this->getContext(),
220  $data['reason'],
221  true
222  );
223 
224  return $status;
225  }
226 
227  public function onSuccess() {
228  $out = $this->getOutput();
229  $out->setPageTitle( $this->msg( 'changecontentmodel-success-title' ) );
230  $out->addWikiMsg( 'changecontentmodel-success-text', $this->title );
231  }
232 
241  public function prefixSearchSubpages( $search, $limit, $offset ) {
242  return $this->prefixSearchString( $search, $limit, $offset );
243  }
244 
245  protected function getGroupName() {
246  return 'pagetools';
247  }
248 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:803
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:332
SpecialChangeContentModel\getOptionsForTitle
getOptionsForTitle(Title $title=null)
Definition: SpecialChangeContentModel.php:164
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
SpecialChangeContentModel\$oldRevision
Revision bool null $oldRevision
A Revision object, false if no revision exists, null if not loaded yet.
Definition: SpecialChangeContentModel.php:42
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialChangeContentModel\$title
Title null $title
Definition: SpecialChangeContentModel.php:35
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:137
true
return true
Definition: router.php:92
SpecialChangeContentModel\postText
postText()
Add post-text to the form.
Definition: SpecialChangeContentModel.php:55
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:28
HTMLForm\setMethod
setMethod( $method='post')
Set the method used to submit the form.
Definition: HTMLForm.php:1628
newArgumentWithDeprecation
static newArgumentWithDeprecation(string $method, string $name, string $version, $value, callable $fallback)
This function is used to introduce new argument into function or __construct.
Definition: DeprecationHelper.php:171
SpecialChangeContentModel\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: SpecialChangeContentModel.php:71
SpecialChangeContentModel\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialChangeContentModel.php:241
Revision
Definition: Revision.php:40
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:138
SpecialChangeContentModel
Definition: SpecialChangeContentModel.php:6
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:839
SpecialPage\prefixSearchString
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
Definition: SpecialPage.php:501
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:143
SpecialChangeContentModel\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: SpecialChangeContentModel.php:110
SpecialChangeContentModel\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialChangeContentModel.php:245
getPermissionManager
getPermissionManager()
SpecialChangeContentModel\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialChangeContentModel.php:227
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:33
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:729
SpecialChangeContentModel\__construct
__construct(?IContentHandlerFactory $contentHandlerFactory=null)
Definition: SpecialChangeContentModel.php:15
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Definition: LogEventsList.php:634
SpecialChangeContentModel\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: SpecialChangeContentModel.php:9
SpecialChangeContentModel\validateTitle
validateTitle( $title)
Definition: SpecialChangeContentModel.php:86
SpecialChangeContentModel\onSubmit
onSubmit(array $data)
Process the form on POST submission.
Definition: SpecialChangeContentModel.php:186
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
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:367
ContentHandler\getLocalizedName
static getLocalizedName( $name, Language $lang=null)
Returns the localized name for a given content model.
Definition: ContentHandler.php:293
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
EditPage\matchSummarySpamRegex
static matchSummarySpamRegex( $text)
Check given input text against $wgSummarySpamRegex, and return the text of the first match.
Definition: EditPage.php:2603
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
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:67
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1387
SpecialChangeContentModel\setParameter
setParameter( $par)
Maybe do something interesting with the subpage parameter.
Definition: SpecialChangeContentModel.php:44
Title
Represents a title within MediaWiki.
Definition: Title.php:42
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
ContentModelChange
Helper class to change the content model of pages.
Definition: ContentModelChange.php:14
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131