2019-11-22 17:28:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Cite;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Datastructure representing <ref> input, in the format of:
|
|
|
|
* <code>
|
|
|
|
* [
|
|
|
|
* 'user supplied' => [
|
|
|
|
* 'text' => 'user supplied reference & key',
|
|
|
|
* 'count' => 1, // occurs twice
|
|
|
|
* 'number' => 1, // The first reference, we want
|
|
|
|
* // all occourances of it to
|
|
|
|
* // use the same number
|
|
|
|
* ],
|
|
|
|
* 0 => [
|
|
|
|
* 'text' => 'Anonymous reference',
|
|
|
|
* 'count' => -1,
|
|
|
|
* ],
|
|
|
|
* 1 => [
|
|
|
|
* 'text' => 'Another anonymous reference',
|
|
|
|
* 'count' => -1,
|
|
|
|
* ],
|
|
|
|
* 'some key' => [
|
|
|
|
* 'text' => 'this one occurs once'
|
|
|
|
* 'count' => 0,
|
|
|
|
* 'number' => 4
|
|
|
|
* ],
|
|
|
|
* 3 => 'more stuff'
|
|
|
|
* ];
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* This works because:
|
|
|
|
* * PHP's datastructures are guaranteed to be returned in the
|
|
|
|
* order that things are inserted into them (unless you mess
|
|
|
|
* with that)
|
|
|
|
* * User supplied keys can't be integers, therefore avoiding
|
|
|
|
* conflict with anonymous keys
|
|
|
|
*
|
|
|
|
* In this structure, 'key' will either be an autoincrementing integer.
|
|
|
|
*
|
|
|
|
* @var array[][]
|
|
|
|
*/
|
|
|
|
private $refs = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count for user displayed output (ref[1], ref[2], ...)
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $refSequence = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Counter for the number of refs in each group.
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $groupRefSequence = [];
|
|
|
|
|
2019-11-27 23:29:47 +00:00
|
|
|
/**
|
|
|
|
* @var int[][]
|
|
|
|
*/
|
|
|
|
private $extendsCount = [];
|
|
|
|
|
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)[]
|
|
|
|
*/
|
|
|
|
private $refCallStack = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated We should be able to push this responsibility to calling code.
|
2019-11-29 13:37:31 +00:00
|
|
|
* @var CiteErrorReporter
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
|
|
|
private $errorReporter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CiteErrorReporter $errorReporter
|
|
|
|
*/
|
|
|
|
public function __construct( CiteErrorReporter $errorReporter ) {
|
|
|
|
$this->errorReporter = $errorReporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Leave a mark in the stack which matches an invalid ref tag.
|
|
|
|
*/
|
|
|
|
public function pushInvalidRef() {
|
|
|
|
$this->refCallStack[] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate $this->refs and $this->refCallStack based on input and arguments to <ref>
|
|
|
|
*
|
2019-11-27 15:31:17 +00:00
|
|
|
* @param ?string $text Content from the <ref> tag
|
|
|
|
* @param ?string $name
|
2019-11-22 17:28:51 +00:00
|
|
|
* @param string $group
|
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
|
2019-11-22 17:28:51 +00:00
|
|
|
* @param string[] $argv
|
2019-11-27 15:31:17 +00:00
|
|
|
* @param ?string $dir ref direction
|
2019-11-22 17:28:51 +00:00
|
|
|
* @param StripState $stripState
|
|
|
|
*
|
2019-12-03 12:25:41 +00:00
|
|
|
* @return ?array ref structure, or null if nothing was pushed
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
|
|
|
public function pushRef(
|
2019-11-27 15:31:17 +00:00
|
|
|
?string $text,
|
|
|
|
?string $name,
|
|
|
|
string $group,
|
2019-11-27 23:27:11 +00:00
|
|
|
?string $extends,
|
2019-11-27 15:31:17 +00:00
|
|
|
?string $follow,
|
|
|
|
array $argv,
|
|
|
|
?string $dir,
|
|
|
|
StripState $stripState
|
|
|
|
) : ?array {
|
2019-11-22 17:28:51 +00:00
|
|
|
if ( !isset( $this->refs[$group] ) ) {
|
|
|
|
$this->refs[$group] = [];
|
|
|
|
}
|
|
|
|
if ( !isset( $this->groupRefSequence[$group] ) ) {
|
|
|
|
$this->groupRefSequence[$group] = 0;
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:08:18 +00:00
|
|
|
if ( $this->refs[$group][$follow] ?? false ) {
|
2019-11-22 17:28:51 +00:00
|
|
|
// We know the parent note already, so just perform the "follow" and bail out
|
2019-11-26 14:08:18 +00:00
|
|
|
// TODO: Separate `pushRef` from these side-effects.
|
|
|
|
$this->refs[$group][$follow]['text'] .= ' ' . $text;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
2019-11-26 14:08:18 +00:00
|
|
|
if ( $follow ) {
|
|
|
|
$ref['follow'] = $follow;
|
2019-11-23 00:03:42 +00:00
|
|
|
// This inserts the broken "follow" at the end of all other broken "follow"
|
|
|
|
$k = 0;
|
|
|
|
foreach ( $this->refs[$group] as $value ) {
|
|
|
|
if ( !isset( $value['follow'] ) ) {
|
2019-11-22 17:28:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-11-23 00:03:42 +00:00
|
|
|
$k++;
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
2019-11-26 14:08:18 +00:00
|
|
|
array_splice( $this->refs[$group], $k, 0, [ $ref ] );
|
2019-11-22 17:28:51 +00:00
|
|
|
array_splice( $this->refCallStack, $k, 0,
|
2019-11-27 23:27:11 +00:00
|
|
|
[ [ 'new', $argv, $text, $name, $extends, $group, $this->refSequence ] ] );
|
2019-11-22 17:28:51 +00:00
|
|
|
|
|
|
|
// A "follow" never gets its own footnote marker
|
|
|
|
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;
|
2019-11-26 14:08:18 +00:00
|
|
|
$action = 'new';
|
2019-11-29 11:50:07 +00:00
|
|
|
} elseif ( isset( $this->refs[$group][$name]['__placeholder__'] ) ) {
|
|
|
|
// Populate a placeholder.
|
|
|
|
unset( $this->refs[$group][$name]['__placeholder__'] );
|
|
|
|
unset( $ref['number'] );
|
|
|
|
$ref = array_merge( $ref, $this->refs[$group][$name] );
|
|
|
|
$this->refs[$group][$name] =& $ref;
|
|
|
|
$action = 'new';
|
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;
|
2019-11-22 17:28:51 +00:00
|
|
|
$action = 'new';
|
|
|
|
} 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--;
|
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;
|
|
|
|
$action = 'assign';
|
|
|
|
} 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
|
|
|
|
// add error message to the original ref
|
|
|
|
// TODO: standardize error display and move to `validateRef`.
|
|
|
|
$ref['text'] .= ' ' . $this->errorReporter->plain(
|
|
|
|
'cite_error_references_duplicate_key', $name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$action = 'increment';
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-29 23:10:11 +00:00
|
|
|
|
|
|
|
$ref['number'] = $ref['number'] ?? ++$this->groupRefSequence[$group];
|
|
|
|
|
|
|
|
if ( $extends ) {
|
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.
|
|
|
|
// TODO: Do we need to roll back the group ref sequence here?
|
2019-11-29 23:10:11 +00:00
|
|
|
$ref['number'] = $this->refs[$group][$extends]['number'];
|
|
|
|
} else {
|
2019-11-29 11:50:07 +00:00
|
|
|
// Transfer my number to parent ref.
|
|
|
|
$this->refs[$group][$extends] = [
|
|
|
|
'number' => $ref['number'],
|
|
|
|
'__placeholder__' => true,
|
|
|
|
];
|
2019-11-29 23:10:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 23:27:11 +00:00
|
|
|
$this->refCallStack[] = [ $action, $argv, $text, $name, $extends, $group, $ref['key'] ];
|
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
|
|
|
|
* @return array Refs to restore under the correct context. [ $argv, $text ]
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
public function rollbackRefs( int $count ) : array {
|
2019-11-22 17:28:51 +00:00
|
|
|
$redoStack = [];
|
|
|
|
for ( $i = 0; $i < $count; $i++ ) {
|
|
|
|
if ( !$this->refCallStack ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$call = array_pop( $this->refCallStack );
|
|
|
|
if ( $call !== false ) {
|
2019-12-09 11:52:26 +00:00
|
|
|
[ $action, $argv, $text, $name, $extends, $group, $key ] = $call;
|
|
|
|
$this->rollbackRef( $action, $name, $extends, $group, $key );
|
2019-11-22 17:28:51 +00:00
|
|
|
$redoStack[] = [ $argv, $text ];
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
*
|
|
|
|
* Called by guardedReferences()
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string|null $name The name attribute passed in the ref tag.
|
2019-11-27 23:27:11 +00:00
|
|
|
* @param string|null $extends
|
2019-11-22 17:28:51 +00:00
|
|
|
* @param string $group
|
|
|
|
* @param int $index Autoincrement counter for this ref.
|
|
|
|
*/
|
2019-11-28 10:15:19 +00:00
|
|
|
private function rollbackRef(
|
|
|
|
string $type,
|
|
|
|
?string $name,
|
|
|
|
?string $extends,
|
|
|
|
string $group,
|
|
|
|
int $index
|
|
|
|
) {
|
2019-11-22 17:28:51 +00:00
|
|
|
if ( !$this->hasGroup( $group ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $name;
|
|
|
|
if ( $name === 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'] ) &&
|
|
|
|
$this->refs[$group][$k]['key'] === $index
|
|
|
|
) {
|
2019-11-22 17:28:51 +00:00
|
|
|
$key = $k;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity checks that specified element exists.
|
|
|
|
if ( $key === null ||
|
|
|
|
!isset( $this->refs[$group][$key] ) ||
|
|
|
|
$this->refs[$group][$key]['key'] !== $index
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-27 23:29:47 +00:00
|
|
|
if ( $extends ) {
|
|
|
|
$this->extendsCount[$group][$extends]--;
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:28:51 +00:00
|
|
|
switch ( $type ) {
|
|
|
|
case 'new':
|
|
|
|
# Rollback the addition of new elements to the stack.
|
|
|
|
unset( $this->refs[$group][$key] );
|
|
|
|
if ( $this->refs[$group] === [] ) {
|
2019-11-27 23:29:47 +00:00
|
|
|
// TODO: Unsetting is unecessary.
|
2019-11-29 22:24:51 +00:00
|
|
|
$this->deleteGroup( $group );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
2019-11-29 22:24:51 +00:00
|
|
|
// TODO: else, don't we need to decrement groupRefSequence?
|
2019-11-22 17:28:51 +00:00
|
|
|
break;
|
|
|
|
case 'assign':
|
|
|
|
# Rollback assignment of text to pre-existing elements.
|
|
|
|
$this->refs[$group][$key]['text'] = null;
|
2019-11-29 22:24:51 +00:00
|
|
|
$this->refs[$group][$key]['count']--;
|
|
|
|
break;
|
2019-11-22 17:28:51 +00:00
|
|
|
case 'increment':
|
|
|
|
# Rollback increase in named ref occurrences.
|
|
|
|
$this->refs[$group][$key]['count']--;
|
|
|
|
break;
|
2019-12-06 13:29:32 +00:00
|
|
|
default:
|
|
|
|
throw new \LogicException( "Unknown call stack action \"$type\"" );
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset all state.
|
|
|
|
*/
|
|
|
|
public function clear() {
|
|
|
|
$this->groupRefSequence = [];
|
|
|
|
$this->refSequence = 0;
|
|
|
|
$this->refs = [];
|
|
|
|
$this->refCallStack = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear state for a single group.
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
*/
|
2019-11-27 15:31:17 +00:00
|
|
|
public function deleteGroup( string $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-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
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function getGroups() : array {
|
|
|
|
$groups = [];
|
|
|
|
foreach ( $this->refs as $group => $refs ) {
|
|
|
|
if ( $refs ) {
|
|
|
|
$groups[] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all references for a group.
|
|
|
|
*
|
2019-12-09 08:37:38 +00:00
|
|
|
* @param ?string $group
|
2019-11-22 17:28:51 +00:00
|
|
|
* @return array[]
|
|
|
|
*/
|
2019-12-09 08:37:38 +00:00
|
|
|
public function getGroupRefs( ?string $group ) : array {
|
|
|
|
return $this->refs[$group ?? Cite::DEFAULT_GROUP] ?? [];
|
2019-11-22 17:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface to set reference text from external code. Ideally we can take over
|
|
|
|
* responsibility for this logic.
|
|
|
|
* @deprecated
|
|
|
|
*
|
|
|
|
* @param string $group
|
|
|
|
* @param string $name
|
2019-11-27 15:31:17 +00:00
|
|
|
* @param ?string $text
|
2019-11-22 17:28:51 +00:00
|
|
|
*/
|
2019-11-27 15:31:17 +00:00
|
|
|
public function setRefText( string $group, string $name, ?string $text ) {
|
2019-11-22 17:28:51 +00:00
|
|
|
$this->refs[$group][$name]['text'] = $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|