2019-11-28 14:39:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Parser
|
|
|
|
*/
|
|
|
|
private $parser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var CiteKeyFormatter
|
|
|
|
*/
|
|
|
|
private $citeKeyFormatter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var CiteErrorReporter
|
|
|
|
*/
|
|
|
|
private $errorReporter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param CiteErrorReporter $errorReporter
|
|
|
|
* @param CiteKeyFormatter $citeKeyFormatter
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Parser $parser,
|
|
|
|
CiteErrorReporter $errorReporter,
|
|
|
|
CiteKeyFormatter $citeKeyFormatter
|
|
|
|
) {
|
|
|
|
$this->parser = $parser;
|
|
|
|
$this->citeKeyFormatter = $citeKeyFormatter;
|
|
|
|
$this->errorReporter = $errorReporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a link (<sup ...) for the <ref> element from a key
|
|
|
|
* and return XHTML ready for output
|
|
|
|
*
|
|
|
|
* @suppress SecurityCheck-DoubleEscaped
|
|
|
|
* @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-11-29 23:26:58 +00:00
|
|
|
public function linkRef( string $group, array $ref ) : string {
|
2019-12-03 09:00:58 +00:00
|
|
|
$language = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
|
|
|
|
$label = $this->getLinkLabel( $group, $ref['number'] );
|
|
|
|
if ( $label === null ) {
|
|
|
|
$label = $language->formatNum( $ref['number'] );
|
|
|
|
if ( $group !== Cite::DEFAULT_GROUP ) {
|
|
|
|
$label = "$group $label";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( isset( $ref['extendsIndex'] ) ) {
|
2019-12-04 08:10:03 +00:00
|
|
|
$label .= '.' . $language->formatNum( $ref['extendsIndex'], true );
|
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-11-28 14:39:11 +00:00
|
|
|
return $this->parser->recursiveTagParse(
|
|
|
|
wfMessage(
|
|
|
|
'cite_reference_link',
|
2019-11-29 14:00:39 +00:00
|
|
|
$this->citeKeyFormatter->refKey( $key, $count ),
|
|
|
|
$this->citeKeyFormatter->getReferencesKey( $key . $subkey ),
|
2019-12-03 09:00:58 +00:00
|
|
|
Sanitizer::safeEncodeAttribute( $label )
|
2019-11-28 14:39:11 +00:00
|
|
|
)->inContentLanguage()->plain()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @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-03 09:00:58 +00:00
|
|
|
private function getLinkLabel( 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-03 09:00:58 +00:00
|
|
|
$msg = wfMessage( $message )->inContentLanguage();
|
|
|
|
$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-03 09:00:58 +00:00
|
|
|
return $this->linkLabels[$group][$number - 1]
|
|
|
|
?? $this->errorReporter->plain( 'cite_error_no_link_label_group', $group, $message );
|
2019-11-28 14:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|