2019-11-22 17:28:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
2019-12-16 16:11:42 +00:00
|
|
|
use LogicException;
|
2019-11-22 17:28:51 +00:00
|
|
|
use StripState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2020-02-03 11:24:28 +00:00
|
|
|
* Elements (almost all are optional):
|
|
|
|
* - 'name': The original name="…" of a reference (also used as the array key), or null for
|
|
|
|
* anonymous references.
|
|
|
|
* - 'key': Sequence number for all references, no matter which group, starting from 1. Used to
|
|
|
|
* generate IDs and anchors.
|
|
|
|
* - 'number': Sequence number per group, starting from 1. To be used in the [1] footnote
|
|
|
|
* marker.
|
|
|
|
* - 'extendsIndex': Sequence number for sub-references with the same extends="…", starting
|
|
|
|
* from 1. Used in addition to the 'number' in [1.1] footnote markers.
|
|
|
|
* - 'count': How often a reference is reused. 0 means not reused, i.e. the reference appears
|
|
|
|
* only one time. -1 for anonymous references that cannot be reused.
|
|
|
|
* - 'extends': Marks a sub-reference. Points to the parent reference by name.
|
2020-02-05 15:28:45 +00:00
|
|
|
* - 'follow': Marks an incomplete follow="…". This is valid e.g. in the Page:… namespace on
|
|
|
|
* Wikisource.
|
2020-02-03 11:24:28 +00:00
|
|
|
* - '__placeholder__': Temporarily marks an incomplete parent reference that was referenced via
|
|
|
|
* extends="…" before it exists.
|
|
|
|
* - 'text': The content inside the <ref>…</ref> tag. Null for <ref /> without content. Also
|
|
|
|
* null for <ref></ref> without any non-whitespace content.
|
|
|
|
* - 'dir': Direction of the text. Should either be "ltr" or "rtl".
|
2023-12-14 12:18:02 +00:00
|
|
|
* - 'warnings': Error messages attached to this reference.
|
2019-11-22 17:28:51 +00:00
|
|
|
*
|
|
|
|
* @var array[][]
|
|
|
|
*/
|
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 = [];
|
|
|
|
/** @var int[][] */
|
|
|
|
private array $extendsCount = [];
|
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';
|
2023-12-21 21:43:12 +00:00
|
|
|
public const PARENT_REF_PLACEHOLDER = '__placeholder__';
|
2023-12-12 09:02:00 +00:00
|
|
|
|
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
|
|
|
*
|
2020-02-05 16:52:21 +00:00
|
|
|
* @return ?array ref structure, or null if no footnote marker should be rendered
|
2020-01-29 01:50:22 +00:00
|
|
|
* @suppress PhanTypePossiblyInvalidDimOffset To many complaints about array indizes
|
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
|
2021-07-22 05:44:15 +00:00
|
|
|
): ?array {
|
2023-12-12 09:09:45 +00:00
|
|
|
$this->refs[$group] ??= [];
|
|
|
|
$this->groupRefSequence[$group] ??= 0;
|
2019-11-22 17:28:51 +00:00
|
|
|
|
2019-11-26 14:08:18 +00:00
|
|
|
$ref = [
|
|
|
|
'count' => $name ? 0 : -1,
|
|
|
|
'dir' => $dir,
|
2019-11-27 15:31:17 +00:00
|
|
|
// This assumes we are going to register a new reference, instead of reusing one
|
2019-11-26 14:08:18 +00:00
|
|
|
'key' => ++$this->refSequence,
|
2019-11-29 22:37:13 +00:00
|
|
|
'name' => $name,
|
2019-11-26 14:08:18 +00:00
|
|
|
'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.
|
|
|
|
$this->refs[$group][] = $ref + [ 'follow' => $follow ];
|
2023-12-12 09:02:00 +00:00
|
|
|
$this->refCallStack[] = [ self::ACTION_NEW, $this->refSequence, $group, $name, $extends, $text,
|
2020-02-17 14:29:33 +00:00
|
|
|
$argv ];
|
|
|
|
} 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
|
|
|
$this->refSequence--;
|
|
|
|
}
|
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;
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_NEW;
|
2023-12-14 08:45:06 +00:00
|
|
|
} elseif ( isset( $this->refs[$group][$name][self::PARENT_REF_PLACEHOLDER] ) ) {
|
2019-11-29 11:50:07 +00:00
|
|
|
// Populate a placeholder.
|
2023-12-14 08:45:06 +00:00
|
|
|
unset( $this->refs[$group][$name][self::PARENT_REF_PLACEHOLDER] );
|
2019-11-29 11:50:07 +00:00
|
|
|
unset( $ref['number'] );
|
|
|
|
$ref = array_merge( $ref, $this->refs[$group][$name] );
|
|
|
|
$this->refs[$group][$name] =& $ref;
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_NEW_FROM_PLACEHOLDER;
|
2019-11-26 14:08:18 +00:00
|
|
|
} elseif ( !isset( $this->refs[$group][$name] ) ) {
|
|
|
|
// Valid key with first occurrence
|
2019-12-02 15:36:49 +00:00
|
|
|
$this->refs[$group][$name] = &$ref;
|
2023-12-12 09:02:00 +00:00
|
|
|
$action = self::ACTION_NEW;
|
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];
|
2019-11-26 14:08:18 +00:00
|
|
|
$ref['count']++;
|
2019-11-29 22:24:51 +00:00
|
|
|
// Rollback the global counter since we won't create a new ref.
|
|
|
|
$this->refSequence--;
|
2023-12-14 08:13:14 +00:00
|
|
|
|
|
|
|
if ( $ref['dir'] && $dir && $ref['dir'] !== $dir ) {
|
|
|
|
$ref['warnings'][] = [ 'cite_error_ref_conflicting_dir', $name ];
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:31:41 +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
|
|
|
|
$ref['text'] = $text;
|
|
|
|
// Use the dir parameter only from the full definition of a named ref tag
|
|
|
|
$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 )
|
|
|
|
!== $stripState->unstripBoth( $ref['text'] )
|
|
|
|
) {
|
|
|
|
// two refs with same name and different text
|
2023-12-14 12:18:02 +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-10-13 22:49:13 +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
|
|
|
|
if ( $extends && !isset( $ref['extendsIndex'] ) ) {
|
2019-11-29 11:50:07 +00:00
|
|
|
$this->extendsCount[$group][$extends] =
|
|
|
|
( $this->extendsCount[$group][$extends] ?? 0 ) + 1;
|
2019-11-29 23:10:11 +00:00
|
|
|
|
2019-11-29 11:50:07 +00:00
|
|
|
$ref['extends'] = $extends;
|
|
|
|
$ref['extendsIndex'] = $this->extendsCount[$group][$extends];
|
2019-12-02 15:36:49 +00:00
|
|
|
|
2019-11-29 11:50:07 +00:00
|
|
|
if ( isset( $this->refs[$group][$extends]['number'] ) ) {
|
|
|
|
// Adopt the parent's number.
|
2019-11-29 23:10:11 +00:00
|
|
|
$ref['number'] = $this->refs[$group][$extends]['number'];
|
2019-12-09 09:19:55 +00:00
|
|
|
// Roll back the group sequence number.
|
|
|
|
--$this->groupRefSequence[$group];
|
2019-11-29 23:10:11 +00:00
|
|
|
} else {
|
2019-11-29 11:50:07 +00:00
|
|
|
// Transfer my number to parent ref.
|
|
|
|
$this->refs[$group][$extends] = [
|
2023-12-21 21:43:12 +00:00
|
|
|
'name' => $extends,
|
2019-11-29 11:50:07 +00:00
|
|
|
'number' => $ref['number'],
|
2023-12-14 08:45:06 +00:00
|
|
|
self::PARENT_REF_PLACEHOLDER => true,
|
2019-11-29 11:50:07 +00:00
|
|
|
];
|
2019-11-29 23:10:11 +00:00
|
|
|
}
|
2020-01-07 15:46:21 +00:00
|
|
|
} elseif ( $extends && $ref['extends'] !== $extends ) {
|
|
|
|
// TODO: Change the error message to talk about "conflicting content or parent"?
|
2023-12-14 12:18:02 +00:00
|
|
|
$ref['warnings'][] = [ 'cite_error_references_duplicate_key', $name ];
|
2019-11-29 23:10:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 09:16:14 +00:00
|
|
|
$this->refCallStack[] = [ $action, $ref['key'], $group, $name, $extends, $text, $argv ];
|
2019-11-29 23:26:58 +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.
|
|
|
|
* @param ?string $extends
|
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 $extends,
|
|
|
|
?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 ) {
|
2019-12-02 09:14:49 +00:00
|
|
|
if ( isset( $this->refs[$group][$k]['key'] ) &&
|
2019-12-09 12:26:22 +00:00
|
|
|
$this->refs[$group][$k]['key'] === $key
|
2019-12-02 09:14:49 +00:00
|
|
|
) {
|
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\"." );
|
|
|
|
} elseif ( $this->refs[$group][$lookup]['key'] !== $key ) {
|
|
|
|
throw new LogicException(
|
|
|
|
"Cannot roll back corrupt named ref \"$lookup\" which should have had key $key." );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 23:29:47 +00:00
|
|
|
if ( $extends ) {
|
|
|
|
$this->extendsCount[$group][$extends]--;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-01-24 12:02:53 +00:00
|
|
|
// TODO: Don't we need to rollback extendsCount as well?
|
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-14 08:45:06 +00:00
|
|
|
$this->refs[$group][$lookup][self::PARENT_REF_PLACEHOLDER] = true;
|
2020-01-08 15:50:25 +00:00
|
|
|
unset( $this->refs[$group][$lookup]['count'] );
|
|
|
|
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
|
2019-12-09 12:18:35 +00:00
|
|
|
$this->refs[$group][$lookup]['text'] = null;
|
|
|
|
$this->refs[$group][$lookup]['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
|
2019-12-09 12:18:35 +00:00
|
|
|
$this->refs[$group][$lookup]['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
|
|
|
*
|
|
|
|
* @return array[] 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-11-29 22:24:51 +00:00
|
|
|
unset( $this->extendsCount[$group] );
|
2019-12-09 14:07:37 +00:00
|
|
|
return $refs;
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-27 10:08:00 +00:00
|
|
|
* Retruns 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 {
|
2019-11-27 10:08:00 +00:00
|
|
|
return isset( $this->refs[$group] ) && $this->refs[$group];
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of all groups with references.
|
|
|
|
*
|
2019-11-27 15:31:17 +00:00
|
|
|
* @return string[]
|
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
|
|
|
*
|
2019-11-22 17:28:51 +00:00
|
|
|
* @return array[]
|
|
|
|
*/
|
2021-07-22 05:44:15 +00:00
|
|
|
public function getGroupRefs( string $group ): array {
|
2019-12-21 01:25:58 +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 {
|
|
|
|
$this->refs[$group][$follow]['text'] ??= '';
|
|
|
|
$this->refs[$group][$follow]['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 {
|
2023-12-21 21:43:12 +00:00
|
|
|
if ( isset( $this->refs[$group][$name][self::PARENT_REF_PLACEHOLDER] ) ) {
|
|
|
|
unset( $this->refs[$group][$name][self::PARENT_REF_PLACEHOLDER] );
|
|
|
|
}
|
2023-12-14 17:30:28 +00:00
|
|
|
if ( !isset( $this->refs[$group][$name]['text'] ) ) {
|
|
|
|
$this->refs[$group][$name]['text'] = $text;
|
|
|
|
} elseif ( $this->refs[$group][$name]['text'] !== $text ) {
|
|
|
|
// two refs with same key and different content
|
|
|
|
$this->refs[$group][$name]['warnings'][] = [ 'cite_error_references_duplicate_key', $name ];
|
|
|
|
}
|
2023-12-14 12:18:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|