From c2365241387a62d99c0eaa83cb52b730826fe946 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Thu, 28 Nov 2019 15:39:11 +0100 Subject: [PATCH] Extract footnote mark rendering Change-Id: I79de89e46da36dc1f0ee2b2fdb9a139e6434fde2 --- src/Cite.php | 97 +++----------------------- src/FootnoteMarkFormatter.php | 128 ++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 88 deletions(-) create mode 100644 src/FootnoteMarkFormatter.php diff --git a/src/Cite.php b/src/Cite.php index fec2fecc5..f50a15eac 100644 --- a/src/Cite.php +++ b/src/Cite.php @@ -34,7 +34,7 @@ use StatusValue; class Cite { - private const DEFAULT_GROUP = ''; + public const DEFAULT_GROUP = ''; /** * Wikitext attribute name for Book Referencing. @@ -55,13 +55,6 @@ class Cite { */ private $mBacklinkLabels; - /** - * The links to use per group, in order. - * - * @var (string[]|false)[] - */ - private $mLinkLabels = []; - /** * @var Parser */ @@ -77,6 +70,11 @@ class Cite { */ private $isSectionPreview; + /** + * @var FootnoteMarkFormatter + */ + private $footnoteMarkFormatter; + /** * @var CiteErrorReporter */ @@ -135,6 +133,8 @@ class Cite { ); $this->referenceStack = new ReferenceStack( $this->errorReporter ); $this->citeKeyFormatter = new CiteKeyFormatter(); + $this->footnoteMarkFormatter = new FootnoteMarkFormatter( + $this->mParser, $this->errorReporter, $this->citeKeyFormatter ); } } @@ -362,7 +362,7 @@ class Cite { return ''; } else { [ $key, $count, $label, $subkey ] = $result; - return $this->linkRef( $group, $key, $count, $label, $subkey ); + return $this->footnoteMarkFormatter->linkRef( $group, $key, $count, $label, $subkey ); } } @@ -667,68 +667,6 @@ class Cite { ?? $this->errorReporter->plain( 'cite_error_references_no_backlink_label', null ); } - /** - * Generate a custom format link for a group given an offset, e.g. - * the second is b if $this->mLinkLabels["foo"] = - * [ 'a', 'b', 'c', ...]. - * Return an error if the offset > the # of array items - * - * @param int $offset - * @param string $group The group name - * @param string $label The text to use if there's no message for them. - * - * @return string - */ - private function getLinkLabel( $offset, $group, $label ) { - $message = "cite_link_label_group-$group"; - if ( !isset( $this->mLinkLabels[$group] ) ) { - $this->genLinkLabels( $group, $message ); - } - if ( $this->mLinkLabels[$group] === false ) { - // Use normal representation, ie. "$group 1", "$group 2"... - return $label; - } - - return $this->mLinkLabels[$group][$offset - 1] - ?? $this->errorReporter->plain( 'cite_error_no_link_label_group', [ $group, $message ] ); - } - - /** - * Generate a link ( element from a key - * and return XHTML ready for output - * - * @suppress SecurityCheck-DoubleEscaped - * @param string $group - * @param string $key The key for the link - * @param int|null $count The index of the key, used for distinguishing - * multiple occurrences of the same key - * @param int $label The label to use for the link, I want to - * use the same label for all occurrences of - * the same named reference. - * @param string|null $subkey - * - * @return string - */ - private function linkRef( $group, $key, $count, $label, $subkey ) { - $contLang = MediaWikiServices::getInstance()->getContentLanguage(); - - return $this->mParser->recursiveTagParse( - wfMessage( - 'cite_reference_link', - $this->citeKeyFormatter->normalizeKey( - $this->citeKeyFormatter->refKey( $key, $count ) - ), - $this->citeKeyFormatter->normalizeKey( - $this->citeKeyFormatter->getReferencesKey( $key . $subkey ) - ), - Sanitizer::safeEncodeAttribute( - $this->getLinkLabel( $label, $group, - ( ( $group === self::DEFAULT_GROUP ) ? '' : "$group " ) . $contLang->formatNum( $label ) ) - ) - )->inContentLanguage()->plain() - ); - } - /** * This does approximately the same thing as * Language::listToText() but due to this being used for a @@ -762,23 +700,6 @@ class Cite { $this->mBacklinkLabels = preg_split( '/\s+/', $text ); } - /** - * 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( $group, $message ) { - $text = false; - $msg = wfMessage( $message )->inContentLanguage(); - if ( $msg->exists() ) { - $text = $msg->plain(); - } - $this->mLinkLabels[$group] = $text ? preg_split( '/\s+/', $text ) : false; - } - /** * Gets run when Parser::clearState() gets run, since we don't * want the counts to transcend pages and other instances diff --git a/src/FootnoteMarkFormatter.php b/src/FootnoteMarkFormatter.php new file mode 100644 index 000000000..5b944238e --- /dev/null +++ b/src/FootnoteMarkFormatter.php @@ -0,0 +1,128 @@ +parser = $parser; + $this->citeKeyFormatter = $citeKeyFormatter; + $this->errorReporter = $errorReporter; + } + + /** + * Generate a link ( element from a key + * and return XHTML ready for output + * + * @suppress SecurityCheck-DoubleEscaped + * @param string $group + * @param string $key The key for the link + * @param int|null $count The index of the key, used for distinguishing + * multiple occurrences of the same key + * @param int $label The label to use for the link, I want to + * use the same label for all occurrences of + * the same named reference. + * @param string|null $subkey + * + * @return string + */ + public function linkRef( $group, $key, $count, $label, $subkey ) { + $contLang = MediaWikiServices::getInstance()->getContentLanguage(); + + return $this->parser->recursiveTagParse( + wfMessage( + 'cite_reference_link', + $this->citeKeyFormatter->normalizeKey( + $this->citeKeyFormatter->refKey( $key, $count ) + ), + $this->citeKeyFormatter->normalizeKey( + $this->citeKeyFormatter->getReferencesKey( $key . $subkey ) + ), + Sanitizer::safeEncodeAttribute( + $this->getLinkLabel( $label, $group, + ( ( $group === Cite::DEFAULT_GROUP ) ? '' : "$group " ) . + $contLang->formatNum( $label ) ) + ) + )->inContentLanguage()->plain() + ); + } + + /** + * Generate a custom format link for a group given an offset, e.g. + * the second is b if $this->mLinkLabels["foo"] = + * [ 'a', 'b', 'c', ...]. + * Return an error if the offset > the # of array items + * + * @param int $offset + * @param string $group The group name + * @param string $label The text to use if there's no message for them. + * + * @return string + */ + private function getLinkLabel( $offset, $group, $label ) { + $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; + } + + return $this->linkLabels[$group][$offset - 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( $group, $message ) { + $text = false; + $msg = wfMessage( $message )->inContentLanguage(); + if ( $msg->exists() ) { + $text = $msg->plain(); + } + $this->linkLabels[$group] = $text ? preg_split( '/\s+/', $text ) : false; + } +}