Debian Codegraph @

includes/specials/redirects/SpecialTalkPage.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Specials\Redirects;
22
23 use MediaWiki\HTMLForm\HTMLForm;
24 use MediaWiki\MainConfigNames;
25 use MediaWiki\SpecialPage\FormSpecialPage;
26 use MediaWiki\Status\Status;
27 use MediaWiki\Title\MalformedTitleException;
28 use MediaWiki\Title\Title;
29 use MediaWiki\Title\TitleParser;
30
31 /**
32 * Redirect to the talk page of a given page.
33 *
34 * @ingroup SpecialPage
35 */
36 class SpecialTalkPage extends FormSpecialPage {
37
38 private TitleParser $titleParser;
39
40 public function __construct( TitleParser $titleParser ) {
41 parent::__construct( 'TalkPage' );
42 $this->titleParser = $titleParser;
43 }
44
45 protected function getFormFields() {
46 return [
47 'target' => [
48 'type' => 'title',
49 'name' => 'target',
50 'label-message' => 'special-talkpage-target',
51 'default' => $this->par,
52 ],
53 ];
54 }
55
56 protected function alterForm( HTMLForm $form ) {
57 if ( $this->par ) { // immediately submit with subpage value
58 $form->setMethod( 'get' );
59 }
60 $form->setSubmitTextMsg( 'special-talkpage-submit' );
61 }
62
63 public function onSubmit( array $formData ) {
64 $target = $formData['target'];
65 try {
66 $title = $this->titleParser->parseTitle( $target );
67 } catch ( MalformedTitleException $e ) {
68 return Status::newFatal( $e->getMessageObject() );
69 }
70 $title = Title::newFromLinkTarget( $title );
71 $talk = $title->getTalkPageIfDefined();
72 if ( $talk === null ) {
73 return Status::newFatal( 'title-invalid-talk-namespace' );
74 }
75
76 // HTTP 302: Found; cache for the Parser Cache length, as an appropriate long time
77 $this->getOutput()->redirect( $talk->getFullUrlForRedirect(), '302' );
78 $this->getOutput()->enableClientCache();
79 $this->getOutput()->setCdnMaxage(
80 $this->getConfig()->get( MainConfigNames::ParserCacheExpireTime )
81 );
82 return true;
83 }
84
85 protected function getDisplayFormat() {
86 return 'ooui';
87 }
88
89 public function requiresWrite() {
90 return false;
91 }
92
93 public function requiresUnblock() {
94 return false;
95 }
96
97 public function isListed() {
98 return false;
99 }
100
101 protected function getMessagePrefix() {
102 return 'special-talkpage';
103 }
104
105 public function getDescription() {
106 // "talkpage" is already taken by CologneBlue
107 return $this->msg( 'special-talkpage' );
108 }
109
110 }