2020-05-22 16:26:05 +00:00
|
|
|
<?php
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
namespace MediaWiki\Extension\DiscussionTools\ThreadItem;
|
2020-05-22 16:26:05 +00:00
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ImmutableRange;
|
2022-02-19 03:25:14 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
|
|
|
use Wikimedia\Parsoid\DOM\Element;
|
|
|
|
|
2022-03-18 03:28:06 +00:00
|
|
|
class ContentHeadingItem extends ContentThreadItem implements HeadingItem {
|
2023-10-11 15:53:55 +00:00
|
|
|
use HeadingItemTrait {
|
|
|
|
jsonSerialize as traitJsonSerialize;
|
|
|
|
}
|
2022-03-18 03:28:06 +00:00
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private bool $placeholderHeading;
|
|
|
|
private int $headingLevel;
|
2023-10-11 15:53:55 +00:00
|
|
|
private bool $uneditableSection = false;
|
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
|
2023-12-07 22:57:21 +00:00
|
|
|
* @param bool|string $transcludedFrom
|
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(
|
2023-12-07 22:57:21 +00:00
|
|
|
ImmutableRange $range, $transcludedFrom, ?int $headingLevel
|
2020-05-22 16:26:05 +00:00
|
|
|
) {
|
2023-12-07 22:57:21 +00:00
|
|
|
parent::__construct( 'heading', 0, $range, $transcludedFrom );
|
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-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() ) {
|
2024-04-20 00:08:35 +00:00
|
|
|
$id = $this->getLinkableId();
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-20 00:08:35 +00:00
|
|
|
/**
|
|
|
|
* Return the ID found on the headline node, if it has one.
|
|
|
|
*
|
|
|
|
* In Parsoid HTML, it is stored in the `<hN id>` attribute.
|
|
|
|
* In legacy parser HTML, it is stored in the `<hN data-mw-anchor>` attribute.
|
|
|
|
* In integration tests and in JS, things are a little bit wilder than that.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLinkableId(): string {
|
|
|
|
$headline = $this->getHeadlineNode();
|
|
|
|
return ( $headline->getAttribute( 'id' ) ?: $headline->getAttribute( 'data-mw-anchor' ) ) ?? '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the node on which the ID attribute is set.
|
|
|
|
*
|
|
|
|
* @return Element Headline node, normally a `<h1>`-`<h6>` element (unless it's a placeholder heading).
|
|
|
|
* In integration tests and in JS, it can be a `<span class="mw-headline">` (see T363031).
|
|
|
|
*/
|
|
|
|
public function getHeadlineNode(): Element {
|
|
|
|
// This value comes from CommentUtils::getHeadlineNode(), this function just guarantees the type
|
|
|
|
$headline = $this->getRange()->startContainer;
|
|
|
|
Assert::precondition( $headline instanceof Element, 'HeadingItem refers to an element node' );
|
|
|
|
return $headline;
|
|
|
|
}
|
|
|
|
|
2023-10-11 15:53:55 +00:00
|
|
|
public function isUneditableSection(): bool {
|
|
|
|
return $this->uneditableSection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $uneditableSection The heading represents a section that can't be
|
|
|
|
* edited on its own.
|
|
|
|
*/
|
|
|
|
public function setUneditableSection( bool $uneditableSection ): void {
|
|
|
|
$this->uneditableSection = $uneditableSection;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:25:13 +00:00
|
|
|
public function isPlaceholderHeading(): bool {
|
2020-05-22 16:26:05 +00:00
|
|
|
return $this->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
|
|
|
|
2023-10-11 15:53:55 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array {
|
|
|
|
$data = $this->traitJsonSerialize( $deep, $callback );
|
|
|
|
|
|
|
|
// When this is false (which is most of the time), omit the key for efficiency
|
|
|
|
if ( $this->isUneditableSection() ) {
|
|
|
|
$data[ 'uneditableSection' ] = true;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2020-05-22 16:26:05 +00:00
|
|
|
}
|