Fail early on nested extends="…", if possible

This partly reverts Ied2e3f5. I haven't properly tested this before.
Rendering a bad extends (that extends a <ref> that's already extended)
not indented messes the order up and rips other extended <ref>s out of
context.

For now it might be better to stick to the previous, "magic" behavior:
Such an extends behaves like it is extending the *parent*, and is
ordered and indented as such. This is still not correct, but I feel
this is much better than rendering such a bad extends on the top level.

This patch also makes the code fail much earlier for a nested extends,
if this decision can be made already. In this case the error message is
rendered in the middle of the text (as other errors also are), not in
the <references> section.

Change-Id: I33c6a763cd6c11df09d10dfab73f955ed15e9d36
This commit is contained in:
Thiemo Kreuz 2019-12-09 09:37:38 +01:00
parent f0d964260d
commit c5fe49ff11
5 changed files with 47 additions and 16 deletions

View file

@ -172,10 +172,18 @@ class Cite {
return StatusValue::newFatal( 'cite_error_ref_numeric_key' );
}
global $wgCiteBookReferencing;
// Temporary feature flag until mainstreamed. See T236255
if ( !$wgCiteBookReferencing && $extends ) {
return StatusValue::newFatal( 'cite_error_ref_too_many_keys' );
if ( $extends ) {
// Temporary feature flag until mainstreamed, see T236255
global $wgCiteBookReferencing;
if ( !$wgCiteBookReferencing ) {
return StatusValue::newFatal( 'cite_error_ref_too_many_keys' );
}
$groupRefs = $this->referenceStack->getGroupRefs( $group );
if ( isset( $groupRefs[$extends]['extends'] ) ) {
// TODO: Introduce a specific error for this case, reuse in referencesFormat()!
return StatusValue::newFatal( 'cite_error_ref_too_many_keys' );
}
}
if ( $follow && ( $name || $extends ) ) {

View file

@ -58,7 +58,7 @@ class FootnoteBodyFormatter {
}
/**
* @param array $groupRefs
* @param array[] $groupRefs
* @param bool $responsive
* @param bool $isSectionPreview
* @return string
@ -92,8 +92,8 @@ class FootnoteBodyFormatter {
if ( isset( $value['extends'] ) &&
isset( $groupRefs[$value['extends']]['extends'] )
) {
unset( $value['extends'] );
$value['text'] = ( $value['text'] ?? '' ) .
$value['text'] = ( $value['text'] ?? '' ) . ' ' .
// TODO: Introduce a specific error for this case; reuse in validateRef()!
$this->errorReporter->plain( 'cite_error_ref_too_many_keys' );
}

View file

@ -391,11 +391,11 @@ class ReferenceStack {
/**
* Return all references for a group.
*
* @param string $group
* @param ?string $group
* @return array[]
*/
public function getGroupRefs( string $group ) : array {
return $this->refs[$group] ?? [];
public function getGroupRefs( ?string $group ) : array {
return $this->refs[$group ?? Cite::DEFAULT_GROUP] ?? [];
}
/**

View file

@ -165,7 +165,7 @@ wgCiteBookReferencing=true
!! end
!! test
T236256 - Can't extend a book reference (no nesting).
T236256 - Can't extend a book reference (no nesting)
!! config
wgCiteBookReferencing=true
!! wikitext
@ -176,16 +176,39 @@ wgCiteBookReferencing=true
!! html/php
<p><sup id="cite_ref-a_1-0" class="reference"><a href="#cite_note-a-1">&#91;1&#93;</a></sup>
<sup id="cite_ref-b_2-0" class="reference"><a href="#cite_note-b-2">&#91;1.1&#93;</a></sup>
<sup id="cite_ref-3" class="reference"><a href="#cite_note-3">&#91;1.1&#93;</a></sup>
<span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: Invalid <code>&lt;ref&gt;</code> tag;
invalid names, e.g. too many</span>
</p>
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-a-1"><span class="mw-cite-backlink"><a href="#cite_ref-a_1-0">↑</a></span> <span class="reference-text">book</span>
<ol class="mw-extended-references"><li id="cite_note-b-2"><span class="mw-cite-backlink"><a href="#cite_ref-b_2-0">↑</a></span> <span class="reference-text">page 2</span>
</li>
</ol></li>
<li id="cite_note-3"><span class="mw-cite-backlink"><a href="#cite_ref-3">↑</a></span> <span class="reference-text">section 3<span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: Invalid <code>&lt;ref&gt;</code> tag;
</ol></div>
!! end
!! test
T236256 - Can't extend a book reference (no nesting), specified in reverse order
!! config
wgCiteBookReferencing=true
!! wikitext
<ref extends="b">section 3</ref>
<ref extends="a" name="b">page 2</ref>
<ref name="a">book</ref>
<references />
!! html/php
<p><sup id="cite_ref-1" class="reference"><a href="#cite_note-1">&#91;1.1&#93;</a></sup>
<sup id="cite_ref-b_2-0" class="reference"><a href="#cite_note-b-2">&#91;1.1&#93;</a></sup>
<sup id="cite_ref-a_3-0" class="reference"><a href="#cite_note-a-3">&#91;1&#93;</a></sup>
</p>
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-a-3"><span class="mw-cite-backlink"><a href="#cite_ref-a_3-0">↑</a></span> <span class="reference-text">book</span>
<ol class="mw-extended-references"><li id="cite_note-1"><span class="mw-cite-backlink"><a href="#cite_ref-1">↑</a></span> <span class="reference-text">section 3 <span class="error mw-ext-cite-error" lang="en" dir="ltr">Cite error: Invalid <code>&lt;ref&gt;</code> tag;
invalid names, e.g. too many</span></span>
</li>
<li id="cite_note-b-2"><span class="mw-cite-backlink"><a href="#cite_ref-b_2-0">↑</a></span> <span class="reference-text">page 2</span>
</li>
</ol></li>
</ol></div>
!! end

View file

@ -124,9 +124,9 @@ class FootnoteBodyFormatterTest extends MediaWikiUnitTestCase {
],
'<div class="mw-references-wrap"><ol class="references">' . "\n" .
'<li>(cite_references_link_many|||<span class="reference-text">t3</span>' . "\n" .
"|)</li>\n<li>(cite_references_link_many|||" .
'<span class="reference-text">t1["cite_error_ref_too_many_keys"]</span>' .
"\n" . '|)<ol class="mw-extended-references">' .
'|)<ol class="mw-extended-references"><li>(cite_references_link_many|||' .
'<span class="reference-text">t1 ["cite_error_ref_too_many_keys"]</span>' .
"\n|)</li>\n" .
'<li>(cite_references_link_many|||<span class="reference-text">t2</span>' .
"\n|)</li>\n</ol></li>\n" .
'</ol></div>'