Merge "Add Special:TalkPage for redirecting to talk pages"

This commit is contained in:
jenkins-bot 2024-05-13 14:13:12 +00:00 committed by Gerrit Code Review
commit 822a0eea73
8 changed files with 220 additions and 0 deletions

View file

@ -52,6 +52,11 @@ For notes on 1.42.x and older releases, see HISTORY.
=== New user-facing features in 1.43 ===
* (T338341) Support reading XMP and EXIF from WebP files
* (T242346) Special:TalkPage is a new special page that will redirect to the
associated talk namespace page, e.g. [[Special:TalkPage/Foo]] redirects to
[[Talk:Foo]]; [[Special:TalkPage/Project:Foo]] goes to [[Project talk:Foo]].
This allows for links by templates and tools without having to parse what
namespaces are valid and have associated talk pages.
* …
=== New features for sysadmins in 1.43 ===

View file

@ -2196,6 +2196,7 @@ $wgAutoloadLocalClasses = [
'MediaWiki\\Specials\\Redirects\\SpecialMypage' => __DIR__ . '/includes/specials/redirects/SpecialMypage.php',
'MediaWiki\\Specials\\Redirects\\SpecialMytalk' => __DIR__ . '/includes/specials/redirects/SpecialMytalk.php',
'MediaWiki\\Specials\\Redirects\\SpecialMyuploads' => __DIR__ . '/includes/specials/redirects/SpecialMyuploads.php',
'MediaWiki\\Specials\\Redirects\\SpecialTalkPage' => __DIR__ . '/includes/specials/redirects/SpecialTalkPage.php',
'MediaWiki\\Specials\\SpecialActiveUsers' => __DIR__ . '/includes/specials/SpecialActiveUsers.php',
'MediaWiki\\Specials\\SpecialAllMessages' => __DIR__ . '/includes/specials/SpecialAllMessages.php',
'MediaWiki\\Specials\\SpecialAllPages' => __DIR__ . '/includes/specials/SpecialAllPages.php',

View file

@ -42,6 +42,7 @@ use MediaWiki\Specials\Redirects\SpecialMylog;
use MediaWiki\Specials\Redirects\SpecialMypage;
use MediaWiki\Specials\Redirects\SpecialMytalk;
use MediaWiki\Specials\Redirects\SpecialMyuploads;
use MediaWiki\Specials\Redirects\SpecialTalkPage;
use MediaWiki\Specials\SpecialActiveUsers;
use MediaWiki\Specials\SpecialAllMessages;
use MediaWiki\Specials\SpecialAllPages;
@ -1207,6 +1208,13 @@ class SpecialPageFactory {
'Contribute' => [
'class' => SpecialContribute::class,
],
'TalkPage' => [
'class' => SpecialTalkPage::class,
'services' => [
'MainConfig',
'TitleParser',
],
],
];
/** @var array Special page name => class name */

View file

@ -0,0 +1,111 @@
<?php
/**
* Special page to redirect to the talk page of a given page.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup SpecialPage
*/
namespace MediaWiki\Specials\Redirects;
use MediaWiki\Config\Config;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\MainConfigNames;
use MediaWiki\SpecialPage\FormSpecialPage;
use MediaWiki\Status\Status;
use MediaWiki\Title\MalformedTitleException;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleParser;
/**
* Special page to redirect to the talk page of a given page.
*
* @ingroup SpecialPage
*/
class SpecialTalkPage extends FormSpecialPage {
private Config $config;
private TitleParser $titleParser;
public function __construct( Config $config, TitleParser $titleParser ) {
parent::__construct( 'TalkPage' );
$this->config = $config;
$this->titleParser = $titleParser;
}
protected function getFormFields() {
return [
'target' => [
'type' => 'title',
'name' => 'target',
'label-message' => 'special-talkpage-target',
'default' => $this->par,
],
];
}
protected function alterForm( HTMLForm $form ) {
if ( $this->par ) { // immediately submit with subpage value
$form->setMethod( 'get' );
}
$form->setSubmitTextMsg( 'special-talkpage-submit' );
}
public function onSubmit( array $formData ) {
$target = $formData['target'];
try {
$title = $this->titleParser->parseTitle( $target );
} catch ( MalformedTitleException $e ) {
return Status::newFatal( $e->getMessageObject() );
}
$title = Title::newFromLinkTarget( $title );
$talk = $title->getTalkPageIfDefined();
if ( $talk === null ) {
return Status::newFatal( 'title-invalid-talk-namespace' );
}
// HTTP 302: Found; cache for the Parser Cache length, as an appropriate long time
$this->getOutput()->redirect( $talk->getFullUrlForRedirect(), '302' );
$this->getOutput()->enableClientCache();
$this->getOutput()->setCdnMaxage(
$this->config->get( MainConfigNames::ParserCacheExpireTime )
);
return true;
}
protected function getDisplayFormat() {
return 'ooui';
}
public function requiresWrite() {
return false;
}
public function requiresUnblock() {
return false;
}
public function isListed() {
return false;
}
protected function getMessagePrefix() {
return 'special-talkpage';
}
}

View file

@ -3696,6 +3696,9 @@
"fileduplicatesearch-result-1": "The file \"$1\" has no identical duplication.",
"fileduplicatesearch-result-n": "The file \"$1\" has {{PLURAL:$2|1 identical duplication|$2 identical duplications}}.",
"fileduplicatesearch-noresults": "No file named \"$1\" found.",
"talkpage": "Talk page",
"special-talkpage-target": "Subject page",
"special-talkpage-submit": "Go to talk page",
"specialpages": "Special pages",
"specialpages-summary": "",
"specialpages-note-top": "Legend",

View file

@ -3954,6 +3954,9 @@
"fileduplicatesearch-result-1": "Result line after the list of files of [[Special:FileDuplicateSearch]].\n\nParameters:\n* $1 - the name of the requested file",
"fileduplicatesearch-result-n": "Result line after the list of files of [[Special:FileDuplicateSearch]]\n\n* $1 is the name of the requested file.\n* $2 is the number of identical duplicates of the requested file",
"fileduplicatesearch-noresults": "Parameters:\n* $1 - file name",
"talkpage": "Name of special page [[Special:TalkPage]].",
"special-talkpage-target": "Input form of [[Special:TalkPage]].",
"special-talkpage-submit": "Button label on [[Special:TalkPage]].",
"specialpages": "{{doc-special|SpecialPages|unlisted=1}}\nDisplay name of link to [[Special:SpecialPages]] shown on all pages in the toolbox.\n\nSee also:\n* {{msg-mw|Specialpages}}\n* {{msg-mw|Accesskey-t-specialpages}}\n* {{msg-mw|Tooltip-t-specialpages}}\n{{Identical|Special page}}",
"specialpages-summary": "{{doc-specialpagesummary|specialpages}}",
"specialpages-note-top": "Heading for {{msg-mw|specialpages-note}}.\n{{Identical|Legend}}",

View file

@ -510,6 +510,7 @@ $specialPageAliases = [
'Specialpages' => [ 'SpecialPages' ],
'Statistics' => [ 'Statistics', 'Stats' ],
'Tags' => [ 'Tags' ],
'TalkPage' => [ 'TalkPage' ],
'TrackingCategories' => [ 'TrackingCategories' ],
'Unblock' => [ 'Unblock' ],
'Uncategorizedcategories' => [ 'UncategorizedCategories' ],

View file

@ -0,0 +1,88 @@
<?php
namespace MediaWiki\Tests\Integration\Specials\Redirects;
use MediaWiki\Context\RequestContext;
use MediaWiki\Output\OutputPage;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Specials\Redirects\SpecialTalkPage;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
/**
* @covers \MediaWiki\Specials\Redirects\SpecialTalkPage
*
* @license GPL-2.0-or-later
*/
class SpecialTalkPageTest extends MediaWikiIntegrationTestCase {
private function executeSpecialPageAndGetOutput(
string $subpage = '',
string $target = null
): OutputPage {
$services = $this->getServiceContainer();
$context = new RequestContext();
if ( $target !== null ) {
$request = new FauxRequest( [ 'target' => $target ], true );
} else {
$request = new FauxRequest();
}
$context->setRequest( $request );
$context->setTitle( Title::newFromText( 'Special:TalkPage' ) );
$context->setLanguage( $services->getLanguageFactory()->getLanguage( 'qqx' ) );
$page = new SpecialTalkPage( $services->getMainConfig(), $services->getTitleParser() );
$page->setContext( $context );
$page->execute( $subpage );
return $page->getOutput();
}
/** @dataProvider provideRedirects */
public function testRedirect( $subpage, $target, $expectedUrl, $expectedRedirect ) {
$output = $this->executeSpecialPageAndGetOutput( $subpage, $target );
$this->assertSame( $expectedUrl, $output->getRedirect(), 'should redirect to URL' );
$this->assertHTMLEquals( $expectedRedirect, $output->getHTML(), 'redirect should contain appropriate HTML' );
}
public function provideRedirects() {
$subjectTitleText = 'MediaWiki:ok';
$subjectTitle = Title::newFromText( $subjectTitleText );
$talkTitle = $subjectTitle->getTalkPageIfDefined();
$talkUrl = $talkTitle->getFullUrlForRedirect();
$standardRedirectHTML = "<div class=\"mw-specialpage-summary\">\n<p>(talkpage-summary)\n</p>\n</div>";
yield [ $subjectTitleText, null, $talkUrl, $standardRedirectHTML ];
yield [ '', $subjectTitleText, $talkUrl, $standardRedirectHTML ];
}
/** @dataProvider provideNoRedirects */
public function testNoRedirect( $subpage, $target, ...$expectedHtmls ) {
$output = $this->executeSpecialPageAndGetOutput( $subpage, $target );
$this->assertSame( '', $output->getRedirect(), 'should not redirect' );
foreach ( $expectedHtmls as $expectedHtml ) {
$this->assertStringContainsString(
$expectedHtml,
$output->getHTML(),
'should contain HTML'
);
}
$this->assertStringContainsString(
'<form',
$output->getHTML(),
'should contain form'
);
}
public function provideNoRedirects() {
yield [ '', null ];
yield [ 'Special:TalkPage', null, 'title-invalid-talk-namespace', "value='Special:TalkPage'" ];
yield [ '', 'Special:TalkPage', 'title-invalid-talk-namespace', "value='Special:TalkPage'" ];
yield [ '', '<>', 'title-invalid-characters', "value='&lt;&gt;'" ];
}
}