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
|
|
|
|
2020-02-03 18:52:06 +00:00
|
|
|
namespace Wikimedia\Parsoid\Ext\Cite;
|
2019-04-02 22:06:21 +00:00
|
|
|
|
2020-05-28 22:26:31 +00:00
|
|
|
use DOMElement;
|
2019-05-25 22:24:47 +00:00
|
|
|
use stdClass;
|
2020-07-08 22:48:02 +00:00
|
|
|
use Wikimedia\Parsoid\Ext\DOMUtils;
|
2020-03-04 21:40:55 +00:00
|
|
|
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
|
2019-04-02 22:06:21 +00:00
|
|
|
|
|
|
|
class ReferencesData {
|
2019-05-25 22:24:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var RefGroup[]
|
|
|
|
*/
|
|
|
|
private $refGroups;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ReferencesData constructor.
|
|
|
|
*/
|
2020-02-07 16:17:42 +00:00
|
|
|
public function __construct() {
|
2019-04-02 22:06:21 +00:00
|
|
|
$this->index = 0;
|
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 $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
|
|
|
/**
|
2020-07-14 15:04:19 +00:00
|
|
|
* @param ?string $groupName
|
2019-05-25 22:24:47 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2020-02-07 16:17:42 +00:00
|
|
|
* @param ParsoidExtensionAPI $extApi
|
2019-05-25 22:24:47 +00:00
|
|
|
* @param string $groupName
|
|
|
|
* @param string $refName
|
2020-07-08 22:48:02 +00:00
|
|
|
* @param string $follow
|
|
|
|
* @param string $contentId
|
2019-05-25 22:24:47 +00:00
|
|
|
* @param string $about
|
|
|
|
* @param bool $skipLinkback
|
2020-05-28 22:26:31 +00:00
|
|
|
* @param DOMElement $linkBack
|
2019-05-25 22:24:47 +00:00
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
public function add(
|
2020-07-08 22:48:02 +00:00
|
|
|
ParsoidExtensionAPI $extApi, string $groupName, string $refName, string $follow,
|
|
|
|
string $contentId, string $about, bool $skipLinkback, DOMElement $linkBack
|
2019-05-25 22:24:47 +00:00
|
|
|
): stdClass {
|
2019-04-02 22:06:21 +00:00
|
|
|
$group = $this->getRefGroup( $groupName, true );
|
2020-02-20 23:12:58 +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:
|
|
|
|
// in PHP: Sanitizer#decodeTagAttributes and
|
|
|
|
// in Parsoid: ExtensionHandler#normalizeExtOptions
|
|
|
|
$refName = $extApi->sanitizeHTMLId( $refName );
|
2019-08-14 22:59:28 +00:00
|
|
|
$hasRefName = strlen( $refName ) > 0;
|
2020-07-08 22:48:02 +00:00
|
|
|
$hasFollow = strlen( $follow ) > 0;
|
|
|
|
|
|
|
|
// Is this a follow ref that has been preceeded by a named ref which defined
|
|
|
|
// content or did not define content, or a self closed named ref without content
|
|
|
|
if ( !$hasRefName && $hasFollow && isset( $group->indexByName[$follow] ) ) {
|
|
|
|
$ref = $group->indexByName[$follow];
|
|
|
|
$contentSup = $extApi->getContentDOM( $contentId );
|
|
|
|
$ownerDoc = $contentSup->ownerDocument;
|
|
|
|
$span = $ownerDoc->createElement( 'span' );
|
|
|
|
DOMUtils::addTypeOf( $span, 'mw:Cite/Follow' );
|
|
|
|
$span->setAttribute( 'about', $about );
|
|
|
|
$spaceNode = $ownerDoc->createTextNode( ' ' );
|
|
|
|
$span->appendChild( $spaceNode );
|
|
|
|
DOMUtils::migrateChildren( $contentSup, $span );
|
|
|
|
|
|
|
|
// contentSup is now empty and can be used as a container for migrate children
|
|
|
|
$contentSup->appendChild( $span );
|
|
|
|
|
|
|
|
// If the named ref has defined content
|
|
|
|
if ( $ref->contentId ) {
|
|
|
|
$refContent = $extApi->getContentDOM( $ref->contentId );
|
|
|
|
ParsoidExtensionAPI::migrateChildrenBetweenDocs( $contentSup, $refContent, false );
|
|
|
|
} else {
|
|
|
|
// Otherwise we have a follow that comes after named ref without content
|
|
|
|
// So create a sup in a fragment, set that into the environment and migrate
|
|
|
|
// the follow content into the fragment
|
|
|
|
$ref->contentId = $contentId;
|
|
|
|
}
|
|
|
|
return $ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must check for error case where $hasRefName and $hasFollow are both present
|
|
|
|
// which still needs to create the ref, but also flag it with an error due to
|
|
|
|
// [ 'key' => 'cite_error_ref_too_many_keys' ]; as trapped in References.php
|
|
|
|
// but which needs to preserve the extra key for round tripping
|
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];
|
2020-02-07 16:17:42 +00:00
|
|
|
if ( $ref->contentId && !$ref->hasMultiples ) {
|
2019-04-02 22:06:21 +00:00
|
|
|
$ref->hasMultiples = true;
|
|
|
|
// Use the non-pp version here since we've already stored attribs
|
|
|
|
// before putting them in the map.
|
2020-02-07 16:17:42 +00:00
|
|
|
$ref->cachedHtml = $extApi->getContentHTML( $ref->contentId );
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
2020-05-28 22:26:31 +00:00
|
|
|
$ref->nodes[] = $linkBack;
|
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,
|
2020-02-07 16:17:42 +00:00
|
|
|
'contentId' => null,
|
2019-04-02 22:06:21 +00:00
|
|
|
'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
|
2020-05-28 22:26:31 +00:00
|
|
|
'cachedHtml' => '',
|
|
|
|
'nodes' => [],
|
2019-04-02 22:06:21 +00:00
|
|
|
];
|
|
|
|
$group->refs[] = $ref;
|
2020-05-28 22:26:31 +00:00
|
|
|
if ( $hasRefName ) {
|
2019-05-25 22:24:47 +00:00
|
|
|
$group->indexByName[$refName] = $ref;
|
2020-05-28 22:26:31 +00:00
|
|
|
$ref->nodes[] = $linkBack;
|
2019-04-02 22:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$skipLinkback ) {
|
|
|
|
$ref->linkbacks[] = $ref->key . '-' . count( $ref->linkbacks );
|
|
|
|
}
|
2020-05-28 22:26:31 +00:00
|
|
|
|
2019-04-02 22:06:21 +00:00
|
|
|
return $ref;
|
|
|
|
}
|
|
|
|
|
2019-05-25 22:24:47 +00:00
|
|
|
/**
|
|
|
|
* @return RefGroup[]
|
|
|
|
*/
|
|
|
|
public function getRefGroups(): array {
|
|
|
|
return $this->refGroups;
|
|
|
|
}
|
|
|
|
}
|