mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2025-01-06 18:54:10 +00:00
60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
|
<?php
|
||
|
|
||
|
|
||
|
namespace Cite;
|
||
|
|
||
|
use Sanitizer;
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
return "$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( $key ) {
|
||
|
$prefix = wfMessage( 'cite_references_link_prefix' )->inContentLanguage()->text();
|
||
|
$suffix = wfMessage( 'cite_references_link_suffix' )->inContentLanguage()->text();
|
||
|
|
||
|
return "$prefix$key$suffix";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Normalizes and sanitizes a reference key
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @return string
|
||
|
*/
|
||
|
public function normalizeKey( $key ) {
|
||
|
$ret = Sanitizer::escapeIdForAttribute( $key );
|
||
|
$ret = preg_replace( '/__+/', '_', $ret );
|
||
|
$ret = Sanitizer::safeEncodeAttribute( $ret );
|
||
|
|
||
|
return $ret;
|
||
|
}
|
||
|
|
||
|
}
|