| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\ThreadItem; |
| 4 |
|
| 5 |
use MediaWiki\Extension\DiscussionTools\ImmutableRange; |
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\Parsoid\DOM\Element; |
| 8 |
|
| 9 |
class ContentHeadingItem extends ContentThreadItem implements HeadingItem { |
| 10 |
use HeadingItemTrait { |
| 11 |
jsonSerialize as traitJsonSerialize; |
| 12 |
} |
| 13 |
|
| 14 |
private bool $placeholderHeading; |
| 15 |
private int $headingLevel; |
| 16 |
private bool $uneditableSection = false; |
| 17 |
|
| 18 |
|
| 19 |
private const PLACEHOLDER_HEADING_LEVEL = 99; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( |
| 27 |
ImmutableRange $range, $transcludedFrom, ?int $headingLevel |
| 28 |
) { |
| 29 |
parent::__construct( 'heading', 0, $range, $transcludedFrom ); |
| 30 |
$this->placeholderHeading = $headingLevel === null; |
| 31 |
$this->headingLevel = $this->placeholderHeading ? static::PLACEHOLDER_HEADING_LEVEL : $headingLevel; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function getLinkableTitle(): string { |
| 40 |
$title = ''; |
| 41 |
|
| 42 |
if ( !$this->isPlaceholderHeading() ) { |
| 43 |
$id = $this->getLinkableId(); |
| 44 |
if ( $id ) { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
$title = str_replace( '_', ' ', $id ); |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
return $title; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function getLinkableId(): string { |
| 65 |
$headline = $this->getHeadlineNode(); |
| 66 |
return ( $headline->getAttribute( 'id' ) ?: $headline->getAttribute( 'data-mw-anchor' ) ) ?? ''; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getHeadlineNode(): Element { |
| 76 |
|
| 77 |
$headline = $this->getRange()->startContainer; |
| 78 |
Assert::precondition( $headline instanceof Element, 'HeadingItem refers to an element node' ); |
| 79 |
return $headline; |
| 80 |
} |
| 81 |
|
| 82 |
public function isUneditableSection(): bool { |
| 83 |
return $this->uneditableSection; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function setUneditableSection( bool $uneditableSection ): void { |
| 91 |
$this->uneditableSection = $uneditableSection; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function getHeadingLevel(): int { |
| 98 |
return $this->headingLevel; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function setHeadingLevel( int $headingLevel ): void { |
| 105 |
$this->headingLevel = $headingLevel; |
| 106 |
} |
| 107 |
|
| 108 |
public function isPlaceholderHeading(): bool { |
| 109 |
return $this->placeholderHeading; |
| 110 |
} |
| 111 |
|
| 112 |
public function setPlaceholderHeading( bool $placeholderHeading ): void { |
| 113 |
$this->placeholderHeading = $placeholderHeading; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array { |
| 120 |
$data = $this->traitJsonSerialize( $deep, $callback ); |
| 121 |
|
| 122 |
|
| 123 |
if ( $this->isUneditableSection() ) { |
| 124 |
$data[ 'uneditableSection' ] = true; |
| 125 |
} |
| 126 |
return $data; |
| 127 |
} |
| 128 |
} |