-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWikiNodeChangesWarmer.php
317 lines (255 loc) · 9.39 KB
/
WikiNodeChangesWarmer.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
<?php
declare(strict_types=1);
namespace Drupal\omnipedia_changes\Plugin\warmer;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Utility\Error;
use Drupal\omnipedia_changes\Service\WikiNodeChangesBuilderInterface;
use Drupal\omnipedia_changes\Service\WikiNodeChangesInfoInterface;
use Drupal\omnipedia_user\Service\RepresentativeRenderUserInterface;
use Drupal\typed_entity\EntityWrapperInterface;
use Drupal\user\RoleStorageInterface;
use Drupal\user\UserInterface;
use Drupal\user\UserStorageInterface;
use Drupal\warmer\Plugin\WarmerPluginBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The Omnipedia wiki node changes cache warmer plug-in.
*
* @Warmer(
* id = "omnipedia_wiki_node_changes",
* label = @Translation("Omnipedia: wiki page changes"),
* description = @Translation("Warms the wiki page changes cache by building any changes not yet cached.")
* )
*
* @see \Drupal\warmer\Plugin\WarmerInterface
* Documentation for public methods.
*
* @see \Drupal\warmer_entity\Plugin\warmer\EntityWarmer
* Example/reference of a warmer plug-in shipped with the Warmer module.
*
* @see \Drupal\warmer_cdn\Plugin\warmer\CdnWarmer
* Example/reference of a warmer plug-in shipped with the Warmer module.
*/
class WikiNodeChangesWarmer extends WarmerPluginBase {
/**
* An array of all possible cache IDs for wiki nodes.
*
* @var string[]
*
* @see \Drupal\omnipedia_changes\Service\WikiNodeChangesInfoInterface::getAllCacheIds()
* Built from the return value of this, but with the array structure
* flattened - instead of keying by node ID (nid), we key by node ID and
* roles, to allow for easy counting of progress.
*/
protected array $cacheIds;
/**
* {@inheritdoc}
*
* @param \Drupal\Core\Session\AccountSwitcherInterface $accountSwitcher
* The Drupal account switcher service.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The Drupal entity type manager.
*
* @param \Psr\Log\LoggerInterface $loggerChannel
* Our logger channel.
*
* @param \Drupal\omnipedia_user\Service\RepresentativeRenderUserInterface $representativeRenderUser
* The Omnipedia representative render user service.
*
* @param \Drupal\typed_entity\EntityWrapperInterface $typedEntityRepositoryManager
* The Typed Entity repository manager.
*
* @param \Drupal\omnipedia_changes\Service\WikiNodeChangesBuilderInterface $wikiNodeChangesBuilder
* The Omnipedia wiki node changes builder service.
*
* @param \Drupal\omnipedia_changes\Service\WikiNodeChangesInfoInterface $wikiNodeChangesInfo
* The Omnipedia wiki node changes info service.
*
* @param \Drupal\Core\State\StateInterface
* The Drupal state service.
*
* @param \Drupal\Component\Datetime\TimeInterface
* The Drupal time service.
*/
public function __construct(
array $configuration, string $pluginId, array $pluginDefinition,
protected readonly AccountSwitcherInterface $accountSwitcher,
protected readonly EntityTypeManagerInterface $entityTypeManager,
protected readonly LoggerInterface $loggerChannel,
protected readonly RepresentativeRenderUserInterface $representativeRenderUser,
protected readonly EntityWrapperInterface $typedEntityRepositoryManager,
protected readonly WikiNodeChangesBuilderInterface $wikiNodeChangesBuilder,
protected readonly WikiNodeChangesInfoInterface $wikiNodeChangesInfo,
StateInterface $state,
TimeInterface $time,
) {
parent::__construct(
$configuration, $pluginId, $pluginDefinition, $state, $time,
);
}
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration, $pluginId, $pluginDefinition
) {
// This replicates what WarmerPluginBase::create() does so that we don't
// need to call parent::create() in order to use PHP 8 constructor property
// promotion for all our dependencies.
$warmersConfig = $container->get('config.factory')
->get('warmer.settings')
->get('warmers');
$pluginSettings = empty(
$warmersConfig[$pluginId]
) ? [] : $warmersConfig[$pluginId];
$configuration = \array_merge($pluginSettings, $configuration);
return new static(
$configuration, $pluginId, $pluginDefinition,
$container->get('account_switcher'),
$container->get('entity_type.manager'),
$container->get('logger.channel.omnipedia_changes'),
$container->get('omnipedia_user.representative_render_user'),
$container->get('Drupal\typed_entity\RepositoryManager'),
$container->get('omnipedia.wiki_node_changes_builder'),
$container->get('omnipedia.wiki_node_changes_info'),
$container->get('state'),
$container->get('datetime.time'),
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
/** @var array */
$config = parent::defaultConfiguration();
// Significantly reduce the batch size as some diffs can take a while.
$config['batchSize'] = 5;
return $config;
}
/**
* {@inheritdoc}
*/
public function loadMultiple(array $ids = []) {
/** @var array */
$items = [];
foreach ($ids as $id => $cacheId) {
list($nid, $roles) = \explode(':', $id);
/** \Drupal\node\NodeInterface|null */
$node = $this->entityTypeManager->getStorage('node')->load($nid);
// Skip if we couldn't load this node.
if (!\is_object($node)) {
continue;
}
/** @var \Drupal\omnipedia_core\WrappedEntities\NodeWithWikiInfoInterface */
$previousWrappedNode = $this->typedEntityRepositoryManager->wrap(
$node,
)->getPreviousWikiRevision();
// Skip if there's no previous wiki node revision.
if (!\is_object($previousWrappedNode)) {
continue;
}
/** @var \Drupal\node\NodeInterface */
$previousNode = $previousWrappedNode->getEntity();
/** @var \Drupal\user\UserInterface|null */
$renderUser = $this->representativeRenderUser->getUserToRenderAs(
\explode(',', $roles),
function(UserInterface $user) use ($node, $previousNode) {
// Check that the provided user can access both the current and
// previous wiki nodes.
return $node->access('view', $user) && $previousNode->access(
'view', $user
);
}
);
// Skip this item if we couldn't find a user to render it as.
if (!\is_object($renderUser)) {
continue;
}
$items[$id] = [
'node' => $node,
'previousNode' => $previousNode,
'user' => $renderUser,
];
}
return $items;
}
/**
* {@inheritdoc}
*/
public function warmMultiple(array $items = []) {
/** @var integer */
$count = 0;
foreach ($items as $key => $item) {
// Switch over to the provided user account for rendering.
$this->accountSwitcher->switchTo($item['user']);
// Attempt to build the changes render array, which will automatically
// cache it if it isn't already cached, or will return it from cache.
try {
$renderArray = $this->wikiNodeChangesBuilder->build($item['node']);
} catch (PluginException $exception) {
// Log the exception.
//
// @see \watchdog_exception()
// We're replicating what this function does, but using the injected
// logger channel.
$this->loggerChannel->error(
'%type: @message in %function (line %line of %file).',
Error::decodeException($exception)
);
}
// Switch back to the current user.
$this->accountSwitcher->switchBack();
// Increment the counter if the render array isn't empty or null.
if (!empty($renderArray)) {
$count++;
}
}
return $count;
}
/**
* {@inheritdoc}
*/
public function buildIdsBatch($cursor) {
if (!isset($this->cacheIds)) {
$this->cacheIds = [];
/** @var array[] */
$cacheIds = $this->wikiNodeChangesInfo->getAllCacheIds();
// This flattens the array structure.
foreach ($cacheIds as $nid => $nodeCacheIds) {
foreach ($nodeCacheIds as $roles => $cacheId) {
$this->cacheIds[$nid . ':' . $roles] = $cacheId;
}
}
}
/** @var int|false */
$cursorPosition = \is_null($cursor) ? -1 :
// Get the integer offset given the current cursor. Note that we have to
// use \array_values($this->cacheIds) to be able to + 1 increment the
// offset in the \array_slice(), since that array uses string keys.
\array_search($cursor, \array_values($this->cacheIds));
// If \array_search() returned false, bail returning an empty array.
if ($cursorPosition === false) {
return [];
}
return \array_slice(
$this->cacheIds, $cursorPosition + 1, (int) $this->getBatchSize()
);
}
/**
* {@inheritdoc}
*/
public function addMoreConfigurationFormElements(
array $form, SubformStateInterface $formState
) {
// We don't have any form elements to add.
return $form;
}
}