Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentHeadingItem
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 11
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getLinkableTitle
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getLinkableId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getHeadlineNode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isUneditableSection
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setUneditableSection
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeadingLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setHeadingLevel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isPlaceholderHeading
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPlaceholderHeading
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools\ThreadItem;
4
5use MediaWiki\Extension\DiscussionTools\ImmutableRange;
6use Wikimedia\Assert\Assert;
7use Wikimedia\Parsoid\DOM\Element;
8
9class 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    // Placeholder headings must have a level higher than real headings (1-6)
19    private const PLACEHOLDER_HEADING_LEVEL = 99;
20
21    /**
22     * @param ImmutableRange $range
23     * @param bool|string $transcludedFrom
24     * @param ?int $headingLevel Heading level (1-6). Use null for a placeholder heading.
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     * Get a title based on the hash ID, such that it can be linked to
36     *
37     * @return string Title
38     */
39    public function getLinkableTitle(): string {
40        $title = '';
41        // If this comment is in 0th section, there's no section title for the edit summary
42        if ( !$this->isPlaceholderHeading() ) {
43            $id = $this->getLinkableId();
44            if ( $id ) {
45                // Replace underscores with spaces to undo Sanitizer::escapeIdInternal().
46                // This assumes that $wgFragmentMode is [ 'html5', 'legacy' ] or [ 'html5' ],
47                // otherwise the escaped IDs are super garbled and can't be unescaped reliably.
48                $title = str_replace( '_', ' ', $id );
49            }
50            // else: Not a real section, probably just HTML markup in wikitext
51        }
52        return $title;
53    }
54
55    /**
56     * Return the ID found on the headline node, if it has one.
57     *
58     * In Parsoid HTML, it is stored in the `<hN id>` attribute.
59     * In legacy parser HTML, it is stored in the `<hN data-mw-anchor>` attribute.
60     * In integration tests and in JS, things are a little bit wilder than that.
61     */
62    public function getLinkableId(): string {
63        $headline = $this->getHeadlineNode();
64        return ( $headline->getAttribute( 'id' ) ?: $headline->getAttribute( 'data-mw-anchor' ) ) ?? '';
65    }
66
67    /**
68     * Return the node on which the ID attribute is set.
69     *
70     * @return Element Headline node, normally a `<h1>`-`<h6>` element (unless it's a placeholder heading).
71     *   In integration tests and in JS, it can be a `<span class="mw-headline">` (see T363031).
72     */
73    public function getHeadlineNode(): Element {
74        // This value comes from CommentUtils::getHeadlineNode(), this function just guarantees the type
75        $headline = $this->getRange()->startContainer;
76        Assert::precondition( $headline instanceof Element, 'HeadingItem refers to an element node' );
77        return $headline;
78    }
79
80    public function isUneditableSection(): bool {
81        return $this->uneditableSection;
82    }
83
84    /**
85     * @param bool $uneditableSection The heading represents a section that can't be
86     *  edited on its own.
87     */
88    public function setUneditableSection( bool $uneditableSection ): void {
89        $this->uneditableSection = $uneditableSection;
90    }
91
92    /**
93     * @return int Heading level (1-6)
94     */
95    public function getHeadingLevel(): int {
96        return $this->headingLevel;
97    }
98
99    /**
100     * @param int $headingLevel Heading level (1-6)
101     */
102    public function setHeadingLevel( int $headingLevel ): void {
103        $this->headingLevel = $headingLevel;
104    }
105
106    public function isPlaceholderHeading(): bool {
107        return $this->placeholderHeading;
108    }
109
110    public function setPlaceholderHeading( bool $placeholderHeading ): void {
111        $this->placeholderHeading = $placeholderHeading;
112    }
113
114    /**
115     * @inheritDoc
116     */
117    public function jsonSerialize( bool $deep = false, ?callable $callback = null ): array {
118        $data = $this->traitJsonSerialize( $deep, $callback );
119
120        // When this is false (which is most of the time), omit the key for efficiency
121        if ( $this->isUneditableSection() ) {
122            $data[ 'uneditableSection' ] = true;
123        }
124        return $data;
125    }
126}