forked from ezsystems/ezpublish-kernel
/
DoctrineDatabase.php
1269 lines (1177 loc) · 44.1 KB
/
DoctrineDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* File containing the DoctrineDatabase UrlAlias Gateway class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway;
use Doctrine\DBAL\Connection;
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway;
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator as LanguageMaskGenerator;
use eZ\Publish\Core\Persistence\Database\Query;
use eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Language;
use RuntimeException;
/**
* UrlAlias Gateway.
*/
class DoctrineDatabase extends Gateway
{
/**
* 2^30, since PHP_INT_MAX can cause overflows in DB systems, if PHP is run
* on 64 bit systems.
*/
const MAX_LIMIT = 1073741824;
/**
* Columns of database tables.
*
* @var array
*
* @todo remove after testing
*/
protected $columns = array(
'ezurlalias_ml' => array(
'action',
'action_type',
'alias_redirects',
'id',
'is_alias',
'is_original',
'lang_mask',
'link',
'parent',
'text',
'text_md5',
),
);
/**
* Doctrine database handler.
*
* @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler
*/
protected $dbHandler;
/**
* Language mask generator.
*
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator
*/
protected $languageMaskGenerator;
/**
* Main URL database table name.
*
* @var string
*/
protected $table;
/**
* Creates a new DoctrineDatabase UrlAlias Gateway.
*
* @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $dbHandler
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $languageMaskGenerator
*/
public function __construct(
DatabaseHandler $dbHandler,
LanguageMaskGenerator $languageMaskGenerator
) {
$this->dbHandler = $dbHandler;
$this->languageMaskGenerator = $languageMaskGenerator;
$this->table = static::TABLE;
}
public function setTable($name)
{
$this->table = $name;
}
/**
* Loads list of aliases by given $locationId.
*
* @param mixed $locationId
* @param bool $custom
* @param mixed $languageId
*
* @return array
*/
public function loadLocationEntries($locationId, $custom = false, $languageId = false)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
$this->dbHandler->quoteColumn('id'),
$this->dbHandler->quoteColumn('link'),
$this->dbHandler->quoteColumn('is_alias'),
$this->dbHandler->quoteColumn('alias_redirects'),
$this->dbHandler->quoteColumn('lang_mask'),
$this->dbHandler->quoteColumn('is_original'),
$this->dbHandler->quoteColumn('parent'),
$this->dbHandler->quoteColumn('text'),
$this->dbHandler->quoteColumn('text_md5'),
$this->dbHandler->quoteColumn('action')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('action'),
$query->bindValue("eznode:{$locationId}", null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(1, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_alias'),
$query->bindValue(
$custom ? 1 : 0,
null,
\PDO::PARAM_INT
)
)
)
);
if ($languageId !== false) {
$query->where(
$query->expr->gt(
$query->expr->bitAnd(
$this->dbHandler->quoteColumn('lang_mask'),
$query->bindValue($languageId, null, \PDO::PARAM_INT)
),
0
)
);
}
$statement = $query->prepare();
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* Loads paged list of global aliases.
*
* @param string|null $languageCode
* @param int $offset
* @param int $limit
*
* @return array
*/
public function listGlobalEntries($languageCode = null, $offset = 0, $limit = -1)
{
$limit = $limit === -1 ? self::MAX_LIMIT : $limit;
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
$this->dbHandler->quoteColumn('action'),
$this->dbHandler->quoteColumn('id'),
$this->dbHandler->quoteColumn('link'),
$this->dbHandler->quoteColumn('is_alias'),
$this->dbHandler->quoteColumn('alias_redirects'),
$this->dbHandler->quoteColumn('lang_mask'),
$this->dbHandler->quoteColumn('is_original'),
$this->dbHandler->quoteColumn('parent'),
$this->dbHandler->quoteColumn('text_md5')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('action_type'),
$query->bindValue('module', null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(1, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_alias'),
$query->bindValue(1, null, \PDO::PARAM_INT)
)
)
)->limit(
$limit,
$offset
);
if (isset($languageCode)) {
$query->where(
$query->expr->gt(
$query->expr->bitAnd(
$this->dbHandler->quoteColumn('lang_mask'),
$query->bindValue(
$this->languageMaskGenerator->generateLanguageIndicator($languageCode, false),
null,
\PDO::PARAM_INT
)
),
0
)
);
}
$statement = $query->prepare();
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* Returns boolean indicating if the row with given $id is special root entry.
*
* Special root entry entry will have parentId=0 and text=''.
* In standard installation this entry will point to location with id=2.
*
* @param mixed $id
*
* @return bool
*/
public function isRootEntry($id)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
$this->dbHandler->quoteColumn('text'),
$this->dbHandler->quoteColumn('parent')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->eq(
$this->dbHandler->quoteColumn('id'),
$query->bindValue($id, null, \PDO::PARAM_INT)
)
);
$statement = $query->prepare();
$statement->execute();
$row = $statement->fetch(\PDO::FETCH_ASSOC);
return strlen($row['text']) == 0 && $row['parent'] == 0;
}
/**
* Downgrades autogenerated entry matched by given $action and $languageId and negatively matched by
* composite primary key.
*
* If language mask of the found entry is composite (meaning it consists of multiple language ids) given
* $languageId will be removed from mask. Otherwise entry will be marked as history.
*
* @param string $action
* @param mixed $languageId
* @param mixed $newId
* @param mixed $parentId
* @param string $textMD5
*/
public function cleanupAfterPublish($action, $languageId, $newId, $parentId, $textMD5)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
$this->dbHandler->quoteColumn('parent'),
$this->dbHandler->quoteColumn('text_md5'),
$this->dbHandler->quoteColumn('lang_mask')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lAnd(
// 1) Autogenerated aliases that match action and language...
$query->expr->eq(
$this->dbHandler->quoteColumn('action'),
$query->bindValue($action, null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(1, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_alias'),
$query->bindValue(0, null, \PDO::PARAM_INT)
),
$query->expr->gt(
$query->expr->bitAnd(
$this->dbHandler->quoteColumn('lang_mask'),
$query->bindValue($languageId, null, \PDO::PARAM_INT)
),
0
),
// 2) ...but not newly published entry
$query->expr->not(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($parentId, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('text_md5'),
$query->bindValue($textMD5, null, \PDO::PARAM_STR)
)
)
)
)
);
$statement = $query->prepare();
$statement->execute();
$row = $statement->fetch(\PDO::FETCH_ASSOC);
if (!empty($row)) {
$this->archiveUrlAliasForDeletedTranslation($row['lang_mask'], $languageId, $row['parent'], $row['text_md5'], $newId);
}
}
/**
* Archive (remove or historize) obsolete URL aliases (for translations that were removed).
*
* @param int $languageMask all languages bit mask
* @param int $languageId removed language Id
* @param int $parent
* @param string $textMD5 checksum
* @param $linkId
*/
private function archiveUrlAliasForDeletedTranslation($languageMask, $languageId, $parent, $textMD5, $linkId)
{
// If language mask is composite (consists of multiple languages) then remove given language from entry
if ($languageMask & ~($languageId | 1)) {
$this->removeTranslation($parent, $textMD5, $languageId);
} else {
// Otherwise mark entry as history
$this->historize($parent, $textMD5, $linkId);
}
}
public function historizeBeforeSwap($action, $languageMask)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
$query = $this->dbHandler->createUpdateQuery();
$query->update(
$this->dbHandler->quoteTable($this->table)
)->set(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(0, null, \PDO::PARAM_INT)
)->set(
$this->dbHandler->quoteColumn('id'),
$query->bindValue(
$this->getNextId(),
null,
\PDO::PARAM_INT
)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('action'),
$query->bindValue($action, null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(1, null, \PDO::PARAM_INT)
),
$query->expr->gt(
$query->expr->bitAnd(
$this->dbHandler->quoteColumn('lang_mask'),
$query->bindValue($languageMask & ~1, null, \PDO::PARAM_INT)
),
0
)
)
);
$query->prepare()->execute();
}
/**
* Updates single row matched by composite primary key.
*
* Sets "is_original" to 0 thus marking entry as history.
*
* Re-links history entries.
*
* When location alias is published we need to check for new history entries created with self::downgrade()
* with the same action and language, update their "link" column with id of the published entry.
* History entry "id" column is moved to next id value so that all active (non-history) entries are kept
* under the same id.
*
* @param int $parentId
* @param string $textMD5
* @param int $newId
*/
protected function historize($parentId, $textMD5, $newId)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
$query = $this->dbHandler->createUpdateQuery();
$query->update(
$this->dbHandler->quoteTable($this->table)
)->set(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(0, null, \PDO::PARAM_INT)
)->set(
$this->dbHandler->quoteColumn('link'),
$query->bindValue($newId, null, \PDO::PARAM_INT)
)->set(
$this->dbHandler->quoteColumn('id'),
$query->bindValue(
$this->getNextId(),
null,
\PDO::PARAM_INT
)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($parentId, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('text_md5'),
$query->bindValue($textMD5, null, \PDO::PARAM_STR)
)
)
);
$query->prepare()->execute();
}
/**
* Updates single row data matched by composite primary key.
*
* Removes given $languageId from entry's language mask
*
* @param mixed $parentId
* @param string $textMD5
* @param mixed $languageId
*/
protected function removeTranslation($parentId, $textMD5, $languageId)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
$query = $this->dbHandler->createUpdateQuery();
$query->update(
$this->dbHandler->quoteTable($this->table)
)->set(
$this->dbHandler->quoteColumn('lang_mask'),
$query->expr->bitAnd(
$this->dbHandler->quoteColumn('lang_mask'),
$query->bindValue(~$languageId, null, \PDO::PARAM_INT)
)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($parentId, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('text_md5'),
$query->bindValue($textMD5, null, \PDO::PARAM_STR)
)
)
);
$query->prepare()->execute();
}
/**
* Marks all entries with given $id as history entries.
*
* This method is used by Handler::locationMoved(). Each row is separately historized
* because future publishing needs to be able to take over history entries safely.
*
* @param mixed $id
* @param mixed $link
*/
public function historizeId($id, $link)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
$this->dbHandler->quoteColumn('parent'),
$this->dbHandler->quoteColumn('text_md5')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('is_alias'),
$query->bindValue(0, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(1, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('action_type'),
$query->bindValue('eznode', null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('link'),
$query->bindValue($id, null, \PDO::PARAM_INT)
)
)
);
$statement = $query->prepare();
$statement->execute();
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$this->historize($row['parent'], $row['text_md5'], $link);
}
}
/**
* Updates parent id of autogenerated entries.
*
* Update includes history entries.
*
* @param mixed $oldParentId
* @param mixed $newParentId
*/
public function reparent($oldParentId, $newParentId)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
$query = $this->dbHandler->createUpdateQuery();
$query->update(
$this->dbHandler->quoteTable($this->table)
)->set(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($newParentId, null, \PDO::PARAM_INT)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('is_alias'),
$query->bindValue(0, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($oldParentId, null, \PDO::PARAM_INT)
)
)
);
$query->prepare()->execute();
}
/**
* Updates single row data matched by composite primary key.
*
* Use optional parameter $languageMaskMatch to additionally limit the query match with languages.
*
* @param mixed $parentId
* @param string $textMD5
* @param array $values associative array with column names as keys and column values as values
*/
public function updateRow($parentId, $textMD5, array $values)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */
$query = $this->dbHandler->createUpdateQuery();
$query->update($this->dbHandler->quoteTable($this->table));
$this->setQueryValues($query, $values);
$query->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($parentId, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('text_md5'),
$query->bindValue($textMD5, null, \PDO::PARAM_STR)
)
)
);
$query->prepare()->execute();
}
/**
* Inserts new row in urlalias_ml table.
*
* @param array $values
*
* @return mixed
*/
public function insertRow(array $values)
{
// @todo remove after testing
if (
!isset($values['text']) ||
!isset($values['text_md5']) ||
!isset($values['action']) ||
!isset($values['parent']) ||
!isset($values['lang_mask'])) {
throw new \Exception('value set is incomplete: ' . var_export($values, true) . ", can't execute insert");
}
if (!isset($values['id'])) {
$values['id'] = $this->getNextId();
}
if (!isset($values['link'])) {
$values['link'] = $values['id'];
}
if (!isset($values['is_original'])) {
$values['is_original'] = ($values['id'] == $values['link'] ? 1 : 0);
}
if (!isset($values['is_alias'])) {
$values['is_alias'] = 0;
}
if (!isset($values['alias_redirects'])) {
$values['alias_redirects'] = 0;
}
if (!isset($values['action_type'])) {
if (preg_match('#^(.+):.*#', $values['action'], $matches)) {
$values['action_type'] = $matches[1];
}
}
if ($values['is_alias']) {
$values['is_original'] = 1;
}
if ($values['action'] === 'nop:') {
$values['is_original'] = 0;
}
/** @var $query \eZ\Publish\Core\Persistence\Database\InsertQuery */
$query = $this->dbHandler->createInsertQuery();
$query->insertInto($this->dbHandler->quoteTable($this->table));
$this->setQueryValues($query, $values);
$query->prepare()->execute();
return $values['id'];
}
/**
* Sets value for insert or update query.
*
* @param \eZ\Publish\Core\Persistence\Database\Query|\eZ\Publish\Core\Persistence\Database\InsertQuery|\eZ\Publish\Core\Persistence\Database\UpdateQuery $query
* @param array $values
*
* @throws \Exception
*/
protected function setQueryValues(Query $query, $values)
{
foreach ($values as $column => $value) {
// @todo remove after testing
if (!in_array($column, $this->columns['ezurlalias_ml'])) {
throw new \Exception("unknown column '$column' for table 'ezurlalias_ml'");
}
switch ($column) {
case 'text':
case 'action':
case 'text_md5':
case 'action_type':
$pdoDataType = \PDO::PARAM_STR;
break;
default:
$pdoDataType = \PDO::PARAM_INT;
}
$query->set(
$this->dbHandler->quoteColumn($column),
$query->bindValue($value, null, $pdoDataType)
);
}
}
/**
* Returns next value for "id" column.
*
* @return mixed
*/
public function getNextId()
{
$sequence = $this->dbHandler->getSequenceName('ezurlalias_ml_incr', 'id');
/** @var $query \eZ\Publish\Core\Persistence\Database\InsertQuery */
$query = $this->dbHandler->createInsertQuery();
$query->insertInto(
$this->dbHandler->quoteTable('ezurlalias_ml_incr')
);
// ezcDatabase does not abstract the "auto increment id"
// INSERT INTO ezurlalias_ml_incr VALUES(DEFAULT) is not an option due
// to this mysql bug: http://bugs.mysql.com/bug.php?id=42270
// as a result we are forced to check which database is currently used
// to generate the correct SQL query
// see https://jira.ez.no/browse/EZP-20652
if ($this->dbHandler->useSequences()) {
$query->set(
$this->dbHandler->quoteColumn('id'),
"nextval('{$sequence}')"
);
} else {
$query->set(
$this->dbHandler->quoteColumn('id'),
$query->bindValue(null, null, \PDO::PARAM_NULL)
);
}
$query->prepare()->execute();
return $this->dbHandler->lastInsertId($sequence);
}
/**
* Loads single row data matched by composite primary key.
*
* @param mixed $parentId
* @param string $textMD5
*
* @return array
*/
public function loadRow($parentId, $textMD5)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select('*')->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($parentId, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('text_md5'),
$query->bindValue($textMD5, null, \PDO::PARAM_STR)
)
)
);
$statement = $query->prepare();
$statement->execute();
return $statement->fetch(\PDO::FETCH_ASSOC);
}
/**
* Loads complete URL alias data by given array of path hashes.
*
* @param string[] $urlHashes URL string hashes
*
* @return array
*/
public function loadUrlAliasData(array $urlHashes)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$count = count($urlHashes);
foreach ($urlHashes as $level => $urlPartHash) {
$tableName = $this->table . ($level === $count - 1 ? '' : $level);
if ($level === $count - 1) {
$query->select(
$this->dbHandler->quoteColumn('id', $tableName),
$this->dbHandler->quoteColumn('link', $tableName),
$this->dbHandler->quoteColumn('is_alias', $tableName),
$this->dbHandler->quoteColumn('alias_redirects', $tableName),
$this->dbHandler->quoteColumn('is_original', $tableName),
$this->dbHandler->quoteColumn('action', $tableName),
$this->dbHandler->quoteColumn('action_type', $tableName),
$this->dbHandler->quoteColumn('lang_mask', $tableName),
$this->dbHandler->quoteColumn('text', $tableName),
$this->dbHandler->quoteColumn('parent', $tableName),
$this->dbHandler->quoteColumn('text_md5', $tableName)
)->from(
$this->dbHandler->quoteTable($this->table)
);
} else {
$query->select(
$this->dbHandler->aliasedColumn($query, 'id', $tableName),
$this->dbHandler->aliasedColumn($query, 'link', $tableName),
$this->dbHandler->aliasedColumn($query, 'is_alias', $tableName),
$this->dbHandler->aliasedColumn($query, 'alias_redirects', $tableName),
$this->dbHandler->aliasedColumn($query, 'is_original', $tableName),
$this->dbHandler->aliasedColumn($query, 'action', $tableName),
$this->dbHandler->aliasedColumn($query, 'action_type', $tableName),
$this->dbHandler->aliasedColumn($query, 'lang_mask', $tableName),
$this->dbHandler->aliasedColumn($query, 'text', $tableName),
$this->dbHandler->aliasedColumn($query, 'parent', $tableName),
$this->dbHandler->aliasedColumn($query, 'text_md5', $tableName)
)->from(
$query->alias($this->table, $tableName)
);
}
$query->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('text_md5', $tableName),
$query->bindValue($urlPartHash, null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('parent', $tableName),
// root entry has parent column set to 0
isset($previousTableName) ? $this->dbHandler->quoteColumn('link', $previousTableName) : $query->bindValue(0, null, \PDO::PARAM_INT)
)
)
);
$previousTableName = $tableName;
}
$query->limit(1);
$statement = $query->prepare();
$statement->execute();
return $statement->fetch(\PDO::FETCH_ASSOC);
}
/**
* Loads autogenerated entry id by given $action and optionally $parentId.
*
* @param string $action
* @param mixed|null $parentId
*
* @return array
*/
public function loadAutogeneratedEntry($action, $parentId = null)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
'*'
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('action'),
$query->bindValue($action, null, \PDO::PARAM_STR)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_original'),
$query->bindValue(1, null, \PDO::PARAM_INT)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('is_alias'),
$query->bindValue(0, null, \PDO::PARAM_INT)
)
)
);
if (isset($parentId)) {
$query->where(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue($parentId, null, \PDO::PARAM_INT)
)
);
}
$statement = $query->prepare();
$statement->execute();
return $statement->fetch(\PDO::FETCH_ASSOC);
}
/**
* Loads all data for the path identified by given $id.
*
* @throws \RuntimeException
*
* @param mixed $id
*
* @return array
*/
public function loadPathData($id)
{
$pathData = array();
while ($id != 0) {
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$query->select(
$this->dbHandler->quoteColumn('parent'),
$this->dbHandler->quoteColumn('lang_mask'),
$this->dbHandler->quoteColumn('text')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->eq(
$this->dbHandler->quoteColumn('id'),
$query->bindValue($id, null, \PDO::PARAM_INT)
)
);
$statement = $query->prepare();
$statement->execute();
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);
if (empty($rows)) {
// Normally this should never happen
// @todo remove throw when tested
$pathDataArray = [];
foreach ($pathData as $path) {
$pathDataArray[] = $path[0]['text'];
}
$path = implode('/', $pathDataArray);
throw new \RuntimeException("Path ({$path}...) is broken, last id is '{$id}': " . __METHOD__);
}
$id = $rows[0]['parent'];
array_unshift($pathData, $rows);
}
return $pathData;
}
/**
* Loads path data identified by given ordered array of hierarchy data.
*
* The first entry in $hierarchyData corresponds to the top-most path element in the path, the second entry the
* child of the first path element and so on.
* This method is faster than self::getPath() since it can fetch all elements using only one query, but can be used
* only for autogenerated paths.
*
* @param array $hierarchyData
*
* @return array
*/
public function loadPathDataByHierarchy(array $hierarchyData)
{
/** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */
$query = $this->dbHandler->createSelectQuery();
$hierarchyConditions = array();
foreach ($hierarchyData as $levelData) {
$hierarchyConditions[] = $query->expr->lAnd(
$query->expr->eq(
$this->dbHandler->quoteColumn('parent'),
$query->bindValue(
$levelData['parent'],
null,
\PDO::PARAM_INT
)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('action'),
$query->bindValue(
$levelData['action'],
null,
\PDO::PARAM_STR
)
),
$query->expr->eq(
$this->dbHandler->quoteColumn('id'),
$query->bindValue(
$levelData['id'],
null,
\PDO::PARAM_INT
)
)
);
}
$query->select(
$this->dbHandler->quoteColumn('action'),
$this->dbHandler->quoteColumn('lang_mask'),
$this->dbHandler->quoteColumn('text')
)->from(
$this->dbHandler->quoteTable($this->table)
)->where(
$query->expr->lOr($hierarchyConditions)
);
$statement = $query->prepare();
$statement->execute();
$rows = $statement->fetchAll(\PDO::FETCH_ASSOC);
$rowsMap = array();
foreach ($rows as $row) {
$rowsMap[$row['action']][] = $row;
}
if (count($rowsMap) !== count($hierarchyData)) {
throw new \RuntimeException('The path is corrupted.');
}
$data = array();
foreach ($hierarchyData as $levelData) {
$data[] = $rowsMap[$levelData['action']];
}
return $data;
}
/**
* Deletes single custom alias row matched by composite primary key.
*
* @param mixed $parentId
* @param string $textMD5
*
* @return bool
*/
public function removeCustomAlias($parentId, $textMD5)
{