-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikiNodeChangesBuilder.php
325 lines (269 loc) · 11.4 KB
/
WikiNodeChangesBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
declare(strict_types=1);
namespace Drupal\omnipedia_changes\Service;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
use Drupal\omnipedia_changes\Event\OmnipediaContentChangesEventInterface;
use Drupal\omnipedia_changes\Event\Omnipedia\Changes\DiffPostBuildEvent;
use Drupal\omnipedia_changes\Event\Omnipedia\Changes\DiffPostRenderPreBuildEvent;
use Drupal\omnipedia_changes\Service\WikiNodeChangesBuilderInterface;
use Drupal\omnipedia_changes\Service\WikiNodeChangesCacheInterface;
use Drupal\omnipedia_changes\Service\WikiNodeChangesInfoInterface;
use Drupal\omnipedia_changes\WikiNodeChangesCssClassesInterface;
use Drupal\omnipedia_changes\WikiNodeChangesCssClassesTrait;
use Drupal\typed_entity\EntityWrapperInterface;
use HtmlDiffAdvancedInterface;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* The Omnipedia wiki node changes builder service.
*/
class WikiNodeChangesBuilder implements WikiNodeChangesBuilderInterface, WikiNodeChangesCssClassesInterface {
use StringTranslationTrait;
use WikiNodeChangesCssClassesTrait;
/**
* Service constructor; saves dependencies.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The Drupal entity type manager.
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The Symfony event dispatcher service.
*
* @param \HtmlDiffAdvancedInterface $htmlDiff
* The HTML diff service provided by the Diff module.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The Drupal renderer service.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
* The Drupal string translation service.
*
* @param \Drupal\typed_entity\EntityWrapperInterface $typedEntityRepositoryManager
* The Typed Entity repository manager.
*
* @param \Drupal\omnipedia_changes\Service\WikiNodeChangesCacheInterface $wikiNodeChangesCache
* The Omnipedia wiki node changes cache service.
*
* @param \Drupal\omnipedia_changes\Service\WikiNodeChangesInfoInterface $wikiNodeChangesInfo
* The Omnipedia wiki node changes info service.
*
* @see $this->alterHtmlDiffConfig()
*/
public function __construct(
protected readonly EntityTypeManagerInterface $entityTypeManager,
protected readonly EventDispatcherInterface $eventDispatcher,
protected readonly HtmlDiffAdvancedInterface $htmlDiff,
protected readonly RendererInterface $renderer,
protected $stringTranslation,
protected readonly EntityWrapperInterface $typedEntityRepositoryManager,
protected readonly WikiNodeChangesCacheInterface $wikiNodeChangesCache,
protected readonly WikiNodeChangesInfoInterface $wikiNodeChangesInfo,
) {
$this->alterHtmlDiffConfig();
}
/**
* Alter the HTML diff configuration used for diffing.
*
* The following changes are made:
*
* - Disables the use of HTML Purifier to avoid having to wade through that
* configuration nightmare to whitelist attributes (e.g. style) and elements
* (such as SVG icons). Drupal's render and filtering systems should take
* care of any security stuff for us as we render before passing the markup
* to the HTML diff service. This config option requires @link
* https://github.com/caxy/php-htmldiff/releases/tag/v0.1.11
* caxy/php-htmldiff >= 0.1.11 @endLink which has been specified in this
* module's composer.json.
*
* - Disables special handling of <a> elements diffing, which would highlight
* changed href attributes. The only instance where this currently happens
* without the link text changing is when an internal wiki link changes to
* point to the new date's revision, which would be irrelevant to highlight.
*/
protected function alterHtmlDiffConfig(): void {
/** @var \Caxy\HtmlDiff\HtmlDiffConfig */
$config = $this->htmlDiff->getConfig();
$config->setPurifierEnabled(false);
/** @var array */
$isolatedDiffElements = $config->getIsolatedDiffTags();
if (isset($isolatedDiffElements['a'])) {
unset($isolatedDiffElements['a']);
}
$config->setIsolatedDiffTags($isolatedDiffElements);
}
/**
* Get diff content for a wiki node.
*
* @param \Drupal\node\NodeInterface $node
* A wiki node object to get the diff content for.
*
* @param boolean $allowInvalid
* Whether to check for rendered cached changes that are still present but
* have been invalidated. Defaults to false.
*
* @return array
* The diff render array.
*
* @see \Drupal\Core\Cache\CacheBackendInterface::get()
* See the $allow_invalid parameter in this method for use cases of our
* $allowInvalid parameter.
*/
protected function getDiff(
NodeInterface $node, bool $allowInvalid = false
): array {
// Return a cached render array if one is found in the cache.
if ($this->wikiNodeChangesCache->isCached($node, $allowInvalid)) {
return $this->wikiNodeChangesCache->get($node, $allowInvalid);
}
/** @var \Drupal\omnipedia_core\WrappedEntities\NodeWithWikiInfoInterface */
$previousWrappedNode = $this->typedEntityRepositoryManager->wrap(
$node,
)->getPreviousWikiRevision();
/** @var \Drupal\node\NodeInterface */
$previousNode = $previousWrappedNode->getEntity();
/** @var \Drupal\Core\Entity\EntityViewBuilderInterface */
$viewBuilder = $this->entityTypeManager->getViewBuilder(
$node->getEntityTypeId()
);
/** @var array */
$previousRenderArray = $viewBuilder->view($previousNode, 'full');
/** @var array */
$currentRenderArray = $viewBuilder->view($node, 'full');
// We need to create a new render context to render the previous and current
// nodes in, so that we can capture the metadata (caching, attachments) and
// store it in the cached copy. This also ensures that any metadata from
// rendering these does not bubble up to the parent context, unless it
// renders the render array that we return.
/** @var \Drupal\Core\Render\RenderContext */
$renderContext = new RenderContext();
/** @var string */
$previousRendered = (string) $this->renderer->executeInRenderContext(
$renderContext, function() use (&$previousRenderArray) {
return $this->renderer->render($previousRenderArray);
}
);
/** @var string */
$currentRendered = (string) $this->renderer->executeInRenderContext(
$renderContext, function() use (&$currentRenderArray) {
return $this->renderer->render($currentRenderArray);
}
);
/** @var \Drupal\omnipedia_changes\Event\Omnipedia\Changes\DiffPostRenderPreBuildEvent */
$postRenderEvent = new DiffPostRenderPreBuildEvent(
$node, $previousNode, $currentRendered, $previousRendered
);
// Dispatch the event with the event object.
$this->eventDispatcher->dispatch(
$postRenderEvent,
OmnipediaContentChangesEventInterface::DIFF_POST_RENDER_PRE_BUILD
);
$this->htmlDiff->setOldHtml($postRenderEvent->getPreviousRendered());
$this->htmlDiff->setNewHtml($postRenderEvent->getCurrentRendered());
// Build and merge the metadata for both nodes. This allows us to store the
// cache metadata and attachments for both, so that they can be retrieved
// along with the rendered diff and passed to Drupal's render system.
/** @var \Drupal\Core\Render\BubbleableMetadata */
$bubbleableMetadata = (BubbleableMetadata::createFromRenderArray(
$previousRenderArray
))->merge(
BubbleableMetadata::createFromRenderArray($currentRenderArray)
);
// Disable PHP libxml errors because we sometimes end up with invalid
// nesting, e.g. <figure> inside of <dl> elements.
//
// @see https://stackoverflow.com/questions/6090667/php-domdocument-errors-warnings-on-html5-tags#6090728
\libxml_use_internal_errors(true);
$this->htmlDiff->build();
\libxml_use_internal_errors(false);
/** @var \Symfony\Component\DomCrawler\Crawler */
$differenceCrawler = (new Crawler(
'<div id="omnipedia-changes-root">' .
$this->htmlDiff->getDifference() .
'</div>'
))->filter('#omnipedia-changes-root');
// Removes the node title element that Drupal generates.
//
// @todo Can this be handled in a node entity view mode instead?
foreach ($differenceCrawler->filter('.node__title') as $element) {
$element->parentNode->removeChild($element);
}
/** @var \Drupal\omnipedia_changes\Event\Omnipedia\Changes\DiffPostBuildEvent */
$postBuildEvent = new DiffPostBuildEvent(
$node, $previousNode, $currentRendered, $previousRendered,
$differenceCrawler
);
// Dispatch the event with the event object.
$this->eventDispatcher->dispatch(
$postBuildEvent, OmnipediaContentChangesEventInterface::DIFF_POST_BUILD
);
/** @var \Symfony\Component\DomCrawler\Crawler */
$differenceCrawler = $postBuildEvent->getCrawler();
/** @var array */
$renderArray = [
'#markup' => $differenceCrawler->html(),
// Since the parsed diffs have already been run through the renderer and
// filtering system, we're setting #printed to true to avoid Drupal
// filtering the output a second time and breaking stuff. For example,
// this would remove style attributes and strip SVG icons.
'#printed' => true,
];
// Apply the metadata to the render array.
$bubbleableMetadata->applyTo($renderArray);
// Save the rendered diff to cache.
$this->wikiNodeChangesCache->set($node, $renderArray);
return $renderArray;
}
/**
* {@inheritdoc}
*
* @see $this->getDiff()
* Gets diff content for a wiki node.
*/
public function build(
NodeInterface $node, bool $allowInvalid = false
): array {
/** @var \Drupal\omnipedia_core\WrappedEntities\NodeWithWikiInfoInterface */
$previousWrappedNode = $this->typedEntityRepositoryManager->wrap(
$node,
)->getPreviousWikiRevision();
// Bail if not a wiki node or the wiki node does not have a previous
// revision.
if (!\is_object($previousWrappedNode)) {
return [];
}
/** @var \Drupal\node\NodeInterface */
$previousNode = $previousWrappedNode->getEntity();
/** @var array */
$renderArray = $this->getDiff($node, $allowInvalid);
$renderArray['#markup'] =
// Note that we can't use '#type' => 'container' or some other wrapper
// while also setting '#printed' => true as we've told Drupal to do no
// further rendering.
//
// @todo Rework this as a Twig template?
'<div class="' . $this->getChangesBaseClass() . '">' .
$renderArray['#markup'] .
'</div>';
$renderArray['#attached']['library'][] =
'omnipedia_changes/component.changes';
return $renderArray;
}
/**
* {@inheritdoc}
*/
public function buildPlaceholder(NodeInterface $node): array {
return [
'#markup' => $this->t(
'The changes for this date are still being built. Please check back in a few minutes.'
),
'#cache' => $this->wikiNodeChangesInfo->getPlaceholderCacheMetadata(
$node->nid->getString()
),
];
}
}