| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite; |
| 4 |
|
| 5 |
use MediaWiki\Parser\Parser; |
| 6 |
use MediaWiki\Parser\Sanitizer; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class FootnoteMarkFormatter { |
| 15 |
|
| 16 |
*/ |
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function linkRef( Parser $parser, ReferenceStackItem $ref ): string { |
| 44 |
$label = $this->makeLabel( $ref, $parser ); |
| 45 |
|
| 46 |
$key = $ref->name ?? $ref->key; |
| 47 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 99 |
if ( !$this->linkLabels[$group] ) { |
| 100 |
return null; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 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 |
} |