| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Wikitext; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Config\Env; |
| 7 |
use Wikimedia\Parsoid\Core\ContentModelHandler as IContentModelHandler; |
| 8 |
use Wikimedia\Parsoid\Core\SelectiveUpdateData; |
| 9 |
use Wikimedia\Parsoid\DOM\Document; |
| 10 |
use Wikimedia\Parsoid\Ext\DOMProcessor as ExtDOMProcessor; |
| 11 |
use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
| 12 |
use Wikimedia\Parsoid\Html2Wt\RemoveRedLinks; |
| 13 |
use Wikimedia\Parsoid\Html2Wt\SelectiveSerializer; |
| 14 |
use Wikimedia\Parsoid\Html2Wt\WikitextSerializer; |
| 15 |
use Wikimedia\Parsoid\Utils\ContentUtils; |
| 16 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 17 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 18 |
use Wikimedia\Parsoid\Utils\Timing; |
| 19 |
|
| 20 |
class ContentModelHandler extends IContentModelHandler { |
| 21 |
|
| 22 |
/** @var Env */ |
| 23 |
private $env; |
| 24 |
|
| 25 |
/** |
| 26 |
* Sneak an environment in here since it's not exposed as part of the |
| 27 |
* ParsoidExtensionAPI |
| 28 |
* |
| 29 |
* @param Env $env |
| 30 |
*/ |
| 31 |
public function __construct( Env $env ) { |
| 32 |
$this->env = $env; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Bring DOM to expected canonical form |
| 37 |
*/ |
| 38 |
private function canonicalizeDOM( |
| 39 |
Env $env, Document $doc, bool $isSelectiveUpdate |
| 40 |
): void { |
| 41 |
$body = DOMCompat::getBody( $doc ); |
| 42 |
|
| 43 |
// Convert DOM to internal canonical form |
| 44 |
DOMDataUtils::visitAndLoadDataAttribs( $body, [ |
| 45 |
'markNew' => !$isSelectiveUpdate, |
| 46 |
] ); |
| 47 |
|
| 48 |
// Update DSR offsets if necessary. |
| 49 |
ContentUtils::convertOffsets( |
| 50 |
$env, $doc, $env->getRequestOffsetType(), 'byte' |
| 51 |
); |
| 52 |
|
| 53 |
// Strip <section> and mw:FallbackId <span> tags, if present, |
| 54 |
// as well as extended annotation wrappers. |
| 55 |
// This ensures that we can accept HTML from CX / VE |
| 56 |
// and other clients that might have stripped them. |
| 57 |
ContentUtils::stripUnnecessaryWrappersAndSyntheticNodes( $body ); |
| 58 |
|
| 59 |
$redLinkRemover = new RemoveRedLinks( $this->env ); |
| 60 |
$redLinkRemover->run( $body ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Fetch prior DOM for selser. |
| 65 |
* |
| 66 |
* @param ParsoidExtensionAPI $extApi |
| 67 |
* @param SelectiveUpdateData $selserData |
| 68 |
*/ |
| 69 |
private function setupSelser( |
| 70 |
ParsoidExtensionAPI $extApi, SelectiveUpdateData $selserData |
| 71 |
) { |
| 72 |
$env = $this->env; |
| 73 |
|
| 74 |
// Why is it safe to use a reparsed dom for dom diff'ing? |
| 75 |
// (Since that's the only use of `env.page.dom`) |
| 76 |
// |
| 77 |
// There are two types of non-determinism to discuss: |
| 78 |
// |
| 79 |
// * The first is from parsoid generated ids. At this point, |
| 80 |
// data-attributes have already been applied so there's no chance |
| 81 |
// that variability in the ids used to associate data-attributes |
| 82 |
// will lead to data being applied to the wrong nodes. |
| 83 |
// |
| 84 |
// Further, although about ids will differ, they belong to the set |
| 85 |
// of ignorable attributes in the dom differ. |
| 86 |
// |
| 87 |
// * Templates, and encapsulated content in general, are the second. |
| 88 |
// Since that content can change in between parses, the resulting |
| 89 |
// dom might not be the same. However, because dom diffing on |
| 90 |
// on those regions only uses data-mw for comparision (which will |
| 91 |
// remain constant between parses), this also shouldn't be an |
| 92 |
// issue. |
| 93 |
// |
| 94 |
// There is one caveat. Because encapsulated content isn't |
| 95 |
// guaranteed to be "balanced", the template affected regions |
| 96 |
// may change between parses. This should be rare. |
| 97 |
// |
| 98 |
// We therefore consider this safe since it won't corrupt the page |
| 99 |
// and, at worst, mixed up diff'ing annotations can end up with an |
| 100 |
// unfaithful serialization of the edit. |
| 101 |
// |
| 102 |
// However, in cases where original content is not returned by the |
| 103 |
// client / RESTBase, selective serialization cannot proceed and |
| 104 |
// we're forced to fallback to normalizing the entire page. This has |
| 105 |
// proved unacceptable to editors as is and, as we lean heavier on |
| 106 |
// selser, will only get worse over time. |
| 107 |
// |
| 108 |
// So, we're forced to trade off the correctness for usability. |
| 109 |
if ( $selserData->revHTML === null ) { |
| 110 |
$env->log( "warn/html2wt", "Missing selserData->revHTML. Regenerating." ); |
| 111 |
|
| 112 |
// FIXME(T266838): Create a new Env for this parse? Something is |
| 113 |
// needed to avoid this rigmarole. |
| 114 |
$topLevelDoc = $env->topLevelDoc; |
| 115 |
$env->setupTopLevelDoc(); |
| 116 |
// This effectively parses $selserData->revText for us because |
| 117 |
// $selserData->revText = $env->getPageconfig()->getPageMainContent() |
| 118 |
$doc = $this->toDOM( $extApi ); |
| 119 |
$env->topLevelDoc = $topLevelDoc; |
| 120 |
} else { |
| 121 |
$doc = ContentUtils::createDocument( $selserData->revHTML, true ); |
| 122 |
} |
| 123 |
|
| 124 |
$this->canonicalizeDOM( $env, $doc, false ); |
| 125 |
$selserData->revDOM = $doc; |
| 126 |
} |
| 127 |
|
| 128 |
private function processIndicators( Document $doc, ParsoidExtensionAPI $extApi ): void { |
| 129 |
// Erroneous indicators without names will be <span>s |
| 130 |
$indicators = DOMCompat::querySelectorAll( $doc, 'meta[typeof~="mw:Extension/indicator"]' ); |
| 131 |
$iData = []; |
| 132 |
|
| 133 |
// https://www.mediawiki.org/wiki/Help:Page_status_indicators#Adding_page_status_indicators |
| 134 |
// says that last one wins. But, that may just be documentation of the |
| 135 |
// implementation vs. being a deliberate strategy. |
| 136 |
// |
| 137 |
// The indicators are ordered by depth-first pre-order DOM traversal. |
| 138 |
// This ensures that the indicators are in document textual order. |
| 139 |
// Given that, the for-loop below implements "last-one-wins" semantics |
| 140 |
// for indicators that use the same name key. |
| 141 |
foreach ( $indicators as $meta ) { |
| 142 |
// Since the DOM is in "stored" state, we have to reparse data-mw here. |
| 143 |
$codec = DOMDataUtils::getCodec( $doc ); |
| 144 |
$dataMwAttr = DOMCompat::getAttribute( $meta, 'data-mw' ); |
| 145 |
$dmw = $dataMwAttr === null ? null : |
| 146 |
$codec->newFromJsonString( $dataMwAttr, DOMDataUtils::getCodecHints()['data-mw'] ); |
| 147 |
$name = $dmw->attrs->name; |
| 148 |
$iData[$name] = $dmw->html; |
| 149 |
} |
| 150 |
|
| 151 |
// set indicator metadata for unique keys |
| 152 |
foreach ( $iData as $name => $html ) { |
| 153 |
$extApi->getMetadata()->setIndicator( (string)$name, $html ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* @inheritDoc |
| 159 |
*/ |
| 160 |
public function toDOM( |
| 161 |
ParsoidExtensionAPI $extApi, ?SelectiveUpdateData $selectiveUpdateData = null |
| 162 |
): Document { |
| 163 |
$env = $this->env; |
| 164 |
$pipelineFactory = $env->getPipelineFactory(); |
| 165 |
|
| 166 |
if ( $selectiveUpdateData ) { |
| 167 |
$doc = ContentUtils::createDocument( $selectiveUpdateData->revHTML, true ); |
| 168 |
$env->setupTopLevelDoc( $doc ); |
| 169 |
$this->canonicalizeDOM( $env, $env->topLevelDoc, true ); |
| 170 |
$selectiveUpdateData->revDOM = $doc; |
| 171 |
$doc = $pipelineFactory->selectiveDOMUpdate( $selectiveUpdateData ); |
| 172 |
} else { |
| 173 |
$doc = $pipelineFactory->parse( |
| 174 |
// @phan-suppress-next-line PhanDeprecatedFunction not ready for topFrame yet |
| 175 |
$env->getPageConfig()->getPageMainContent() |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
// Hardcoded support for indicators |
| 180 |
// TODO: Eventually we'll want to apply this to selective updates as well |
| 181 |
if ( !$selectiveUpdateData ) { |
| 182 |
$this->processIndicators( $doc, $extApi ); |
| 183 |
} |
| 184 |
|
| 185 |
return $doc; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Preprocess the edited DOM as required before attempting to convert it to wikitext |
| 190 |
* 1. The edited DOM (represented by body) might not be in canonical form |
| 191 |
* because Parsoid might be providing server-side management of global state |
| 192 |
* for extensions. To address this and bring the DOM back to canonical form, |
| 193 |
* we run extension-provided handlers. The original DOM isn't subject to this problem. |
| 194 |
* FIXME: But, this is not the only reason an extension might register a preprocessor. |
| 195 |
* How do we know when to run a preprocessor on both original & edited DOMs? |
| 196 |
* 2. We need to do this after all data attributes have been loaded. |
| 197 |
* 3. We need to do this before we run dom-diffs to eliminate spurious diffs. |
| 198 |
* |
| 199 |
* @param Env $env |
| 200 |
* @param Document $doc |
| 201 |
*/ |
| 202 |
private function preprocessEditedDOM( Env $env, Document $doc ): void { |
| 203 |
$siteConfig = $env->getSiteConfig(); |
| 204 |
|
| 205 |
// Run any registered DOM preprocessors |
| 206 |
foreach ( $siteConfig->getExtDOMProcessors() as $extName => $domProcs ) { |
| 207 |
foreach ( $domProcs as $i => $classNameOrSpec ) { |
| 208 |
$c = $siteConfig->getObjectFactory()->createObject( $classNameOrSpec, [ |
| 209 |
'allowClassName' => true, |
| 210 |
'assertClass' => ExtDOMProcessor::class, |
| 211 |
] ); |
| 212 |
$c->htmlPreprocess( |
| 213 |
new ParsoidExtensionAPI( $env ), DOMCompat::getBody( $doc ) |
| 214 |
); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* @inheritDoc |
| 221 |
*/ |
| 222 |
public function fromDOM( |
| 223 |
ParsoidExtensionAPI $extApi, ?SelectiveUpdateData $selserData = null |
| 224 |
): string { |
| 225 |
$env = $this->env; |
| 226 |
$siteConfig = $env->getSiteConfig(); |
| 227 |
$setupTiming = Timing::start( $siteConfig ); |
| 228 |
|
| 229 |
$this->canonicalizeDOM( $env, $env->topLevelDoc, false ); |
| 230 |
|
| 231 |
$serializerOpts = [ 'selserData' => $selserData ]; |
| 232 |
if ( $selserData ) { |
| 233 |
$serializer = new SelectiveSerializer( $env, $serializerOpts ); |
| 234 |
$this->setupSelser( $extApi, $selserData ); |
| 235 |
$wtsType = 'selser'; |
| 236 |
} else { |
| 237 |
// Fallback |
| 238 |
$serializer = new WikitextSerializer( $env, $serializerOpts ); |
| 239 |
$wtsType = 'noselser'; |
| 240 |
} |
| 241 |
|
| 242 |
$setupTiming->end( 'html2wt.setup', 'html2wt_setup_seconds', [] ); |
| 243 |
|
| 244 |
$preprocTiming = Timing::start( $siteConfig ); |
| 245 |
$this->preprocessEditedDOM( $env, $env->topLevelDoc ); |
| 246 |
$preprocTiming->end( 'html2wt.preprocess', 'html2wt_preprocess_seconds', [] ); |
| 247 |
|
| 248 |
$serializeTiming = Timing::start( $siteConfig ); |
| 249 |
$res = $serializer->serializeDOM( $env->topLevelDoc ); |
| 250 |
$serializeTiming->end( |
| 251 |
"html2wt.{$wtsType}.serialize", |
| 252 |
"html2wt_serialize_seconds", |
| 253 |
[ 'wts' => $wtsType ] |
| 254 |
); |
| 255 |
|
| 256 |
return $res; |
| 257 |
} |
| 258 |
|
| 259 |
} |