| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Cite\Parsoid; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Core\Sanitizer; |
| 7 |
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
| 8 |
use Wikimedia\Parsoid\NodeData\DataMwError; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class ReferencesData { |
| 14 |
|
| 15 |
private int $index = 0; |
| 16 |
|
| 17 |
private array $refGroups = []; |
| 18 |
|
| 19 |
public array $embeddedErrors = []; |
| 20 |
|
| 21 |
private array $inEmbeddedContent = []; |
| 22 |
public string $referencesGroup = ''; |
| 23 |
|
| 24 |
public function inReferencesContent(): bool { |
| 25 |
return $this->inEmbeddedContent( 'references' ); |
| 26 |
} |
| 27 |
|
| 28 |
public function inEmbeddedContent( ?string $needle = null ): bool { |
| 29 |
if ( $needle ) { |
| 30 |
return in_array( $needle, $this->inEmbeddedContent, true ); |
| 31 |
} else { |
| 32 |
return $this->inEmbeddedContent !== []; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function pushEmbeddedContentFlag( string $needle = 'embed' ): void { |
| 37 |
array_unshift( $this->inEmbeddedContent, $needle ); |
| 38 |
} |
| 39 |
|
| 40 |
public function popEmbeddedContentFlag() { |
| 41 |
array_shift( $this->inEmbeddedContent ); |
| 42 |
} |
| 43 |
|
| 44 |
public function getRefGroup( |
| 45 |
string $groupName, bool $allocIfMissing = false |
| 46 |
): ?RefGroup { |
| 47 |
if ( $allocIfMissing ) { |
| 48 |
$this->refGroups[$groupName] ??= new RefGroup( $groupName ); |
| 49 |
} |
| 50 |
return $this->refGroups[$groupName] ?? null; |
| 51 |
} |
| 52 |
|
| 53 |
public function removeRefGroup( string $groupName ): void { |
| 54 |
|
| 55 |
unset( $this->refGroups[$groupName] ); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private function normalizeKey( string $key ): string { |
| 65 |
$ret = Sanitizer::escapeIdForLink( $key ); |
| 66 |
$ret = preg_replace( '/[_\s]+/u', '_', $ret ); |
| 67 |
return $ret; |
| 68 |
} |
| 69 |
|
| 70 |
public function add( |
| 71 |
ParsoidExtensionAPI $extApi, string $groupName, string $refName, string $refDir |
| 72 |
): RefGroupItem { |
| 73 |
$group = $this->getRefGroup( $groupName, true ); |
| 74 |
$hasRefName = $refName !== ''; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
$n = $this->index; |
| 82 |
$refKey = strval( 1 + $n ); |
| 83 |
|
| 84 |
$refNameSanitized = $this->normalizeKey( $refName ); |
| 85 |
|
| 86 |
$refIdBase = 'cite_ref-' . ( $hasRefName ? $refNameSanitized . '_' . $refKey : $refKey ); |
| 87 |
$noteId = 'cite_note-' . ( $hasRefName ? $refNameSanitized . '-' . $refKey : $refKey ); |
| 88 |
|
| 89 |
|
| 90 |
$this->index++; |
| 91 |
|
| 92 |
$ref = new RefGroupItem(); |
| 93 |
$ref->dir = $refDir; |
| 94 |
$ref->group = $group->name; |
| 95 |
$ref->groupIndex = count( $group->refs ) + 1; |
| 96 |
$ref->index = $n; |
| 97 |
$ref->key = $refIdBase; |
| 98 |
$ref->id = $hasRefName ? $refIdBase . '-0' : $refIdBase; |
| 99 |
$ref->name = $refName; |
| 100 |
$ref->target = $noteId; |
| 101 |
|
| 102 |
$group->refs[] = $ref; |
| 103 |
|
| 104 |
if ( $hasRefName ) { |
| 105 |
$group->indexByName[$refName] = $ref; |
| 106 |
} |
| 107 |
|
| 108 |
return $ref; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function getRefGroups(): array { |
| 115 |
return $this->refGroups; |
| 116 |
} |
| 117 |
} |