mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-15 02:55:04 +00:00
Inline and streamline code in the formatter classes
* Don't use string comparisons to compare numbers. * Avoid isset() for variables that are guaranteed to exist. * Inline two small "gen…" functions that are only called once. * Move the fallback code path out of getLinkLabel(). Before it was always called. Now it's only called when needed. Change-Id: I42073f57f21d32c7936954da776ef3a393410020
This commit is contained in:
parent
008526b3aa
commit
b145869980
|
@ -16,9 +16,9 @@ class FootnoteBodyFormatter {
|
|||
* 'cite_references_link_many_format', defined in
|
||||
* 'cite_references_link_many_format_backlink_labels
|
||||
*
|
||||
* @var string[]
|
||||
* @var string[]|null
|
||||
*/
|
||||
private $backlinkLabels;
|
||||
private $backlinkLabels = null;
|
||||
|
||||
/**
|
||||
* @var CiteErrorReporter
|
||||
|
@ -68,14 +68,15 @@ class FootnoteBodyFormatter {
|
|||
// Add new lines between the list items (ref entries) to avoid confusing tidy (T15073).
|
||||
// Note: This builds a string of wikitext, not html.
|
||||
$parserInput = "\n";
|
||||
/** @var string|bool $indented */
|
||||
$indented = false;
|
||||
// After sorting the list, we can assume that references are in the same order as their
|
||||
// numbering. Subreferences will come immediately after their parent.
|
||||
uasort(
|
||||
$groupRefs,
|
||||
function ( array $a, array $b ) : int {
|
||||
return ( $a['number'] ?? '' ) <=> ( $b['number'] ?? '' ) ?:
|
||||
( $a['extendsIndex'] ?? '0' ) <=> ( $b['extendsIndex'] ?? '0' );
|
||||
$cmp = ( $a['number'] ?? 0 ) - ( $b['number'] ?? 0 );
|
||||
return $cmp ?: ( $a['extendsIndex'] ?? 0 ) - ( $b['extendsIndex'] ?? 0 );
|
||||
}
|
||||
);
|
||||
foreach ( $groupRefs as $key => $value ) {
|
||||
|
@ -104,10 +105,10 @@ class FootnoteBodyFormatter {
|
|||
if ( count( $groupRefs ) > 10 ) {
|
||||
$wrapClasses[] = 'mw-references-columns';
|
||||
}
|
||||
return Html::rawElement( 'div', [ 'class' => $wrapClasses ], $ret );
|
||||
} else {
|
||||
return $ret;
|
||||
$ret = Html::rawElement( 'div', [ 'class' => $wrapClasses ], $ret );
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -132,7 +133,7 @@ class FootnoteBodyFormatter {
|
|||
* @return string Wikitext, wrapped in a single <li> element
|
||||
*/
|
||||
private function referencesFormatEntry( $key, array $val, bool $isSectionPreview ) : string {
|
||||
$text = $this->referenceText( $key, ( $val['text'] ?? null ), $isSectionPreview );
|
||||
$text = $this->referenceText( $key, $val['text'] ?? null, $isSectionPreview );
|
||||
$error = '';
|
||||
$extraAttributes = '';
|
||||
|
||||
|
@ -252,22 +253,17 @@ class FootnoteBodyFormatter {
|
|||
* @return string
|
||||
*/
|
||||
private function referencesFormatEntryAlternateBacklinkLabel( int $offset ) : string {
|
||||
if ( !isset( $this->backlinkLabels ) ) {
|
||||
$this->genBacklinkLabels();
|
||||
if ( $this->backlinkLabels === null ) {
|
||||
$this->backlinkLabels = preg_split(
|
||||
'/\s+/',
|
||||
wfMessage( 'cite_references_link_many_format_backlink_labels' )
|
||||
->inContentLanguage()
|
||||
->plain()
|
||||
);
|
||||
}
|
||||
return $this->backlinkLabels[$offset]
|
||||
?? $this->errorReporter->plain( 'cite_error_references_no_backlink_label', null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the labels to pass to the
|
||||
* 'cite_references_link_many_format' message, the format is an
|
||||
* arbitrary number of tokens separated by whitespace.
|
||||
*/
|
||||
private function genBacklinkLabels() {
|
||||
$text = wfMessage( 'cite_references_link_many_format_backlink_labels' )
|
||||
->inContentLanguage()->plain();
|
||||
$this->backlinkLabels = preg_split( '/\s+/', $text );
|
||||
return $this->backlinkLabels[$offset]
|
||||
?? $this->errorReporter->plain( 'cite_error_references_no_backlink_label' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -12,9 +12,7 @@ use Sanitizer;
|
|||
class FootnoteMarkFormatter {
|
||||
|
||||
/**
|
||||
* The links to use per group, in order.
|
||||
*
|
||||
* @var (string[]|false)[]
|
||||
* @var string[][]
|
||||
*/
|
||||
private $linkLabels = [];
|
||||
|
||||
|
@ -59,10 +57,21 @@ class FootnoteMarkFormatter {
|
|||
* @return string
|
||||
*/
|
||||
public function linkRef( string $group, array $ref ) : string {
|
||||
$language = MediaWikiServices::getInstance()->getContentLanguage();
|
||||
|
||||
$label = $this->getLinkLabel( $group, $ref['number'] );
|
||||
if ( $label === null ) {
|
||||
$label = $language->formatNum( $ref['number'] );
|
||||
if ( $group !== Cite::DEFAULT_GROUP ) {
|
||||
$label = "$group $label";
|
||||
}
|
||||
}
|
||||
if ( isset( $ref['extendsIndex'] ) ) {
|
||||
$label .= $language->formatNum( '.' . $ref['extendsIndex'] );
|
||||
}
|
||||
|
||||
$key = $ref['name'] ?? $ref['key'];
|
||||
$count = $ref['name'] ? $ref['key'] . '-' . $ref['count'] : null;
|
||||
$label = $ref['number'] .
|
||||
( isset( $ref['extendsIndex'] ) ? '.' . $ref['extendsIndex'] : '' );
|
||||
$subkey = $ref['name'] ? '-' . $ref['key'] : null;
|
||||
|
||||
return $this->parser->recursiveTagParse(
|
||||
|
@ -70,12 +79,7 @@ class FootnoteMarkFormatter {
|
|||
'cite_reference_link',
|
||||
$this->citeKeyFormatter->refKey( $key, $count ),
|
||||
$this->citeKeyFormatter->getReferencesKey( $key . $subkey ),
|
||||
Sanitizer::safeEncodeAttribute(
|
||||
$this->getLinkLabel( $label, $group,
|
||||
( ( $group === Cite::DEFAULT_GROUP ) ? '' : "$group " ) .
|
||||
MediaWikiServices::getInstance()->getContentLanguage()
|
||||
->formatNum( $label ) )
|
||||
)
|
||||
Sanitizer::safeEncodeAttribute( $label )
|
||||
)->inContentLanguage()->plain()
|
||||
);
|
||||
}
|
||||
|
@ -86,41 +90,24 @@ class FootnoteMarkFormatter {
|
|||
* [ 'a', 'b', 'c', ...].
|
||||
* Return an error if the offset > the # of array items
|
||||
*
|
||||
* @param int|string $offset
|
||||
* @param string $group The group name
|
||||
* @param string $label The text to use if there's no message for them.
|
||||
* @param int $number Expected to start at 1
|
||||
*
|
||||
* @return string
|
||||
* @return string|null Returns null if no custom labels for this group exist
|
||||
*/
|
||||
private function getLinkLabel( $offset, string $group, string $label ) : string {
|
||||
private function getLinkLabel( string $group, int $number ) : ?string {
|
||||
$message = "cite_link_label_group-$group";
|
||||
if ( !isset( $this->linkLabels[$group] ) ) {
|
||||
$this->genLinkLabels( $group, $message );
|
||||
}
|
||||
if ( $this->linkLabels[$group] === false ) {
|
||||
// Use normal representation, ie. "$group 1", "$group 2"...
|
||||
return $label;
|
||||
$msg = wfMessage( $message )->inContentLanguage();
|
||||
$this->linkLabels[$group] = $msg->isDisabled() ? [] : preg_split( '/\s+/', $msg->plain() );
|
||||
}
|
||||
|
||||
// FIXME: This doesn't work as expected when $offset is a string like "1.2"
|
||||
return $this->linkLabels[$group][$offset - 1]
|
||||
?? $this->errorReporter->plain( 'cite_error_no_link_label_group', [ $group, $message ] );
|
||||
if ( !$this->linkLabels[$group] ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->linkLabels[$group][$number - 1]
|
||||
?? $this->errorReporter->plain( 'cite_error_no_link_label_group', $group, $message );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the labels to pass to the
|
||||
* 'cite_reference_link' message instead of numbers, the format is an
|
||||
* arbitrary number of tokens separated by whitespace.
|
||||
*
|
||||
* @param string $group
|
||||
* @param string $message
|
||||
*/
|
||||
private function genLinkLabels( string $group, string $message ) {
|
||||
$text = false;
|
||||
$msg = wfMessage( $message )->inContentLanguage();
|
||||
if ( $msg->exists() ) {
|
||||
$text = $msg->plain();
|
||||
}
|
||||
$this->linkLabels[$group] = $text ? preg_split( '/\s+/', $text ) : false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue