2019-04-02 22:06:21 +00:00
|
|
|
<?php
|
2019-05-25 22:24:47 +00:00
|
|
|
declare( strict_types = 1 );
|
2019-04-02 22:06:21 +00:00
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
namespace Parsoid\Ext\Cite;
|
2019-04-02 22:06:21 +00:00
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
use Parsoid\Config\Env;
|
|
|
|
use Parsoid\Utils\ContentUtils;
|
|
|
|
use Parsoid\Wt2Html\TT\Sanitizer;
|
|
|
|
use stdClass;
|
2019-04-02 22:06:21 +00:00
|
|
|
|
|
|
|
class ReferencesData {
|
2019-05-25 22:24:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Env
|
|
|
|
*/
|
|
|
|
private $env;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RefGroup[]
|
|
|
|
*/
|
|
|
|
private $refGroups;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ReferencesData constructor.
|
|
|
|
* @param Env $env
|
|
|
|
*/
|
|
|
|
public function __construct( Env $env ) {
|
2019-04-02 22:06:21 +00:00
|
|
|
$this->index = 0;
|
|
|
|
$this->env = $env;
|
2019-05-25 22:24:47 +00:00
|
|
|
$this->refGroups = [];
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
/**
|
|
|
|
* @param string $val
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
public function makeValidIdAttr( string $val ) {
|
2019-04-02 22:06:21 +00:00
|
|
|
// Looks like Cite.php doesn't try to fix ids that already have
|
|
|
|
// a "_" in them. Ex: name="a b" and name="a_b" are considered
|
|
|
|
// identical. Not sure if this is a feature or a bug.
|
|
|
|
// It also considers entities equal to their encoding
|
|
|
|
// (i.e. '&' === '&'), which is done:
|
2019-06-05 20:09:31 +00:00
|
|
|
// in PHP: Sanitizer#decodeTagAttributes and
|
|
|
|
// in Parsoid: ExtensionHandler#normalizeExtOptions
|
2019-04-02 22:06:21 +00:00
|
|
|
return Sanitizer::escapeIdForAttribute( $val );
|
|
|
|
}
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
/**
|
|
|
|
* @param string $groupName
|
|
|
|
* @param bool $allocIfMissing
|
|
|
|
* @return RefGroup|null
|
|
|
|
*/
|
|
|
|
public function getRefGroup( string $groupName = '', bool $allocIfMissing = false ): ?RefGroup {
|
|
|
|
if ( !isset( $this->refGroups[$groupName] ) && $allocIfMissing ) {
|
|
|
|
$this->refGroups[$groupName] = new RefGroup( $groupName );
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
2019-05-25 22:24:47 +00:00
|
|
|
return $this->refGroups[$groupName] ?? null;
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
/**
|
|
|
|
* @param string|null $groupName
|
|
|
|
*/
|
|
|
|
public function removeRefGroup( ?string $groupName = null ): void {
|
|
|
|
if ( $groupName !== null ) {
|
2019-04-02 22:06:21 +00:00
|
|
|
// '' is a valid group (the default group)
|
2019-05-25 22:24:47 +00:00
|
|
|
unset( $this->refGroups[$groupName] );
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
/**
|
|
|
|
* @param Env $env
|
|
|
|
* @param string $groupName
|
|
|
|
* @param string $refName
|
|
|
|
* @param string $about
|
|
|
|
* @param bool $skipLinkback
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function add(
|
|
|
|
Env $env, string $groupName, string $refName, string $about, bool $skipLinkback
|
|
|
|
): stdClass {
|
2019-04-02 22:06:21 +00:00
|
|
|
$group = $this->getRefGroup( $groupName, true );
|
|
|
|
$refName = $this->makeValidIdAttr( $refName );
|
2019-08-14 22:59:28 +00:00
|
|
|
$hasRefName = strlen( $refName ) > 0;
|
2019-04-02 22:06:21 +00:00
|
|
|
|
2019-08-14 22:59:28 +00:00
|
|
|
if ( $hasRefName && isset( $group->indexByName[$refName] ) ) {
|
2019-05-25 22:24:47 +00:00
|
|
|
$ref = $group->indexByName[$refName];
|
2019-04-02 22:06:21 +00:00
|
|
|
if ( $ref->content && !$ref->hasMultiples ) {
|
|
|
|
$ref->hasMultiples = true;
|
|
|
|
// Use the non-pp version here since we've already stored attribs
|
|
|
|
// before putting them in the map.
|
2019-05-25 22:24:47 +00:00
|
|
|
$ref->cachedHtml = ContentUtils::toXML(
|
|
|
|
$env->getFragment( $ref->content )[0],
|
|
|
|
[ 'innerXML' => true ]
|
|
|
|
);
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The ids produced Cite.php have some particulars:
|
|
|
|
// Simple refs get 'cite_ref-' + index
|
|
|
|
// Refs with names get 'cite_ref-' + name + '_' + index + (backlink num || 0)
|
|
|
|
// Notes (references) whose ref doesn't have a name are 'cite_note-' + index
|
|
|
|
// Notes whose ref has a name are 'cite_note-' + name + '-' + index
|
|
|
|
$n = $this->index;
|
2019-05-25 22:24:47 +00:00
|
|
|
$refKey = strval( 1 + $n );
|
2019-08-14 22:59:28 +00:00
|
|
|
$refIdBase = 'cite_ref-' . ( $hasRefName ? $refName . '_' . $refKey : $refKey );
|
|
|
|
$noteId = 'cite_note-' . ( $hasRefName ? $refName . '-' . $refKey : $refKey );
|
2019-04-02 22:06:21 +00:00
|
|
|
|
|
|
|
// bump index
|
|
|
|
$this->index += 1;
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
$ref = (object)[
|
2019-04-02 22:06:21 +00:00
|
|
|
'about' => $about,
|
|
|
|
'content' => null,
|
|
|
|
'dir' => '',
|
|
|
|
'group' => $group->name,
|
|
|
|
'groupIndex' => count( $group->refs ) + 1,
|
|
|
|
'index' => $n,
|
|
|
|
'key' => $refIdBase,
|
2019-08-14 22:59:28 +00:00
|
|
|
'id' => $hasRefName ? $refIdBase . '-0' : $refIdBase,
|
2019-04-02 22:06:21 +00:00
|
|
|
'linkbacks' => [],
|
|
|
|
'name' => $refName,
|
|
|
|
'target' => $noteId,
|
|
|
|
'hasMultiples' => false,
|
|
|
|
// Just used for comparison when we have multiples
|
|
|
|
'cachedHtml' => ''
|
|
|
|
];
|
|
|
|
$group->refs[] = $ref;
|
|
|
|
if ( $refName ) {
|
2019-05-25 22:24:47 +00:00
|
|
|
$group->indexByName[$refName] = $ref;
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$skipLinkback ) {
|
|
|
|
$ref->linkbacks[] = $ref->key . '-' . count( $ref->linkbacks );
|
|
|
|
}
|
|
|
|
return $ref;
|
|
|
|
}
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
/**
|
|
|
|
* @return Env
|
|
|
|
*/
|
|
|
|
public function getEnv(): Env {
|
|
|
|
return $this->env;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return RefGroup[]
|
|
|
|
*/
|
|
|
|
public function getRefGroups(): array {
|
|
|
|
return $this->refGroups;
|
|
|
|
}
|
|
|
|
}
|