2019-11-28 14:35:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
|
|
|
use Sanitizer;
|
|
|
|
|
2019-11-29 14:00:39 +00:00
|
|
|
/**
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2019-12-09 16:08:57 +00:00
|
|
|
class AnchorFormatter {
|
2019-11-28 14:35:23 +00:00
|
|
|
|
2019-12-04 17:20:06 +00:00
|
|
|
/**
|
|
|
|
* @var ReferenceMessageLocalizer
|
|
|
|
*/
|
|
|
|
private $messageLocalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ReferenceMessageLocalizer $messageLocalizer
|
|
|
|
*/
|
2019-12-09 16:08:57 +00:00
|
|
|
public function __construct( ReferenceMessageLocalizer $messageLocalizer ) {
|
2019-12-04 17:20:06 +00:00
|
|
|
$this->messageLocalizer = $messageLocalizer;
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:35:23 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2019-11-28 10:15:19 +00:00
|
|
|
* @param string|null $num The number of the key
|
2019-11-28 14:35:23 +00:00
|
|
|
* @return string A key for use in wikitext
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
public function refKey( string $key, string $num = null ) : string {
|
2019-12-04 17:08:46 +00:00
|
|
|
// FIXME: Move the message to 'cite_ref_link_*'
|
2019-12-04 17:20:06 +00:00
|
|
|
$prefix = $this->messageLocalizer->msg( 'cite_reference_link_prefix' )->text();
|
|
|
|
$suffix = $this->messageLocalizer->msg( 'cite_reference_link_suffix' )->text();
|
2019-11-28 14:35:23 +00:00
|
|
|
if ( $num !== null ) {
|
2019-12-04 17:20:06 +00:00
|
|
|
$key = $this->messageLocalizer->msg( 'cite_reference_link_key_with_num', $key, $num )
|
|
|
|
->plain();
|
2019-11-28 14:35:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 17:08:46 +00:00
|
|
|
return $this->normalizeKey( $prefix . $key . $suffix );
|
2019-11-28 14:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
public function getReferencesKey( string $key ) : string {
|
2019-12-04 17:20:06 +00:00
|
|
|
$prefix = $this->messageLocalizer->msg( 'cite_references_link_prefix' )->text();
|
|
|
|
$suffix = $this->messageLocalizer->msg( 'cite_references_link_suffix' )->text();
|
2019-11-28 14:35:23 +00:00
|
|
|
|
2019-12-04 17:08:46 +00:00
|
|
|
return $this->normalizeKey( $prefix . $key . $suffix );
|
2019-11-28 14:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes and sanitizes a reference key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
private function normalizeKey( string $key ) : string {
|
2019-11-28 14:35:23 +00:00
|
|
|
$ret = Sanitizer::escapeIdForAttribute( $key );
|
|
|
|
$ret = preg_replace( '/__+/', '_', $ret );
|
|
|
|
$ret = Sanitizer::safeEncodeAttribute( $ret );
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|