2020-05-22 16:26:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2022-02-19 03:25:14 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
class HeadingItem extends ThreadItem {
|
|
|
|
private $placeholderHeading = false;
|
2020-10-01 19:36:11 +00:00
|
|
|
private $headingLevel;
|
2020-05-22 16:26:05 +00:00
|
|
|
|
2022-06-06 17:35:43 +00:00
|
|
|
// Placeholder headings must have a level higher than real headings (1-6)
|
|
|
|
private const PLACEHOLDER_HEADING_LEVEL = 99;
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @param ImmutableRange $range
|
2022-06-06 17:35:43 +00:00
|
|
|
* @param ?int $headingLevel Heading level (1-6). Use null for a placeholder heading.
|
2020-05-22 16:26:05 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-06-06 17:35:43 +00:00
|
|
|
ImmutableRange $range, ?int $headingLevel
|
2020-05-22 16:26:05 +00:00
|
|
|
) {
|
|
|
|
parent::__construct( 'heading', 0, $range );
|
2022-06-06 17:35:43 +00:00
|
|
|
$this->placeholderHeading = $headingLevel === null;
|
|
|
|
$this->headingLevel = $this->placeholderHeading ? static::PLACEHOLDER_HEADING_LEVEL : $headingLevel;
|
2020-05-22 16:26:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 12:06:14 +00:00
|
|
|
/**
|
2022-03-21 21:14:47 +00:00
|
|
|
* @inheritDoc
|
2020-09-16 12:06:14 +00:00
|
|
|
*/
|
2022-03-21 21:14:47 +00:00
|
|
|
public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array {
|
|
|
|
return array_merge( parent::jsonSerialize( $deep, $callback ), [
|
2022-06-06 17:35:43 +00:00
|
|
|
'headingLevel' => $this->headingLevel === static::PLACEHOLDER_HEADING_LEVEL ? null : $this->headingLevel,
|
2022-03-24 15:12:23 +00:00
|
|
|
// Used for topic subscriptions. Not added to CommentItem's yet as there is
|
|
|
|
// no use case for it.
|
|
|
|
'name' => $this->name,
|
2020-09-16 12:06:14 +00:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2020-11-05 16:07:56 +00:00
|
|
|
/**
|
|
|
|
* Get a title based on the hash ID, such that it can be linked to
|
|
|
|
*
|
|
|
|
* @return string Title
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getLinkableTitle(): string {
|
2020-11-05 16:07:56 +00:00
|
|
|
$title = '';
|
|
|
|
// If this comment is in 0th section, there's no section title for the edit summary
|
|
|
|
if ( !$this->isPlaceholderHeading() ) {
|
2022-02-22 09:36:45 +00:00
|
|
|
// <span class="mw-headline" …>, or <hN …> in Parsoid HTML
|
|
|
|
$headline = $this->getRange()->startContainer;
|
2022-02-19 03:25:14 +00:00
|
|
|
Assert::precondition( $headline instanceof Element, 'HeadingItem refers to an element node' );
|
2022-02-22 09:36:45 +00:00
|
|
|
$id = $headline->getAttribute( 'id' );
|
2020-11-05 16:07:56 +00:00
|
|
|
if ( $id ) {
|
|
|
|
// Replace underscores with spaces to undo Sanitizer::escapeIdInternal().
|
|
|
|
// This assumes that $wgFragmentMode is [ 'html5', 'legacy' ] or [ 'html5' ],
|
|
|
|
// otherwise the escaped IDs are super garbled and can't be unescaped reliably.
|
|
|
|
$title = str_replace( '_', ' ', $id );
|
|
|
|
}
|
|
|
|
// else: Not a real section, probably just HTML markup in wikitext
|
|
|
|
}
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
2020-10-01 19:36:11 +00:00
|
|
|
/**
|
|
|
|
* @return int Heading level (1-6)
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function getHeadingLevel(): int {
|
2020-10-01 19:36:11 +00:00
|
|
|
return $this->headingLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $headingLevel Heading level (1-6)
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function setHeadingLevel( int $headingLevel ): void {
|
2020-10-01 19:36:11 +00:00
|
|
|
$this->headingLevel = $headingLevel;
|
|
|
|
}
|
|
|
|
|
2020-05-22 16:26:05 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function isPlaceholderHeading(): bool {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->placeholderHeading;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $placeholderHeading
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
public function setPlaceholderHeading( bool $placeholderHeading ): void {
|
2020-05-22 16:26:05 +00:00
|
|
|
$this->placeholderHeading = $placeholderHeading;
|
|
|
|
}
|
2021-09-01 22:05:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether this heading can be used for topic subscriptions.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSubscribable(): bool {
|
|
|
|
return (
|
|
|
|
// Placeholder headings have nothing to attach the button to.
|
|
|
|
!$this->isPlaceholderHeading() &&
|
|
|
|
// We only allow subscribing to level 2 headings, because the user interface for sub-headings
|
|
|
|
// would be difficult to present.
|
|
|
|
$this->getHeadingLevel() === 2 &&
|
|
|
|
// Check if the name corresponds to a section that contain no comments (only sub-sections).
|
|
|
|
// They can't be distinguished from each other, so disallow subscribing.
|
|
|
|
$this->getName() !== 'h-'
|
|
|
|
);
|
|
|
|
}
|
2022-01-11 21:58:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getTranscludedFrom() {
|
|
|
|
// Placeholder headings break the usual logic, because their ranges are collapsed
|
|
|
|
if ( $this->isPlaceholderHeading() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-20 18:54:46 +00:00
|
|
|
// Collapsed ranges should otherwise be impossible, but they're not (T299583)
|
|
|
|
// TODO: See if we can fix the root cause, and remove this?
|
|
|
|
if ( $this->getRange()->collapsed ) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-11 21:58:57 +00:00
|
|
|
return parent::getTranscludedFrom();
|
|
|
|
}
|
2020-05-22 16:26:05 +00:00
|
|
|
}
|