2022-02-16 23:29:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\SpecialPage\RedirectSpecialPage;
|
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-02-16 23:29:10 +00:00
|
|
|
|
|
|
|
class SpecialGoToComment extends RedirectSpecialPage {
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private ThreadItemStore $threadItemStore;
|
2022-02-16 23:29:10 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ThreadItemStore $threadItemStore
|
|
|
|
) {
|
|
|
|
parent::__construct( 'GoToComment' );
|
|
|
|
$this->threadItemStore = $threadItemStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getRedirect( $subpage ) {
|
|
|
|
$results = [];
|
|
|
|
|
|
|
|
// Search for all thread items with the given ID or name, returning results from the latest
|
|
|
|
// revision of each page they appeared on.
|
|
|
|
//
|
2024-03-29 00:35:32 +00:00
|
|
|
// If there is exactly one good result (see isCanonicalPermalink()), redirect to it.
|
2022-02-16 23:29:10 +00:00
|
|
|
// Otherwise, redirect to full search results on Special:FindComment.
|
|
|
|
|
|
|
|
if ( $subpage ) {
|
|
|
|
$threadItems = $this->threadItemStore->findNewestRevisionsById( $subpage );
|
|
|
|
foreach ( $threadItems as $item ) {
|
2024-03-29 00:35:32 +00:00
|
|
|
if ( $item->isCanonicalPermalink() ) {
|
2022-02-16 23:29:10 +00:00
|
|
|
$results[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$threadItems = $this->threadItemStore->findNewestRevisionsByName( $subpage );
|
|
|
|
foreach ( $threadItems as $item ) {
|
2024-03-29 00:35:32 +00:00
|
|
|
if ( $item->isCanonicalPermalink() ) {
|
2022-02-16 23:29:10 +00:00
|
|
|
$results[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $results ) === 1 ) {
|
|
|
|
return Title::castFromPageIdentity( $results[0]->getPage() )->createFragmentTarget( $results[0]->getId() );
|
|
|
|
} else {
|
|
|
|
return SpecialPage::getTitleFor( 'FindComment', $subpage ?: false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|