Completed
Push — ezp25366-copy_content_relation... ( 7d5327...4b97d3 )
by André
74:42 queued 12:30
created

DoctrineDatabase::copyRelations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 1
f 0
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
3
/**
4
 * File containing the DoctrineDatabase Content Gateway class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Legacy\Content\Gateway;
12
13
use Doctrine\DBAL\Connection;
14
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway;
15
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder;
16
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
17
use eZ\Publish\Core\Persistence\Database\UpdateQuery;
18
use eZ\Publish\Core\Persistence\Database\InsertQuery;
19
use eZ\Publish\Core\Persistence\Database\SelectQuery;
20
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
21
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator as LanguageMaskGenerator;
22
use eZ\Publish\SPI\Persistence\Content;
23
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
24
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
25
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
26
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
27
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
28
use eZ\Publish\SPI\Persistence\Content\Field;
29
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
30
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
31
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
32
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
33
use DOMXPath;
34
use DOMDocument;
35
use PDO;
36
37
/**
38
 * Doctrine database based content gateway.
39
 */
40
class DoctrineDatabase extends Gateway
41
{
42
    /**
43
     * eZ Doctrine database handler.
44
     *
45
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
46
     */
47
    protected $dbHandler;
48
49
    /**
50
     * The native Doctrine connection.
51
     *
52
     * Meant to be used to transition from eZ/Zeta interface to Doctrine.
53
     *
54
     * @var \Doctrine\DBAL\Connection
55
     */
56
    protected $connection;
57
58
    /**
59
     * Query builder.
60
     *
61
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
62
     */
63
    protected $queryBuilder;
64
65
    /**
66
     * Caching language handler.
67
     *
68
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler
69
     */
70
    protected $languageHandler;
71
72
    /**
73
     * Language mask generator.
74
     *
75
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator
76
     */
77
    protected $languageMaskGenerator;
78
79
    /**
80
     * Creates a new gateway based on $db.
81
     *
82
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $db
83
     * @param \Doctrine\DBAL\Connection $connection
84
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder $queryBuilder
85
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler
86
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $languageMaskGenerator
87
     */
88
    public function __construct(
89
        DatabaseHandler $db,
90
        Connection $connection,
91
        QueryBuilder $queryBuilder,
92
        LanguageHandler $languageHandler,
93
        LanguageMaskGenerator $languageMaskGenerator
94
    ) {
95
        $this->dbHandler = $db;
96
        $this->connection = $connection;
97
        $this->queryBuilder = $queryBuilder;
98
        $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...
99
        $this->languageMaskGenerator = $languageMaskGenerator;
100
    }
101
102
    /**
103
     * Get context definition for external storage layers.
104
     *
105
     * @return array
106
     */
107
    public function getContext()
108
    {
109
        return array(
110
            'identifier' => 'LegacyStorage',
111
            'connection' => $this->dbHandler,
112
        );
113
    }
114
115
    /**
116
     * Inserts a new content object.
117
     *
118
     * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct
119
     * @param mixed $currentVersionNo
120
     *
121
     * @return int ID
122
     */
123
    public function insertContentObject(CreateStruct $struct, $currentVersionNo = 1)
124
    {
125
        $initialLanguageCode = $this->languageHandler->load($struct->initialLanguageId)->languageCode;
126
        if (isset($struct->name[$initialLanguageCode])) {
127
            $name = $struct->name[$initialLanguageCode];
128
        } else {
129
            $name = '';
130
        }
131
132
        $q = $this->dbHandler->createInsertQuery();
133
        $q->insertInto(
134
            $this->dbHandler->quoteTable('ezcontentobject')
135
        )->set(
136
            $this->dbHandler->quoteColumn('id'),
137
            $this->dbHandler->getAutoIncrementValue('ezcontentobject', 'id')
138
        )->set(
139
            $this->dbHandler->quoteColumn('current_version'),
140
            $q->bindValue($currentVersionNo, null, \PDO::PARAM_INT)
141
        )->set(
142
            $this->dbHandler->quoteColumn('name'),
143
            $q->bindValue($name, null, \PDO::PARAM_STR)
144
        )->set(
145
            $this->dbHandler->quoteColumn('contentclass_id'),
146
            $q->bindValue($struct->typeId, null, \PDO::PARAM_INT)
147
        )->set(
148
            $this->dbHandler->quoteColumn('section_id'),
149
            $q->bindValue($struct->sectionId, null, \PDO::PARAM_INT)
150
        )->set(
151
            $this->dbHandler->quoteColumn('owner_id'),
152
            $q->bindValue($struct->ownerId, null, \PDO::PARAM_INT)
153
        )->set(
154
            $this->dbHandler->quoteColumn('initial_language_id'),
155
            $q->bindValue($struct->initialLanguageId, null, \PDO::PARAM_INT)
156
        )->set(
157
            $this->dbHandler->quoteColumn('remote_id'),
158
            $q->bindValue($struct->remoteId, null, \PDO::PARAM_STR)
159
        )->set(
160
            $this->dbHandler->quoteColumn('modified'),
161
            $q->bindValue(0, null, \PDO::PARAM_INT)
162
        )->set(
163
            $this->dbHandler->quoteColumn('published'),
164
            $q->bindValue(0, null, \PDO::PARAM_INT)
165
        )->set(
166
            $this->dbHandler->quoteColumn('status'),
167
            $q->bindValue(ContentInfo::STATUS_DRAFT, null, \PDO::PARAM_INT)
168
        )->set(
169
            $this->dbHandler->quoteColumn('language_mask'),
170
            $q->bindValue(
171
                $this->generateLanguageMask(
172
                    $struct->fields,
173
                    $this->languageHandler->load($struct->initialLanguageId)->languageCode,
174
                    $struct->alwaysAvailable
175
                ),
176
                null,
177
                \PDO::PARAM_INT
178
            )
179
        );
180
181
        $q->prepare()->execute();
182
183
        return $this->dbHandler->lastInsertId(
184
            $this->dbHandler->getSequenceName('ezcontentobject', 'id')
185
        );
186
    }
187
188
    /**
189
     * Generates a language mask for $version.
190
     *
191
     * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields
192
     * @param string $initialLanguageCode
193
     * @param bool $alwaysAvailable
194
     *
195
     * @return int
196
     */
197
    protected function generateLanguageMask(array $fields, $initialLanguageCode, $alwaysAvailable)
198
    {
199
        $languages = array($initialLanguageCode => true);
200
        foreach ($fields as $field) {
201
            if (isset($languages[$field->languageCode])) {
202
                continue;
203
            }
204
205
            $languages[$field->languageCode] = true;
206
        }
207
208
        if ($alwaysAvailable) {
209
            $languages['always-available'] = true;
210
        }
211
212
        return $this->languageMaskGenerator->generateLanguageMask($languages);
213
    }
214
215
    /**
216
     * Inserts a new version.
217
     *
218
     * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo
219
     * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields
220
     *
221
     * @return int ID
222
     */
223
    public function insertVersion(VersionInfo $versionInfo, array $fields)
224
    {
225
        /** @var $q \eZ\Publish\Core\Persistence\Database\InsertQuery */
226
        $q = $this->dbHandler->createInsertQuery();
227
        $q->insertInto(
228
            $this->dbHandler->quoteTable('ezcontentobject_version')
229
        )->set(
230
            $this->dbHandler->quoteColumn('id'),
231
            $this->dbHandler->getAutoIncrementValue('ezcontentobject_version', 'id')
232
        )->set(
233
            $this->dbHandler->quoteColumn('version'),
234
            $q->bindValue($versionInfo->versionNo, null, \PDO::PARAM_INT)
235
        )->set(
236
            $this->dbHandler->quoteColumn('modified'),
237
            $q->bindValue($versionInfo->modificationDate, null, \PDO::PARAM_INT)
238
        )->set(
239
            $this->dbHandler->quoteColumn('creator_id'),
240
            $q->bindValue($versionInfo->creatorId, null, \PDO::PARAM_INT)
241
        )->set(
242
            $this->dbHandler->quoteColumn('created'),
243
            $q->bindValue($versionInfo->creationDate, null, \PDO::PARAM_INT)
244
        )->set(
245
            $this->dbHandler->quoteColumn('status'),
246
            $q->bindValue($versionInfo->status, null, \PDO::PARAM_INT)
247
        )->set(
248
            $this->dbHandler->quoteColumn('initial_language_id'),
249
            $q->bindValue(
250
                $this->languageHandler->loadByLanguageCode($versionInfo->initialLanguageCode)->id,
251
                null,
252
                \PDO::PARAM_INT
253
            )
254
        )->set(
255
            $this->dbHandler->quoteColumn('contentobject_id'),
256
            $q->bindValue($versionInfo->contentInfo->id, null, \PDO::PARAM_INT)
257
        )->set(
258
            // As described in field mapping document
259
            $this->dbHandler->quoteColumn('workflow_event_pos'),
260
            $q->bindValue(0, null, \PDO::PARAM_INT)
261
        )->set(
262
            $this->dbHandler->quoteColumn('language_mask'),
263
            $q->bindValue(
264
                $this->generateLanguageMask(
265
                    $fields,
266
                    $versionInfo->initialLanguageCode,
267
                    $versionInfo->contentInfo->alwaysAvailable
268
                ),
269
                null,
270
                \PDO::PARAM_INT
271
            )
272
        );
273
274
        $q->prepare()->execute();
275
276
        return $this->dbHandler->lastInsertId(
277
            $this->dbHandler->getSequenceName('ezcontentobject_version', 'id')
278
        );
279
    }
280
281
    /**
282
     * Updates an existing content identified by $contentId in respect to $struct.
283
     *
284
     * @param int $contentId
285
     * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $struct
286
     * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $prePublishVersionInfo Provided on publish
287
     */
288
    public function updateContent($contentId, MetadataUpdateStruct $struct, VersionInfo $prePublishVersionInfo = null)
289
    {
290
        $q = $this->dbHandler->createUpdateQuery();
291
        $q->update($this->dbHandler->quoteTable('ezcontentobject'));
292
293
        if (isset($struct->name)) {
294
            $q->set(
295
                $this->dbHandler->quoteColumn('name'),
296
                $q->bindValue($struct->name, null, \PDO::PARAM_STR)
297
            );
298
        }
299
        if (isset($struct->mainLanguageId)) {
300
            $q->set(
301
                $this->dbHandler->quoteColumn('initial_language_id'),
302
                $q->bindValue($struct->mainLanguageId, null, \PDO::PARAM_INT)
303
            );
304
        }
305
        if (isset($struct->modificationDate)) {
306
            $q->set(
307
                $this->dbHandler->quoteColumn('modified'),
308
                $q->bindValue($struct->modificationDate, null, \PDO::PARAM_INT)
309
            );
310
        }
311
        if (isset($struct->ownerId)) {
312
            $q->set(
313
                $this->dbHandler->quoteColumn('owner_id'),
314
                $q->bindValue($struct->ownerId, null, \PDO::PARAM_INT)
315
            );
316
        }
317
        if (isset($struct->publicationDate)) {
318
            $q->set(
319
                $this->dbHandler->quoteColumn('published'),
320
                $q->bindValue($struct->publicationDate, null, \PDO::PARAM_INT)
321
            );
322
        }
323
        if (isset($struct->remoteId)) {
324
            $q->set(
325
                $this->dbHandler->quoteColumn('remote_id'),
326
                $q->bindValue($struct->remoteId, null, \PDO::PARAM_STR)
327
            );
328
        }
329
        if ($prePublishVersionInfo !== null) {
330
            $mask = 0;
331
332
            if (isset($struct->alwaysAvailable)) {
333
                $mask |= $struct->alwaysAvailable ? 1 : 0;
334
            } else {
335
                $mask |= $prePublishVersionInfo->contentInfo->alwaysAvailable ? 1 : 0;
336
            }
337
338
            foreach ($prePublishVersionInfo->languageIds as $languageId) {
339
                $mask |= $languageId;
340
            }
341
342
            $q->set(
343
                $this->dbHandler->quoteColumn('language_mask'),
344
                $q->bindValue($mask, null, \PDO::PARAM_INT)
345
            );
346
        }
347
        $q->where(
348
            $q->expr->eq(
349
                $this->dbHandler->quoteColumn('id'),
350
                $q->bindValue($contentId, null, \PDO::PARAM_INT)
351
            )
352
        );
353
        $q->prepare()->execute();
354
355
        // Handle alwaysAvailable flag update separately as it's a more complex task and has impact on several tables
356
        if (isset($struct->alwaysAvailable) || isset($struct->mainLanguageId)) {
357
            $this->updateAlwaysAvailableFlag($contentId, $struct->alwaysAvailable);
358
        }
359
    }
360
361
    /**
362
     * Updates version $versionNo for content identified by $contentId, in respect to $struct.
363
     *
364
     * @param int $contentId
365
     * @param int $versionNo
366
     * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $struct
367
     */
368
    public function updateVersion($contentId, $versionNo, UpdateStruct $struct)
369
    {
370
        $q = $this->dbHandler->createUpdateQuery();
371
        $q->update(
372
            $this->dbHandler->quoteTable('ezcontentobject_version')
373
        )->set(
374
            $this->dbHandler->quoteColumn('creator_id'),
375
            $q->bindValue($struct->creatorId, null, \PDO::PARAM_INT)
376
        )->set(
377
            $this->dbHandler->quoteColumn('modified'),
378
            $q->bindValue($struct->modificationDate, null, \PDO::PARAM_INT)
379
        )->set(
380
            $this->dbHandler->quoteColumn('initial_language_id'),
381
            $q->bindValue($struct->initialLanguageId, null, \PDO::PARAM_INT)
382
        )->set(
383
            $this->dbHandler->quoteColumn('language_mask'),
384
            $q->expr->bitOr(
385
                $this->dbHandler->quoteColumn('language_mask'),
386
                $q->bindValue(
387
                    $this->generateLanguageMask(
388
                        $struct->fields,
389
                        $this->languageHandler->load($struct->initialLanguageId)->languageCode,
390
                        false
391
                    ),
392
                    null,
393
                    \PDO::PARAM_INT
394
                )
395
            )
396
        )->where(
397
            $q->expr->lAnd(
398
                $q->expr->eq(
399
                    $this->dbHandler->quoteColumn('contentobject_id'),
400
                    $q->bindValue($contentId, null, \PDO::PARAM_INT)
401
                ),
402
                $q->expr->eq(
403
                    $this->dbHandler->quoteColumn('version'),
404
                    $q->bindValue($versionNo, null, \PDO::PARAM_INT)
405
                )
406
            )
407
        );
408
        $q->prepare()->execute();
409
    }
410
411
    /**
412
     * Updates "always available" flag for Content identified by $contentId, in respect to
413
     * Content's current main language and optionally new $alwaysAvailable state.
414
     *
415
     * @param int $contentId
416
     * @param bool|null $alwaysAvailable New "always available" value or null if not defined
417
     */
418
    public function updateAlwaysAvailableFlag($contentId, $alwaysAvailable = null)
419
    {
420
        // We will need to know some info on the current language mask to update the flag
421
        // everywhere needed
422
        $contentInfoRow = $this->loadContentInfo($contentId);
423
        if (!isset($alwaysAvailable)) {
424
            $alwaysAvailable = (bool)$contentInfoRow['language_mask'] & 1;
425
        }
426
427
        /** @var $q \eZ\Publish\Core\Persistence\Database\UpdateQuery */
428
        $q = $this->dbHandler->createUpdateQuery();
429
        $q
430
            ->update($this->dbHandler->quoteTable('ezcontentobject'))
431
            ->set(
432
                $this->dbHandler->quoteColumn('language_mask'),
433
                $alwaysAvailable ?
434
                    $q->expr->bitOr($this->dbHandler->quoteColumn('language_mask'), 1) :
435
                    $q->expr->bitAnd($this->dbHandler->quoteColumn('language_mask'), -2)
436
            )
437
            ->where(
438
                $q->expr->eq(
439
                    $this->dbHandler->quoteColumn('id'),
440
                    $q->bindValue($contentId, null, \PDO::PARAM_INT)
441
                )
442
            );
443
        $q->prepare()->execute();
444
445
        // Now we need to update ezcontentobject_name
446
        /** @var $qName \eZ\Publish\Core\Persistence\Database\UpdateQuery */
447
        $qName = $this->dbHandler->createUpdateQuery();
448
        $qName
449
            ->update($this->dbHandler->quoteTable('ezcontentobject_name'))
450
            ->set(
451
                $this->dbHandler->quoteColumn('language_id'),
452
                $alwaysAvailable ?
453
                    $qName->expr->bitOr($this->dbHandler->quoteColumn('language_id'), 1) :
454
                    $qName->expr->bitAnd($this->dbHandler->quoteColumn('language_id'), -2)
455
            )
456
            ->where(
457
                $qName->expr->lAnd(
458
                    $qName->expr->eq(
459
                        $this->dbHandler->quoteColumn('contentobject_id'),
460
                        $qName->bindValue($contentId, null, \PDO::PARAM_INT)
461
                    ),
462
                    $qName->expr->eq(
463
                        $this->dbHandler->quoteColumn('content_version'),
464
                        $qName->bindValue(
465
                            $contentInfoRow['current_version'],
466
                            null,
467
                            \PDO::PARAM_INT
468
                        )
469
                    )
470
                )
471
            );
472
        $qName->prepare()->execute();
473
474
        // Now update ezcontentobject_attribute for current version
475
        // Create update query that will be reused
476
        /** @var $qAttr \eZ\Publish\Core\Persistence\Database\UpdateQuery */
477
        $qAttr = $this->dbHandler->createUpdateQuery();
478
        $qAttr
479
            ->update($this->dbHandler->quoteTable('ezcontentobject_attribute'))
480
            ->where(
481
                $qAttr->expr->lAnd(
482
                    $qAttr->expr->eq(
483
                        $this->dbHandler->quoteColumn('contentobject_id'),
484
                        $qAttr->bindValue($contentId, null, \PDO::PARAM_INT)
485
                    ),
486
                    $qAttr->expr->eq(
487
                        $this->dbHandler->quoteColumn('version'),
488
                        $qAttr->bindValue(
489
                            $contentInfoRow['current_version'],
490
                            null,
491
                            \PDO::PARAM_INT
492
                        )
493
                    )
494
                )
495
            );
496
497
        // If there is only a single language, update all fields and return
498
        if (!$this->languageMaskGenerator->isLanguageMaskComposite($contentInfoRow['language_mask'])) {
499
            $qAttr->set(
500
                $this->dbHandler->quoteColumn('language_id'),
501
                $alwaysAvailable ?
502
                    $qAttr->expr->bitOr($this->dbHandler->quoteColumn('language_id'), 1) :
503
                    $qAttr->expr->bitAnd($this->dbHandler->quoteColumn('language_id'), -2)
504
            );
505
            $qAttr->prepare()->execute();
506
507
            return;
508
        }
509
510
        // Otherwise:
511
        // 1. Remove always available flag on all fields
512
        $qAttr->set(
513
            $this->dbHandler->quoteColumn('language_id'),
514
            $qAttr->expr->bitAnd($this->dbHandler->quoteColumn('language_id'), -2)
515
        );
516
        $qAttr->prepare()->execute();
517
518
        // 2. If Content is always available set the flag only on fields in main language
519
        if ($alwaysAvailable) {
520
            $qAttr->set(
521
                $this->dbHandler->quoteColumn('language_id'),
522
                $qAttr->expr->bitOr($this->dbHandler->quoteColumn('language_id'), 1)
523
            );
524
            $qAttr->where(
525
                $qAttr->expr->gt(
526
                    $qAttr->expr->bitAnd(
527
                        $this->dbHandler->quoteColumn('language_id'),
528
                        $qAttr->bindValue($contentInfoRow['initial_language_id'], null, PDO::PARAM_INT)
529
                    ),
530
                    $qAttr->bindValue(0, null, PDO::PARAM_INT)
531
                )
532
            );
533
            $qAttr->prepare()->execute();
534
        }
535
    }
536
537
    /**
538
     * Sets the status of the version identified by $contentId and $version to $status.
539
     *
540
     * The $status can be one of STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED
541
     *
542
     * @param int $contentId
543
     * @param int $version
544
     * @param int $status
545
     *
546
     * @return bool
547
     */
548
    public function setStatus($contentId, $version, $status)
549
    {
550
        $q = $this->dbHandler->createUpdateQuery();
551
        $q->update(
552
            $this->dbHandler->quoteTable('ezcontentobject_version')
553
        )->set(
554
            $this->dbHandler->quoteColumn('status'),
555
            $q->bindValue($status, null, \PDO::PARAM_INT)
556
        )->set(
557
            $this->dbHandler->quoteColumn('modified'),
558
            $q->bindValue(time(), null, \PDO::PARAM_INT)
559
        )->where(
560
            $q->expr->lAnd(
561
                $q->expr->eq(
562
                    $this->dbHandler->quoteColumn('contentobject_id'),
563
                    $q->bindValue($contentId, null, \PDO::PARAM_INT)
564
                ),
565
                $q->expr->eq(
566
                    $this->dbHandler->quoteColumn('version'),
567
                    $q->bindValue($version, null, \PDO::PARAM_INT)
568
                )
569
            )
570
        );
571
        $statement = $q->prepare();
572
        $statement->execute();
573
574
        if ((bool)$statement->rowCount() === false) {
575
            return false;
576
        }
577
578
        if ($status !== APIVersionInfo::STATUS_PUBLISHED) {
579
            return true;
580
        }
581
582
        // If the version's status is PUBLISHED, we set the content to published status as well
583
        $q = $this->dbHandler->createUpdateQuery();
584
        $q->update(
585
            $this->dbHandler->quoteTable('ezcontentobject')
586
        )->set(
587
            $this->dbHandler->quoteColumn('status'),
588
            $q->bindValue(ContentInfo::STATUS_PUBLISHED, null, \PDO::PARAM_INT)
589
        )->set(
590
            $this->dbHandler->quoteColumn('current_version'),
591
            $q->bindValue($version, null, \PDO::PARAM_INT)
592
        )->where(
593
            $q->expr->eq(
594
                $this->dbHandler->quoteColumn('id'),
595
                $q->bindValue($contentId, null, \PDO::PARAM_INT)
596
            )
597
        );
598
        $statement = $q->prepare();
599
        $statement->execute();
600
601
        return (bool)$statement->rowCount();
602
    }
603
604
    /**
605
     * Inserts a new field.
606
     *
607
     * Only used when a new field is created (i.e. a new object or a field in a
608
     * new language!). After that, field IDs need to stay the same, only the
609
     * version number changes.
610
     *
611
     * @param \eZ\Publish\SPI\Persistence\Content $content
612
     * @param \eZ\Publish\SPI\Persistence\Content\Field $field
613
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
614
     *
615
     * @return int ID
616
     */
617
    public function insertNewField(Content $content, Field $field, StorageFieldValue $value)
618
    {
619
        $q = $this->dbHandler->createInsertQuery();
620
621
        $this->setInsertFieldValues($q, $content, $field, $value);
622
623
        // Insert with auto increment ID
624
        $q->set(
625
            $this->dbHandler->quoteColumn('id'),
626
            $this->dbHandler->getAutoIncrementValue('ezcontentobject_attribute', 'id')
627
        );
628
629
        $q->prepare()->execute();
630
631
        return $this->dbHandler->lastInsertId(
632
            $this->dbHandler->getSequenceName('ezcontentobject_attribute', 'id')
633
        );
634
    }
635
636
    /**
637
     * Inserts an existing field.
638
     *
639
     * Used to insert a field with an exsting ID but a new version number.
640
     *
641
     * @param Content $content
642
     * @param Field $field
643
     * @param StorageFieldValue $value
644
     */
645
    public function insertExistingField(Content $content, Field $field, StorageFieldValue $value)
646
    {
647
        $q = $this->dbHandler->createInsertQuery();
648
649
        $this->setInsertFieldValues($q, $content, $field, $value);
650
651
        $q->set(
652
            $this->dbHandler->quoteColumn('id'),
653
            $q->bindValue($field->id, null, \PDO::PARAM_INT)
654
        );
655
656
        $q->prepare()->execute();
657
    }
658
659
    /**
660
     * Sets field (ezcontentobject_attribute) values to the given query.
661
     *
662
     * @param \eZ\Publish\Core\Persistence\Database\InsertQuery $q
663
     * @param Content $content
664
     * @param Field $field
665
     * @param StorageFieldValue $value
666
     */
667
    protected function setInsertFieldValues(InsertQuery $q, Content $content, Field $field, StorageFieldValue $value)
668
    {
669
        $q->insertInto(
670
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
671
        )->set(
672
            $this->dbHandler->quoteColumn('contentobject_id'),
673
            $q->bindValue($content->versionInfo->contentInfo->id, null, \PDO::PARAM_INT)
674
        )->set(
675
            $this->dbHandler->quoteColumn('contentclassattribute_id'),
676
            $q->bindValue($field->fieldDefinitionId, null, \PDO::PARAM_INT)
677
        )->set(
678
            $this->dbHandler->quoteColumn('data_type_string'),
679
            $q->bindValue($field->type)
680
        )->set(
681
            $this->dbHandler->quoteColumn('language_code'),
682
            $q->bindValue($field->languageCode)
683
        )->set(
684
            $this->dbHandler->quoteColumn('version'),
685
            $q->bindValue($field->versionNo, null, \PDO::PARAM_INT)
686
        )->set(
687
            $this->dbHandler->quoteColumn('data_float'),
688
            $q->bindValue($value->dataFloat)
689
        )->set(
690
            $this->dbHandler->quoteColumn('data_int'),
691
            $q->bindValue($value->dataInt, null, \PDO::PARAM_INT)
692
        )->set(
693
            $this->dbHandler->quoteColumn('data_text'),
694
            $q->bindValue($value->dataText)
695
        )->set(
696
            $this->dbHandler->quoteColumn('sort_key_int'),
697
            $q->bindValue($value->sortKeyInt, null, \PDO::PARAM_INT)
698
        )->set(
699
            $this->dbHandler->quoteColumn('sort_key_string'),
700
            $q->bindValue($value->sortKeyString)
701
        )->set(
702
            $this->dbHandler->quoteColumn('language_id'),
703
            $q->bindValue(
704
                $this->languageMaskGenerator->generateLanguageIndicator(
705
                    $field->languageCode,
706
                    $this->isLanguageAlwaysAvailable($content, $field->languageCode)
707
                ),
708
                null,
709
                \PDO::PARAM_INT
710
            )
711
        );
712
    }
713
714
    /**
715
     * Checks if $languageCode is always available in $content.
716
     *
717
     * @param \eZ\Publish\SPI\Persistence\Content $content
718
     * @param string $languageCode
719
     *
720
     * @return bool
721
     */
722
    protected function isLanguageAlwaysAvailable(Content $content, $languageCode)
723
    {
724
        return (
725
            $content->versionInfo->contentInfo->alwaysAvailable &&
726
            $content->versionInfo->contentInfo->mainLanguageCode === $languageCode
727
        );
728
    }
729
730
    /**
731
     * Updates an existing field.
732
     *
733
     * @param Field $field
734
     * @param StorageFieldValue $value
735
     */
736
    public function updateField(Field $field, StorageFieldValue $value)
737
    {
738
        // Note, no need to care for language_id here, since Content->$alwaysAvailable
739
        // cannot change on update
740
        $q = $this->dbHandler->createUpdateQuery();
741
        $this->setFieldUpdateValues($q, $value);
742
        $q->where(
743
            $q->expr->lAnd(
744
                $q->expr->eq(
745
                    $this->dbHandler->quoteColumn('id'),
746
                    $q->bindValue($field->id, null, \PDO::PARAM_INT)
747
                ),
748
                $q->expr->eq(
749
                    $this->dbHandler->quoteColumn('version'),
750
                    $q->bindValue($field->versionNo, null, \PDO::PARAM_INT)
751
                )
752
            )
753
        );
754
        $q->prepare()->execute();
755
    }
756
757
    /**
758
     * Sets update fields for $value on $q.
759
     *
760
     * @param \eZ\Publish\Core\Persistence\Database\UpdateQuery $q
761
     * @param StorageFieldValue $value
762
     */
763
    protected function setFieldUpdateValues(UpdateQuery $q, StorageFieldValue $value)
764
    {
765
        $q->update(
766
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
767
        )->set(
768
            $this->dbHandler->quoteColumn('data_float'),
769
            $q->bindValue($value->dataFloat)
770
        )->set(
771
            $this->dbHandler->quoteColumn('data_int'),
772
            $q->bindValue($value->dataInt, null, \PDO::PARAM_INT)
773
        )->set(
774
            $this->dbHandler->quoteColumn('data_text'),
775
            $q->bindValue($value->dataText)
776
        )->set(
777
            $this->dbHandler->quoteColumn('sort_key_int'),
778
            $q->bindValue($value->sortKeyInt, null, \PDO::PARAM_INT)
779
        )->set(
780
            $this->dbHandler->quoteColumn('sort_key_string'),
781
            $q->bindValue($value->sortKeyString)
782
        );
783
    }
784
785
    /**
786
     * Updates an existing, non-translatable field.
787
     *
788
     * @param \eZ\Publish\SPI\Persistence\Content\Field $field
789
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value
790
     * @param int $contentId
791
     */
792
    public function updateNonTranslatableField(
793
        Field $field,
794
        StorageFieldValue $value,
795
        $contentId
796
    ) {
797
        // Note, no need to care for language_id here, since Content->$alwaysAvailable
798
        // cannot change on update
799
        $q = $this->dbHandler->createUpdateQuery();
800
        $this->setFieldUpdateValues($q, $value);
801
        $q->where(
802
            $q->expr->lAnd(
803
                $q->expr->eq(
804
                    $this->dbHandler->quoteColumn('contentclassattribute_id'),
805
                    $q->bindValue($field->fieldDefinitionId, null, \PDO::PARAM_INT)
806
                ),
807
                $q->expr->eq(
808
                    $this->dbHandler->quoteColumn('contentobject_id'),
809
                    $q->bindValue($contentId, null, \PDO::PARAM_INT)
810
                ),
811
                $q->expr->eq(
812
                    $this->dbHandler->quoteColumn('version'),
813
                    $q->bindValue($field->versionNo, null, \PDO::PARAM_INT)
814
                )
815
            )
816
        );
817
        $q->prepare()->execute();
818
    }
819
820
    /**
821
     * Loads data for a content object.
822
     *
823
     * Returns an array with the relevant data.
824
     *
825
     * @param mixed $contentId
826
     * @param mixed $version
827
     * @param string[] $translations
828
     *
829
     * @return array
830
     */
831
    public function load($contentId, $version, array $translations = null)
832
    {
833
        $query = $this->queryBuilder->createFindQuery($translations);
834
        $query->where(
835
            $query->expr->lAnd(
836
                $query->expr->eq(
837
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
838
                    $query->bindValue($contentId)
839
                ),
840
                $query->expr->eq(
841
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_version'),
842
                    $query->bindValue($version)
843
                )
844
            )
845
        );
846
        $statement = $query->prepare();
847
        $statement->execute();
848
849
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
850
    }
851
852
    /**
853
     * @see loadContentInfo(), loadContentInfoByRemoteId()
854
     *
855
     * @param string $column
856
     * @param mixed $id
857
     *
858
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
859
     *
860
     * @return array
861
     */
862
    private function internalLoadContentInfo($column, $id)
863
    {
864
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
865
        $query = $this->dbHandler->createSelectQuery();
866
        $query->select(
867
            'ezcontentobject.*',
868
            $this->dbHandler->aliasedColumn($query, 'main_node_id', 'ezcontentobject_tree')
869
        )->from(
870
            $this->dbHandler->quoteTable('ezcontentobject')
871
        )->leftJoin(
872
            $this->dbHandler->quoteTable('ezcontentobject_tree'),
873
            $query->expr->lAnd(
874
                $query->expr->eq(
875
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_tree'),
876
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject')
877
                ),
878
                $query->expr->eq(
879
                    $this->dbHandler->quoteColumn('main_node_id', 'ezcontentobject_tree'),
880
                    $this->dbHandler->quoteColumn('node_id', 'ezcontentobject_tree')
881
                )
882
            )
883
        )->where(
884
            $query->expr->eq(
885
                $this->dbHandler->quoteColumn($column, 'ezcontentobject'),
886
                $query->bindValue($id, null, $column === 'id' ? PDO::PARAM_INT : PDO::PARAM_STR)
887
            )
888
        );
889
        $statement = $query->prepare();
890
        $statement->execute();
891
        $row = $statement->fetch(PDO::FETCH_ASSOC);
892
893
        if (empty($row)) {
894
            throw new NotFound('content', "$column: $id");
895
        }
896
897
        return $row;
898
    }
899
    /**
900
     * Loads info for content identified by $contentId.
901
     * Will basically return a hash containing all field values for ezcontentobject table plus some additional keys:
902
     *  - always_available => Boolean indicating if content's language mask contains alwaysAvailable bit field
903
     *  - main_language_code => Language code for main (initial) language. E.g. "eng-GB".
904
     *
905
     * @param int $contentId
906
     *
907
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
908
     *
909
     * @return array
910
     */
911
    public function loadContentInfo($contentId)
912
    {
913
        return $this->internalLoadContentInfo('id', $contentId);
914
    }
915
916
    /**
917
     * Loads info for a content object identified by its remote ID.
918
     *
919
     * Returns an array with the relevant data.
920
     *
921
     * @param mixed $remoteId
922
     *
923
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
924
     *
925
     * @return array
926
     */
927
    public function loadContentInfoByRemoteId($remoteId)
928
    {
929
        return $this->internalLoadContentInfo('remote_id', $remoteId);
930
    }
931
932
    /**
933
     * Loads version info for content identified by $contentId and $versionNo.
934
     * Will basically return a hash containing all field values from ezcontentobject_version table plus following keys:
935
     *  - names => Hash of content object names. Key is the language code, value is the name.
936
     *  - languages => Hash of language ids. Key is the language code (e.g. "eng-GB"), value is the language numeric id without the always available bit.
937
     *  - initial_language_code => Language code for initial language in this version.
938
     *
939
     * @param int $contentId
940
     * @param int $versionNo
941
     *
942
     * @return array
943
     */
944 View Code Duplication
    public function loadVersionInfo($contentId, $versionNo)
945
    {
946
        $query = $this->queryBuilder->createVersionInfoFindQuery();
947
        $query->where(
948
            $query->expr->lAnd(
949
                $query->expr->eq(
950
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version'),
951
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
952
                ),
953
                $query->expr->eq(
954
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_version'),
955
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
956
                )
957
            )
958
        );
959
        $statement = $query->prepare();
960
        $statement->execute();
961
962
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
963
    }
964
965
    /**
966
     * Returns data for all versions with given status created by the given $userId.
967
     *
968
     * @param int $userId
969
     * @param int $status
970
     *
971
     * @return string[][]
972
     */
973
    public function listVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT)
974
    {
975
        $query = $this->queryBuilder->createVersionInfoFindQuery();
976
        $query->where(
977
            $query->expr->lAnd(
978
                $query->expr->eq(
979
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject_version'),
980
                    $query->bindValue($status, null, \PDO::PARAM_INT)
981
                ),
982
                $query->expr->eq(
983
                    $this->dbHandler->quoteColumn('creator_id', 'ezcontentobject_version'),
984
                    $query->bindValue($userId, null, \PDO::PARAM_INT)
985
                )
986
            )
987
        );
988
989
        return $this->listVersionsHelper($query);
990
    }
991
992
    /**
993
     * Returns all version data for the given $contentId.
994
     *
995
     * @param mixed $contentId
996
     *
997
     * @return string[][]
998
     */
999
    public function listVersions($contentId)
1000
    {
1001
        $query = $this->queryBuilder->createVersionInfoFindQuery();
1002
        $query->where(
1003
            $query->expr->eq(
1004
                $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version'),
1005
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1006
            )
1007
        );
1008
1009
        return $this->listVersionsHelper($query);
1010
    }
1011
1012
    /**
1013
     * Helper for {@see listVersions()} and {@see listVersionsForUser()} that filters duplicates
1014
     * that are the result of the cartesian product performed by createVersionInfoFindQuery().
1015
     *
1016
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
1017
     *
1018
     * @return string[][]
1019
     */
1020
    private function listVersionsHelper(SelectQuery $query)
1021
    {
1022
        $query->orderBy(
1023
            $this->dbHandler->quoteColumn('id', 'ezcontentobject_version')
1024
        );
1025
1026
        $statement = $query->prepare();
1027
        $statement->execute();
1028
1029
        $results = array();
1030
        $previousId = null;
1031
        foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) {
1032
            if ($row['ezcontentobject_version_id'] == $previousId) {
1033
                continue;
1034
            }
1035
1036
            $previousId = $row['ezcontentobject_version_id'];
1037
            $results[] = $row;
1038
        }
1039
1040
        return $results;
1041
    }
1042
1043
    /**
1044
     * Returns all version numbers for the given $contentId.
1045
     *
1046
     * @param mixed $contentId
1047
     *
1048
     * @return int[]
1049
     */
1050
    public function listVersionNumbers($contentId)
1051
    {
1052
        $query = $this->dbHandler->createSelectQuery();
1053
        $query->selectDistinct(
1054
            $this->dbHandler->quoteColumn('version')
1055
        )->from(
1056
            $this->dbHandler->quoteTable('ezcontentobject_version')
1057
        )->where(
1058
            $query->expr->eq(
1059
                $this->dbHandler->quoteColumn('contentobject_id'),
1060
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1061
            )
1062
        );
1063
1064
        $statement = $query->prepare();
1065
        $statement->execute();
1066
1067
        return $statement->fetchAll(\PDO::FETCH_COLUMN);
1068
    }
1069
1070
    /**
1071
     * Returns last version number for content identified by $contentId.
1072
     *
1073
     * @param int $contentId
1074
     *
1075
     * @return int
1076
     */
1077
    public function getLastVersionNumber($contentId)
1078
    {
1079
        $query = $this->dbHandler->createSelectQuery();
1080
        $query->select(
1081
            $query->expr->max($this->dbHandler->quoteColumn('version'))
1082
        )->from(
1083
            $this->dbHandler->quoteTable('ezcontentobject_version')
1084
        )->where(
1085
            $query->expr->eq(
1086
                $this->dbHandler->quoteColumn('contentobject_id'),
1087
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1088
            )
1089
        );
1090
1091
        $statement = $query->prepare();
1092
        $statement->execute();
1093
1094
        return (int)$statement->fetchColumn();
1095
    }
1096
1097
    /**
1098
     * Returns all IDs for locations that refer to $contentId.
1099
     *
1100
     * @param int $contentId
1101
     *
1102
     * @return int[]
1103
     */
1104
    public function getAllLocationIds($contentId)
1105
    {
1106
        $query = $this->dbHandler->createSelectQuery();
1107
        $query->select(
1108
            $this->dbHandler->quoteColumn('node_id')
1109
        )->from(
1110
            $this->dbHandler->quoteTable('ezcontentobject_tree')
1111
        )->where(
1112
            $query->expr->eq(
1113
                $this->dbHandler->quoteColumn('contentobject_id'),
1114
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1115
            )
1116
        );
1117
1118
        $statement = $query->prepare();
1119
        $statement->execute();
1120
1121
        return $statement->fetchAll(\PDO::FETCH_COLUMN);
1122
    }
1123
1124
    /**
1125
     * Returns all field IDs of $contentId grouped by their type.
1126
     * If $versionNo is set only field IDs for that version are returned.
1127
     *
1128
     * @param int $contentId
1129
     * @param int|null $versionNo
1130
     *
1131
     * @return int[][]
1132
     */
1133
    public function getFieldIdsByType($contentId, $versionNo = null)
1134
    {
1135
        $query = $this->dbHandler->createSelectQuery();
1136
        $query->select(
1137
            $this->dbHandler->quoteColumn('id'),
1138
            $this->dbHandler->quoteColumn('data_type_string')
1139
        )->from(
1140
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
1141
        )->where(
1142
            $query->expr->eq(
1143
                $this->dbHandler->quoteColumn('contentobject_id'),
1144
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1145
            )
1146
        );
1147
1148
        if (isset($versionNo)) {
1149
            $query->where(
1150
                $query->expr->eq(
1151
                    $this->dbHandler->quoteColumn('version'),
1152
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1153
                )
1154
            );
1155
        }
1156
1157
        $statement = $query->prepare();
1158
        $statement->execute();
1159
1160
        $result = array();
1161
        foreach ($statement->fetchAll() as $row) {
1162
            if (!isset($result[$row['data_type_string']])) {
1163
                $result[$row['data_type_string']] = array();
1164
            }
1165
            $result[$row['data_type_string']][] = (int)$row['id'];
1166
        }
1167
1168
        return $result;
1169
    }
1170
1171
    /**
1172
     * Deletes relations to and from $contentId.
1173
     * If $versionNo is set only relations for that version are deleted.
1174
     *
1175
     * @param int $contentId
1176
     * @param int|null $versionNo
1177
     */
1178
    public function deleteRelations($contentId, $versionNo = null)
1179
    {
1180
        $query = $this->dbHandler->createDeleteQuery();
1181
        $query->deleteFrom(
1182
            $this->dbHandler->quoteTable('ezcontentobject_link')
1183
        );
1184
1185
        if (isset($versionNo)) {
1186
            $query->where(
1187
                $query->expr->lAnd(
1188
                    $query->expr->eq(
1189
                        $this->dbHandler->quoteColumn('from_contentobject_id'),
1190
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
1191
                    ),
1192
                    $query->expr->eq(
1193
                        $this->dbHandler->quoteColumn('from_contentobject_version'),
1194
                        $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1195
                    )
1196
                )
1197
            );
1198
        } else {
1199
            $query->where(
1200
                $query->expr->lOr(
1201
                    $query->expr->eq(
1202
                        $this->dbHandler->quoteColumn('from_contentobject_id'),
1203
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
1204
                    ),
1205
                    $query->expr->eq(
1206
                        $this->dbHandler->quoteColumn('to_contentobject_id'),
1207
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
1208
                    )
1209
                )
1210
            );
1211
        }
1212
1213
        $query->prepare()->execute();
1214
    }
1215
1216
    /**
1217
     * Removes relations to Content with $contentId from Relation and RelationList field type fields.
1218
     *
1219
     * @param int $contentId
1220
     */
1221
    public function removeReverseFieldRelations($contentId)
1222
    {
1223
        $query = $this->dbHandler->createSelectQuery();
1224
        $query
1225
            ->select('ezcontentobject_attribute.*')
1226
            ->from('ezcontentobject_attribute')
1227
            ->innerJoin(
1228
                'ezcontentobject_link',
1229
                $query->expr->lAnd(
1230
                    $query->expr->eq(
1231
                        $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link'),
1232
                        $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_attribute')
1233
                    ),
1234
                    $query->expr->eq(
1235
                        $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link'),
1236
                        $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute')
1237
                    ),
1238
                    $query->expr->eq(
1239
                        $this->dbHandler->quoteColumn('contentclassattribute_id', 'ezcontentobject_link'),
1240
                        $this->dbHandler->quoteColumn('contentclassattribute_id', 'ezcontentobject_attribute')
1241
                    )
1242
                )
1243
            )
1244
            ->where(
1245
                $query->expr->eq(
1246
                    $this->dbHandler->quoteColumn('to_contentobject_id', 'ezcontentobject_link'),
1247
                    $query->bindValue($contentId, null, PDO::PARAM_INT)
1248
                ),
1249
                $query->expr->gt(
1250
                    $query->expr->bitAnd(
1251
                        $this->dbHandler->quoteColumn('relation_type', 'ezcontentobject_link'),
1252
                        $query->bindValue(8, null, PDO::PARAM_INT)
1253
                    ),
1254
                    0
1255
                )
1256
            );
1257
1258
        $statement = $query->prepare();
1259
        $statement->execute();
1260
1261
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
1262
            if ($row['data_type_string'] === 'ezobjectrelation') {
1263
                $this->removeRelationFromRelationField($row);
1264
            }
1265
1266
            if ($row['data_type_string'] === 'ezobjectrelationlist') {
1267
                $this->removeRelationFromRelationListField($contentId, $row);
1268
            }
1269
        }
1270
    }
1271
1272
    /**
1273
     * Updates field value of RelationList field type identified by given $row data,
1274
     * removing relations toward given $contentId.
1275
     *
1276
     * @param int $contentId
1277
     * @param array $row
1278
     */
1279
    protected function removeRelationFromRelationListField($contentId, array $row)
1280
    {
1281
        $document = new DOMDocument('1.0', 'utf-8');
1282
        $document->loadXML($row['data_text']);
1283
1284
        $xpath = new DOMXPath($document);
1285
        $xpathExpression = "//related-objects/relation-list/relation-item[@contentobject-id='{$contentId}']";
1286
1287
        $relationItems = $xpath->query($xpathExpression);
1288
        foreach ($relationItems as $relationItem) {
1289
            $relationItem->parentNode->removeChild($relationItem);
1290
        }
1291
1292
        $query = $this->dbHandler->createUpdateQuery();
1293
        $query
1294
            ->update('ezcontentobject_attribute')
1295
            ->set(
1296
                'data_text',
1297
                $query->bindValue($document->saveXML(), null, PDO::PARAM_STR)
1298
            )
1299
            ->where(
1300
                $query->expr->lAnd(
1301
                    $query->expr->eq(
1302
                        $this->dbHandler->quoteColumn('id'),
1303
                        $query->bindValue($row['id'], null, PDO::PARAM_INT)
1304
                    ),
1305
                    $query->expr->eq(
1306
                        $this->dbHandler->quoteColumn('version'),
1307
                        $query->bindValue($row['version'], null, PDO::PARAM_INT)
1308
                    )
1309
                )
1310
            );
1311
1312
        $query->prepare()->execute();
1313
    }
1314
1315
    /**
1316
     * Updates field value of Relation field type identified by given $row data,
1317
     * removing relation data.
1318
     *
1319
     * @param array $row
1320
     */
1321
    protected function removeRelationFromRelationField(array $row)
1322
    {
1323
        $query = $this->dbHandler->createUpdateQuery();
1324
        $query
1325
            ->update('ezcontentobject_attribute')
1326
            ->set('data_int', $query->bindValue(null, null, PDO::PARAM_INT))
1327
            ->set('sort_key_int', $query->bindValue(0, null, PDO::PARAM_INT))
1328
            ->where(
1329
                $query->expr->lAnd(
1330
                    $query->expr->eq(
1331
                        $this->dbHandler->quoteColumn('id'),
1332
                        $query->bindValue($row['id'], null, PDO::PARAM_INT)
1333
                    ),
1334
                    $query->expr->eq(
1335
                        $this->dbHandler->quoteColumn('version'),
1336
                        $query->bindValue($row['version'], null, PDO::PARAM_INT)
1337
                    )
1338
                )
1339
            );
1340
1341
        $query->prepare()->execute();
1342
    }
1343
1344
    /**
1345
     * Deletes the field with the given $fieldId.
1346
     *
1347
     * @param int $fieldId
1348
     */
1349
    public function deleteField($fieldId)
1350
    {
1351
        $query = $this->dbHandler->createDeleteQuery();
1352
        $query->deleteFrom(
1353
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
1354
        )->where(
1355
            $query->expr->eq(
1356
                $this->dbHandler->quoteColumn('id'),
1357
                $query->bindValue($fieldId, null, \PDO::PARAM_INT)
1358
            )
1359
        );
1360
1361
        $query->prepare()->execute();
1362
    }
1363
1364
    /**
1365
     * Deletes all fields of $contentId in all versions.
1366
     * If $versionNo is set only fields for that version are deleted.
1367
     *
1368
     * @param int $contentId
1369
     * @param int|null $versionNo
1370
     */
1371
    public function deleteFields($contentId, $versionNo = null)
1372
    {
1373
        $query = $this->dbHandler->createDeleteQuery();
1374
        $query->deleteFrom('ezcontentobject_attribute')
1375
            ->where(
1376
                $query->expr->eq(
1377
                    $this->dbHandler->quoteColumn('contentobject_id'),
1378
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1379
                )
1380
            );
1381
1382
        if (isset($versionNo)) {
1383
            $query->where(
1384
                $query->expr->eq(
1385
                    $this->dbHandler->quoteColumn('version'),
1386
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1387
                )
1388
            );
1389
        }
1390
1391
        $query->prepare()->execute();
1392
    }
1393
1394
    /**
1395
     * Deletes all versions of $contentId.
1396
     * If $versionNo is set only that version is deleted.
1397
     *
1398
     * @param int $contentId
1399
     * @param int|null $versionNo
1400
     */
1401
    public function deleteVersions($contentId, $versionNo = null)
1402
    {
1403
        $query = $this->dbHandler->createDeleteQuery();
1404
        $query->deleteFrom('ezcontentobject_version')
1405
            ->where(
1406
                $query->expr->eq(
1407
                    $this->dbHandler->quoteColumn('contentobject_id'),
1408
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1409
                )
1410
            );
1411
1412
        if (isset($versionNo)) {
1413
            $query->where(
1414
                $query->expr->eq(
1415
                    $this->dbHandler->quoteColumn('version'),
1416
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1417
                )
1418
            );
1419
        }
1420
1421
        $query->prepare()->execute();
1422
    }
1423
1424
    /**
1425
     * Deletes all names of $contentId.
1426
     * If $versionNo is set only names for that version are deleted.
1427
     *
1428
     * @param int $contentId
1429
     * @param int|null $versionNo
1430
     */
1431
    public function deleteNames($contentId, $versionNo = null)
1432
    {
1433
        $query = $this->dbHandler->createDeleteQuery();
1434
        $query->deleteFrom('ezcontentobject_name')
1435
            ->where(
1436
                $query->expr->eq(
1437
                    $this->dbHandler->quoteColumn('contentobject_id'),
1438
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1439
                )
1440
            );
1441
1442
        if (isset($versionNo)) {
1443
            $query->where(
1444
                $query->expr->eq(
1445
                    $this->dbHandler->quoteColumn('content_version'),
1446
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1447
                )
1448
            );
1449
        }
1450
1451
        $query->prepare()->execute();
1452
    }
1453
1454
    /**
1455
     * Sets the name for Content $contentId in version $version to $name in $language.
1456
     *
1457
     * @param int $contentId
1458
     * @param int $version
1459
     * @param string $name
1460
     * @param string $language
1461
     */
1462
    public function setName($contentId, $version, $name, $language)
1463
    {
1464
        $language = $this->languageHandler->loadByLanguageCode($language);
1465
1466
        // Is it an insert or an update ?
1467
        $qSelect = $this->dbHandler->createSelectQuery();
1468
        $qSelect
1469
            ->select(
1470
                $qSelect->alias($qSelect->expr->count('*'), 'count')
1471
            )
1472
            ->from($this->dbHandler->quoteTable('ezcontentobject_name'))
1473
            ->where(
1474
                $qSelect->expr->lAnd(
1475
                    $qSelect->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), $qSelect->bindValue($contentId)),
1476
                    $qSelect->expr->eq($this->dbHandler->quoteColumn('content_version'), $qSelect->bindValue($version)),
1477
                    $qSelect->expr->eq($this->dbHandler->quoteColumn('content_translation'), $qSelect->bindValue($language->languageCode))
1478
                )
1479
            );
1480
        $stmt = $qSelect->prepare();
1481
        $stmt->execute();
1482
        $res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
1483
1484
        $insert = $res[0]['count'] == 0;
1485
        if ($insert) {
1486
            $q = $this->dbHandler->createInsertQuery();
1487
            $q->insertInto($this->dbHandler->quoteTable('ezcontentobject_name'));
1488
        } else {
1489
            $q = $this->dbHandler->createUpdateQuery();
1490
            $q->update($this->dbHandler->quoteTable('ezcontentobject_name'))
1491
                ->where(
1492
                    $q->expr->lAnd(
1493
                        $q->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), $q->bindValue($contentId)),
1494
                        $q->expr->eq($this->dbHandler->quoteColumn('content_version'), $q->bindValue($version)),
1495
                        $q->expr->eq($this->dbHandler->quoteColumn('content_translation'), $q->bindValue($language->languageCode))
1496
                    )
1497
                );
1498
        }
1499
1500
        $q->set(
1501
            $this->dbHandler->quoteColumn('contentobject_id'),
1502
            $q->bindValue($contentId, null, \PDO::PARAM_INT)
1503
        )->set(
1504
            $this->dbHandler->quoteColumn('content_version'),
1505
            $q->bindValue($version, null, \PDO::PARAM_INT)
1506
        )->set(
1507
            $this->dbHandler->quoteColumn('language_id'),
1508
            $q->bindValue($language->id, null, \PDO::PARAM_INT)
1509
        )->set(
1510
            $this->dbHandler->quoteColumn('content_translation'),
1511
            $q->bindValue($language->languageCode)
1512
        )->set(
1513
            $this->dbHandler->quoteColumn('real_translation'),
1514
            $q->bindValue($language->languageCode)
1515
        )->set(
1516
            $this->dbHandler->quoteColumn('name'),
1517
            $q->bindValue($name)
1518
        );
1519
        $q->prepare()->execute();
1520
    }
1521
1522
    /**
1523
     * Deletes the actual content object referred to by $contentId.
1524
     *
1525
     * @param int $contentId
1526
     */
1527
    public function deleteContent($contentId)
1528
    {
1529
        $query = $this->dbHandler->createDeleteQuery();
1530
        $query->deleteFrom('ezcontentobject')
1531
            ->where(
1532
                $query->expr->eq(
1533
                    $this->dbHandler->quoteColumn('id'),
1534
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1535
                )
1536
            );
1537
1538
        $query->prepare()->execute();
1539
    }
1540
1541
    /**
1542
     * Loads relations from $contentId to published content, optionally only from $contentVersionNo.
1543
     *
1544
     * $relationType can also be filtered.
1545
     *
1546
     * @param int $contentId
1547
     * @param int $contentVersionNo
1548
     * @param int $relationType
1549
     *
1550
     * @return string[][] array of relation data
1551
     */
1552
    public function loadRelations($contentId, $contentVersionNo = null, $relationType = null)
1553
    {
1554
        $query = $this->queryBuilder->createRelationFindQuery();
1555
        $query->innerJoin(
1556
            $query->alias(
1557
                $this->dbHandler->quoteTable('ezcontentobject'),
1558
                'ezcontentobject_to'
1559
            ),
1560
            $query->expr->lAnd(
1561
                $query->expr->eq(
1562
                    $this->dbHandler->quoteColumn('to_contentobject_id', 'ezcontentobject_link'),
1563
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject_to')
1564
                ),
1565
                $query->expr->eq(
1566
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject_to'),
1567
                    $query->bindValue(1, null, \PDO::PARAM_INT)
1568
                )
1569
            )
1570
        )->where(
1571
            $query->expr->eq(
1572
                $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link'),
1573
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1574
            )
1575
        );
1576
1577
        // source version number
1578
        if (isset($contentVersionNo)) {
1579
            $query->where(
1580
                $query->expr->eq(
1581
                    $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link'),
1582
                    $query->bindValue($contentVersionNo, null, \PDO::PARAM_INT)
1583
                )
1584
            );
1585
        } else { // from published version only
1586
            $query->from(
1587
                $this->dbHandler->quoteTable('ezcontentobject')
1588
            )->where(
1589
                $query->expr->lAnd(
1590
                    $query->expr->eq(
1591
                        $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
1592
                        $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link')
1593
                    ),
1594
                    $query->expr->eq(
1595
                        $this->dbHandler->quoteColumn('current_version', 'ezcontentobject'),
1596
                        $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link')
1597
                    )
1598
                )
1599
            );
1600
        }
1601
1602
        // relation type
1603 View Code Duplication
        if (isset($relationType)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1604
            $query->where(
1605
                $query->expr->gt(
1606
                    $query->expr->bitAnd(
1607
                        $this->dbHandler->quoteColumn('relation_type', 'ezcontentobject_link'),
1608
                        $query->bindValue($relationType, null, \PDO::PARAM_INT)
1609
                    ),
1610
                    0
1611
                )
1612
            );
1613
        }
1614
1615
        $statement = $query->prepare();
1616
        $statement->execute();
1617
1618
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
1619
    }
1620
1621
    /**
1622
     * Loads data that related to $toContentId.
1623
     *
1624
     * @param int $toContentId
1625
     * @param int $relationType
1626
     *
1627
     * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()}
1628
     */
1629
    public function loadReverseRelations($toContentId, $relationType = null)
1630
    {
1631
        $query = $this->queryBuilder->createRelationFindQuery();
1632
        $query->where(
1633
            $query->expr->eq(
1634
                $this->dbHandler->quoteColumn('to_contentobject_id', 'ezcontentobject_link'),
1635
                $query->bindValue($toContentId, null, \PDO::PARAM_INT)
1636
            )
1637
        );
1638
1639
        // ezcontentobject join
1640
        $query->from(
1641
            $this->dbHandler->quoteTable('ezcontentobject')
1642
        )->where(
1643
            $query->expr->lAnd(
1644
                $query->expr->eq(
1645
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
1646
                    $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link')
1647
                ),
1648
                $query->expr->eq(
1649
                    $this->dbHandler->quoteColumn('current_version', 'ezcontentobject'),
1650
                    $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link')
1651
                ),
1652
                $query->expr->eq(
1653
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject'),
1654
                    $query->bindValue(1, null, \PDO::PARAM_INT)
1655
                )
1656
            )
1657
        );
1658
1659
        // relation type
1660 View Code Duplication
        if (isset($relationType)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1661
            $query->where(
1662
                $query->expr->gt(
1663
                    $query->expr->bitAnd(
1664
                        $this->dbHandler->quoteColumn('relation_type', 'ezcontentobject_link'),
1665
                        $query->bindValue($relationType, null, \PDO::PARAM_INT)
1666
                    ),
1667
                    0
1668
                )
1669
            );
1670
        }
1671
1672
        $statement = $query->prepare();
1673
1674
        $statement->execute();
1675
1676
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
1677
    }
1678
1679
    /**
1680
     * Inserts a new relation database record.
1681
     *
1682
     * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct
1683
     *
1684
     * @return int ID the inserted ID
1685
     */
1686 View Code Duplication
    public function insertRelation(RelationCreateStruct $createStruct)
1687
    {
1688
        $q = $this->dbHandler->createInsertQuery();
1689
        $q->insertInto(
1690
            $this->dbHandler->quoteTable('ezcontentobject_link')
1691
        )->set(
1692
            $this->dbHandler->quoteColumn('id'),
1693
            $this->dbHandler->getAutoIncrementValue('ezcontentobject_link', 'id')
1694
        )->set(
1695
            $this->dbHandler->quoteColumn('contentclassattribute_id'),
1696
            $q->bindValue((int)$createStruct->sourceFieldDefinitionId, null, \PDO::PARAM_INT)
1697
        )->set(
1698
            $this->dbHandler->quoteColumn('from_contentobject_id'),
1699
            $q->bindValue($createStruct->sourceContentId, null, \PDO::PARAM_INT)
1700
        )->set(
1701
            $this->dbHandler->quoteColumn('from_contentobject_version'),
1702
            $q->bindValue($createStruct->sourceContentVersionNo, null, \PDO::PARAM_INT)
1703
        )->set(
1704
            $this->dbHandler->quoteColumn('relation_type'),
1705
            $q->bindValue($createStruct->type, null, \PDO::PARAM_INT)
1706
        )->set(
1707
            $this->dbHandler->quoteColumn('to_contentobject_id'),
1708
            $q->bindValue($createStruct->destinationContentId, null, \PDO::PARAM_INT)
1709
        );
1710
1711
        $q->prepare()->execute();
1712
1713
        return $this->dbHandler->lastInsertId(
1714
            $this->dbHandler->getSequenceName('ezcontentobject_link', 'id')
1715
        );
1716
    }
1717
1718
    /**
1719
     * Deletes the relation with the given $relationId.
1720
     *
1721
     * @param int $relationId
1722
     * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
1723
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
1724
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
1725
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
1726
     */
1727
    public function deleteRelation($relationId, $type)
1728
    {
1729
        // Legacy Storage stores COMMON, LINK and EMBED types using bitmask, therefore first load
1730
        // existing relation type by given $relationId for comparison
1731
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
1732
        $query = $this->dbHandler->createSelectQuery();
1733
        $query->select(
1734
            $this->dbHandler->quoteColumn('relation_type')
1735
        )->from(
1736
            $this->dbHandler->quoteTable('ezcontentobject_link')
1737
        )->where(
1738
            $query->expr->eq(
1739
                $this->dbHandler->quoteColumn('id'),
1740
                $query->bindValue($relationId, null, \PDO::PARAM_INT)
1741
            )
1742
        );
1743
1744
        $statement = $query->prepare();
1745
        $statement->execute();
1746
        $loadedRelationType = $statement->fetchColumn();
1747
1748
        if (!$loadedRelationType) {
1749
            return;
1750
        }
1751
1752
        // If relation type matches then delete
1753
        if ($loadedRelationType == $type) {
1754
            /** @var $query \eZ\Publish\Core\Persistence\Database\DeleteQuery */
1755
            $query = $this->dbHandler->createDeleteQuery();
1756
            $query->deleteFrom(
1757
                'ezcontentobject_link'
1758
            )->where(
1759
                $query->expr->eq(
1760
                    $this->dbHandler->quoteColumn('id'),
1761
                    $query->bindValue($relationId, null, \PDO::PARAM_INT)
1762
                )
1763
            );
1764
1765
            $query->prepare()->execute();
1766
        } elseif ($loadedRelationType & $type) { // If relation type is composite update bitmask
1767
            /** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
1768
            $query = $this->dbHandler->createUpdateQuery();
1769
            $query->update(
1770
                $this->dbHandler->quoteTable('ezcontentobject_link')
1771
            )->set(
1772
                $this->dbHandler->quoteColumn('relation_type'),
1773
                $query->expr->bitAnd(
1774
                    $this->dbHandler->quoteColumn('relation_type'),
1775
                    $query->bindValue(~$type, null, \PDO::PARAM_INT)
1776
                )
1777
            )->where(
1778
                $query->expr->eq(
1779
                    $this->dbHandler->quoteColumn('id'),
1780
                    $query->bindValue($relationId, null, \PDO::PARAM_INT)
1781
                )
1782
            );
1783
1784
            $query->prepare()->execute();
1785
        } else {
1786
            // No match, do nothing
1787
        }
1788
    }
1789
1790
    /**
1791
     * Returns all Content IDs for a given $contentTypeId.
1792
     *
1793
     * @param int $contentTypeId
1794
     *
1795
     * @return int[]
1796
     */
1797
    public function getContentIdsByContentTypeId($contentTypeId)
1798
    {
1799
        $query = $this->dbHandler->createSelectQuery();
1800
        $query
1801
            ->select($this->dbHandler->quoteColumn('id'))
1802
            ->from($this->dbHandler->quoteTable('ezcontentobject'))
1803
            ->where(
1804
                $query->expr->eq(
1805
                    $this->dbHandler->quoteColumn('contentclass_id'),
1806
                    $query->bindValue($contentTypeId, null, PDO::PARAM_INT)
1807
                )
1808
            );
1809
1810
        $statement = $query->prepare();
1811
        $statement->execute();
1812
1813
        return $statement->fetchAll(PDO::FETCH_COLUMN);
1814
    }
1815
1816
    /**
1817
     * Load name data for set of content id's and corresponding version number.
1818
     *
1819
     * @param array[] $rows array of hashes with 'id' and 'version' to load names for
1820
     *
1821
     * @return array
1822
     */
1823
    public function loadVersionedNameData($rows)
1824
    {
1825
        $query = $this->queryBuilder->createNamesQuery();
1826
        $conditions = array();
1827
        foreach ($rows as $row) {
1828
            $conditions[] = $query->expr->lAnd(
1829
                $query->expr->eq(
1830
                    $this->dbHandler->quoteColumn('contentobject_id'),
1831
                    $query->bindValue($row['id'], null, \PDO::PARAM_INT)
1832
                ),
1833
                $query->expr->eq(
1834
                    $this->dbHandler->quoteColumn('content_version'),
1835
                    $query->bindValue($row['version'], null, \PDO::PARAM_INT)
1836
                )
1837
            );
1838
        }
1839
1840
        $query->where($query->expr->lOr($conditions));
1841
        $stmt = $query->prepare();
1842
        $stmt->execute();
1843
1844
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
1845
    }
1846
1847
    /**
1848
     * Batch method for copying all relation meta data for copied Content object.
1849
     *
1850
     * {@inheritdoc}
1851
     *
1852
     * @param int $originalContentId
1853
     * @param int $copiedContentId
1854
     * @param int|null $versionNo If specified only copy for a given version number, otherwise all.
1855
     */
1856
    public function copyRelations($originalContentId, $copiedContentId, $versionNo = null)
1857
    {
1858
        // Given we can retain all columns, we just create copies with new `from_contentobject_id` using INSERT INTO SELECT
1859
        $sql = "INSERT INTO ezcontentobject_link ( contentclassattribute_id, from_contentobject_id, from_contentobject_version, relation_type, to_contentobject_id )
1860
                SELECT  L2.contentclassattribute_id, :copied_id, L2.from_contentobject_version, L2.relation_type, L2.to_contentobject_id
1861
                FROM    ezcontentobject_link AS L2
1862
                WHERE   L2.from_contentobject_id = :original_id";
1863
1864
        if ($versionNo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $versionNo of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1865
            $stmt = $this->connection->prepare($sql . " AND L2.from_contentobject_version = :version");
1866
            $stmt->bindValue("version", $versionNo, PDO::PARAM_INT);
1867
        } else {
1868
            $stmt = $this->connection->prepare($sql);
1869
        }
1870
1871
        $stmt->bindValue("original_id", $originalContentId, PDO::PARAM_INT);
1872
        $stmt->bindValue("copied_id", $copiedContentId, PDO::PARAM_INT);
1873
1874
        $stmt->execute();
1875
    }
1876
}
1877