2019-11-28 14:39:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
|
|
|
use Parser;
|
|
|
|
use Sanitizer;
|
|
|
|
|
2019-11-29 14:00:39 +00:00
|
|
|
/**
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2019-11-28 14:39:11 +00:00
|
|
|
class FootnoteMarkFormatter {
|
|
|
|
|
|
|
|
/**
|
2019-12-03 09:00:58 +00:00
|
|
|
* @var string[][]
|
2019-11-28 14:39:11 +00:00
|
|
|
*/
|
|
|
|
private $linkLabels = [];
|
|
|
|
|
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @var AnchorFormatter
|
2019-11-28 14:39:11 +00:00
|
|
|
*/
|
2019-12-09 16:08:57 +00:00
|
|
|
private $anchorFormatter;
|
2019-11-28 14:39:11 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @var ErrorReporter
|
2019-11-28 14:39:11 +00:00
|
|
|
*/
|
|
|
|
private $errorReporter;
|
|
|
|
|
2019-12-05 08:37:17 +00:00
|
|
|
/**
|
|
|
|
* @var ReferenceMessageLocalizer
|
|
|
|
*/
|
|
|
|
private $messageLocalizer;
|
|
|
|
|
2019-11-28 14:39:11 +00:00
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @param ErrorReporter $errorReporter
|
|
|
|
* @param AnchorFormatter $anchorFormatter
|
2019-12-05 08:37:17 +00:00
|
|
|
* @param ReferenceMessageLocalizer $messageLocalizer
|
2019-11-28 14:39:11 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2019-12-09 16:08:57 +00:00
|
|
|
ErrorReporter $errorReporter,
|
|
|
|
AnchorFormatter $anchorFormatter,
|
2019-12-05 08:37:17 +00:00
|
|
|
ReferenceMessageLocalizer $messageLocalizer
|
2019-11-28 14:39:11 +00:00
|
|
|
) {
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->anchorFormatter = $anchorFormatter;
|
2019-11-28 14:39:11 +00:00
|
|
|
$this->errorReporter = $errorReporter;
|
2019-12-05 08:37:17 +00:00
|
|
|
$this->messageLocalizer = $messageLocalizer;
|
2019-11-28 14:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a link (<sup ...) for the <ref> element from a key
|
|
|
|
* and return XHTML ready for output
|
|
|
|
*
|
|
|
|
* @suppress SecurityCheck-DoubleEscaped
|
2019-12-11 14:03:09 +00:00
|
|
|
* @param Parser $parser
|
2019-11-28 14:39:11 +00:00
|
|
|
* @param string $group
|
2019-11-29 23:26:58 +00:00
|
|
|
* @param array $ref Dictionary with ReferenceStack ref format
|
2019-11-28 14:39:11 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-11 14:03:09 +00:00
|
|
|
public function linkRef( Parser $parser, string $group, array $ref ) : string {
|
2019-12-11 15:05:19 +00:00
|
|
|
$label = $this->getLinkLabel( $parser, $group, $ref['number'] );
|
2019-12-03 09:00:58 +00:00
|
|
|
if ( $label === null ) {
|
2019-12-06 09:12:25 +00:00
|
|
|
$label = $this->messageLocalizer->formatNum( $ref['number'] );
|
2019-12-03 09:00:58 +00:00
|
|
|
if ( $group !== Cite::DEFAULT_GROUP ) {
|
|
|
|
$label = "$group $label";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( isset( $ref['extendsIndex'] ) ) {
|
2019-12-10 15:21:12 +00:00
|
|
|
$label .= '.' . $this->messageLocalizer->localizeDigits( $ref['extendsIndex'] );
|
2019-12-03 09:00:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 23:26:58 +00:00
|
|
|
$key = $ref['name'] ?? $ref['key'];
|
|
|
|
$count = $ref['name'] ? $ref['key'] . '-' . $ref['count'] : null;
|
|
|
|
$subkey = $ref['name'] ? '-' . $ref['key'] : null;
|
|
|
|
|
2019-12-11 14:03:09 +00:00
|
|
|
return $parser->recursiveTagParse(
|
2019-12-05 08:37:17 +00:00
|
|
|
$this->messageLocalizer->msg(
|
2019-11-28 14:39:11 +00:00
|
|
|
'cite_reference_link',
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->anchorFormatter->refKey( $key, $count ),
|
|
|
|
$this->anchorFormatter->getReferencesKey( $key . $subkey ),
|
2019-12-03 09:00:58 +00:00
|
|
|
Sanitizer::safeEncodeAttribute( $label )
|
2019-12-05 08:37:17 +00:00
|
|
|
)->plain()
|
2019-11-28 14:39:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a custom format link for a group given an offset, e.g.
|
|
|
|
* the second <ref group="foo"> is b if $this->mLinkLabels["foo"] =
|
|
|
|
* [ 'a', 'b', 'c', ...].
|
|
|
|
* Return an error if the offset > the # of array items
|
|
|
|
*
|
2019-12-11 15:05:19 +00:00
|
|
|
* @param Parser $parser
|
2019-11-28 14:39:11 +00:00
|
|
|
* @param string $group The group name
|
2019-12-03 09:00:58 +00:00
|
|
|
* @param int $number Expected to start at 1
|
2019-11-28 14:39:11 +00:00
|
|
|
*
|
2019-12-03 09:00:58 +00:00
|
|
|
* @return string|null Returns null if no custom labels for this group exist
|
2019-11-28 14:39:11 +00:00
|
|
|
*/
|
2019-12-11 15:05:19 +00:00
|
|
|
private function getLinkLabel( Parser $parser, string $group, int $number ) : ?string {
|
2019-11-28 14:39:11 +00:00
|
|
|
$message = "cite_link_label_group-$group";
|
|
|
|
if ( !isset( $this->linkLabels[$group] ) ) {
|
2019-12-05 08:37:17 +00:00
|
|
|
$msg = $this->messageLocalizer->msg( $message );
|
2019-12-03 09:00:58 +00:00
|
|
|
$this->linkLabels[$group] = $msg->isDisabled() ? [] : preg_split( '/\s+/', $msg->plain() );
|
2019-11-28 14:39:11 +00:00
|
|
|
}
|
2019-12-03 09:00:58 +00:00
|
|
|
|
|
|
|
if ( !$this->linkLabels[$group] ) {
|
|
|
|
return null;
|
2019-11-28 14:39:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:07:37 +00:00
|
|
|
return $this->linkLabels[$group][$number - 1] ?? $this->errorReporter->plain(
|
|
|
|
$parser,
|
|
|
|
'cite_error_no_link_label_group',
|
|
|
|
$group,
|
|
|
|
$message
|
|
|
|
);
|
2019-11-28 14:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|