2019-11-22 17:28:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
2019-12-16 16:11:42 +00:00
|
|
|
use LogicException;
|
2024-10-19 21:25:16 +00:00
|
|
|
use MediaWiki\Parser\StripState;
|
2019-11-22 17:28:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulates most of Cite state during parsing. This includes metadata about each ref tag,
|
|
|
|
* and a rollback stack to correct confusion caused by lost context when `{{#tag` is used.
|
2019-11-29 14:00:39 +00:00
|
|
|
*
|
|
|
|
* @license GPL-2.0-or-later
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
|
|
|
class ReferenceStack {
|
|
|
|
|
|
|
|
/**
|
2020-02-03 11:24:28 +00:00
|
|
|
* Data structure representing all <ref> tags parsed so far, indexed by group name (an empty
|
|
|
|
* string for the default group) and reference name.
|
2019-11-22 17:28:51 +00:00
|
|
|
*
|
2020-02-03 11:24:28 +00:00
|
|
|
* References without a name get a numeric index, starting from 0. Conflicts are avoided by
|
2023-12-12 10:15:14 +00:00
|
|
|
* disallowing numeric names (e.g. <ref name="1">) in {@see Validator::validateRef}.
|
2019-11-22 17:28:51 +00:00
|
|
|
*
|
2023-12-22 22:40:52 +00:00
|
|
|
* @var array<string,array<string|int,ReferenceStackItem>>
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2023-06-05 14:36:03 +00:00
|
|
|
private array $refs = [];
|
2019-11-22 17:28:51 +00:00
|
|
|
|
|
|
|
/**
|
2020-02-03 11:24:28 +00:00
|
|
|
* Auto-incrementing sequence number for all <ref>, no matter which group
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2023-06-05 14:36:03 +00:00
|
|
|
private int $refSequence = 0;
|
2019-11-22 17:28:51 +00:00
|
|
|
|
2023-11-30 09:42:11 +00:00
|
|
|
/** @var int[] Counter for the number of refs in each group */
|
2023-06-05 14:36:03 +00:00
|
|
|
private array $groupRefSequence = [];
|
2019-11-27 23:29:47 +00:00
|
|
|
|
2019-11-22 17:28:51 +00:00
|
|
|
/**
|
|
|
|
* <ref> call stack
|
|
|
|
* Used to cleanup out of sequence ref calls created by #tag
|
|
|
|
* See description of function rollbackRef.
|
|
|
|
*
|
|
|
|
* @var (array|false)[]
|
2022-10-09 02:34:58 +00:00
|
|
|
* @phan-var array<array{0:string,1:int,2:string,3:?string,4:?string,5:?string,6:array}|false>
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2023-06-05 14:36:03 +00:00
|
|
|
private array $refCallStack = [];
|
2019-11-22 17:28:51 +00:00
|
|
|
|
2023-12-12 09:02:00 +00:00
|
|
|
private const ACTION_ASSIGN = 'assign';
|
|
|
|
private const ACTION_INCREMENT = 'increment';
|
|
|
|
private const ACTION_NEW_FROM_PLACEHOLDER = 'new-from-placeholder';
|
|
|
|
private const ACTION_NEW = 'new';
|
|
|
|
|
2019-11-22 17:28:51 +00:00
|
|
|
/**
|
|
|
|
* Leave a mark in the stack which matches an invalid ref tag.
|
|
|
|
*/
|
2023-11-30 09:42:11 +00:00
|
|
|
public function pushInvalidRef(): void {
|
2019-11-22 17:28:51 +00:00
|
|
|
$this->refCallStack[] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate $this->refs and $this->refCallStack based on input and arguments to <ref>
|
|
|
|
*
|
2019-12-19 09:16:14 +00:00
|
|
|
* @param StripState $stripState
|
2019-11-27 15:31:17 +00:00
|
|
|
* @param ?string $text Content from the <ref> tag
|
2019-12-19 09:16:14 +00:00
|
|
|
* @param string[] $argv
|
2019-11-22 17:28:51 +00:00
|
|
|
* @param string $group
|
2019-12-19 09:16:14 +00:00
|
|
|
* @param ?string $name
|
2019-11-27 23:27:11 +00:00
|
|
|
* @param ?string $extends
|
2019-11-27 15:31:17 +00:00
|
|
|
* @param ?string $follow Guaranteed to not be a numeric string
|
|
|
|
* @param ?string $dir ref direction
|
2019-11-22 17:28:51 +00:00
|
|
|
*
|
2023-12-22 22:46:54 +00:00
|
|
|
* @return ?ReferenceStackItem ref structure, or null if no footnote marker should be rendered
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
|
|
|
public function pushRef(
|
2019-12-19 09:16:14 +00:00
|
|
|
StripState $stripState,
|
2019-11-27 15:31:17 +00:00
|
|
|
?string $text,
|
2019-12-19 09:16:14 +00:00
|
|
|
array $argv,
|
2019-11-27 15:31:17 +00:00
|
|
|
string $group,
|
2019-12-19 09:16:14 +00:00
|
|
|
?string $name,
|
2019-11-27 23:27:11 +00:00
|
|
|
?string $extends,
|
2019-11-27 15:31:17 +00:00
|
|
|
?string $follow,
|
2019-12-19 09:16:14 +00:00
|
|
|
?string $dir
|
2023-12-22 22:46:54 +00:00
|
|
|
): ?ReferenceStackItem {
|
2023-12-12 09:09:45 +00:00
|
|
|
$this->refs[$group] ??= [];
|
|
|
|
$this->groupRefSequence[$group] ??= 0;
|
2019-11-22 17:28:51 +00:00
|
|
|
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref = new ReferenceStackItem();
|
|
|
|
$ref->count = 1;
|
|
|
|
$ref->dir = $dir;
|
|
|
|
// TODO: Read from this group field or deprecate it.
|
|
|
|
$ref->group = $group;
|
|
|
|
$ref->name = $name;
|
|
|
|
$ref->text = $text;
|
2019-11-22 17:28:51 +00:00
|
|
|
|
2020-02-05 10:39:49 +00:00
|
|
|
if ( $follow ) {
|
2020-02-17 14:29:33 +00:00
|
|
|
if ( !isset( $this->refs[$group][$follow] ) ) {
|
|
|
|
// Mark an incomplete follow="…" as such. This is valid e.g. in the Page:… namespace
|
|
|
|
// on Wikisource.
|
2024-01-10 08:58:17 +00:00
|
|
|
$ref->follow = $follow;
|
2024-01-10 09:11:50 +00:00
|
|
|
$ref->key = $this->nextRefSequence();
|
2024-01-10 08:58:17 +00:00
|
|
|
$this->refs[$group][] = $ref;
|
2024-01-10 09:11:50 +00:00
|
|
|
$this->refCallStack[] = [ self::ACTION_NEW, $ref->key, $group, $name, $text, $argv ];
|
2020-02-17 14:29:33 +00:00
|
|
|
} elseif ( $text !== null ) {
|
|
|
|
// We know the parent already, so just perform the follow="…" and bail out
|
2023-12-19 13:49:52 +00:00
|
|
|
$this->resolveFollow( $group, $follow, $text );
|
2020-02-17 14:29:33 +00:00
|
|
|
}
|
2020-02-05 16:52:21 +00:00
|
|
|
// A follow="…" never gets its own footnote marker
|
2020-02-05 10:39:49 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:08:18 +00:00
|
|
|
if ( !$name ) {
|
2019-11-25 15:10:05 +00:00
|
|
|
// This is an anonymous reference, which will be given a numeric index.
|
2019-12-02 15:36:49 +00:00
|
|
|
$this->refs[$group][] = &$ref;
|
2024-01-10 09:11:50 +00:00
|
|
|
$ref->key = $this->nextRefSequence();
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_NEW;
|
2024-01-09 08:57:03 +00:00
|
|
|
} elseif ( !isset( $this->refs[$group][$name] ) ) {
|
|
|
|
// Valid key with first occurrence
|
|
|
|
$this->refs[$group][$name] = &$ref;
|
2024-01-10 09:11:50 +00:00
|
|
|
$ref->key = $this->nextRefSequence();
|
2024-01-09 08:57:03 +00:00
|
|
|
$action = self::ACTION_NEW;
|
2023-12-22 22:40:52 +00:00
|
|
|
} elseif ( $this->refs[$group][$name]->placeholder ) {
|
2019-11-29 11:50:07 +00:00
|
|
|
// Populate a placeholder.
|
2024-01-09 16:24:22 +00:00
|
|
|
$ref->extendsCount = $this->refs[$group][$name]->extendsCount;
|
2024-01-10 09:11:50 +00:00
|
|
|
$ref->key = $this->nextRefSequence();
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->number = $this->refs[$group][$name]->number;
|
2019-11-29 11:50:07 +00:00
|
|
|
$this->refs[$group][$name] =& $ref;
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_NEW_FROM_PLACEHOLDER;
|
2019-11-22 17:28:51 +00:00
|
|
|
} else {
|
2019-11-29 18:48:21 +00:00
|
|
|
// Change an existing entry.
|
2019-12-02 15:36:49 +00:00
|
|
|
$ref = &$this->refs[$group][$name];
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->count++;
|
2023-12-14 08:13:14 +00:00
|
|
|
|
2023-12-22 22:40:52 +00:00
|
|
|
if ( $ref->dir && $dir && $ref->dir !== $dir ) {
|
|
|
|
$ref->warnings[] = [ 'cite_error_ref_conflicting_dir', $name ];
|
2023-12-14 08:13:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 22:40:52 +00:00
|
|
|
if ( $ref->text === null && $text !== null ) {
|
2019-11-26 14:08:18 +00:00
|
|
|
// If no text was set before, use this text
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->text = $text;
|
2019-11-26 14:08:18 +00:00
|
|
|
// Use the dir parameter only from the full definition of a named ref tag
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->dir = $dir;
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_ASSIGN;
|
2019-11-26 14:08:18 +00:00
|
|
|
} else {
|
2019-11-26 09:31:41 +00:00
|
|
|
if ( $text !== null
|
2019-11-26 14:08:18 +00:00
|
|
|
// T205803 different strip markers might hide the same text
|
|
|
|
&& $stripState->unstripBoth( $text )
|
2023-12-22 22:40:52 +00:00
|
|
|
!== $stripState->unstripBoth( $ref->text )
|
2019-11-26 14:08:18 +00:00
|
|
|
) {
|
|
|
|
// two refs with same name and different text
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->warnings[] = [ 'cite_error_references_duplicate_key', $name ];
|
2019-11-26 14:08:18 +00:00
|
|
|
}
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_INCREMENT;
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-29 23:10:11 +00:00
|
|
|
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->number ??= ++$this->groupRefSequence[$group];
|
2019-11-29 23:10:11 +00:00
|
|
|
|
2020-01-07 14:52:59 +00:00
|
|
|
// Do not mess with a known parent a second time
|
2024-10-27 13:13:06 +00:00
|
|
|
if ( $extends && $ref->extendsIndex === null ) {
|
2024-01-09 16:30:10 +00:00
|
|
|
$parentRef =& $this->refs[$group][$extends];
|
2024-10-27 13:13:06 +00:00
|
|
|
if ( $parentRef === null ) {
|
2024-01-09 16:30:10 +00:00
|
|
|
// Create a new placeholder and give it the current sequence number.
|
|
|
|
$parentRef = new ReferenceStackItem();
|
|
|
|
$parentRef->name = $extends;
|
|
|
|
$parentRef->number = $ref->number;
|
|
|
|
$parentRef->placeholder = true;
|
|
|
|
} else {
|
|
|
|
$ref->number = $parentRef->number;
|
|
|
|
// Roll back the group sequence number.
|
|
|
|
--$this->groupRefSequence[$group];
|
|
|
|
}
|
2024-01-09 16:24:22 +00:00
|
|
|
$parentRef->extendsCount ??= 0;
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->extends = $extends;
|
2024-01-09 16:24:22 +00:00
|
|
|
$ref->extendsIndex = ++$parentRef->extendsCount;
|
2023-12-22 22:40:52 +00:00
|
|
|
} elseif ( $extends && $ref->extends !== $extends ) {
|
2020-01-07 15:46:21 +00:00
|
|
|
// TODO: Change the error message to talk about "conflicting content or parent"?
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->warnings[] = [ 'cite_error_references_duplicate_key', $name ];
|
2019-11-29 23:10:11 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:24:22 +00:00
|
|
|
$this->refCallStack[] = [ $action, $ref->key, $group, $name, $text, $argv ];
|
2023-12-22 22:46:54 +00:00
|
|
|
return $ref;
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Undo the changes made by the last $count ref tags. This is used when we discover that the
|
|
|
|
* last few tags were actually inside of a references tag.
|
|
|
|
*
|
|
|
|
* @param int $count
|
2020-01-08 15:55:49 +00:00
|
|
|
*
|
2019-12-19 09:16:14 +00:00
|
|
|
* @return array[] Refs to restore under the correct context, as a list of [ $text, $argv ]
|
2022-10-09 02:34:58 +00:00
|
|
|
* @phan-return array<array{0:?string,1:array}>
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2021-07-22 05:44:15 +00:00
|
|
|
public function rollbackRefs( int $count ): array {
|
2019-11-22 17:28:51 +00:00
|
|
|
$redoStack = [];
|
2020-01-08 15:55:49 +00:00
|
|
|
while ( $count-- && $this->refCallStack ) {
|
2019-11-22 17:28:51 +00:00
|
|
|
$call = array_pop( $this->refCallStack );
|
2019-12-19 08:23:16 +00:00
|
|
|
if ( $call ) {
|
2022-10-09 02:34:58 +00:00
|
|
|
// @phan-suppress-next-line PhanParamTooFewUnpack
|
2019-12-19 10:32:01 +00:00
|
|
|
$redoStack[] = $this->rollbackRef( ...$call );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 15:55:49 +00:00
|
|
|
|
2019-11-29 18:48:21 +00:00
|
|
|
// Drop unused rollbacks, this group is finished.
|
2019-11-22 17:28:51 +00:00
|
|
|
$this->refCallStack = [];
|
|
|
|
|
|
|
|
return array_reverse( $redoStack );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partially undoes the effect of calls to stack()
|
|
|
|
*
|
|
|
|
* The option to define <ref> within <references> makes the
|
|
|
|
* behavior of <ref> context dependent. This is normally fine
|
|
|
|
* but certain operations (especially #tag) lead to out-of-order
|
|
|
|
* parser evaluation with the <ref> tags being processed before
|
|
|
|
* their containing <reference> element is read. This leads to
|
|
|
|
* stack corruption that this function works to fix.
|
|
|
|
*
|
|
|
|
* This function is not a total rollback since some internal
|
|
|
|
* counters remain incremented. Doing so prevents accidentally
|
|
|
|
* corrupting certain links.
|
|
|
|
*
|
2019-12-10 15:04:59 +00:00
|
|
|
* @param string $action
|
2019-12-09 12:26:22 +00:00
|
|
|
* @param int $key Autoincrement counter for this ref.
|
2019-12-19 08:23:16 +00:00
|
|
|
* @param string $group
|
|
|
|
* @param ?string $name The name attribute passed in the ref tag.
|
2019-12-19 10:32:01 +00:00
|
|
|
* @param ?string $text
|
|
|
|
* @param array $argv
|
|
|
|
*
|
|
|
|
* @return array [ $text, $argv ] Ref redo item.
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
private function rollbackRef(
|
2019-12-10 15:04:59 +00:00
|
|
|
string $action,
|
2019-12-19 08:23:16 +00:00
|
|
|
int $key,
|
2019-11-28 10:15:19 +00:00
|
|
|
string $group,
|
2019-12-19 08:23:16 +00:00
|
|
|
?string $name,
|
2019-12-19 10:32:01 +00:00
|
|
|
?string $text,
|
|
|
|
array $argv
|
2021-07-22 05:44:15 +00:00
|
|
|
): array {
|
2019-11-22 17:28:51 +00:00
|
|
|
if ( !$this->hasGroup( $group ) ) {
|
2019-12-19 10:27:00 +00:00
|
|
|
throw new LogicException( "Cannot roll back ref with unknown group \"$group\"." );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 10:40:12 +00:00
|
|
|
$lookup = $name ?: null;
|
|
|
|
if ( $lookup === null ) {
|
2019-11-29 18:48:21 +00:00
|
|
|
// Find anonymous ref by key.
|
2019-11-22 17:28:51 +00:00
|
|
|
foreach ( $this->refs[$group] as $k => $v ) {
|
2024-05-16 12:39:03 +00:00
|
|
|
if ( $v->key === $key ) {
|
2019-12-09 12:18:35 +00:00
|
|
|
$lookup = $k;
|
2019-11-22 17:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 10:27:00 +00:00
|
|
|
// Obsessive sanity checks that the specified element exists.
|
|
|
|
if ( $lookup === null ) {
|
|
|
|
throw new LogicException( "Cannot roll back unknown ref by key $key." );
|
|
|
|
} elseif ( !isset( $this->refs[$group][$lookup] ) ) {
|
|
|
|
throw new LogicException( "Cannot roll back missing named ref \"$lookup\"." );
|
2023-12-22 22:40:52 +00:00
|
|
|
} elseif ( $this->refs[$group][$lookup]->key !== $key ) {
|
2019-12-19 10:27:00 +00:00
|
|
|
throw new LogicException(
|
|
|
|
"Cannot roll back corrupt named ref \"$lookup\" which should have had key $key." );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
2024-01-06 00:12:51 +00:00
|
|
|
$ref =& $this->refs[$group][$lookup];
|
2019-11-22 17:28:51 +00:00
|
|
|
|
2019-12-10 15:04:59 +00:00
|
|
|
switch ( $action ) {
|
2023-12-12 09:02:00 +00:00
|
|
|
case self::ACTION_NEW:
|
2020-01-09 10:53:43 +00:00
|
|
|
// Rollback the addition of new elements to the stack
|
2019-12-09 12:18:35 +00:00
|
|
|
unset( $this->refs[$group][$lookup] );
|
2020-01-20 20:11:54 +00:00
|
|
|
if ( !$this->refs[$group] ) {
|
2019-12-09 14:07:37 +00:00
|
|
|
$this->popGroup( $group );
|
2020-01-24 12:02:53 +00:00
|
|
|
} elseif ( isset( $this->groupRefSequence[$group] ) ) {
|
|
|
|
$this->groupRefSequence[$group]--;
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
2024-01-09 16:24:22 +00:00
|
|
|
if ( $ref->extends ) {
|
|
|
|
$this->refs[$group][$ref->extends]->extendsCount--;
|
|
|
|
}
|
2019-11-22 17:28:51 +00:00
|
|
|
break;
|
2023-12-12 09:02:00 +00:00
|
|
|
case self::ACTION_NEW_FROM_PLACEHOLDER:
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->placeholder = true;
|
|
|
|
$ref->count = 0;
|
2020-01-08 15:50:25 +00:00
|
|
|
break;
|
2023-12-12 09:02:00 +00:00
|
|
|
case self::ACTION_ASSIGN:
|
2020-01-09 10:53:43 +00:00
|
|
|
// Rollback assignment of text to pre-existing elements
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->text = null;
|
|
|
|
$ref->count--;
|
2019-11-29 22:24:51 +00:00
|
|
|
break;
|
2023-12-12 09:02:00 +00:00
|
|
|
case self::ACTION_INCREMENT:
|
2020-01-09 10:53:43 +00:00
|
|
|
// Rollback increase in named ref occurrences
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->count--;
|
2019-11-22 17:28:51 +00:00
|
|
|
break;
|
2019-12-06 13:29:32 +00:00
|
|
|
default:
|
2019-12-19 10:27:00 +00:00
|
|
|
throw new LogicException( "Unknown call stack action \"$action\"" );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
2019-12-19 10:32:01 +00:00
|
|
|
return [ $text, $argv ];
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear state for a single group.
|
|
|
|
*
|
|
|
|
* @param string $group
|
2019-12-09 14:07:37 +00:00
|
|
|
*
|
2023-12-22 22:49:47 +00:00
|
|
|
* @return array<string|int,ReferenceStackItem> The references from the removed group
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2021-07-22 05:44:15 +00:00
|
|
|
public function popGroup( string $group ): array {
|
2019-12-09 14:07:37 +00:00
|
|
|
$refs = $this->getGroupRefs( $group );
|
2019-11-22 17:28:51 +00:00
|
|
|
unset( $this->refs[$group] );
|
|
|
|
unset( $this->groupRefSequence[$group] );
|
2019-12-09 14:07:37 +00:00
|
|
|
return $refs;
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-22 22:40:52 +00:00
|
|
|
* Returns true if the group exists and contains references.
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2021-07-22 05:44:15 +00:00
|
|
|
public function hasGroup( string $group ): bool {
|
2024-05-16 12:39:03 +00:00
|
|
|
return (bool)( $this->refs[$group] ?? false );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-16 12:39:03 +00:00
|
|
|
* @return string[] List of group names that contain at least one reference
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2021-07-22 05:44:15 +00:00
|
|
|
public function getGroups(): array {
|
2019-11-22 17:28:51 +00:00
|
|
|
$groups = [];
|
|
|
|
foreach ( $this->refs as $group => $refs ) {
|
|
|
|
if ( $refs ) {
|
|
|
|
$groups[] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all references for a group.
|
|
|
|
*
|
2019-12-21 01:25:58 +00:00
|
|
|
* @param string $group
|
2020-01-09 10:50:32 +00:00
|
|
|
*
|
2023-12-22 22:49:47 +00:00
|
|
|
* @return array<string|int,ReferenceStackItem>
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2021-07-22 05:44:15 +00:00
|
|
|
public function getGroupRefs( string $group ): array {
|
2023-12-22 22:49:47 +00:00
|
|
|
return $this->refs[$group] ?? [];
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 13:49:52 +00:00
|
|
|
private function resolveFollow( string $group, string $follow, string $text ): void {
|
2024-01-06 00:12:51 +00:00
|
|
|
$previousRef =& $this->refs[$group][$follow];
|
2023-12-22 22:40:52 +00:00
|
|
|
$previousRef->text ??= '';
|
|
|
|
$previousRef->text .= " $text";
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 18:36:33 +00:00
|
|
|
public function listDefinedRef( string $group, string $name, string $text ): void {
|
2024-01-06 00:12:51 +00:00
|
|
|
$ref =& $this->refs[$group][$name];
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref ??= new ReferenceStackItem();
|
|
|
|
$ref->placeholder = false;
|
2024-10-27 13:13:06 +00:00
|
|
|
if ( $ref->text === null ) {
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->text = $text;
|
|
|
|
} elseif ( $ref->text !== $text ) {
|
2023-12-14 17:30:28 +00:00
|
|
|
// two refs with same key and different content
|
2023-12-22 22:40:52 +00:00
|
|
|
$ref->warnings[] = [ 'cite_error_references_duplicate_key', $name ];
|
2023-12-14 17:30:28 +00:00
|
|
|
}
|
2023-12-14 12:18:02 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 12:39:03 +00:00
|
|
|
private function nextRefSequence(): int {
|
2024-01-10 09:11:50 +00:00
|
|
|
return ++$this->refSequence;
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|