Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
15 / 15 |
AnchorFormatter | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
15 / 15 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
refKey | |
100.00% |
1 / 1 |
2 | |
100.00% |
6 / 6 |
|||
getReferencesKey | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
normalizeKey | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
<?php | |
namespace Cite; | |
use Sanitizer; | |
/** | |
* @license GPL-2.0-or-later | |
*/ | |
class AnchorFormatter { | |
/** | |
* @var ReferenceMessageLocalizer | |
*/ | |
private $messageLocalizer; | |
/** | |
* @param ReferenceMessageLocalizer $messageLocalizer | |
*/ | |
public function __construct( ReferenceMessageLocalizer $messageLocalizer ) { | |
$this->messageLocalizer = $messageLocalizer; | |
} | |
/** | |
* Return an id for use in wikitext output based on a key and | |
* optionally the number of it, used in <references>, not <ref> | |
* (since otherwise it would link to itself) | |
* | |
* @param string $key | |
* @param string|null $num The number of the key | |
* | |
* @return string A key for use in wikitext | |
*/ | |
public function refKey( string $key, string $num = null ): string { | |
$prefix = $this->messageLocalizer->msg( 'cite_reference_link_prefix' )->text(); | |
$suffix = $this->messageLocalizer->msg( 'cite_reference_link_suffix' )->text(); | |
if ( $num !== null ) { | |
$key = $this->messageLocalizer->msg( 'cite_reference_link_key_with_num', $key, $num ) | |
->plain(); | |
} | |
return $this->normalizeKey( $prefix . $key . $suffix ); | |
} | |
/** | |
* Return an id for use in wikitext output based on a key and | |
* optionally the number of it, used in <ref>, not <references> | |
* (since otherwise it would link to itself) | |
* | |
* @param string $key | |
* | |
* @return string A key for use in wikitext | |
*/ | |
public function getReferencesKey( string $key ): string { | |
$prefix = $this->messageLocalizer->msg( 'cite_references_link_prefix' )->text(); | |
$suffix = $this->messageLocalizer->msg( 'cite_references_link_suffix' )->text(); | |
return $this->normalizeKey( $prefix . $key . $suffix ); | |
} | |
/** | |
* Normalizes and sanitizes a reference key | |
* | |
* @param string $key | |
* | |
* @return string | |
*/ | |
private function normalizeKey( string $key ): string { | |
$ret = Sanitizer::escapeIdForLink( $key ); | |
$ret = preg_replace( '/__+/', '_', $ret ); | |
$ret = Sanitizer::safeEncodeAttribute( $ret ); | |
return $ret; | |
} | |
} |