MediaWiki master
SpecialTalkPage.php
Go to the documentation of this file.
1<?php
22
31
38
39 private Config $config;
40 private TitleParser $titleParser;
41
42 public function __construct( Config $config, TitleParser $titleParser ) {
43 parent::__construct( 'TalkPage' );
44 $this->config = $config;
45 $this->titleParser = $titleParser;
46 }
47
48 protected function getFormFields() {
49 return [
50 'target' => [
51 'type' => 'title',
52 'name' => 'target',
53 'label-message' => 'special-talkpage-target',
54 'default' => $this->par,
55 ],
56 ];
57 }
58
59 protected function alterForm( HTMLForm $form ) {
60 if ( $this->par ) { // immediately submit with subpage value
61 $form->setMethod( 'get' );
62 }
63 $form->setSubmitTextMsg( 'special-talkpage-submit' );
64 }
65
66 public function onSubmit( array $formData ) {
67 $target = $formData['target'];
68 try {
69 $title = $this->titleParser->parseTitle( $target );
70 } catch ( MalformedTitleException $e ) {
71 return Status::newFatal( $e->getMessageObject() );
72 }
73 $title = Title::newFromLinkTarget( $title );
74 $talk = $title->getTalkPageIfDefined();
75 if ( $talk === null ) {
76 return Status::newFatal( 'title-invalid-talk-namespace' );
77 }
78
79 // HTTP 302: Found; cache for the Parser Cache length, as an appropriate long time
80 $this->getOutput()->redirect( $talk->getFullUrlForRedirect(), '302' );
81 $this->getOutput()->enableClientCache();
82 $this->getOutput()->setCdnMaxage(
83 $this->config->get( MainConfigNames::ParserCacheExpireTime )
84 );
85 return true;
86 }
87
88 protected function getDisplayFormat() {
89 return 'ooui';
90 }
91
92 public function requiresWrite() {
93 return false;
94 }
95
96 public function requiresUnblock() {
97 return false;
98 }
99
100 public function isListed() {
101 return false;
102 }
103
104 protected function getMessagePrefix() {
105 return 'special-talkpage';
106 }
107
108 public function getDescription() {
109 // "talkpage" is already taken by CologneBlue
110 return $this->msg( 'special-talkpage' );
111 }
112
113}
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:206
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setMethod( $method='post')
Set the method used to submit the form.
A class containing constants representing the names of configuration variables.
const ParserCacheExpireTime
Name constant for the ParserCacheExpireTime setting, for use with Config::get()
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Redirect to the talk page of a given page.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
isListed()
Whether this special page is listed in Special:SpecialPages.
requiresUnblock()
Whether this action cannot be executed by a blocked user, default to requiresPost()
__construct(Config $config, TitleParser $titleParser)
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
getDisplayFormat()
Get display format for the form.
getFormFields()
Get an HTMLForm descriptor array.
getMessagePrefix()
Get message prefix for HTMLForm.
requiresWrite()
Whether this action requires the wiki not to be locked, default to requiresPost()
onSubmit(array $formData)
Process the form on submission.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
Represents a title within MediaWiki.
Definition Title.php:78
Interface for configuration instances.
Definition Config.php:32
A title parser service for MediaWiki.