Completed
Push — 6.7 ( 0b0687...bec934 )
by Łukasz
20:09
created

DoctrineDatabase::insertNewField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 3
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
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
namespace eZ\Publish\Core\Persistence\Legacy\Content\Gateway;
10
11
use Doctrine\DBAL\Connection;
12
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway;
13
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder;
14
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
15
use eZ\Publish\Core\Persistence\Database\UpdateQuery;
16
use eZ\Publish\Core\Persistence\Database\InsertQuery;
17
use eZ\Publish\Core\Persistence\Database\SelectQuery;
18
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
19
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator as LanguageMaskGenerator;
20
use eZ\Publish\SPI\Persistence\Content;
21
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
22
use eZ\Publish\SPI\Persistence\Content\UpdateStruct;
23
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
24
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
25
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
26
use eZ\Publish\SPI\Persistence\Content\Field;
27
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
28
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
29
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
30
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
31
use DOMXPath;
32
use DOMDocument;
33
use PDO;
34
35
/**
36
 * Doctrine database based content gateway.
37
 */
38
class DoctrineDatabase extends Gateway
39
{
40
    /**
41
     * eZ Doctrine database handler.
42
     *
43
     * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
44
     */
45
    protected $dbHandler;
46
47
    /**
48
     * The native Doctrine connection.
49
     *
50
     * Meant to be used to transition from eZ/Zeta interface to Doctrine.
51
     *
52
     * @var \Doctrine\DBAL\Connection
53
     */
54
    protected $connection;
55
56
    /**
57
     * Query builder.
58
     *
59
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder
60
     */
61
    protected $queryBuilder;
62
63
    /**
64
     * Caching language handler.
65
     *
66
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler
67
     */
68
    protected $languageHandler;
69
70
    /**
71
     * Language mask generator.
72
     *
73
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator
74
     */
75
    protected $languageMaskGenerator;
76
77
    /**
78
     * Creates a new gateway based on $db.
79
     *
80
     * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $db
81
     * @param \Doctrine\DBAL\Connection $connection
82
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder $queryBuilder
83
     * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler
84
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $languageMaskGenerator
85
     */
86
    public function __construct(
87
        DatabaseHandler $db,
88
        Connection $connection,
89
        QueryBuilder $queryBuilder,
90
        LanguageHandler $languageHandler,
91
        LanguageMaskGenerator $languageMaskGenerator
92
    ) {
93
        $this->dbHandler = $db;
94
        $this->connection = $connection;
95
        $this->queryBuilder = $queryBuilder;
96
        $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...
97
        $this->languageMaskGenerator = $languageMaskGenerator;
98
    }
99
100
    /**
101
     * Get context definition for external storage layers.
102
     *
103
     * @return array
104
     */
105
    public function getContext()
106
    {
107
        return array(
108
            'identifier' => 'LegacyStorage',
109
            'connection' => $this->dbHandler,
110
        );
111
    }
112
113
    /**
114
     * Inserts a new content object.
115
     *
116
     * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct
117
     * @param mixed $currentVersionNo
118
     *
119
     * @return int ID
120
     */
121
    public function insertContentObject(CreateStruct $struct, $currentVersionNo = 1)
122
    {
123
        $initialLanguageId = !empty($struct->mainLanguageId) ? $struct->mainLanguageId : $struct->initialLanguageId;
124
        $initialLanguageCode = $this->languageHandler->load($initialLanguageId)->languageCode;
125
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($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
                    $initialLanguageCode,
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(mb_substr($value->sortKeyString, 0, 255))
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(mb_substr($value->sortKeyString, 0, 255))
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(), loadContentInfoByLocationId()
854
     *
855
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
856
     *
857
     * @return array
858
     */
859
    private function internalLoadContentInfo(SelectQuery $query)
860
    {
861
        $query->select(
862
            'ezcontentobject.*',
863
            $this->dbHandler->aliasedColumn($query, 'main_node_id', 'ezcontentobject_tree')
864
        )->from(
865
            $this->dbHandler->quoteTable('ezcontentobject')
866
        )->leftJoin(
867
            $this->dbHandler->quoteTable('ezcontentobject_tree'),
868
            $query->expr->lAnd(
869
                $query->expr->eq(
870
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_tree'),
871
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject')
872
                ),
873
                $query->expr->eq(
874
                    $this->dbHandler->quoteColumn('main_node_id', 'ezcontentobject_tree'),
875
                    $this->dbHandler->quoteColumn('node_id', 'ezcontentobject_tree')
876
                )
877
            )
878
        );
879
        $statement = $query->prepare();
880
        $statement->execute();
881
882
        return $statement->fetch(PDO::FETCH_ASSOC);
883
    }
884
885
    /**
886
     * Loads info for content identified by $contentId.
887
     * Will basically return a hash containing all field values for ezcontentobject table plus some additional keys:
888
     *  - always_available => Boolean indicating if content's language mask contains alwaysAvailable bit field
889
     *  - main_language_code => Language code for main (initial) language. E.g. "eng-GB".
890
     *
891
     * @param int $contentId
892
     *
893
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
894
     *
895
     * @return array
896
     */
897 View Code Duplication
    public function loadContentInfo($contentId)
898
    {
899
        $query = $this->dbHandler->createSelectQuery();
900
        $query->where(
901
            $query->expr->eq(
902
                $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
903
                $query->bindValue($contentId, null, PDO::PARAM_INT)
904
            )
905
        );
906
907
        $row = $this->internalLoadContentInfo($query);
908
909
        if (empty($row)) {
910
            throw new NotFound('content', "id: $contentId");
911
        }
912
913
        return $row;
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 View Code Duplication
    public function loadContentInfoByRemoteId($remoteId)
928
    {
929
        $query = $this->dbHandler->createSelectQuery();
930
        $query->where(
931
            $query->expr->eq(
932
                $this->dbHandler->quoteColumn('remote_id', 'ezcontentobject'),
933
                $query->bindValue($remoteId)
934
            )
935
        );
936
937
        $row = $this->internalLoadContentInfo($query);
938
939
        if (empty($row)) {
940
            throw new NotFound('content', "remote_id: $remoteId");
941
        }
942
943
        return $row;
944
    }
945
946
    /**
947
     * Loads info for a content object identified by its location ID (node ID).
948
     *
949
     * Returns an array with the relevant data.
950
     *
951
     * @param int $locationId
952
     *
953
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
954
     *
955
     * @return array
956
     */
957 View Code Duplication
    public function loadContentInfoByLocationId($locationId)
958
    {
959
        $query = $this->dbHandler->createSelectQuery();
960
        $query->where(
961
            $query->expr->eq(
962
                $this->dbHandler->quoteColumn('main_node_id', 'ezcontentobject_tree'),
963
                $query->bindValue($locationId, null, PDO::PARAM_INT)
964
            )
965
        );
966
967
        $row = $this->internalLoadContentInfo($query);
968
969
        if (empty($row)) {
970
            throw new NotFound('content', "main_node_id: $locationId");
971
        }
972
973
        return $row;
974
    }
975
976
    /**
977
     * Loads version info for content identified by $contentId and $versionNo.
978
     * Will basically return a hash containing all field values from ezcontentobject_version table plus following keys:
979
     *  - names => Hash of content object names. Key is the language code, value is the name.
980
     *  - 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.
981
     *  - initial_language_code => Language code for initial language in this version.
982
     *
983
     * @param int $contentId
984
     * @param int $versionNo
985
     *
986
     * @return array
987
     */
988 View Code Duplication
    public function loadVersionInfo($contentId, $versionNo)
989
    {
990
        $query = $this->queryBuilder->createVersionInfoFindQuery();
991
        $query->where(
992
            $query->expr->lAnd(
993
                $query->expr->eq(
994
                    $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version'),
995
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
996
                ),
997
                $query->expr->eq(
998
                    $this->dbHandler->quoteColumn('version', 'ezcontentobject_version'),
999
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1000
                )
1001
            )
1002
        );
1003
        $statement = $query->prepare();
1004
        $statement->execute();
1005
1006
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
1007
    }
1008
1009
    /**
1010
     * Returns data for all versions with given status created by the given $userId.
1011
     *
1012
     * @param int $userId
1013
     * @param int $status
1014
     *
1015
     * @return string[][]
1016
     */
1017
    public function listVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT)
1018
    {
1019
        $query = $this->queryBuilder->createVersionInfoFindQuery();
1020
        $query->where(
1021
            $query->expr->lAnd(
1022
                $query->expr->eq(
1023
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject_version'),
1024
                    $query->bindValue($status, null, \PDO::PARAM_INT)
1025
                ),
1026
                $query->expr->eq(
1027
                    $this->dbHandler->quoteColumn('creator_id', 'ezcontentobject_version'),
1028
                    $query->bindValue($userId, null, \PDO::PARAM_INT)
1029
                )
1030
            )
1031
        );
1032
1033
        return $this->listVersionsHelper($query);
1034
    }
1035
1036
    /**
1037
     * Returns all version data for the given $contentId, optionally filtered by status.
1038
     *
1039
     * Result is returned with oldest version first (using version id as it has index and is auto increment).
1040
     *
1041
     * @param mixed $contentId
1042
     * @param mixed|null $status Optional argument to filter versions by status, like {@see VersionInfo::STATUS_ARCHIVED}.
1043
     * @param int $limit Limit for items returned, -1 means none.
1044
     *
1045
     * @return string[][]
1046
     */
1047
    public function listVersions($contentId, $status = null, $limit = -1)
1048
    {
1049
        $query = $this->queryBuilder->createVersionInfoFindQuery();
1050
1051
        $filter = $query->expr->eq(
1052
            $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_version'),
1053
            $query->bindValue($contentId, null, \PDO::PARAM_INT)
1054
        );
1055
1056
        if ($status !== null) {
1057
            $filter = $query->expr->lAnd(
1058
                $filter,
1059
                $query->expr->eq(
1060
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject_version'),
1061
                    $query->bindValue($status, null, \PDO::PARAM_INT)
1062
                )
1063
            );
1064
        }
1065
1066
        $query->where($filter);
1067
1068
        if ($limit > 0) {
1069
            $query->limit($limit);
1070
        }
1071
1072
        return $this->listVersionsHelper($query);
1073
    }
1074
1075
    /**
1076
     * Helper for {@see listVersions()} and {@see listVersionsForUser()} that filters duplicates
1077
     * that are the result of the cartesian product performed by createVersionInfoFindQuery().
1078
     *
1079
     * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query
1080
     *
1081
     * @return string[][]
1082
     */
1083
    private function listVersionsHelper(SelectQuery $query)
1084
    {
1085
        $query->orderBy(
1086
            $this->dbHandler->quoteColumn('id', 'ezcontentobject_version')
1087
        );
1088
1089
        $statement = $query->prepare();
1090
        $statement->execute();
1091
1092
        $results = array();
1093
        $previousId = null;
1094
        foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) {
1095
            if ($row['ezcontentobject_version_id'] == $previousId) {
1096
                continue;
1097
            }
1098
1099
            $previousId = $row['ezcontentobject_version_id'];
1100
            $results[] = $row;
1101
        }
1102
1103
        return $results;
1104
    }
1105
1106
    /**
1107
     * Returns all version numbers for the given $contentId.
1108
     *
1109
     * @param mixed $contentId
1110
     *
1111
     * @return int[]
1112
     */
1113
    public function listVersionNumbers($contentId)
1114
    {
1115
        $query = $this->dbHandler->createSelectQuery();
1116
        $query->selectDistinct(
1117
            $this->dbHandler->quoteColumn('version')
1118
        )->from(
1119
            $this->dbHandler->quoteTable('ezcontentobject_version')
1120
        )->where(
1121
            $query->expr->eq(
1122
                $this->dbHandler->quoteColumn('contentobject_id'),
1123
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1124
            )
1125
        );
1126
1127
        $statement = $query->prepare();
1128
        $statement->execute();
1129
1130
        return $statement->fetchAll(\PDO::FETCH_COLUMN);
1131
    }
1132
1133
    /**
1134
     * Returns last version number for content identified by $contentId.
1135
     *
1136
     * @param int $contentId
1137
     *
1138
     * @return int
1139
     */
1140
    public function getLastVersionNumber($contentId)
1141
    {
1142
        $query = $this->dbHandler->createSelectQuery();
1143
        $query->select(
1144
            $query->expr->max($this->dbHandler->quoteColumn('version'))
1145
        )->from(
1146
            $this->dbHandler->quoteTable('ezcontentobject_version')
1147
        )->where(
1148
            $query->expr->eq(
1149
                $this->dbHandler->quoteColumn('contentobject_id'),
1150
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1151
            )
1152
        );
1153
1154
        $statement = $query->prepare();
1155
        $statement->execute();
1156
1157
        return (int)$statement->fetchColumn();
1158
    }
1159
1160
    /**
1161
     * Returns all IDs for locations that refer to $contentId.
1162
     *
1163
     * @param int $contentId
1164
     *
1165
     * @return int[]
1166
     */
1167
    public function getAllLocationIds($contentId)
1168
    {
1169
        $query = $this->dbHandler->createSelectQuery();
1170
        $query->select(
1171
            $this->dbHandler->quoteColumn('node_id')
1172
        )->from(
1173
            $this->dbHandler->quoteTable('ezcontentobject_tree')
1174
        )->where(
1175
            $query->expr->eq(
1176
                $this->dbHandler->quoteColumn('contentobject_id'),
1177
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1178
            )
1179
        );
1180
1181
        $statement = $query->prepare();
1182
        $statement->execute();
1183
1184
        return $statement->fetchAll(\PDO::FETCH_COLUMN);
1185
    }
1186
1187
    /**
1188
     * Returns all field IDs of $contentId grouped by their type.
1189
     * If $versionNo is set only field IDs for that version are returned.
1190
     *
1191
     * @param int $contentId
1192
     * @param int|null $versionNo
1193
     *
1194
     * @return int[][]
1195
     */
1196
    public function getFieldIdsByType($contentId, $versionNo = null)
1197
    {
1198
        $query = $this->dbHandler->createSelectQuery();
1199
        $query->select(
1200
            $this->dbHandler->quoteColumn('id'),
1201
            $this->dbHandler->quoteColumn('data_type_string')
1202
        )->from(
1203
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
1204
        )->where(
1205
            $query->expr->eq(
1206
                $this->dbHandler->quoteColumn('contentobject_id'),
1207
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1208
            )
1209
        );
1210
1211
        if (isset($versionNo)) {
1212
            $query->where(
1213
                $query->expr->eq(
1214
                    $this->dbHandler->quoteColumn('version'),
1215
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1216
                )
1217
            );
1218
        }
1219
1220
        $statement = $query->prepare();
1221
        $statement->execute();
1222
1223
        $result = array();
1224
        foreach ($statement->fetchAll() as $row) {
1225
            if (!isset($result[$row['data_type_string']])) {
1226
                $result[$row['data_type_string']] = array();
1227
            }
1228
            $result[$row['data_type_string']][] = (int)$row['id'];
1229
        }
1230
1231
        return $result;
1232
    }
1233
1234
    /**
1235
     * Deletes relations to and from $contentId.
1236
     * If $versionNo is set only relations for that version are deleted.
1237
     *
1238
     * @param int $contentId
1239
     * @param int|null $versionNo
1240
     */
1241
    public function deleteRelations($contentId, $versionNo = null)
1242
    {
1243
        $query = $this->dbHandler->createDeleteQuery();
1244
        $query->deleteFrom(
1245
            $this->dbHandler->quoteTable('ezcontentobject_link')
1246
        );
1247
1248
        if (isset($versionNo)) {
1249
            $query->where(
1250
                $query->expr->lAnd(
1251
                    $query->expr->eq(
1252
                        $this->dbHandler->quoteColumn('from_contentobject_id'),
1253
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
1254
                    ),
1255
                    $query->expr->eq(
1256
                        $this->dbHandler->quoteColumn('from_contentobject_version'),
1257
                        $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1258
                    )
1259
                )
1260
            );
1261
        } else {
1262
            $query->where(
1263
                $query->expr->lOr(
1264
                    $query->expr->eq(
1265
                        $this->dbHandler->quoteColumn('from_contentobject_id'),
1266
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
1267
                    ),
1268
                    $query->expr->eq(
1269
                        $this->dbHandler->quoteColumn('to_contentobject_id'),
1270
                        $query->bindValue($contentId, null, \PDO::PARAM_INT)
1271
                    )
1272
                )
1273
            );
1274
        }
1275
1276
        $query->prepare()->execute();
1277
    }
1278
1279
    /**
1280
     * Removes relations to Content with $contentId from Relation and RelationList field type fields.
1281
     *
1282
     * @param int $contentId
1283
     */
1284
    public function removeReverseFieldRelations($contentId)
1285
    {
1286
        $query = $this->dbHandler->createSelectQuery();
1287
        $query
1288
            ->select('ezcontentobject_attribute.*')
1289
            ->from('ezcontentobject_attribute')
1290
            ->innerJoin(
1291
                'ezcontentobject_link',
1292
                $query->expr->lAnd(
1293
                    $query->expr->eq(
1294
                        $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link'),
1295
                        $this->dbHandler->quoteColumn('contentobject_id', 'ezcontentobject_attribute')
1296
                    ),
1297
                    $query->expr->eq(
1298
                        $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link'),
1299
                        $this->dbHandler->quoteColumn('version', 'ezcontentobject_attribute')
1300
                    ),
1301
                    $query->expr->eq(
1302
                        $this->dbHandler->quoteColumn('contentclassattribute_id', 'ezcontentobject_link'),
1303
                        $this->dbHandler->quoteColumn('contentclassattribute_id', 'ezcontentobject_attribute')
1304
                    )
1305
                )
1306
            )
1307
            ->where(
1308
                $query->expr->eq(
1309
                    $this->dbHandler->quoteColumn('to_contentobject_id', 'ezcontentobject_link'),
1310
                    $query->bindValue($contentId, null, PDO::PARAM_INT)
1311
                ),
1312
                $query->expr->gt(
1313
                    $query->expr->bitAnd(
1314
                        $this->dbHandler->quoteColumn('relation_type', 'ezcontentobject_link'),
1315
                        $query->bindValue(8, null, PDO::PARAM_INT)
1316
                    ),
1317
                    0
1318
                )
1319
            );
1320
1321
        $statement = $query->prepare();
1322
        $statement->execute();
1323
1324
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
1325
            if ($row['data_type_string'] === 'ezobjectrelation') {
1326
                $this->removeRelationFromRelationField($row);
1327
            }
1328
1329
            if ($row['data_type_string'] === 'ezobjectrelationlist') {
1330
                $this->removeRelationFromRelationListField($contentId, $row);
1331
            }
1332
        }
1333
    }
1334
1335
    /**
1336
     * Updates field value of RelationList field type identified by given $row data,
1337
     * removing relations toward given $contentId.
1338
     *
1339
     * @param int $contentId
1340
     * @param array $row
1341
     */
1342
    protected function removeRelationFromRelationListField($contentId, array $row)
1343
    {
1344
        $document = new DOMDocument('1.0', 'utf-8');
1345
        $document->loadXML($row['data_text']);
1346
1347
        $xpath = new DOMXPath($document);
1348
        $xpathExpression = "//related-objects/relation-list/relation-item[@contentobject-id='{$contentId}']";
1349
1350
        $relationItems = $xpath->query($xpathExpression);
1351
        foreach ($relationItems as $relationItem) {
1352
            $relationItem->parentNode->removeChild($relationItem);
1353
        }
1354
1355
        $query = $this->dbHandler->createUpdateQuery();
1356
        $query
1357
            ->update('ezcontentobject_attribute')
1358
            ->set(
1359
                'data_text',
1360
                $query->bindValue($document->saveXML(), null, PDO::PARAM_STR)
1361
            )
1362
            ->where(
1363
                $query->expr->lAnd(
1364
                    $query->expr->eq(
1365
                        $this->dbHandler->quoteColumn('id'),
1366
                        $query->bindValue($row['id'], null, PDO::PARAM_INT)
1367
                    ),
1368
                    $query->expr->eq(
1369
                        $this->dbHandler->quoteColumn('version'),
1370
                        $query->bindValue($row['version'], null, PDO::PARAM_INT)
1371
                    )
1372
                )
1373
            );
1374
1375
        $query->prepare()->execute();
1376
    }
1377
1378
    /**
1379
     * Updates field value of Relation field type identified by given $row data,
1380
     * removing relation data.
1381
     *
1382
     * @param array $row
1383
     */
1384
    protected function removeRelationFromRelationField(array $row)
1385
    {
1386
        $query = $this->dbHandler->createUpdateQuery();
1387
        $query
1388
            ->update('ezcontentobject_attribute')
1389
            ->set('data_int', $query->bindValue(null, null, PDO::PARAM_INT))
1390
            ->set('sort_key_int', $query->bindValue(0, null, PDO::PARAM_INT))
1391
            ->where(
1392
                $query->expr->lAnd(
1393
                    $query->expr->eq(
1394
                        $this->dbHandler->quoteColumn('id'),
1395
                        $query->bindValue($row['id'], null, PDO::PARAM_INT)
1396
                    ),
1397
                    $query->expr->eq(
1398
                        $this->dbHandler->quoteColumn('version'),
1399
                        $query->bindValue($row['version'], null, PDO::PARAM_INT)
1400
                    )
1401
                )
1402
            );
1403
1404
        $query->prepare()->execute();
1405
    }
1406
1407
    /**
1408
     * Deletes the field with the given $fieldId.
1409
     *
1410
     * @param int $fieldId
1411
     */
1412
    public function deleteField($fieldId)
1413
    {
1414
        $query = $this->dbHandler->createDeleteQuery();
1415
        $query->deleteFrom(
1416
            $this->dbHandler->quoteTable('ezcontentobject_attribute')
1417
        )->where(
1418
            $query->expr->eq(
1419
                $this->dbHandler->quoteColumn('id'),
1420
                $query->bindValue($fieldId, null, \PDO::PARAM_INT)
1421
            )
1422
        );
1423
1424
        $query->prepare()->execute();
1425
    }
1426
1427
    /**
1428
     * Deletes all fields of $contentId in all versions.
1429
     * If $versionNo is set only fields for that version are deleted.
1430
     *
1431
     * @param int $contentId
1432
     * @param int|null $versionNo
1433
     */
1434
    public function deleteFields($contentId, $versionNo = null)
1435
    {
1436
        $query = $this->dbHandler->createDeleteQuery();
1437
        $query->deleteFrom('ezcontentobject_attribute')
1438
            ->where(
1439
                $query->expr->eq(
1440
                    $this->dbHandler->quoteColumn('contentobject_id'),
1441
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1442
                )
1443
            );
1444
1445
        if (isset($versionNo)) {
1446
            $query->where(
1447
                $query->expr->eq(
1448
                    $this->dbHandler->quoteColumn('version'),
1449
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1450
                )
1451
            );
1452
        }
1453
1454
        $query->prepare()->execute();
1455
    }
1456
1457
    /**
1458
     * Deletes all versions of $contentId.
1459
     * If $versionNo is set only that version is deleted.
1460
     *
1461
     * @param int $contentId
1462
     * @param int|null $versionNo
1463
     */
1464
    public function deleteVersions($contentId, $versionNo = null)
1465
    {
1466
        $query = $this->dbHandler->createDeleteQuery();
1467
        $query->deleteFrom('ezcontentobject_version')
1468
            ->where(
1469
                $query->expr->eq(
1470
                    $this->dbHandler->quoteColumn('contentobject_id'),
1471
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1472
                )
1473
            );
1474
1475
        if (isset($versionNo)) {
1476
            $query->where(
1477
                $query->expr->eq(
1478
                    $this->dbHandler->quoteColumn('version'),
1479
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1480
                )
1481
            );
1482
        }
1483
1484
        $query->prepare()->execute();
1485
    }
1486
1487
    /**
1488
     * Deletes all names of $contentId.
1489
     * If $versionNo is set only names for that version are deleted.
1490
     *
1491
     * @param int $contentId
1492
     * @param int|null $versionNo
1493
     */
1494
    public function deleteNames($contentId, $versionNo = null)
1495
    {
1496
        $query = $this->dbHandler->createDeleteQuery();
1497
        $query->deleteFrom('ezcontentobject_name')
1498
            ->where(
1499
                $query->expr->eq(
1500
                    $this->dbHandler->quoteColumn('contentobject_id'),
1501
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1502
                )
1503
            );
1504
1505
        if (isset($versionNo)) {
1506
            $query->where(
1507
                $query->expr->eq(
1508
                    $this->dbHandler->quoteColumn('content_version'),
1509
                    $query->bindValue($versionNo, null, \PDO::PARAM_INT)
1510
                )
1511
            );
1512
        }
1513
1514
        $query->prepare()->execute();
1515
    }
1516
1517
    /**
1518
     * Sets the name for Content $contentId in version $version to $name in $language.
1519
     *
1520
     * @param int $contentId
1521
     * @param int $version
1522
     * @param string $name
1523
     * @param string $language
1524
     */
1525
    public function setName($contentId, $version, $name, $language)
1526
    {
1527
        $language = $this->languageHandler->loadByLanguageCode($language);
1528
1529
        // Is it an insert or an update ?
1530
        $qSelect = $this->dbHandler->createSelectQuery();
1531
        $qSelect
1532
            ->select(
1533
                $qSelect->alias($qSelect->expr->count('*'), 'count')
1534
            )
1535
            ->from($this->dbHandler->quoteTable('ezcontentobject_name'))
1536
            ->where(
1537
                $qSelect->expr->lAnd(
1538
                    $qSelect->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), $qSelect->bindValue($contentId)),
1539
                    $qSelect->expr->eq($this->dbHandler->quoteColumn('content_version'), $qSelect->bindValue($version)),
1540
                    $qSelect->expr->eq($this->dbHandler->quoteColumn('content_translation'), $qSelect->bindValue($language->languageCode))
1541
                )
1542
            );
1543
        $stmt = $qSelect->prepare();
1544
        $stmt->execute();
1545
        $res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
1546
1547
        $insert = $res[0]['count'] == 0;
1548
        if ($insert) {
1549
            $q = $this->dbHandler->createInsertQuery();
1550
            $q->insertInto($this->dbHandler->quoteTable('ezcontentobject_name'));
1551
        } else {
1552
            $q = $this->dbHandler->createUpdateQuery();
1553
            $q->update($this->dbHandler->quoteTable('ezcontentobject_name'))
1554
                ->where(
1555
                    $q->expr->lAnd(
1556
                        $q->expr->eq($this->dbHandler->quoteColumn('contentobject_id'), $q->bindValue($contentId)),
1557
                        $q->expr->eq($this->dbHandler->quoteColumn('content_version'), $q->bindValue($version)),
1558
                        $q->expr->eq($this->dbHandler->quoteColumn('content_translation'), $q->bindValue($language->languageCode))
1559
                    )
1560
                );
1561
        }
1562
1563
        $q->set(
1564
            $this->dbHandler->quoteColumn('contentobject_id'),
1565
            $q->bindValue($contentId, null, \PDO::PARAM_INT)
1566
        )->set(
1567
            $this->dbHandler->quoteColumn('content_version'),
1568
            $q->bindValue($version, null, \PDO::PARAM_INT)
1569
        )->set(
1570
            $this->dbHandler->quoteColumn('language_id'),
1571
            '(' . $this->getLanguageQuery()->getQuery() . ')'
1572
        )->set(
1573
            $this->dbHandler->quoteColumn('content_translation'),
1574
            $q->bindValue($language->languageCode)
1575
        )->set(
1576
            $this->dbHandler->quoteColumn('real_translation'),
1577
            $q->bindValue($language->languageCode)
1578
        )->set(
1579
            $this->dbHandler->quoteColumn('name'),
1580
            $q->bindValue($name)
1581
        );
1582
        $q->bindValue($language->id, ':languageId', \PDO::PARAM_INT);
1583
        $q->bindValue($contentId, ':contentId', \PDO::PARAM_INT);
1584
        $q->prepare()->execute();
1585
    }
1586
1587
    /**
1588
     * Returns a language sub select query for setName.
1589
     *
1590
     * Return sub select query which gets proper language mask for alwaysAvailable Content.
1591
     *
1592
     * @return \eZ\Publish\Core\Persistence\Database\SelectQuery
1593
     */
1594
    private function getLanguageQuery()
1595
    {
1596
        $languageQuery = $this->dbHandler->createSelectQuery();
1597
        $languageQuery
1598
            ->select(
1599
                $languageQuery->expr->searchedCase(
1600
                    [
1601
                        $languageQuery->expr->lAnd(
1602
                            $languageQuery->expr->eq(
1603
                                $this->dbHandler->quoteColumn('initial_language_id'),
1604
                                ':languageId'
1605
                            ),
1606
                            // wrap bitwise check into another "neq" to provide cross-DBMS compatibility
1607
                            $languageQuery->expr->neq(
1608
                                $languageQuery->expr->bitAnd(
1609
                                    $this->dbHandler->quoteColumn('language_mask'),
1610
                                    ':languageId'
1611
                                ),
1612
                                0
1613
                            )
1614
                        ),
1615
                        $languageQuery->expr->bitOr(
1616
                            ':languageId',
1617
                            1
1618
                        ),
1619
                    ],
1620
                    ':languageId'
1621
                )
1622
            )
1623
            ->from('ezcontentobject')
1624
            ->where(
1625
                $languageQuery->expr->eq(
1626
                    'id',
1627
                    ':contentId'
1628
                )
1629
            );
1630
1631
        return $languageQuery;
1632
    }
1633
1634
    /**
1635
     * Deletes the actual content object referred to by $contentId.
1636
     *
1637
     * @param int $contentId
1638
     */
1639
    public function deleteContent($contentId)
1640
    {
1641
        $query = $this->dbHandler->createDeleteQuery();
1642
        $query->deleteFrom('ezcontentobject')
1643
            ->where(
1644
                $query->expr->eq(
1645
                    $this->dbHandler->quoteColumn('id'),
1646
                    $query->bindValue($contentId, null, \PDO::PARAM_INT)
1647
                )
1648
            );
1649
1650
        $query->prepare()->execute();
1651
    }
1652
1653
    /**
1654
     * Loads relations from $contentId to published content, optionally only from $contentVersionNo.
1655
     *
1656
     * $relationType can also be filtered.
1657
     *
1658
     * @param int $contentId
1659
     * @param int $contentVersionNo
1660
     * @param int $relationType
1661
     *
1662
     * @return string[][] array of relation data
1663
     */
1664
    public function loadRelations($contentId, $contentVersionNo = null, $relationType = null)
1665
    {
1666
        $query = $this->queryBuilder->createRelationFindQuery();
1667
        $query->innerJoin(
1668
            $query->alias(
1669
                $this->dbHandler->quoteTable('ezcontentobject'),
1670
                'ezcontentobject_to'
1671
            ),
1672
            $query->expr->lAnd(
1673
                $query->expr->eq(
1674
                    $this->dbHandler->quoteColumn('to_contentobject_id', 'ezcontentobject_link'),
1675
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject_to')
1676
                ),
1677
                $query->expr->eq(
1678
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject_to'),
1679
                    $query->bindValue(1, null, \PDO::PARAM_INT)
1680
                )
1681
            )
1682
        )->where(
1683
            $query->expr->eq(
1684
                $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link'),
1685
                $query->bindValue($contentId, null, \PDO::PARAM_INT)
1686
            )
1687
        );
1688
1689
        // source version number
1690
        if (isset($contentVersionNo)) {
1691
            $query->where(
1692
                $query->expr->eq(
1693
                    $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link'),
1694
                    $query->bindValue($contentVersionNo, null, \PDO::PARAM_INT)
1695
                )
1696
            );
1697
        } else { // from published version only
1698
            $query->from(
1699
                $this->dbHandler->quoteTable('ezcontentobject')
1700
            )->where(
1701
                $query->expr->lAnd(
1702
                    $query->expr->eq(
1703
                        $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
1704
                        $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link')
1705
                    ),
1706
                    $query->expr->eq(
1707
                        $this->dbHandler->quoteColumn('current_version', 'ezcontentobject'),
1708
                        $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link')
1709
                    )
1710
                )
1711
            );
1712
        }
1713
1714
        // relation type
1715 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...
1716
            $query->where(
1717
                $query->expr->gt(
1718
                    $query->expr->bitAnd(
1719
                        $this->dbHandler->quoteColumn('relation_type', 'ezcontentobject_link'),
1720
                        $query->bindValue($relationType, null, \PDO::PARAM_INT)
1721
                    ),
1722
                    0
1723
                )
1724
            );
1725
        }
1726
1727
        $statement = $query->prepare();
1728
        $statement->execute();
1729
1730
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
1731
    }
1732
1733
    /**
1734
     * Loads data that related to $toContentId.
1735
     *
1736
     * @param int $toContentId
1737
     * @param int $relationType
1738
     *
1739
     * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()}
1740
     */
1741
    public function loadReverseRelations($toContentId, $relationType = null)
1742
    {
1743
        $query = $this->queryBuilder->createRelationFindQuery();
1744
        $query->where(
1745
            $query->expr->eq(
1746
                $this->dbHandler->quoteColumn('to_contentobject_id', 'ezcontentobject_link'),
1747
                $query->bindValue($toContentId, null, \PDO::PARAM_INT)
1748
            )
1749
        );
1750
1751
        // ezcontentobject join
1752
        $query->from(
1753
            $this->dbHandler->quoteTable('ezcontentobject')
1754
        )->where(
1755
            $query->expr->lAnd(
1756
                $query->expr->eq(
1757
                    $this->dbHandler->quoteColumn('id', 'ezcontentobject'),
1758
                    $this->dbHandler->quoteColumn('from_contentobject_id', 'ezcontentobject_link')
1759
                ),
1760
                $query->expr->eq(
1761
                    $this->dbHandler->quoteColumn('current_version', 'ezcontentobject'),
1762
                    $this->dbHandler->quoteColumn('from_contentobject_version', 'ezcontentobject_link')
1763
                ),
1764
                $query->expr->eq(
1765
                    $this->dbHandler->quoteColumn('status', 'ezcontentobject'),
1766
                    $query->bindValue(1, null, \PDO::PARAM_INT)
1767
                )
1768
            )
1769
        );
1770
1771
        // relation type
1772 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...
1773
            $query->where(
1774
                $query->expr->gt(
1775
                    $query->expr->bitAnd(
1776
                        $this->dbHandler->quoteColumn('relation_type', 'ezcontentobject_link'),
1777
                        $query->bindValue($relationType, null, \PDO::PARAM_INT)
1778
                    ),
1779
                    0
1780
                )
1781
            );
1782
        }
1783
1784
        $statement = $query->prepare();
1785
1786
        $statement->execute();
1787
1788
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
1789
    }
1790
1791
    /**
1792
     * Inserts a new relation database record.
1793
     *
1794
     * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct
1795
     *
1796
     * @return int ID the inserted ID
1797
     */
1798 View Code Duplication
    public function insertRelation(RelationCreateStruct $createStruct)
1799
    {
1800
        $q = $this->dbHandler->createInsertQuery();
1801
        $q->insertInto(
1802
            $this->dbHandler->quoteTable('ezcontentobject_link')
1803
        )->set(
1804
            $this->dbHandler->quoteColumn('id'),
1805
            $this->dbHandler->getAutoIncrementValue('ezcontentobject_link', 'id')
1806
        )->set(
1807
            $this->dbHandler->quoteColumn('contentclassattribute_id'),
1808
            $q->bindValue((int)$createStruct->sourceFieldDefinitionId, null, \PDO::PARAM_INT)
1809
        )->set(
1810
            $this->dbHandler->quoteColumn('from_contentobject_id'),
1811
            $q->bindValue($createStruct->sourceContentId, null, \PDO::PARAM_INT)
1812
        )->set(
1813
            $this->dbHandler->quoteColumn('from_contentobject_version'),
1814
            $q->bindValue($createStruct->sourceContentVersionNo, null, \PDO::PARAM_INT)
1815
        )->set(
1816
            $this->dbHandler->quoteColumn('relation_type'),
1817
            $q->bindValue($createStruct->type, null, \PDO::PARAM_INT)
1818
        )->set(
1819
            $this->dbHandler->quoteColumn('to_contentobject_id'),
1820
            $q->bindValue($createStruct->destinationContentId, null, \PDO::PARAM_INT)
1821
        );
1822
1823
        $q->prepare()->execute();
1824
1825
        return $this->dbHandler->lastInsertId(
1826
            $this->dbHandler->getSequenceName('ezcontentobject_link', 'id')
1827
        );
1828
    }
1829
1830
    /**
1831
     * Deletes the relation with the given $relationId.
1832
     *
1833
     * @param int $relationId
1834
     * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON,
1835
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::EMBED,
1836
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::LINK,
1837
     *                 \eZ\Publish\API\Repository\Values\Content\Relation::FIELD}
1838
     */
1839
    public function deleteRelation($relationId, $type)
1840
    {
1841
        // Legacy Storage stores COMMON, LINK and EMBED types using bitmask, therefore first load
1842
        // existing relation type by given $relationId for comparison
1843
        /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
1844
        $query = $this->dbHandler->createSelectQuery();
1845
        $query->select(
1846
            $this->dbHandler->quoteColumn('relation_type')
1847
        )->from(
1848
            $this->dbHandler->quoteTable('ezcontentobject_link')
1849
        )->where(
1850
            $query->expr->eq(
1851
                $this->dbHandler->quoteColumn('id'),
1852
                $query->bindValue($relationId, null, \PDO::PARAM_INT)
1853
            )
1854
        );
1855
1856
        $statement = $query->prepare();
1857
        $statement->execute();
1858
        $loadedRelationType = $statement->fetchColumn();
1859
1860
        if (!$loadedRelationType) {
1861
            return;
1862
        }
1863
1864
        // If relation type matches then delete
1865
        if ($loadedRelationType == $type) {
1866
            /** @var $query \eZ\Publish\Core\Persistence\Database\DeleteQuery */
1867
            $query = $this->dbHandler->createDeleteQuery();
1868
            $query->deleteFrom(
1869
                'ezcontentobject_link'
1870
            )->where(
1871
                $query->expr->eq(
1872
                    $this->dbHandler->quoteColumn('id'),
1873
                    $query->bindValue($relationId, null, \PDO::PARAM_INT)
1874
                )
1875
            );
1876
1877
            $query->prepare()->execute();
1878
        } elseif ($loadedRelationType & $type) { // If relation type is composite update bitmask
1879
            /** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
1880
            $query = $this->dbHandler->createUpdateQuery();
1881
            $query->update(
1882
                $this->dbHandler->quoteTable('ezcontentobject_link')
1883
            )->set(
1884
                $this->dbHandler->quoteColumn('relation_type'),
1885
                $query->expr->bitAnd(
1886
                    $this->dbHandler->quoteColumn('relation_type'),
1887
                    $query->bindValue(~$type, null, \PDO::PARAM_INT)
1888
                )
1889
            )->where(
1890
                $query->expr->eq(
1891
                    $this->dbHandler->quoteColumn('id'),
1892
                    $query->bindValue($relationId, null, \PDO::PARAM_INT)
1893
                )
1894
            );
1895
1896
            $query->prepare()->execute();
1897
        } else {
1898
            // No match, do nothing
1899
        }
1900
    }
1901
1902
    /**
1903
     * Returns all Content IDs for a given $contentTypeId.
1904
     *
1905
     * @param int $contentTypeId
1906
     *
1907
     * @return int[]
1908
     */
1909
    public function getContentIdsByContentTypeId($contentTypeId)
1910
    {
1911
        $query = $this->dbHandler->createSelectQuery();
1912
        $query
1913
            ->select($this->dbHandler->quoteColumn('id'))
1914
            ->from($this->dbHandler->quoteTable('ezcontentobject'))
1915
            ->where(
1916
                $query->expr->eq(
1917
                    $this->dbHandler->quoteColumn('contentclass_id'),
1918
                    $query->bindValue($contentTypeId, null, PDO::PARAM_INT)
1919
                )
1920
            );
1921
1922
        $statement = $query->prepare();
1923
        $statement->execute();
1924
1925
        return $statement->fetchAll(PDO::FETCH_COLUMN);
1926
    }
1927
1928
    /**
1929
     * Load name data for set of content id's and corresponding version number.
1930
     *
1931
     * @param array[] $rows array of hashes with 'id' and 'version' to load names for
1932
     *
1933
     * @return array
1934
     */
1935
    public function loadVersionedNameData($rows)
1936
    {
1937
        $query = $this->queryBuilder->createNamesQuery();
1938
        $conditions = array();
1939
        foreach ($rows as $row) {
1940
            $conditions[] = $query->expr->lAnd(
1941
                $query->expr->eq(
1942
                    $this->dbHandler->quoteColumn('contentobject_id'),
1943
                    $query->bindValue($row['id'], null, \PDO::PARAM_INT)
1944
                ),
1945
                $query->expr->eq(
1946
                    $this->dbHandler->quoteColumn('content_version'),
1947
                    $query->bindValue($row['version'], null, \PDO::PARAM_INT)
1948
                )
1949
            );
1950
        }
1951
1952
        $query->where($query->expr->lOr($conditions));
1953
        $stmt = $query->prepare();
1954
        $stmt->execute();
1955
1956
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
1957
    }
1958
1959
    /**
1960
     * Batch method for copying all relation meta data for copied Content object.
1961
     *
1962
     * {@inheritdoc}
1963
     *
1964
     * @param int $originalContentId
1965
     * @param int $copiedContentId
1966
     * @param int|null $versionNo If specified only copy for a given version number, otherwise all.
1967
     */
1968
    public function copyRelations($originalContentId, $copiedContentId, $versionNo = null)
1969
    {
1970
        // Given we can retain all columns, we just create copies with new `from_contentobject_id` using INSERT INTO SELECT
1971
        $sql = 'INSERT INTO ezcontentobject_link ( contentclassattribute_id, from_contentobject_id, from_contentobject_version, relation_type, to_contentobject_id )
1972
                SELECT  L2.contentclassattribute_id, :copied_id, L2.from_contentobject_version, L2.relation_type, L2.to_contentobject_id
1973
                FROM    ezcontentobject_link AS L2
1974
                WHERE   L2.from_contentobject_id = :original_id';
1975
1976
        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...
1977
            $stmt = $this->connection->prepare($sql . ' AND L2.from_contentobject_version = :version');
1978
            $stmt->bindValue('version', $versionNo, PDO::PARAM_INT);
1979
        } else {
1980
            $stmt = $this->connection->prepare($sql);
1981
        }
1982
1983
        $stmt->bindValue('original_id', $originalContentId, PDO::PARAM_INT);
1984
        $stmt->bindValue('copied_id', $copiedContentId, PDO::PARAM_INT);
1985
1986
        $stmt->execute();
1987
    }
1988
}
1989