Debian Codegraph @

extensions/Cite/src/Parsoid/RefGroup.php

raw symbols SCIP
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Cite\Parsoid;
5
6 use Wikimedia\Parsoid\DOM\Document;
7 use Wikimedia\Parsoid\DOM\Element;
8 use Wikimedia\Parsoid\Ext\DOMUtils;
9 use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
10 use Wikimedia\Parsoid\Utils\DOMCompat;
11
12 /**
13 * Helper class used by `<references>` implementation.
14 * @license GPL-2.0-or-later
15 */
16 class RefGroup {
17
18 public string $name;
19 /** @var RefGroupItem[] */
20 public array $refs = [];
21 /** @var array<string,RefGroupItem> Lookup map only for named refs */
22 public array $indexByName = [];
23
24 public function __construct( string $group = '' ) {
25 $this->name = $group;
26 }
27
28 /**
29 * Generate leading linkbacks
30 */
31 private static function createLinkback(
32 ParsoidExtensionAPI $extApi, string $target, ?string $group,
33 string $text, Document $ownerDoc
34 ): Element {
35 $a = $ownerDoc->createElement( 'a' );
36 $span = $ownerDoc->createElement( 'span' );
37 $a->setAttribute( 'href', $extApi->getPageUri() . '#' . $target );
38 $span->setAttribute( 'class', 'mw-linkback-text' );
39 if ( $group ) {
40 $a->setAttribute( 'data-mw-group', $group );
41 }
42 $span->appendChild( $ownerDoc->createTextNode( $text . ' ' ) );
43 $a->appendChild( $span );
44 return $a;
45 }
46
47 public function renderLine(
48 ParsoidExtensionAPI $extApi, Element $refsList, RefGroupItem $ref
49 ): void {
50 $ownerDoc = $refsList->ownerDocument;
51
52 // Generate the li and set ref content first, so the HTML gets parsed.
53 // We then append the rest of the ref nodes before the first node
54 $li = $ownerDoc->createElement( 'li' );
55 $refDir = $ref->dir;
56 $refTarget = $ref->target;
57 $refContentId = $ref->contentId;
58 $refGroup = $ref->group;
59 DOMUtils::addAttributes( $li, [
60 'about' => '#' . $refTarget,
61 'id' => $refTarget,
62 'class' => ( $refDir === 'rtl' || $refDir === 'ltr' ) ? 'mw-cite-dir-' . $refDir : null
63 ]
64 );
65 $reftextSpan = $ownerDoc->createElement( 'span' );
66 DOMUtils::addAttributes(
67 $reftextSpan,
68 [
69 'id' => 'mw-reference-text-' . $refTarget,
70 // Add both mw-reference-text & reference-text for b/c.
71 // We will remove duplicate classes in the future.
72 'class' => 'mw-reference-text reference-text',
73 ]
74 );
75 if ( $refContentId ) {
76 // `sup` is the wrapper created by Ref::sourceToDom()'s call to
77 // `extApi->extTagToDOM()`. Only its contents are relevant.
78 $sup = $extApi->getContentDOM( $refContentId )->firstChild;
79 DOMUtils::migrateChildren( $sup, $reftextSpan );
80 '@phan-var Element $sup'; /** @var Element $sup */
81 DOMCompat::remove( $sup );
82 $extApi->clearContentDOM( $refContentId );
83 }
84 $li->appendChild( $reftextSpan );
85
86 // mw:referencedBy is added to the <span> for the named refs case
87 // and to the <a> tag to the unnamed refs case. This difference
88 // is used by CSS to style backlinks in MediaWiki:Common.css
89 // of various wikis.
90 $linkbackSpan = $ownerDoc->createElement( 'span' );
91 if ( count( $ref->linkbacks ) === 1 ) {
92 $linkback = self::createLinkback( $extApi, $ref->id, $refGroup, "↑", $ownerDoc );
93 DOMUtils::addRel( $linkback, 'mw:referencedBy' );
94 $linkbackSpan->appendChild( $linkback );
95 } else {
96 DOMUtils::addRel( $linkbackSpan, 'mw:referencedBy' );
97 foreach ( $ref->linkbacks as $i => $lb ) {
98 $linkbackSpan->appendChild(
99 self::createLinkback( $extApi, $lb, $refGroup, (string)( $i + 1 ), $ownerDoc )
100 );
101 }
102 }
103 DOMCompat::getClassList( $linkbackSpan )->add( 'mw-cite-backlink' );
104 $li->insertBefore( $linkbackSpan, $reftextSpan );
105
106 // Space before content node
107 $li->insertBefore( $ownerDoc->createTextNode( ' ' ), $reftextSpan );
108
109 // Add it to the ref list
110 $refsList->appendChild( $li );
111
112 // Backward-compatibility: add newline (T372889)
113 $refsList->appendChild( $ownerDoc->createTextNode( "\n" ) );
114 }
115 }