| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cite; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Parser\StripState; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ReferenceStack { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private array $refs = []; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private int $refSequence = 0; |
| 31 |
|
| 32 |
|
| 33 |
private array $groupRefSequence = []; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private array $refCallStack = []; |
| 44 |
|
| 45 |
private const ACTION_ASSIGN = 'assign'; |
| 46 |
private const ACTION_INCREMENT = 'increment'; |
| 47 |
private const ACTION_NEW_FROM_PLACEHOLDER = 'new-from-placeholder'; |
| 48 |
private const ACTION_NEW = 'new'; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function pushInvalidRef(): void { |
| 54 |
$this->refCallStack[] = false; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function pushRef( |
| 72 |
StripState $stripState, |
| 73 |
?string $text, |
| 74 |
array $argv, |
| 75 |
string $group, |
| 76 |
?string $name, |
| 77 |
?string $extends, |
| 78 |
?string $follow, |
| 79 |
?string $dir |
| 80 |
): ?ReferenceStackItem { |
| 81 |
$this->refs[$group] ??= []; |
| 82 |
$this->groupRefSequence[$group] ??= 0; |
| 83 |
|
| 84 |
$ref = new ReferenceStackItem(); |
| 85 |
$ref->count = 1; |
| 86 |
$ref->dir = $dir; |
| 87 |
|
| 88 |
$ref->group = $group; |
| 89 |
$ref->name = $name; |
| 90 |
$ref->text = $text; |
| 91 |
|
| 92 |
if ( $follow ) { |
| 93 |
if ( !isset( $this->refs[$group][$follow] ) ) { |
| 94 |
pace |
| 95 |
|
| 96 |
$ref->follow = $follow; |
| 97 |
$ref->key = $this->nextRefSequence(); |
| 98 |
$this->refs[$group][] = $ref; |
| 99 |
$this->refCallStack[] = [ self::ACTION_NEW, $ref->key, $group, $name, $text, $argv ]; |
| 100 |
} elseif ( $text !== null ) { |
| 101 |
ut |
| 102 |
$this->resolveFollow( $group, $follow, $text ); |
| 103 |
} |
| 104 |
er |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
if ( !$name ) { |
| 109 |
|
| 110 |
$this->refs[$group][] = &$ref; |
| 111 |
$ref->key = $this->nextRefSequence(); |
| 112 |
$action = self::ACTION_NEW; |
| 113 |
} elseif ( !isset( $this->refs[$group][$name] ) ) { |
| 114 |
|
| 115 |
$this->refs[$group][$name] = &$ref; |
| 116 |
$ref->key = $this->nextRefSequence(); |
| 117 |
$action = self::ACTION_NEW; |
| 118 |
} elseif ( $this->refs[$group][$name]->placeholder ) { |
| 119 |
|
| 120 |
$ref->extendsCount = $this->refs[$group][$name]->extendsCount; |
| 121 |
$ref->key = $this->nextRefSequence(); |
| 122 |
$ref->number = $this->refs[$group][$name]->number; |
| 123 |
$this->refs[$group][$name] =& $ref; |
| 124 |
$action = self::ACTION_NEW_FROM_PLACEHOLDER; |
| 125 |
} else { |
| 126 |
|
| 127 |
$ref = &$this->refs[$group][$name]; |
| 128 |
$ref->count++; |
| 129 |
|
| 130 |
if ( $ref->dir && $dir && $ref->dir !== $dir ) { |
| 131 |
$ref->warnings[] = [ 'cite_error_ref_conflicting_dir', $name ]; |
| 132 |
} |
| 133 |
|
| 134 |
if ( $ref->text === null && $text !== null ) { |
| 135 |
|
| 136 |
$ref->text = $text; |
| 137 |
|
| 138 |
$ref->dir = $dir; |
| 139 |
$action = self::ACTION_ASSIGN; |
| 140 |
} else { |
| 141 |
if ( $text !== null |
| 142 |
|
| 143 |
&& $stripState->unstripBoth( $text ) |
| 144 |
!== $stripState->unstripBoth( $ref->text ) |
| 145 |
) { |
| 146 |
|
| 147 |
$ref->warnings[] = [ 'cite_error_references_duplicate_key', $name ]; |
| 148 |
} |
| 149 |
$action = self::ACTION_INCREMENT; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
$ref->number ??= ++$this->groupRefSequence[$group]; |
| 154 |
|
| 155 |
|
| 156 |
if ( $extends && $ref->extendsIndex === null ) { |
| 157 |
$parentRef =& $this->refs[$group][$extends]; |
| 158 |
if ( $parentRef === null ) { |
| 159 |
|
| 160 |
$parentRef = new ReferenceStackItem(); |
| 161 |
$parentRef->name = $extends; |
| 162 |
$parentRef->number = $ref->number; |
| 163 |
$parentRef->placeholder = true; |
| 164 |
} else { |
| 165 |
$ref->number = $parentRef->number; |
| 166 |
|
| 167 |
--$this->groupRefSequence[$group]; |
| 168 |
} |
| 169 |
$parentRef->extendsCount ??= 0; |
| 170 |
$ref->extends = $extends; |
| 171 |
$ref->extendsIndex = ++$parentRef->extendsCount; |
| 172 |
} elseif ( $extends && $ref->extends !== $extends ) { |
| 173 |
|
| 174 |
$ref->warnings[] = [ 'cite_error_references_duplicate_key', $name ]; |
| 175 |
} |
| 176 |
|
| 177 |
$this->refCallStack[] = [ $action, $ref->key, $group, $name, $text, $argv ]; |
| 178 |
return $ref; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function rollbackRefs( int $count ): array { |
| 191 |
$redoStack = []; |
| 192 |
while ( $count-- && $this->refCallStack ) { |
| 193 |
$call = array_pop( $this->refCallStack ); |
| 194 |
if ( $call ) { |
| 195 |
|
| 196 |
$redoStack[] = $this->rollbackRef( ...$call ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
$this->refCallStack = []; |
| 202 |
|
| 203 |
return array_reverse( $redoStack ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
private function rollbackRef( |
| 230 |
string $action, |
| 231 |
int $key, |
| 232 |
string $group, |
| 233 |
?string $name, |
| 234 |
?string $text, |
| 235 |
array $argv |
| 236 |
): array { |
| 237 |
if ( !$this->hasGroup( $group ) ) { |
| 238 |
throw new LogicException( "Cannot roll back ref with unknown group \"$group\"." ); |
| 239 |
} |
| 240 |
|
| 241 |
$lookup = $name ?: null; |
| 242 |
if ( $lookup === null ) { |
| 243 |
|
| 244 |
foreach ( $this->refs[$group] as $k => $v ) { |
| 245 |
if ( $v->key === $key ) { |
| 246 |
$lookup = $k; |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
if ( $lookup === null ) { |
| 254 |
throw new LogicException( "Cannot roll back unknown ref by key $key." ); |
| 255 |
} elseif ( !isset( $this->refs[$group][$lookup] ) ) { |
| 256 |
throw new LogicException( "Cannot roll back missing named ref \"$lookup\"." ); |
| 257 |
} elseif ( $this->refs[$group][$lookup]->key !== $key ) { |
| 258 |
throw new LogicException( |
| 259 |
"Cannot roll back corrupt named ref \"$lookup\" which should have had key $key." ); |
| 260 |
} |
| 261 |
$ref =& $this->refs[$group][$lookup]; |
| 262 |
|
| 263 |
switch ( $action ) { |
| 264 |
case self::ACTION_NEW: |
| 265 |
|
| 266 |
unset( $this->refs[$group][$lookup] ); |
| 267 |
if ( !$this->refs[$group] ) { |
| 268 |
$this->popGroup( $group ); |
| 269 |
} elseif ( isset( $this->groupRefSequence[$group] ) ) { |
| 270 |
$this->groupRefSequence[$group]--; |
| 271 |
} |
| 272 |
if ( $ref->extends ) { |
| 273 |
$this->refs[$group][$ref->extends]->extendsCount--; |
| 274 |
} |
| 275 |
break; |
| 276 |
case self::ACTION_NEW_FROM_PLACEHOLDER: |
| 277 |
$ref->placeholder = true; |
| 278 |
$ref->count = 0; |
| 279 |
break; |
| 280 |
case self::ACTION_ASSIGN: |
| 281 |
|
| 282 |
$ref->text = null; |
| 283 |
$ref->count--; |
| 284 |
break; |
| 285 |
case self::ACTION_INCREMENT: |
| 286 |
|
| 287 |
$ref->count--; |
| 288 |
break; |
| 289 |
default: |
| 290 |
throw new LogicException( "Unknown call stack action \"$action\"" ); |
| 291 |
} |
| 292 |
return [ $text, $argv ]; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public function popGroup( string $group ): array { |
| 303 |
$refs = $this->getGroupRefs( $group ); |
| 304 |
unset( $this->refs[$group] ); |
| 305 |
unset( $this->groupRefSequence[$group] ); |
| 306 |
return $refs; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public function hasGroup( string $group ): bool { |
| 313 |
return (bool)( $this->refs[$group] ?? false ); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function getGroups(): array { |
| 320 |
$groups = []; |
| 321 |
foreach ( $this->refs as $group => $refs ) { |
| 322 |
if ( $refs ) { |
| 323 |
$groups[] = $group; |
| 324 |
} |
| 325 |
} |
| 326 |
return $groups; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function getGroupRefs( string $group ): array { |
| 337 |
return $this->refs[$group] ?? []; |
| 338 |
} |
| 339 |
|
| 340 |
private function resolveFollow( string $group, string $follow, string $text ): void { |
| 341 |
$previousRef =& $this->refs[$group][$follow]; |
| 342 |
$previousRef->text ??= ''; |
| 343 |
$previousRef->text .= " $text"; |
| 344 |
} |
| 345 |
|
| 346 |
public function listDefinedRef( string $group, string $name, string $text ): void { |
| 347 |
$ref =& $this->refs[$group][$name]; |
| 348 |
$ref ??= new ReferenceStackItem(); |
| 349 |
$ref->placeholder = false; |
| 350 |
if ( $ref->text === null ) { |
| 351 |
$ref->text = $text; |
| 352 |
} elseif ( $ref->text !== $text ) { |
| 353 |
|
| 354 |
$ref->warnings[] = [ 'cite_error_references_duplicate_key', $name ]; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
private function nextRefSequence(): int { |
| 359 |
return ++$this->refSequence; |
| 360 |
} |
| 361 |
|
| 362 |
} |