Extract key formatting

Change-Id: I155ec6f3e21075587dbcfdfdc346f28f958e3c15
This commit is contained in:
Adam Wight 2019-11-28 15:35:23 +01:00
parent 13598ba11e
commit a4c056f59b
4 changed files with 122 additions and 88 deletions

View file

@ -116,6 +116,11 @@ class Cite {
*/
private $referenceStack;
/**
* @var CiteKeyFormatter
*/
private $citeKeyFormatter;
/**
* @param Parser $parser
*/
@ -129,6 +134,7 @@ class Cite {
$parser
);
$this->referenceStack = new ReferenceStack( $this->errorReporter );
$this->citeKeyFormatter = new CiteKeyFormatter();
}
}
@ -555,8 +561,8 @@ class Cite {
if ( isset( $val['follow'] ) ) {
return wfMessage(
'cite_references_no_link',
$this->normalizeKey(
self::getReferencesKey( $val['follow'] )
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->getReferencesKey( $val['follow'] )
),
$text
)->inContentLanguage()->plain();
@ -567,15 +573,15 @@ class Cite {
// Anonymous, auto-numbered references can't be reused and get marked with a -1.
if ( $val['count'] < 0 ) {
$id = $val['key'];
$backlinkId = $this->refKey( $val['key'] );
$backlinkId = $this->citeKeyFormatter->refKey( $val['key'] );
} else {
$id = $key . '-' . $val['key'];
$backlinkId = $this->refKey( $key, $val['key'] . '-' . $val['count'] );
$backlinkId = $this->citeKeyFormatter->refKey( $key, $val['key'] . '-' . $val['count'] );
}
return wfMessage(
'cite_references_link_one',
$this->normalizeKey( self::getReferencesKey( $id ) ),
$this->normalizeKey( $backlinkId ),
$this->citeKeyFormatter->normalizeKey( $this->citeKeyFormatter->getReferencesKey( $id ) ),
$this->citeKeyFormatter->normalizeKey( $backlinkId ),
$text . $error,
$extraAttributes
)->inContentLanguage()->plain();
@ -587,8 +593,8 @@ class Cite {
for ( $i = 0; $i <= ( $val['count'] ?? -1 ); $i++ ) {
$backlinks[] = wfMessage(
'cite_references_link_many_format',
$this->normalizeKey(
$this->refKey( $key, $val['key'] . '-' . $i )
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->refKey( $key, $val['key'] . '-' . $i )
),
$this->referencesFormatEntryNumericBacklinkLabel(
$val['number'],
@ -600,8 +606,8 @@ class Cite {
}
return wfMessage(
'cite_references_link_many',
$this->normalizeKey(
self::getReferencesKey( $key . '-' . ( $val['key'] ?? '' ) )
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->getReferencesKey( $key . '-' . ( $val['key'] ?? '' ) )
),
$this->listToText( $backlinks ),
$text . $error,
@ -687,41 +693,6 @@ class Cite {
?? $this->errorReporter->plain( 'cite_error_no_link_label_group', [ $group, $message ] );
}
/**
* 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
*/
private 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 static function getReferencesKey( $key ) {
$prefix = wfMessage( 'cite_references_link_prefix' )->inContentLanguage()->text();
$suffix = wfMessage( 'cite_references_link_suffix' )->inContentLanguage()->text();
return "$prefix$key$suffix";
}
/**
* Generate a link (<sup ...) for the <ref> element from a key
* and return XHTML ready for output
@ -744,11 +715,11 @@ class Cite {
return $this->mParser->recursiveTagParse(
wfMessage(
'cite_reference_link',
$this->normalizeKey(
$this->refKey( $key, $count )
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->refKey( $key, $count )
),
$this->normalizeKey(
self::getReferencesKey( $key . $subkey )
$this->citeKeyFormatter->normalizeKey(
$this->citeKeyFormatter->getReferencesKey( $key . $subkey )
),
Sanitizer::safeEncodeAttribute(
$this->getLinkLabel( $label, $group,
@ -758,20 +729,6 @@ class Cite {
);
}
/**
* Normalizes and sanitizes a reference key
*
* @param string $key
* @return string
*/
private function normalizeKey( $key ) {
$ret = Sanitizer::escapeIdForAttribute( $key );
$ret = preg_replace( '/__+/', '_', $ret );
$ret = Sanitizer::safeEncodeAttribute( $ret );
return $ret;
}
/**
* This does approximately the same thing as
* Language::listToText() but due to this being used for a

59
src/CiteKeyFormatter.php Normal file
View file

@ -0,0 +1,59 @@
<?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;
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace Cite\Tests\Unit;
use Cite\CiteKeyFormatter;
use MediaWikiUnitTestCase;
/**
* @coversDefaultClass \Cite\CiteKeyFormatter
*/
class CiteKeyFormatterTest extends MediaWikiUnitTestCase {
public function setUp() : void {
parent::setUp();
global $wgFragmentMode;
$wgFragmentMode = [ 'html5' ];
}
/**
* @covers ::normalizeKey
* @dataProvider provideKeyNormalizations
*/
public function testNormalizeKey( $key, $expected ) {
$keyFormatter = new CiteKeyFormatter();
$this->assertSame( $expected, $keyFormatter->normalizeKey( $key ) );
}
public function provideKeyNormalizations() {
return [
[ 'a b', 'a_b' ],
[ 'a __ b', 'a_b' ],
[ ':', ':' ],
[ "\t\n", '&#9;&#10;' ],
[ "'", '&#039;' ],
[ "''", '&#039;&#039;' ],
[ '"%&/<>?[]{|}', '&quot;%&amp;/&lt;&gt;?&#91;&#93;&#123;&#124;&#125;' ],
[ 'ISBN', '&#73;SBN' ],
];
}
}

View file

@ -15,11 +15,10 @@ use Wikimedia\TestingAccessWrapper;
class CiteUnitTest extends \MediaWikiUnitTestCase {
protected function setUp() : void {
global $wgCiteBookReferencing, $wgFragmentMode;
global $wgCiteBookReferencing;
parent::setUp();
$wgCiteBookReferencing = true;
$wgFragmentMode = [ 'html5' ];
}
/**
@ -160,27 +159,4 @@ class CiteUnitTest extends \MediaWikiUnitTestCase {
];
}
/**
* @covers ::normalizeKey
* @dataProvider provideKeyNormalizations
*/
public function testNormalizeKey( $key, $expected ) {
/** @var Cite $cite */
$cite = TestingAccessWrapper::newFromObject( new Cite() );
$this->assertSame( $expected, $cite->normalizeKey( $key ) );
}
public function provideKeyNormalizations() {
return [
[ 'a b', 'a_b' ],
[ 'a __ b', 'a_b' ],
[ ':', ':' ],
[ "\t\n", '&#9;&#10;' ],
[ "'", '&#039;' ],
[ "''", '&#039;&#039;' ],
[ '"%&/<>?[]{|}', '&quot;%&amp;/&lt;&gt;?&#91;&#93;&#123;&#124;&#125;' ],
[ 'ISBN', '&#73;SBN' ],
];
}
}