Debian Codegraph @

extensions/Cite/src/FootnoteMarkFormatter.php

raw symbols SCIP
1 <?php
2
3 namespace Cite;
4
5 use MediaWiki\Parser\Parser;
6 use MediaWiki\Parser\Sanitizer;
7
8 /**
9 * Footnote markers in the context of the Cite extension are the numbers in the article text, e.g.
10 * [1], that can be hovered or clicked to be able to read the attached footnote.
11 *
12 * @license GPL-2.0-or-later
13 */
14 class FootnoteMarkFormatter {
15
16 /** @var array<string,string[]> In-memory cache for the cite_link_label_group-… link label lists */
17 private array $linkLabels = [];
18
19 private AnchorFormatter $anchorFormatter;
20 private ErrorReporter $errorReporter;
21 private ReferenceMessageLocalizer $messageLocalizer;
22
23 public function __construct(
24 ErrorReporter $errorReporter,
25 AnchorFormatter $anchorFormatter,
26 ReferenceMessageLocalizer $messageLocalizer
27 ) {
28 $this->anchorFormatter = $anchorFormatter;
29 $this->errorReporter = $errorReporter;
30 $this->messageLocalizer = $messageLocalizer;
31 }
32
33 /**
34 * Generate a link (<sup ...) for the <ref> element from a key
35 * and return XHTML ready for output
36 *
37 * @suppress SecurityCheck-DoubleEscaped
38 * @param Parser $parser
39 * @param ReferenceStackItem $ref
40 *
41 * @return string HTML
42 */
43 public function linkRef( Parser $parser, ReferenceStackItem $ref ): string {
44 $label = $this->makeLabel( $ref, $parser );
45
46 $key = $ref->name ?? $ref->key;
47 // TODO: Use count without decrementing.
48 $count = $ref->name ? $ref->key . '-' . ( $ref->count - 1 ) : null;
49 $subkey = $ref->name ? '-' . $ref->key : null;
50
51 return $parser->recursiveTagParse(
52 $this->messageLocalizer->msg(
53 'cite_reference_link',
54 $this->anchorFormatter->backLinkTarget( $key, $count ),
55 $this->anchorFormatter->jumpLink( $key . $subkey ),
56 Sanitizer::safeEncodeAttribute( $label )
57 )->plain()
58 );
59 }
60
61 private function makeLabel( ReferenceStackItem $ref, Parser $parser ): string {
62 $label = $this->fetchCustomizedLinkLabel( $parser, $ref->group, $ref->number ) ??
63 $this->makeDefaultLabel( $ref );
64 if ( $ref->extendsIndex !== null ) {
65 $label .= '.' . $this->messageLocalizer->localizeDigits( (string)$ref->extendsIndex );
66 }
67 return $label;
68 }
69
70 private function makeDefaultLabel( ReferenceStackItem $ref ): string {
71 $label = $this->messageLocalizer->localizeDigits( (string)$ref->number );
72 return $ref->group === Cite::DEFAULT_GROUP ? $label : "$ref->group $label";
73 }
74
75 /**
76 * Generate a custom format link for a group given an offset, e.g.
77 * the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
78 * [ 'a', 'b', 'c', ...].
79 * Return an error if the offset > the # of array items
80 *
81 * @param Parser $parser
82 * @param string $group The group name
83 * @param int $number Expected to start at 1
84 *
85 * @return string|null Returns null if no custom labels for this group exist
86 */
87 private function fetchCustomizedLinkLabel( Parser $parser, string $group, int $number ): ?string {
88 if ( $group === Cite::DEFAULT_GROUP ) {
89 return null;
90 }
91
92 $message = "cite_link_label_group-$group";
93 if ( !array_key_exists( $group, $this->linkLabels ) ) {
94 $msg = $this->messageLocalizer->msg( $message );
95 $this->linkLabels[$group] = $msg->isDisabled() ? [] : preg_split( '/\s+/', $msg->plain() );
96 }
97
98 // Expected behavior for groups without custom labels
99 if ( !$this->linkLabels[$group] ) {
100 return null;
101 }
102
103 // Error message in case we run out of custom labels
104 return $this->linkLabels[$group][$number - 1] ?? $this->errorReporter->plain(
105 $parser,
106 'cite_error_no_link_label_group',
107 $group,
108 $message
109 );
110 }
111
112 }