Completed
Push — 7.5 ( 670678...55f04b )
by
unknown
19:38
created

Handler::removeSubtree()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 3
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias;
8
9
use eZ\Publish\Core\Base\Exceptions\BadStateException;
10
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
11
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator;
12
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties;
13
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\UrlAliasForSwappedLocation;
14
use eZ\Publish\SPI\Persistence\Content\Language;
15
use eZ\Publish\SPI\Persistence\Content\UrlAlias;
16
use eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler as UrlAliasHandlerInterface;
17
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
18
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
19
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
20
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
21
use eZ\Publish\Core\Base\Exceptions\ForbiddenException;
22
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
23
use eZ\Publish\SPI\Persistence\TransactionHandler;
24
25
/**
26
 * The UrlAlias Handler provides nice urls management.
27
 *
28
 * Its methods operate on a representation of the url alias data structure held
29
 * inside a storage engine.
30
 */
31
class Handler implements UrlAliasHandlerInterface
32
{
33
    const ROOT_LOCATION_ID = 1;
34
35
    /**
36
     * This is intentionally hardcoded for now as:
37
     * 1. We don't implement this configuration option.
38
     * 2. Such option should not be in this layer, should be handled higher up.
39
     *
40
     * @deprecated
41
     */
42
    const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2;
43
44
    /**
45
     * The maximum level of alias depth.
46
     */
47
    const MAX_URL_ALIAS_DEPTH_LEVEL = 60;
48
49
    /**
50
     * UrlAlias Gateway.
51
     *
52
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway
53
     */
54
    protected $gateway;
55
56
    /**
57
     * Gateway for handling location data.
58
     *
59
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway
60
     */
61
    protected $locationGateway;
62
63
    /**
64
     * UrlAlias Mapper.
65
     *
66
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper
67
     */
68
    protected $mapper;
69
70
    /**
71
     * Caching language handler.
72
     *
73
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler
74
     */
75
    protected $languageHandler;
76
77
    /**
78
     * URL slug converter.
79
     *
80
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter
81
     */
82
    protected $slugConverter;
83
84
    /**
85
     * Gateway for handling content data.
86
     *
87
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway
88
     */
89
    protected $contentGateway;
90
91
    /**
92
     * Language mask generator.
93
     *
94
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator
95
     */
96
    protected $maskGenerator;
97
98
    /** @var \eZ\Publish\SPI\Persistence\TransactionHandler */
99
    private $transactionHandler;
100
101
    /**
102
     * Creates a new UrlAlias Handler.
103
     *
104
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway
105
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper
106
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway
107
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler
108
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter
109
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway
110
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $maskGenerator
111
     * @param \eZ\Publish\SPI\Persistence\TransactionHandler $transactionHandler
112
     */
113
    public function __construct(
114
        Gateway $gateway,
115
        Mapper $mapper,
116
        LocationGateway $locationGateway,
117
        LanguageHandler $languageHandler,
118
        SlugConverter $slugConverter,
119
        ContentGateway $contentGateway,
120
        MaskGenerator $maskGenerator,
121
        TransactionHandler $transactionHandler
122
    ) {
123
        $this->gateway = $gateway;
124
        $this->mapper = $mapper;
125
        $this->locationGateway = $locationGateway;
126
        $this->languageHandler = $languageHandler;
0 ignored issues
show
Documentation Bug introduced by
$languageHandler is of type object<eZ\Publish\SPI\Pe...ntent\Language\Handler>, but the property $languageHandler was declared to be of type object<eZ\Publish\Core\P...anguage\CachingHandler>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
127
        $this->slugConverter = $slugConverter;
128
        $this->contentGateway = $contentGateway;
129
        $this->maskGenerator = $maskGenerator;
130
        $this->transactionHandler = $transactionHandler;
131
    }
132
133
    public function publishUrlAliasForLocation(
134
        $locationId,
135
        $parentLocationId,
136
        $name,
137
        $languageCode,
138
        $alwaysAvailable = false,
139
        $updatePathIdentificationString = false
140
    ) {
141
        $languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id;
142
143
        $this->internalPublishUrlAliasForLocation(
144
            $locationId,
145
            $parentLocationId,
146
            $name,
147
            $languageId,
148
            $alwaysAvailable,
149
            $updatePathIdentificationString
150
        );
151
    }
152
153
    /**
154
     * Internal publish method, accepting language ID instead of language code and optionally
155
     * new alias ID (used when swapping Locations).
156
     *
157
     * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped()
158
     *
159
     * @param int $locationId
160
     * @param int $parentLocationId
161
     * @param string $name
162
     * @param int $languageId
163
     * @param bool $alwaysAvailable
164
     * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string
165
     * @param int $newId
166
     */
167
    private function internalPublishUrlAliasForLocation(
168
        $locationId,
169
        $parentLocationId,
170
        $name,
171
        $languageId,
172
        $alwaysAvailable = false,
173
        $updatePathIdentificationString = false,
174
        $newId = null
175
    ) {
176
        $parentId = $this->getRealAliasId($parentLocationId);
177
        $name = $this->slugConverter->convert($name, 'location_' . $locationId);
178
        $uniqueCounter = $this->slugConverter->getUniqueCounterValue($name, $parentId == 0);
179
        $languageMask = $languageId | (int)$alwaysAvailable;
180
        $action = 'eznode:' . $locationId;
181
        $cleanup = false;
182
183
        // Exiting the loop with break;
184
        while (true) {
185
            $newText = '';
186
            if ($locationId != self::CONTENT_REPOSITORY_ROOT_LOCATION_ID) {
0 ignored issues
show
Deprecated Code introduced by
The constant eZ\Publish\Core\Persiste...SITORY_ROOT_LOCATION_ID has been deprecated.

This class constant has been deprecated.

Loading history...
187
                $newText = $name . ($name == '' || $uniqueCounter > 1 ? $uniqueCounter : '');
188
            }
189
            $newTextMD5 = $this->getHash($newText);
190
191
            // Try to load existing entry
192
            $row = $this->gateway->loadRow($parentId, $newTextMD5);
193
194
            // If nothing was returned insert new entry
195
            if (empty($row)) {
196
                // Check for existing active location entry on this level and reuse it's id
197
                $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId);
198
                if (!empty($existingLocationEntry)) {
199
                    $cleanup = true;
200
                    $newId = $existingLocationEntry['id'];
201
                }
202
203
                try {
204
                    $newId = $this->gateway->insertRow(
205
                        [
206
                            'id' => $newId,
207
                            'link' => $newId,
208
                            'parent' => $parentId,
209
                            'action' => $action,
210
                            'lang_mask' => $languageMask,
211
                            'text' => $newText,
212
                            'text_md5' => $newTextMD5,
213
                        ]
214
                    );
215
                } catch (\RuntimeException $e) {
216
                    while ($e->getPrevious() !== null) {
217
                        $e = $e->getPrevious();
218
                        if ($e instanceof UniqueConstraintViolationException) {
219
                            // Concurrency! someone else inserted the same row that we where going to.
220
                            // let's do another loop pass
221
                            ++$uniqueCounter;
222
                            continue 2;
223
                        }
224
                    }
225
226
                    throw $e;
227
                }
228
229
                break;
230
            }
231
232
            // Row exists, check if it is reusable. There are 3 cases when this is possible:
233
            // 1. NOP entry
234
            // 2. existing location or custom alias entry
235
            // 3. history entry
236
            if (
237
                $row['action'] === Gateway::NOP_ACTION ||
238
                $row['action'] === $action ||
239
                (int)$row['is_original'] === 0
240
            ) {
241
                // Check for existing location entry on this level, if it exists and it's id differs from reusable
242
                // entry id then reusable entry should be updated with the existing location entry id.
243
                // Note: existing location entry may be downgraded and relinked later, depending on its language.
244
                $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId);
245
246
                if (!empty($existingLocationEntry)) {
247
                    // Always cleanup when active autogenerated entry exists on the same level
248
                    $cleanup = true;
249
                    $newId = $existingLocationEntry['id'];
250
                    if ($existingLocationEntry['id'] == $row['id']) {
251
                        // If we are reusing existing location entry merge existing language mask
252
                        $languageMask |= ($row['lang_mask'] & ~1);
253
                    }
254
                } elseif ($newId === null) {
255
                    // Use reused row ID only if publishing normally, else use given $newId
256
                    $newId = $row['id'];
257
                }
258
259
                $this->gateway->updateRow(
260
                    $parentId,
261
                    $newTextMD5,
262
                    [
263
                        'action' => $action,
264
                        // In case when NOP row was reused
265
                        'action_type' => 'eznode',
266
                        'lang_mask' => $languageMask,
267
                        // Updating text ensures that letter case changes are stored
268
                        'text' => $newText,
269
                        // Set "id" and "link" for case when reusable entry is history
270
                        'id' => $newId,
271
                        'link' => $newId,
272
                        // Entry should be active location entry (original and not alias).
273
                        // Note: this takes care of taking over custom alias entry for the location on the same level
274
                        // and with same name and action.
275
                        'alias_redirects' => 1,
276
                        'is_original' => 1,
277
                        'is_alias' => 0,
278
                    ]
279
                );
280
281
                break;
282
            }
283
284
            // If existing row is not reusable, increment $uniqueCounter and try again
285
            ++$uniqueCounter;
286
        }
287
288
        /* @var $newText */
289
        if ($updatePathIdentificationString) {
290
            $this->locationGateway->updatePathIdentificationString(
291
                $locationId,
292
                $parentLocationId,
293
                $this->slugConverter->convert($newText, 'node_' . $locationId, 'urlalias_compat')
0 ignored issues
show
Bug introduced by
The variable $newText does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
294
            );
295
        }
296
297
        /* @var $newId */
298
        /* @var $newTextMD5 */
299
        // Note: cleanup does not touch custom and global entries
300
        if ($cleanup) {
301
            $this->gateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $newTextMD5);
0 ignored issues
show
Bug introduced by
The variable $newTextMD5 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
302
        }
303
    }
304
305
    /**
306
     * Create a user chosen $alias pointing to $locationId in $languageCode.
307
     *
308
     * If $languageCode is null the $alias is created in the system's default
309
     * language. $alwaysAvailable makes the alias available in all languages.
310
     *
311
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
312
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
313
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
314
     *
315
     * @param mixed $locationId
316
     * @param string $path
317
     * @param bool $forwarding
318
     * @param string $languageCode
319
     * @param bool $alwaysAvailable
320
     *
321
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias
322
     */
323
    public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false)
324
    {
325
        return $this->createUrlAlias(
326
            'eznode:' . $locationId,
327
            $path,
328
            $forwarding,
329
            $languageCode,
330
            $alwaysAvailable
331
        );
332
    }
333
334
    /**
335
     * Create a user chosen $alias pointing to a resource in $languageCode.
336
     * This method does not handle location resources - if a user enters a location target
337
     * the createCustomUrlAlias method has to be used.
338
     *
339
     * If $languageCode is null the $alias is created in the system's default
340
     * language. $alwaysAvailable makes the alias available in all languages.
341
     *
342
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given resource
343
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the path is broken
344
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
345
     *
346
     * @param string $resource
347
     * @param string $path
348
     * @param bool $forwarding
349
     * @param string $languageCode
350
     * @param bool $alwaysAvailable
351
     *
352
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias
353
     */
354
    public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false)
355
    {
356
        return $this->createUrlAlias(
357
            $resource,
358
            $path,
359
            $forwarding,
360
            $languageCode,
361
            $alwaysAvailable
362
        );
363
    }
364
365
    /**
366
     * Internal method for creating global or custom URL alias (these are handled in the same way).
367
     *
368
     * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given context
369
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
370
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
371
     *
372
     * @param string $action
373
     * @param string $path
374
     * @param bool $forward
375
     * @param string|null $languageCode
376
     * @param bool $alwaysAvailable
377
     *
378
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias
379
     */
380
    protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable): UrlAlias
381
    {
382
        $pathElements = explode('/', $path);
383
        $topElement = array_pop($pathElements);
384
        $languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id;
385
        $parentId = 0;
386
387
        // Handle all path elements except topmost one
388
        $isPathNew = false;
389
        foreach ($pathElements as $level => $pathElement) {
390
            $pathElement = $this->slugConverter->convert($pathElement, 'noname' . ($level + 1));
391
            $pathElementMD5 = $this->getHash($pathElement);
392
            if (!$isPathNew) {
393
                $row = $this->gateway->loadRow($parentId, $pathElementMD5);
394
                if (empty($row)) {
395
                    $isPathNew = true;
396
                } else {
397
                    $parentId = $row['link'];
398
                }
399
            }
400
401
            if ($isPathNew) {
402
                $parentId = $this->insertNopEntry($parentId, $pathElement, $pathElementMD5);
403
            }
404
        }
405
406
        // Handle topmost path element
407
        $topElement = $this->slugConverter->convert(
408
            $topElement,
409
            'noname' . (count($pathElements) + 1)
410
        );
411
412
        // If last (next to topmost) entry parent is special root entry we handle topmost entry as first level entry
413
        // That is why we need to reset $parentId to 0
414
        if ($parentId !== 0 && $this->gateway->isRootEntry($parentId)) {
415
            $parentId = 0;
416
        }
417
418
        $topElementMD5 = $this->getHash($topElement);
419
        // Set common values for two cases below
420
        $data = [
421
            'action' => $action,
422
            'is_alias' => 1,
423
            'alias_redirects' => $forward ? 1 : 0,
424
            'parent' => $parentId,
425
            'text' => $topElement,
426
            'text_md5' => $topElementMD5,
427
            'is_original' => 1,
428
        ];
429
        // Try to load topmost element
430
        if (!$isPathNew) {
431
            $row = $this->gateway->loadRow($parentId, $topElementMD5);
432
        }
433
434
        // If nothing was returned perform insert
435
        if ($isPathNew || empty($row)) {
436
            $data['lang_mask'] = $languageId | (int)$alwaysAvailable;
437
            $id = $this->gateway->insertRow($data);
438
        } elseif ($row['action'] === Gateway::NOP_ACTION || (int)$row['is_original'] === 0) {
439
            // Row exists, check if it is reusable. There are 2 cases when this is possible:
440
            // 1. NOP entry
441
            // 2. history entry
442
            $data['lang_mask'] = $languageId | (int)$alwaysAvailable;
443
            // If history is reused move link to id
444
            $data['link'] = $id = $row['id'];
445
            $this->gateway->updateRow(
446
                $parentId,
447
                $topElementMD5,
448
                $data
449
            );
450
        } elseif (
451
            $row['action'] === $action &&
452
            (int)$row['is_alias'] === 1 &&
453
            0 === ((int)$row['lang_mask'] & $languageId)
454
        ) {
455
            // add another language to the same custom alias
456
            $data['link'] = $id = $row['id'];
457
            $data['lang_mask'] = $row['lang_mask'] | $languageId | (int)$alwaysAvailable;
458
            $this->gateway->updateRow(
459
                $parentId,
460
                $topElementMD5,
461
                $data
462
            );
463
        } else {
464
            throw new ForbiddenException(
465
                "Path '%path%' already exists for the given context",
466
                ['%path%' => $path]
467
            );
468
        }
469
470
        $data['raw_path_data'] = $this->gateway->loadPathData($id);
471
472
        return $this->mapper->extractUrlAliasFromData($data);
473
    }
474
475
    /**
476
     * Convenience method for inserting nop type row.
477
     *
478
     * @param mixed $parentId
479
     * @param string $text
480
     * @param string $textMD5
481
     *
482
     * @return mixed
483
     */
484
    protected function insertNopEntry($parentId, $text, $textMD5)
485
    {
486
        return $this->gateway->insertRow(
487
            [
488
                'lang_mask' => 1,
489
                'action' => Gateway::NOP_ACTION,
490
                'parent' => $parentId,
491
                'text' => $text,
492
                'text_md5' => $textMD5,
493
            ]
494
        );
495
    }
496
497
    /**
498
     * List of user generated or autogenerated url entries, pointing to $locationId.
499
     *
500
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
501
     *
502
     * @param mixed $locationId
503
     * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated
504
     *
505
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[]
506
     */
507 View Code Duplication
    public function listURLAliasesForLocation($locationId, $custom = false)
508
    {
509
        $data = $this->gateway->loadLocationEntries($locationId, $custom);
510
        foreach ($data as &$entry) {
511
            $entry['raw_path_data'] = $this->gateway->loadPathData($entry['id']);
512
        }
513
514
        return $this->mapper->extractUrlAliasListFromData($data);
515
    }
516
517
    /**
518
     * List global aliases.
519
     *
520
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
521
     *
522
     * @param string|null $languageCode
523
     * @param int $offset
524
     * @param int $limit
525
     *
526
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[]
527
     */
528 View Code Duplication
    public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1)
529
    {
530
        $data = $this->gateway->listGlobalEntries($languageCode, $offset, $limit);
531
        foreach ($data as &$entry) {
532
            $entry['raw_path_data'] = $this->gateway->loadPathData($entry['id']);
533
        }
534
535
        return $this->mapper->extractUrlAliasListFromData($data);
536
    }
537
538
    /**
539
     * Removes url aliases.
540
     *
541
     * Autogenerated aliases are not removed by this method.
542
     *
543
     * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases
544
     *
545
     * @return bool
546
     */
547
    public function removeURLAliases(array $urlAliases)
548
    {
549
        foreach ($urlAliases as $urlAlias) {
550
            if ($urlAlias->isCustom) {
551
                list($parentId, $textMD5) = explode('-', $urlAlias->id);
552
                if (!$this->gateway->removeCustomAlias($parentId, $textMD5)) {
553
                    return false;
554
                }
555
            }
556
        }
557
558
        return true;
559
    }
560
561
    /**
562
     * Looks up a url alias for the given url.
563
     *
564
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
565
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
566
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
567
     *
568
     * @param string $url
569
     *
570
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias
571
     */
572
    public function lookup($url)
573
    {
574
        $urlHashes = [];
575
        foreach (explode('/', $url) as $level => $text) {
576
            $urlHashes[$level] = $this->getHash($text);
577
        }
578
579
        $pathDepth = count($urlHashes);
580
        if ($pathDepth > self::MAX_URL_ALIAS_DEPTH_LEVEL) {
581
            throw new InvalidArgumentException('$urlHashes', 'Exceeded maximum depth level of content url alias.');
582
        }
583
584
        $data = $this->gateway->loadUrlAliasData($urlHashes);
585
        if (empty($data)) {
586
            throw new NotFoundException('URLAlias', $url);
587
        }
588
589
        $hierarchyData = [];
590
        $isPathHistory = false;
591
        for ($level = 0; $level < $pathDepth; ++$level) {
592
            $prefix = $level === $pathDepth - 1 ? '' : 'ezurlalias_ml' . $level . '_';
593
            $isPathHistory = $isPathHistory ?: ($data[$prefix . 'link'] != $data[$prefix . 'id']);
594
            $hierarchyData[$level] = [
595
                'id' => $data[$prefix . 'id'],
596
                'parent' => $data[$prefix . 'parent'],
597
                'action' => $data[$prefix . 'action'],
598
            ];
599
        }
600
601
        $data['is_path_history'] = $isPathHistory;
602
        $data['raw_path_data'] = ($data['action_type'] == 'eznode' && !$data['is_alias'])
603
            ? $this->gateway->loadPathDataByHierarchy($hierarchyData)
604
            : $this->gateway->loadPathData($data['id']);
605
606
        return $this->mapper->extractUrlAliasFromData($data);
607
    }
608
609
    /**
610
     * Loads URL alias by given $id.
611
     *
612
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
613
     * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException
614
     *
615
     * @param string $id
616
     *
617
     * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias
618
     */
619
    public function loadUrlAlias($id)
620
    {
621
        list($parentId, $textMD5) = explode('-', $id);
622
        $data = $this->gateway->loadRow($parentId, $textMD5);
623
624
        if (empty($data)) {
625
            throw new NotFoundException('URLAlias', $id);
626
        }
627
628
        $data['raw_path_data'] = $this->gateway->loadPathData($data['id']);
629
630
        return $this->mapper->extractUrlAliasFromData($data);
631
    }
632
633
    /**
634
     * Notifies the underlying engine that a location has moved.
635
     *
636
     * This method triggers the change of the autogenerated aliases.
637
     *
638
     * @param mixed $locationId
639
     * @param mixed $oldParentId
640
     * @param mixed $newParentId
641
     */
642
    public function locationMoved($locationId, $oldParentId, $newParentId)
643
    {
644
        // @todo optimize: $newLocationAliasId is already available in self::publishUrlAliasForLocation() as $newId
645
        $newParentLocationAliasId = $this->getRealAliasId($newParentId);
646
        $newLocationAlias = $this->gateway->loadAutogeneratedEntry(
647
            'eznode:' . $locationId,
648
            $newParentLocationAliasId
649
        );
650
651
        $oldParentLocationAliasId = $this->getRealAliasId($oldParentId);
652
        $oldLocationAlias = $this->gateway->loadAutogeneratedEntry(
653
            'eznode:' . $locationId,
654
            $oldParentLocationAliasId
655
        );
656
657
        // Historize alias for old location
658
        $this->gateway->historizeId($oldLocationAlias['id'], $newLocationAlias['id']);
659
        // Reparent subtree of old location to new location
660
        $this->gateway->reparent($oldLocationAlias['id'], $newLocationAlias['id']);
661
    }
662
663
    /**
664
     * Notifies the underlying engine that a location was copied.
665
     *
666
     * This method triggers the creation of the autogenerated aliases for the copied locations
667
     *
668
     * @param mixed $locationId
669
     * @param mixed $newLocationId
670
     * @param mixed $newParentId
671
     */
672
    public function locationCopied($locationId, $newLocationId, $newParentId)
673
    {
674
        $newParentAliasId = $this->getRealAliasId($newLocationId);
675
        $oldParentAliasId = $this->getRealAliasId($locationId);
676
677
        $actionMap = $this->getCopiedLocationsMap($locationId, $newLocationId);
678
679
        $this->copySubtree(
680
            $actionMap,
681
            $oldParentAliasId,
682
            $newParentAliasId
683
        );
684
    }
685
686
    /**
687
     * Notify the underlying engine that a Location has been swapped.
688
     *
689
     * This method triggers the change of the autogenerated aliases.
690
     *
691
     * @param int $location1Id
692
     * @param int $location1ParentId
693
     * @param int $location2Id
694
     * @param int $location2ParentId
695
     *
696
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
697
     */
698
    public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId)
699
    {
700
        $location1 = new SwappedLocationProperties($location1Id, $location1ParentId);
701
        $location2 = new SwappedLocationProperties($location2Id, $location2ParentId);
702
703
        $location1->entries = $this->gateway->loadAllLocationEntries($location1Id);
704
        $location2->entries = $this->gateway->loadAllLocationEntries($location2Id);
705
706
        $location1->mainLanguageId = $this->gateway->getLocationContentMainLanguageId($location1Id);
707
        $location2->mainLanguageId = $this->gateway->getLocationContentMainLanguageId($location2Id);
708
709
        // Load autogenerated entries to find alias ID
710
        $location1->autogeneratedId = $this->gateway->loadAutogeneratedEntry("eznode:{$location1Id}")['id'];
711
        $location2->autogeneratedId = $this->gateway->loadAutogeneratedEntry("eznode:{$location2Id}")['id'];
712
713
        $contentInfo1 = $this->contentGateway->loadContentInfoByLocationId($location1Id);
714
        $contentInfo2 = $this->contentGateway->loadContentInfoByLocationId($location2Id);
715
716
        $names1 = $this->getNamesForAllLanguages($contentInfo1);
717
        $names2 = $this->getNamesForAllLanguages($contentInfo2);
718
719
        $location1->isAlwaysAvailable = $this->maskGenerator->isAlwaysAvailable($contentInfo1['language_mask']);
720
        $location2->isAlwaysAvailable = $this->maskGenerator->isAlwaysAvailable($contentInfo2['language_mask']);
721
722
        $languages = $this->languageHandler->loadAll();
723
724
        // Historize everything first to avoid name conflicts in case swapped Locations are siblings
725
        $this->historizeBeforeSwap($location1->entries, $location2->entries);
726
727
        foreach ($languages as $languageCode => $language) {
728
            $location1->name = isset($names1[$languageCode]) ? $names1[$languageCode] : null;
729
            $location2->name = isset($names2[$languageCode]) ? $names2[$languageCode] : null;
730
            $urlAliasesForSwappedLocations = $this->getUrlAliasesForSwappedLocations(
731
                $language,
732
                $location1,
733
                $location2
734
            );
735
            foreach ($urlAliasesForSwappedLocations as $urlAliasForLocation) {
736
                $this->internalPublishUrlAliasForLocation(
737
                    $urlAliasForLocation->id,
738
                    $urlAliasForLocation->parentId,
739
                    $urlAliasForLocation->name,
740
                    $language->id,
741
                    $urlAliasForLocation->isAlwaysAvailable,
742
                    $urlAliasForLocation->isPathIdentificationStringModified,
743
                    $urlAliasForLocation->newId
744
                );
745
            }
746
        }
747
748
        $this->internalPublishCustomUrlAliasForLocation($location1, $contentInfo1['language_mask']);
749
        $this->internalPublishCustomUrlAliasForLocation($location2, $contentInfo2['language_mask']);
750
    }
751
752
    /**
753
     * @param array $contentInfo
754
     *
755
     * @return array
756
     */
757
    private function getNamesForAllLanguages(array $contentInfo)
758
    {
759
        $nameDataArray = $this->contentGateway->loadVersionedNameData([
760
            [
761
                'id' => $contentInfo['id'],
762
                'version' => $contentInfo['current_version'],
763
            ],
764
        ]);
765
766
        $namesForAllLanguages = [];
767
        foreach ($nameDataArray as $nameData) {
768
            $namesForAllLanguages[$nameData['ezcontentobject_name_content_translation']]
769
                = $nameData['ezcontentobject_name_name'];
770
        }
771
772
        return $namesForAllLanguages;
773
    }
774
775
    /**
776
     * Historizes given existing active entries for two swapped Locations.
777
     *
778
     * This should be done before republishing URL aliases, in order to avoid unnecessary
779
     * conflicts when swapped Locations are siblings.
780
     *
781
     * We need to historize everything separately per language (mask), in case the entries
782
     * remain history future publishing reusages need to be able to take them over cleanly.
783
     *
784
     * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped()
785
     *
786
     * @param array $location1Entries
787
     * @param array $location2Entries
788
     */
789
    private function historizeBeforeSwap($location1Entries, $location2Entries)
790
    {
791
        foreach ($location1Entries as $row) {
792
            $this->gateway->historizeBeforeSwap($row['action'], $row['lang_mask']);
793
        }
794
795
        foreach ($location2Entries as $row) {
796
            $this->gateway->historizeBeforeSwap($row['action'], $row['lang_mask']);
797
        }
798
    }
799
800
    /**
801
     * Decides if UrlAlias for $location2 should be published first.
802
     *
803
     * The order in which Locations are published only matters if swapped Locations are siblings and they have the same
804
     * name in a given language. In this case, the UrlAlias for Location which previously had lower number at the end of
805
     * its UrlAlias text (or no number at all) should be published first. This ensures that the number still stays lower
806
     * for this Location after the swap. If it wouldn't stay lower, then swapping Locations in conjunction with swapping
807
     * UrlAliases would effectively cancel each other.
808
     *
809
     * @param array $location1Entries
810
     * @param int $location1ParentId
811
     * @param string $name1
812
     * @param array $location2Entries
813
     * @param int $location2ParentId
814
     * @param string $name2
815
     * @param int $languageId
816
     *
817
     * @return bool
818
     */
819
    private function shouldUrlAliasForSecondLocationBePublishedFirst(
820
        array $location1Entries,
821
        $location1ParentId,
822
        $name1,
823
        array $location2Entries,
824
        $location2ParentId,
825
        $name2,
826
        $languageId
827
    ) {
828
        if ($location1ParentId === $location2ParentId && $name1 === $name2) {
829
            $locationEntry1 = $this->getLocationEntryInLanguage($location1Entries, $languageId);
830
            $locationEntry2 = $this->getLocationEntryInLanguage($location2Entries, $languageId);
831
832
            if ($locationEntry1 === null || $locationEntry2 === null) {
833
                return false;
834
            }
835
836
            if ($locationEntry2['text'] < $locationEntry1['text']) {
837
                return true;
838
            }
839
        }
840
841
        return false;
842
    }
843
844
    /**
845
     * Get in a proper order - to be published - a list of URL aliases for swapped Locations.
846
     *
847
     * @see shouldUrlAliasForSecondLocationBePublishedFirst
848
     *
849
     * @param \eZ\Publish\SPI\Persistence\Content\Language $language
850
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties $location1
851
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties $location2
852
     *
853
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\UrlAliasForSwappedLocation[]
854
     */
855
    private function getUrlAliasesForSwappedLocations(
856
        Language $language,
857
        SwappedLocationProperties $location1,
858
        SwappedLocationProperties $location2
859
    ) {
860
        $isMainLanguage1 = $language->id == $location1->mainLanguageId;
861
        $isMainLanguage2 = $language->id == $location2->mainLanguageId;
862
        $urlAliases = [];
863
        if (isset($location1->name)) {
864
            $urlAliases[] = new UrlAliasForSwappedLocation(
865
                $location1->id,
866
                $location1->parentId,
867
                $location1->name,
868
                $isMainLanguage2 && $location1->isAlwaysAvailable,
869
                $isMainLanguage2,
870
                $location1->autogeneratedId
871
            );
872
        }
873
874
        if (isset($location2->name)) {
875
            $urlAliases[] = new UrlAliasForSwappedLocation(
876
                $location2->id,
877
                $location2->parentId,
878
                $location2->name,
879
                $isMainLanguage1 && $location2->isAlwaysAvailable,
880
                $isMainLanguage1,
881
                $location2->autogeneratedId
882
            );
883
884
            if (isset($location1->name) && $this->shouldUrlAliasForSecondLocationBePublishedFirst(
885
                    $location1->entries,
886
                    $location1->parentId,
887
                    $location1->name,
888
                    $location2->entries,
889
                    $location2->parentId,
890
                    $location2->name,
891
                    $language->id
892
                )) {
893
                $urlAliases = array_reverse($urlAliases);
894
            }
895
        }
896
897
        return $urlAliases;
898
    }
899
900
    /**
901
     * @param array $locationEntries
902
     * @param int $languageId
903
     *
904
     * @return array|null
905
     */
906
    private function getLocationEntryInLanguage(array $locationEntries, $languageId)
907
    {
908
        $entries = array_filter(
909
            $locationEntries,
910
            function (array $row) use ($languageId) {
911
                return (bool) ($row['lang_mask'] & $languageId);
912
            }
913
        );
914
915
        return !empty($entries) ? array_shift($entries) : null;
916
    }
917
918
    /**
919
     * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic.
920
     *
921
     * First level entries must have parent id set to 0 instead of their parent location alias id.
922
     * There are two cases when alias id needs to be corrected:
923
     * 1) location is special location without URL alias (location with id=1 in standard installation)
924
     * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2
925
     *    in standard installation)
926
     *
927
     * @param mixed $locationId
928
     *
929
     * @return mixed
930
     */
931
    protected function getRealAliasId($locationId)
932
    {
933
        // Absolute root location does have a url alias entry so we can skip lookup
934
        if ($locationId == self::ROOT_LOCATION_ID) {
935
            return 0;
936
        }
937
938
        $data = $this->gateway->loadAutogeneratedEntry('eznode:' . $locationId);
939
940
        // Root entries (URL wise) can return 0 as the returned value is used as parent (parent is 0 for root entries)
941
        if (empty($data) || ($data['id'] != 0 && $data['parent'] == 0 && strlen($data['text']) == 0)) {
942
            $id = 0;
943
        } else {
944
            $id = $data['id'];
945
        }
946
947
        return $id;
948
    }
949
950
    /**
951
     * Recursively copies aliases from old parent under new parent.
952
     *
953
     * @param array $actionMap
954
     * @param mixed $oldParentAliasId
955
     * @param mixed $newParentAliasId
956
     */
957
    protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId)
958
    {
959
        $rows = $this->gateway->loadAutogeneratedEntries($oldParentAliasId);
960
        $newIdsMap = [];
961
        foreach ($rows as $row) {
962
            $oldParentAliasId = $row['id'];
963
964
            // Ensure that same action entries remain grouped by the same id
965
            if (!isset($newIdsMap[$oldParentAliasId])) {
966
                $newIdsMap[$oldParentAliasId] = $this->gateway->getNextId();
967
            }
968
969
            $row['action'] = $actionMap[$row['action']];
970
            $row['parent'] = $newParentAliasId;
971
            $row['id'] = $row['link'] = $newIdsMap[$oldParentAliasId];
972
            $this->gateway->insertRow($row);
973
974
            $this->copySubtree(
975
                $actionMap,
976
                $oldParentAliasId,
977
                $row['id']
978
            );
979
        }
980
    }
981
982
    /**
983
     * @param mixed $oldParentId
984
     * @param mixed $newParentId
985
     *
986
     * @return array
987
     */
988
    protected function getCopiedLocationsMap($oldParentId, $newParentId)
989
    {
990
        $originalLocations = $this->locationGateway->getSubtreeContent($oldParentId);
991
        $copiedLocations = $this->locationGateway->getSubtreeContent($newParentId);
992
993
        $map = [];
994
        foreach ($originalLocations as $index => $originalLocation) {
995
            $map['eznode:' . $originalLocation['node_id']] = 'eznode:' . $copiedLocations[$index]['node_id'];
996
        }
997
998
        return $map;
999
    }
1000
1001
    public function locationDeleted($locationId): array
1002
    {
1003
        $action = 'eznode:' . $locationId;
1004
        $entry = $this->gateway->loadAutogeneratedEntry($action);
1005
        $entryId = $entry['id'];
1006
1007
        $this->removeSubtree($entryId, $action, $entry['is_original']);
1008
1009
        // after location-delete process completed
1010
        // check if entry had children; if yes - then restore them as nop-type
1011
        // for historical aliases relates to that entry
1012
        $notDeletedChildrenAliases = $this->gateway->getAllChildrenAliases($entryId);
1013
        if (count($notDeletedChildrenAliases) > 0) {
1014
            $this->insertAliasEntryAsNop($entry);
1015
        }
1016
1017
        return $notDeletedChildrenAliases;
1018
    }
1019
1020
    /**
1021
     * Notifies the underlying engine that Locations Content Translation was removed.
1022
     *
1023
     * @param int[] $locationIds all Locations of the Content that got Translation removed
1024
     * @param string $languageCode language code of the removed Translation
1025
     */
1026
    public function translationRemoved(array $locationIds, $languageCode)
1027
    {
1028
        $languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id;
1029
1030
        $actions = [];
1031
        foreach ($locationIds as $locationId) {
1032
            $actions[] = 'eznode:' . $locationId;
1033
        }
1034
        $this->gateway->bulkRemoveTranslation($languageId, $actions);
1035
    }
1036
1037
    /**
1038
     * Recursively removes aliases by given $id and $action.
1039
     *
1040
     * $original parameter is used to limit removal of moved Location aliases to history entries only.
1041
     *
1042
     * @param mixed $id
1043
     * @param string $action
1044
     * @param mixed $original
1045
     */
1046
    protected function removeSubtree($id, $action, $original)
1047
    {
1048
        // Remove first to avoid unnecessary recursion.
1049
        if ($original) {
1050
            // If entry is original remove all for action (history and custom entries included).
1051
            $this->gateway->remove($action);
1052
        } else {
1053
            // Else entry is history, so remove only for action with the id.
1054
            // This means $id grouped history entries are removed, other history, active autogenerated
1055
            // and custom are left alone.
1056
            $this->gateway->remove($action, $id);
1057
        }
1058
1059
        // Load all autogenerated for parent $id, including history.
1060
        $entries = $this->gateway->loadAutogeneratedEntries($id, true);
1061
1062
        foreach ($entries as $entry) {
1063
            $this->removeSubtree($entry['id'], $entry['action'], $entry['is_original']);
1064
        }
1065
    }
1066
1067
    /**
1068
     * @param string $text
1069
     *
1070
     * @return string
1071
     */
1072
    protected function getHash($text)
1073
    {
1074
        return md5(mb_strtolower($text, 'UTF-8'));
1075
    }
1076
1077
    /**
1078
     * {@inheritdoc}
1079
     */
1080
    public function archiveUrlAliasesForDeletedTranslations($locationId, $parentLocationId, array $languageCodes)
1081
    {
1082
        $parentId = $this->getRealAliasId($parentLocationId);
1083
1084
        $data = $this->gateway->loadLocationEntries($locationId);
1085
        // filter removed Translations
1086
        $removedLanguages = array_diff(
1087
            $this->mapper->extractLanguageCodesFromData($data),
1088
            $languageCodes
1089
        );
1090
1091
        if (empty($removedLanguages)) {
1092
            return;
1093
        }
1094
1095
        // map languageCodes to their IDs
1096
        $languageIds = array_map(
1097
            function ($languageCode) {
1098
                return $this->languageHandler->loadByLanguageCode($languageCode)->id;
1099
            },
1100
            $removedLanguages
1101
        );
1102
1103
        $this->gateway->archiveUrlAliasesForDeletedTranslations($locationId, $parentId, $languageIds);
1104
    }
1105
1106
    /**
1107
     * Remove corrupted URL aliases (global, custom and system).
1108
     *
1109
     * @return int Number of removed URL aliases
1110
     *
1111
     * @throws \Exception
1112
     */
1113
    public function deleteCorruptedUrlAliases()
1114
    {
1115
        $this->transactionHandler->beginTransaction();
1116
        try {
1117
            $totalCount = $this->gateway->deleteUrlAliasesWithoutLocation();
1118
            $totalCount += $this->gateway->deleteUrlAliasesWithoutParent();
1119
            $totalCount += $this->gateway->deleteUrlAliasesWithBrokenLink();
1120
            $totalCount += $this->gateway->deleteUrlNopAliasesWithoutChildren();
1121
1122
            $this->transactionHandler->commit();
1123
1124
            return $totalCount;
1125
        } catch (\Exception $e) {
1126
            $this->transactionHandler->rollback();
1127
            throw $e;
1128
        }
1129
    }
1130
1131
    /**
1132
     * Attempt repairing auto-generated URL aliases for the given Location (including history).
1133
     *
1134
     * Note: it is assumed that at this point original, working, URL Alias for Location is published.
1135
     *
1136
     * @param int $locationId
1137
     *
1138
     * @throws \eZ\Publish\Core\Base\Exceptions\BadStateException
1139
     */
1140
    public function repairBrokenUrlAliasesForLocation(int $locationId)
1141
    {
1142
        try {
1143
            $this->gateway->repairBrokenUrlAliasesForLocation($locationId);
1144
        } catch (\RuntimeException $e) {
1145
            throw new BadStateException('locationId', $e->getMessage(), $e);
1146
        }
1147
    }
1148
1149
    private function insertAliasEntryAsNop(array $aliasEntry): void
1150
    {
1151
        $aliasEntry['action'] = Gateway::NOP_ACTION;
1152
        $aliasEntry['action_type'] = Gateway::NOP;
1153
1154
        $this->gateway->insertRow($aliasEntry);
1155
    }
1156
1157
    /**
1158
     * Internal publish custom aliases method, accepting language mask to set correct language mask on url aliases
1159
     * new alias ID (used when swapping Locations).
1160
     */
1161
    private function internalPublishCustomUrlAliasForLocation(SwappedLocationProperties $location, int $languageMask)
1162
    {
1163
        foreach ($location->entries as $entry) {
1164
            if ((int)$entry['is_alias'] === 0) {
1165
                continue;
1166
            }
1167
1168
            $mask = (int)$entry['lang_mask'] & $languageMask;
1169
1170
            if ($mask <= 1) {
1171
                continue;
1172
            }
1173
1174
            $this->gateway->updateRow(
1175
                (int)$entry['parent'],
1176
                $entry['text_md5'],
1177
                [
1178
                    'id' => (int)$entry['id'],
1179
                    'is_original' => 1,
1180
                    'is_alias' => 1,
1181
                    'lang_mask' => $mask,
1182
                ]
1183
            );
1184
        }
1185
    }
1186
}
1187