2019-11-28 14:39:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
|
|
|
use Html;
|
|
|
|
use Parser;
|
|
|
|
|
2019-11-29 14:00:39 +00:00
|
|
|
/**
|
|
|
|
* @license GPL-2.0-or-later
|
|
|
|
*/
|
2019-12-09 15:36:34 +00:00
|
|
|
class ReferencesFormatter {
|
2019-11-28 14:39:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The backlinks, in order, to pass as $3 to
|
|
|
|
* 'cite_references_link_many_format', defined in
|
|
|
|
* 'cite_references_link_many_format_backlink_labels
|
|
|
|
*
|
2019-12-03 09:00:58 +00:00
|
|
|
* @var string[]|null
|
2019-11-28 14:39:58 +00:00
|
|
|
*/
|
2019-12-03 09:00:58 +00:00
|
|
|
private $backlinkLabels = null;
|
2019-11-28 14:39:58 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @var ErrorReporter
|
2019-11-28 14:39:58 +00:00
|
|
|
*/
|
|
|
|
private $errorReporter;
|
|
|
|
|
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @var AnchorFormatter
|
2019-11-28 14:39:58 +00:00
|
|
|
*/
|
2019-12-09 16:08:57 +00:00
|
|
|
private $anchorFormatter;
|
2019-11-28 14:39:58 +00:00
|
|
|
|
2019-12-03 23:53:55 +00:00
|
|
|
/**
|
|
|
|
* @var ReferenceMessageLocalizer
|
|
|
|
*/
|
|
|
|
private $messageLocalizer;
|
|
|
|
|
2019-11-28 14:39:58 +00:00
|
|
|
/**
|
2019-12-09 16:08:57 +00:00
|
|
|
* @param ErrorReporter $errorReporter
|
|
|
|
* @param AnchorFormatter $anchorFormatter
|
2019-12-03 23:53:55 +00:00
|
|
|
* @param ReferenceMessageLocalizer $messageLocalizer
|
2019-11-28 14:39:58 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2019-12-09 16:08:57 +00:00
|
|
|
ErrorReporter $errorReporter,
|
|
|
|
AnchorFormatter $anchorFormatter,
|
2019-12-03 23:53:55 +00:00
|
|
|
ReferenceMessageLocalizer $messageLocalizer
|
2019-11-28 14:39:58 +00:00
|
|
|
) {
|
|
|
|
$this->errorReporter = $errorReporter;
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->anchorFormatter = $anchorFormatter;
|
2019-12-03 23:53:55 +00:00
|
|
|
$this->messageLocalizer = $messageLocalizer;
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-11 15:02:49 +00:00
|
|
|
* @param Parser $parser
|
2019-12-09 08:37:38 +00:00
|
|
|
* @param array[] $groupRefs
|
2019-11-28 14:39:58 +00:00
|
|
|
* @param bool $responsive
|
|
|
|
* @param bool $isSectionPreview
|
2019-12-09 15:36:34 +00:00
|
|
|
*
|
|
|
|
* @return string HTML
|
2019-11-28 14:39:58 +00:00
|
|
|
*/
|
2019-12-09 15:36:34 +00:00
|
|
|
public function formatReferences(
|
2019-12-11 15:02:49 +00:00
|
|
|
Parser $parser,
|
2019-11-28 10:15:19 +00:00
|
|
|
array $groupRefs,
|
|
|
|
bool $responsive,
|
|
|
|
bool $isSectionPreview
|
|
|
|
) : string {
|
2019-11-28 14:39:58 +00:00
|
|
|
if ( !$groupRefs ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:07:37 +00:00
|
|
|
$wikitext = $this->formatRefsList( $parser, $groupRefs, $isSectionPreview );
|
|
|
|
|
|
|
|
// Live hack: parse() adds two newlines on WM, can't reproduce it locally -ævar
|
|
|
|
$html = rtrim( $parser->recursiveTagParse( $wikitext ), "\n" );
|
|
|
|
|
|
|
|
if ( $responsive ) {
|
|
|
|
$wrapClasses = [ 'mw-references-wrap' ];
|
|
|
|
if ( count( $groupRefs ) > 10 ) {
|
|
|
|
$wrapClasses[] = 'mw-references-columns';
|
|
|
|
}
|
|
|
|
// Use a DIV wrap because column-count on a list directly is broken in Chrome.
|
|
|
|
// See https://bugs.chromium.org/p/chromium/issues/detail?id=498730.
|
|
|
|
return Html::rawElement( 'div', [ 'class' => $wrapClasses ], $html );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param array[] $groupRefs
|
|
|
|
* @param bool $isSectionPreview
|
|
|
|
*
|
|
|
|
* @return string Wikitext
|
|
|
|
*/
|
|
|
|
private function formatRefsList(
|
|
|
|
Parser $parser,
|
|
|
|
array $groupRefs,
|
|
|
|
bool $isSectionPreview
|
|
|
|
) : string {
|
2019-11-29 11:50:07 +00:00
|
|
|
// 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 {
|
2019-12-03 09:00:58 +00:00
|
|
|
$cmp = ( $a['number'] ?? 0 ) - ( $b['number'] ?? 0 );
|
|
|
|
return $cmp ?: ( $a['extendsIndex'] ?? 0 ) - ( $b['extendsIndex'] ?? 0 );
|
2019-11-29 11:50:07 +00:00
|
|
|
}
|
|
|
|
);
|
2019-12-09 14:07:37 +00:00
|
|
|
|
|
|
|
// 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;
|
2019-11-28 14:39:58 +00:00
|
|
|
foreach ( $groupRefs as $key => $value ) {
|
2019-12-04 12:54:34 +00:00
|
|
|
// Make sure the parent is not a subreference.
|
|
|
|
// FIXME: Move to a validation function.
|
|
|
|
if ( isset( $value['extends'] ) &&
|
|
|
|
isset( $groupRefs[$value['extends']]['extends'] )
|
|
|
|
) {
|
2019-12-09 08:37:38 +00:00
|
|
|
$value['text'] = ( $value['text'] ?? '' ) . ' ' .
|
2020-01-23 14:00:26 +00:00
|
|
|
$this->errorReporter->plain( $parser, 'cite_error_ref_nested_extends',
|
|
|
|
$value['extends'], $groupRefs[$value['extends']]['extends'] );
|
2019-12-04 12:54:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 14:39:58 +00:00
|
|
|
if ( !$indented && isset( $value['extends'] ) ) {
|
2019-11-29 16:27:16 +00:00
|
|
|
// The nested <ol> must be inside the parent's <li>
|
|
|
|
if ( preg_match( '#</li>\s*$#D', $parserInput, $matches, PREG_OFFSET_CAPTURE ) ) {
|
|
|
|
$parserInput = substr( $parserInput, 0, $matches[0][1] );
|
|
|
|
}
|
2019-11-28 14:39:58 +00:00
|
|
|
$parserInput .= Html::openElement( 'ol', [ 'class' => 'mw-extended-references' ] );
|
2019-11-29 16:27:16 +00:00
|
|
|
$indented = $matches[0][0] ?? true;
|
2019-11-28 14:39:58 +00:00
|
|
|
} elseif ( $indented && !isset( $value['extends'] ) ) {
|
2019-11-29 16:27:16 +00:00
|
|
|
$parserInput .= $this->closeIndention( $indented );
|
2019-11-28 14:39:58 +00:00
|
|
|
$indented = false;
|
|
|
|
}
|
2019-12-11 15:05:19 +00:00
|
|
|
$parserInput .= $this->formatListItem( $parser, $key, $value, $isSectionPreview ) . "\n";
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
2019-11-29 16:27:16 +00:00
|
|
|
$parserInput .= $this->closeIndention( $indented );
|
2019-12-09 14:07:37 +00:00
|
|
|
return Html::rawElement( 'ol', [ 'class' => [ 'references' ] ], $parserInput );
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 16:27:16 +00:00
|
|
|
/**
|
|
|
|
* @param string|bool $closingLi
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function closeIndention( $closingLi ) : string {
|
|
|
|
if ( !$closingLi ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return Html::closeElement( 'ol' ) . ( is_string( $closingLi ) ? $closingLi : '' );
|
|
|
|
}
|
|
|
|
|
2019-11-28 14:39:58 +00:00
|
|
|
/**
|
2019-12-11 15:05:19 +00:00
|
|
|
* @param Parser $parser
|
2019-11-28 14:39:58 +00:00
|
|
|
* @param string|int $key The key of the reference
|
|
|
|
* @param array $val A single reference as documented at {@see ReferenceStack::$refs}
|
|
|
|
* @param bool $isSectionPreview
|
2019-12-09 15:36:34 +00:00
|
|
|
*
|
2019-11-28 14:39:58 +00:00
|
|
|
* @return string Wikitext, wrapped in a single <li> element
|
|
|
|
*/
|
2019-12-11 15:05:19 +00:00
|
|
|
private function formatListItem(
|
|
|
|
Parser $parser, $key, array $val, bool $isSectionPreview
|
|
|
|
) : string {
|
|
|
|
$text = $this->referenceText( $parser, $key, $val['text'] ?? null, $isSectionPreview );
|
2019-11-28 14:39:58 +00:00
|
|
|
$error = '';
|
|
|
|
$extraAttributes = '';
|
|
|
|
|
|
|
|
if ( isset( $val['dir'] ) ) {
|
|
|
|
$dir = strtolower( $val['dir'] );
|
2019-12-16 14:33:55 +00:00
|
|
|
$extraAttributes = Html::expandAttributes( [ 'class' => 'mw-cite-dir-' . $dir ] );
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This counts the number of reuses. 0 means the reference appears only 1 time.
|
|
|
|
if ( isset( $val['count'] ) && $val['count'] < 1 ) {
|
|
|
|
// Anonymous, auto-numbered references can't be reused and get marked with a -1.
|
|
|
|
if ( $val['count'] < 0 ) {
|
|
|
|
$id = $val['key'];
|
2019-12-09 16:08:57 +00:00
|
|
|
$backlinkId = $this->anchorFormatter->refKey( $val['key'] );
|
2019-11-28 14:39:58 +00:00
|
|
|
} else {
|
|
|
|
$id = $key . '-' . $val['key'];
|
2019-12-09 16:08:57 +00:00
|
|
|
$backlinkId = $this->anchorFormatter->refKey( $key, $val['key'] . '-' . $val['count'] );
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
2019-12-03 23:53:55 +00:00
|
|
|
return $this->messageLocalizer->msg(
|
2019-11-28 14:39:58 +00:00
|
|
|
'cite_references_link_one',
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->anchorFormatter->getReferencesKey( $id ),
|
2019-11-29 14:00:39 +00:00
|
|
|
$backlinkId,
|
2019-11-28 14:39:58 +00:00
|
|
|
$text . $error,
|
|
|
|
$extraAttributes
|
2019-12-03 23:53:55 +00:00
|
|
|
)->plain();
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Named references with >1 occurrences
|
|
|
|
$backlinks = [];
|
|
|
|
// There is no count in case of a section preview
|
|
|
|
for ( $i = 0; $i <= ( $val['count'] ?? -1 ); $i++ ) {
|
2019-12-03 23:53:55 +00:00
|
|
|
$backlinks[] = $this->messageLocalizer->msg(
|
2019-11-28 14:39:58 +00:00
|
|
|
'cite_references_link_many_format',
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->anchorFormatter->refKey( $key, $val['key'] . '-' . $i ),
|
2019-11-28 14:39:58 +00:00
|
|
|
$this->referencesFormatEntryNumericBacklinkLabel(
|
2019-11-29 23:10:11 +00:00
|
|
|
$val['number'] .
|
|
|
|
( isset( $val['extendsIndex'] ) ? '.' . $val['extendsIndex'] : '' ),
|
2019-11-28 14:39:58 +00:00
|
|
|
$i,
|
|
|
|
$val['count']
|
|
|
|
),
|
2019-12-11 15:05:19 +00:00
|
|
|
$this->referencesFormatEntryAlternateBacklinkLabel( $parser, $i )
|
2019-12-03 23:53:55 +00:00
|
|
|
)->plain();
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
2019-12-03 23:53:55 +00:00
|
|
|
return $this->messageLocalizer->msg(
|
2019-11-28 14:39:58 +00:00
|
|
|
'cite_references_link_many',
|
2019-12-09 16:08:57 +00:00
|
|
|
$this->anchorFormatter->getReferencesKey( $key . '-' . ( $val['key'] ?? '' ) ),
|
2019-11-28 14:39:58 +00:00
|
|
|
$this->listToText( $backlinks ),
|
|
|
|
$text . $error,
|
|
|
|
$extraAttributes
|
2019-12-03 23:53:55 +00:00
|
|
|
)->plain();
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-20 10:13:31 +00:00
|
|
|
* @param Parser $parser
|
2019-11-28 14:39:58 +00:00
|
|
|
* @param string|int $key
|
|
|
|
* @param ?string $text
|
|
|
|
* @param bool $isSectionPreview
|
2019-12-04 06:54:30 +00:00
|
|
|
*
|
2019-11-28 14:39:58 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-11 15:05:19 +00:00
|
|
|
private function referenceText(
|
|
|
|
Parser $parser, $key, ?string $text, bool $isSectionPreview
|
|
|
|
) : string {
|
2019-11-26 09:31:41 +00:00
|
|
|
if ( $text === null ) {
|
2019-12-11 15:05:19 +00:00
|
|
|
return $this->errorReporter->plain( $parser,
|
|
|
|
$isSectionPreview
|
|
|
|
? 'cite_warning_sectionpreview_no_text'
|
|
|
|
: 'cite_error_references_no_text', $key );
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
2019-12-04 06:54:30 +00:00
|
|
|
|
2019-11-28 14:39:58 +00:00
|
|
|
return '<span class="reference-text">' . rtrim( $text, "\n" ) . "</span>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a numeric backlink given a base number and an
|
|
|
|
* offset, e.g. $base = 1, $offset = 2; = 1.2
|
|
|
|
* Since bug #5525, it correctly does 1.9 -> 1.10 as well as 1.099 -> 1.100
|
|
|
|
*
|
2019-11-28 10:15:19 +00:00
|
|
|
* @param int|string $base
|
2019-11-28 14:39:58 +00:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $max Maximum value expected.
|
2019-12-04 06:54:30 +00:00
|
|
|
*
|
2019-11-28 14:39:58 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
private function referencesFormatEntryNumericBacklinkLabel(
|
|
|
|
$base,
|
|
|
|
int $offset,
|
|
|
|
int $max
|
|
|
|
) : string {
|
2019-12-10 15:21:12 +00:00
|
|
|
return $this->messageLocalizer->localizeDigits( $base ) .
|
2019-12-09 15:01:13 +00:00
|
|
|
$this->messageLocalizer->formatNum(
|
|
|
|
'.' .
|
2019-12-21 01:25:58 +00:00
|
|
|
str_pad( (string)$offset, strlen( (string)$max ), '0', STR_PAD_LEFT )
|
2019-12-09 15:01:13 +00:00
|
|
|
);
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a custom format backlink given an offset, e.g.
|
|
|
|
* $offset = 2; = c if $this->mBacklinkLabels = [ 'a',
|
|
|
|
* 'b', 'c', ...]. Return an error if the offset > the # of
|
|
|
|
* array items
|
|
|
|
*
|
2019-12-11 15:05:19 +00:00
|
|
|
* @param Parser $parser
|
2019-11-28 14:39:58 +00:00
|
|
|
* @param int $offset
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-11 15:05:19 +00:00
|
|
|
private function referencesFormatEntryAlternateBacklinkLabel(
|
|
|
|
Parser $parser, int $offset
|
|
|
|
) : string {
|
2019-12-03 09:00:58 +00:00
|
|
|
if ( $this->backlinkLabels === null ) {
|
|
|
|
$this->backlinkLabels = preg_split(
|
|
|
|
'/\s+/',
|
2019-12-03 23:53:55 +00:00
|
|
|
$this->messageLocalizer->msg( 'cite_references_link_many_format_backlink_labels' )
|
2019-12-03 09:00:58 +00:00
|
|
|
->plain()
|
|
|
|
);
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 09:00:58 +00:00
|
|
|
return $this->backlinkLabels[$offset]
|
2019-12-11 15:05:19 +00:00
|
|
|
?? $this->errorReporter->plain( $parser, 'cite_error_references_no_backlink_label' );
|
2019-11-28 14:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This does approximately the same thing as
|
|
|
|
* Language::listToText() but due to this being used for a
|
|
|
|
* slightly different purpose (people might not want , as the
|
|
|
|
* first separator and not 'and' as the second, and this has to
|
|
|
|
* use messages from the content language) I'm rolling my own.
|
|
|
|
*
|
|
|
|
* @param string[] $arr The array to format
|
2019-12-09 14:07:37 +00:00
|
|
|
*
|
2019-11-28 14:39:58 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
private function listToText( array $arr ) : string {
|
2019-11-28 14:39:58 +00:00
|
|
|
$lastElement = array_pop( $arr );
|
|
|
|
|
|
|
|
if ( $arr === [] ) {
|
|
|
|
return (string)$lastElement;
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:53:55 +00:00
|
|
|
$sep = $this->messageLocalizer->msg( 'cite_references_link_many_sep' )->plain();
|
|
|
|
$and = $this->messageLocalizer->msg( 'cite_references_link_many_and' )->plain();
|
2019-11-28 14:39:58 +00:00
|
|
|
return implode( $sep, $arr ) . $and . $lastElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|