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-11-28 14:35:23 +00:00
|
|
|
class CiteKeyFormatter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 int|null $num The number of the key
|
|
|
|
* @return string A key for use in wikitext
|
|
|
|
*/
|
|
|
|
public function refKey( $key, $num = null ) {
|
|
|
|
$prefix = wfMessage( 'cite_reference_link_prefix' )->inContentLanguage()->text();
|
|
|
|
$suffix = wfMessage( 'cite_reference_link_suffix' )->inContentLanguage()->text();
|
|
|
|
if ( $num !== null ) {
|
|
|
|
$key = wfMessage( 'cite_reference_link_key_with_num', $key, $num )
|
|
|
|
->inContentLanguage()->plain();
|
|
|
|
}
|
|
|
|
|
2019-11-29 14:00:39 +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
|
|
|
|
*/
|
|
|
|
public function getReferencesKey( $key ) {
|
|
|
|
$prefix = wfMessage( 'cite_references_link_prefix' )->inContentLanguage()->text();
|
|
|
|
$suffix = wfMessage( 'cite_references_link_suffix' )->inContentLanguage()->text();
|
|
|
|
|
2019-11-29 14:00:39 +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-29 14:00:39 +00:00
|
|
|
private function normalizeKey( $key ) {
|
2019-11-28 14:35:23 +00:00
|
|
|
$ret = Sanitizer::escapeIdForAttribute( $key );
|
|
|
|
$ret = preg_replace( '/__+/', '_', $ret );
|
|
|
|
$ret = Sanitizer::safeEncodeAttribute( $ret );
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|