Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DoctrineDatabase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DoctrineDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class DoctrineDatabase extends Gateway |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * eZ Doctrine database handler. |
||
| 47 | * |
||
| 48 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
| 49 | * @deprecated Start to use DBAL $connection instead. |
||
| 50 | */ |
||
| 51 | protected $dbHandler; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The native Doctrine connection. |
||
| 55 | * |
||
| 56 | * Meant to be used to transition from eZ/Zeta interface to Doctrine. |
||
| 57 | * |
||
| 58 | * @var \Doctrine\DBAL\Connection |
||
| 59 | */ |
||
| 60 | protected $connection; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Query builder. |
||
| 64 | * |
||
| 65 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder |
||
| 66 | */ |
||
| 67 | protected $queryBuilder; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Caching language handler. |
||
| 71 | * |
||
| 72 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
||
| 73 | */ |
||
| 74 | protected $languageHandler; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Language mask generator. |
||
| 78 | * |
||
| 79 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator |
||
| 80 | */ |
||
| 81 | protected $languageMaskGenerator; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Creates a new gateway based on $db. |
||
| 85 | * |
||
| 86 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $db |
||
| 87 | * @param \Doctrine\DBAL\Connection $connection |
||
| 88 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway\DoctrineDatabase\QueryBuilder $queryBuilder |
||
| 89 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 90 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $languageMaskGenerator |
||
| 91 | */ |
||
| 92 | public function __construct( |
||
| 93 | DatabaseHandler $db, |
||
| 94 | Connection $connection, |
||
| 95 | QueryBuilder $queryBuilder, |
||
| 96 | LanguageHandler $languageHandler, |
||
| 97 | LanguageMaskGenerator $languageMaskGenerator |
||
| 98 | ) { |
||
| 99 | $this->dbHandler = $db; |
||
| 100 | $this->connection = $connection; |
||
| 101 | $this->queryBuilder = $queryBuilder; |
||
| 102 | $this->languageHandler = $languageHandler; |
||
|
|
|||
| 103 | $this->languageMaskGenerator = $languageMaskGenerator; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get context definition for external storage layers. |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function getContext() |
||
| 112 | { |
||
| 113 | return [ |
||
| 114 | 'identifier' => 'LegacyStorage', |
||
| 115 | 'connection' => $this->dbHandler, |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Inserts a new content object. |
||
| 121 | * |
||
| 122 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
| 123 | * @param mixed $currentVersionNo |
||
| 124 | * |
||
| 125 | * @return int ID |
||
| 126 | */ |
||
| 127 | public function insertContentObject(CreateStruct $struct, $currentVersionNo = 1) |
||
| 128 | { |
||
| 129 | $initialLanguageId = !empty($struct->mainLanguageId) ? $struct->mainLanguageId : $struct->initialLanguageId; |
||
| 130 | $initialLanguageCode = $this->languageHandler->load($initialLanguageId)->languageCode; |
||
| 131 | |||
| 132 | if (isset($struct->name[$initialLanguageCode])) { |
||
| 133 | $name = $struct->name[$initialLanguageCode]; |
||
| 134 | } else { |
||
| 135 | $name = ''; |
||
| 136 | } |
||
| 137 | |||
| 138 | $q = $this->dbHandler->createInsertQuery(); |
||
| 139 | $q->insertInto( |
||
| 140 | $this->dbHandler->quoteTable('ezcontentobject') |
||
| 141 | )->set( |
||
| 142 | $this->dbHandler->quoteColumn('id'), |
||
| 143 | $this->dbHandler->getAutoIncrementValue('ezcontentobject', 'id') |
||
| 144 | )->set( |
||
| 145 | $this->dbHandler->quoteColumn('current_version'), |
||
| 146 | $q->bindValue($currentVersionNo, null, \PDO::PARAM_INT) |
||
| 147 | )->set( |
||
| 148 | $this->dbHandler->quoteColumn('name'), |
||
| 149 | $q->bindValue($name, null, \PDO::PARAM_STR) |
||
| 150 | )->set( |
||
| 151 | $this->dbHandler->quoteColumn('contentclass_id'), |
||
| 152 | $q->bindValue($struct->typeId, null, \PDO::PARAM_INT) |
||
| 153 | )->set( |
||
| 154 | $this->dbHandler->quoteColumn('section_id'), |
||
| 155 | $q->bindValue($struct->sectionId, null, \PDO::PARAM_INT) |
||
| 156 | )->set( |
||
| 157 | $this->dbHandler->quoteColumn('owner_id'), |
||
| 158 | $q->bindValue($struct->ownerId, null, \PDO::PARAM_INT) |
||
| 159 | )->set( |
||
| 160 | $this->dbHandler->quoteColumn('initial_language_id'), |
||
| 161 | $q->bindValue($initialLanguageId, null, \PDO::PARAM_INT) |
||
| 162 | )->set( |
||
| 163 | $this->dbHandler->quoteColumn('remote_id'), |
||
| 164 | $q->bindValue($struct->remoteId, null, \PDO::PARAM_STR) |
||
| 165 | )->set( |
||
| 166 | $this->dbHandler->quoteColumn('modified'), |
||
| 167 | $q->bindValue(0, null, \PDO::PARAM_INT) |
||
| 168 | )->set( |
||
| 169 | $this->dbHandler->quoteColumn('published'), |
||
| 170 | $q->bindValue(0, null, \PDO::PARAM_INT) |
||
| 171 | )->set( |
||
| 172 | $this->dbHandler->quoteColumn('status'), |
||
| 173 | $q->bindValue(ContentInfo::STATUS_DRAFT, null, \PDO::PARAM_INT) |
||
| 174 | )->set( |
||
| 175 | $this->dbHandler->quoteColumn('language_mask'), |
||
| 176 | $q->bindValue( |
||
| 177 | $this->generateLanguageMask( |
||
| 178 | $struct->fields, |
||
| 179 | $initialLanguageCode, |
||
| 180 | $struct->alwaysAvailable |
||
| 181 | ), |
||
| 182 | null, |
||
| 183 | \PDO::PARAM_INT |
||
| 184 | ) |
||
| 185 | ); |
||
| 186 | |||
| 187 | $q->prepare()->execute(); |
||
| 188 | |||
| 189 | return $this->dbHandler->lastInsertId( |
||
| 190 | $this->dbHandler->getSequenceName('ezcontentobject', 'id') |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Generates a language mask for $fields. |
||
| 196 | * |
||
| 197 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields |
||
| 198 | * @param string $initialLanguageCode |
||
| 199 | * @param bool $isAlwaysAvailable |
||
| 200 | * |
||
| 201 | * @return int |
||
| 202 | */ |
||
| 203 | protected function generateLanguageMask(array $fields, string $initialLanguageCode, bool $isAlwaysAvailable): int |
||
| 204 | { |
||
| 205 | $languages = [$initialLanguageCode => true]; |
||
| 206 | View Code Duplication | foreach ($fields as $field) { |
|
| 207 | if (isset($languages[$field->languageCode])) { |
||
| 208 | continue; |
||
| 209 | } |
||
| 210 | |||
| 211 | $languages[$field->languageCode] = true; |
||
| 212 | } |
||
| 213 | |||
| 214 | return $this->languageMaskGenerator->generateLanguageMaskFromLanguageCodes(array_keys($languages), $isAlwaysAvailable); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Inserts a new version. |
||
| 219 | * |
||
| 220 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo |
||
| 221 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields |
||
| 222 | * |
||
| 223 | * @return int ID |
||
| 224 | */ |
||
| 225 | public function insertVersion(VersionInfo $versionInfo, array $fields) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Updates an existing content identified by $contentId in respect to $struct. |
||
| 285 | * |
||
| 286 | * @param int $contentId |
||
| 287 | * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $struct |
||
| 288 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $prePublishVersionInfo Provided on publish |
||
| 289 | */ |
||
| 290 | public function updateContent($contentId, MetadataUpdateStruct $struct, VersionInfo $prePublishVersionInfo = null) |
||
| 291 | { |
||
| 292 | $q = $this->dbHandler->createUpdateQuery(); |
||
| 293 | $q->update($this->dbHandler->quoteTable('ezcontentobject')); |
||
| 294 | |||
| 295 | if (isset($struct->name)) { |
||
| 296 | $q->set( |
||
| 297 | $this->dbHandler->quoteColumn('name'), |
||
| 298 | $q->bindValue($struct->name, null, \PDO::PARAM_STR) |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | if (isset($struct->mainLanguageId)) { |
||
| 302 | $q->set( |
||
| 303 | $this->dbHandler->quoteColumn('initial_language_id'), |
||
| 304 | $q->bindValue($struct->mainLanguageId, null, \PDO::PARAM_INT) |
||
| 305 | ); |
||
| 306 | } |
||
| 307 | if (isset($struct->modificationDate)) { |
||
| 308 | $q->set( |
||
| 309 | $this->dbHandler->quoteColumn('modified'), |
||
| 310 | $q->bindValue($struct->modificationDate, null, \PDO::PARAM_INT) |
||
| 311 | ); |
||
| 312 | } |
||
| 313 | if (isset($struct->ownerId)) { |
||
| 314 | $q->set( |
||
| 315 | $this->dbHandler->quoteColumn('owner_id'), |
||
| 316 | $q->bindValue($struct->ownerId, null, \PDO::PARAM_INT) |
||
| 317 | ); |
||
| 318 | } |
||
| 319 | if (isset($struct->publicationDate)) { |
||
| 320 | $q->set( |
||
| 321 | $this->dbHandler->quoteColumn('published'), |
||
| 322 | $q->bindValue($struct->publicationDate, null, \PDO::PARAM_INT) |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | if (isset($struct->remoteId)) { |
||
| 326 | $q->set( |
||
| 327 | $this->dbHandler->quoteColumn('remote_id'), |
||
| 328 | $q->bindValue($struct->remoteId, null, \PDO::PARAM_STR) |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | if ($prePublishVersionInfo !== null) { |
||
| 332 | $mask = $this->languageMaskGenerator->generateLanguageMaskFromLanguageCodes( |
||
| 333 | $prePublishVersionInfo->languageCodes, |
||
| 334 | $struct->alwaysAvailable ?? $prePublishVersionInfo->contentInfo->alwaysAvailable |
||
| 335 | ); |
||
| 336 | |||
| 337 | $q->set( |
||
| 338 | $this->dbHandler->quoteColumn('language_mask'), |
||
| 339 | $q->bindValue($mask, null, \PDO::PARAM_INT) |
||
| 340 | ); |
||
| 341 | } |
||
| 342 | if (isset($struct->isHidden)) { |
||
| 343 | $q->set( |
||
| 344 | $this->dbHandler->quoteColumn('is_hidden'), |
||
| 345 | $q->bindValue($struct->isHidden, null, \PDO::PARAM_BOOL) |
||
| 346 | ); |
||
| 347 | } |
||
| 348 | $q->where( |
||
| 349 | $q->expr->eq( |
||
| 350 | $this->dbHandler->quoteColumn('id'), |
||
| 351 | $q->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 352 | ) |
||
| 353 | ); |
||
| 354 | $q->prepare()->execute(); |
||
| 355 | |||
| 356 | // Handle alwaysAvailable flag update separately as it's a more complex task and has impact on several tables |
||
| 357 | if (isset($struct->alwaysAvailable) || isset($struct->mainLanguageId)) { |
||
| 358 | $this->updateAlwaysAvailableFlag($contentId, $struct->alwaysAvailable); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Updates version $versionNo for content identified by $contentId, in respect to $struct. |
||
| 364 | * |
||
| 365 | * @param int $contentId |
||
| 366 | * @param int $versionNo |
||
| 367 | * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $struct |
||
| 368 | */ |
||
| 369 | public function updateVersion($contentId, $versionNo, UpdateStruct $struct) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Updates "always available" flag for Content identified by $contentId, in respect to |
||
| 414 | * Content's current main language and optionally new $alwaysAvailable state. |
||
| 415 | * |
||
| 416 | * @param int $contentId |
||
| 417 | * @param bool|null $alwaysAvailable New "always available" value or null if not defined |
||
| 418 | */ |
||
| 419 | public function updateAlwaysAvailableFlag($contentId, $alwaysAvailable = null) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Sets the status of the version identified by $contentId and $version to $status. |
||
| 540 | * |
||
| 541 | * The $status can be one of STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED |
||
| 542 | * |
||
| 543 | * @param int $contentId |
||
| 544 | * @param int $version |
||
| 545 | * @param int $status |
||
| 546 | * |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | public function setStatus($contentId, $version, $status) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Inserts a new field. |
||
| 607 | * |
||
| 608 | * Only used when a new field is created (i.e. a new object or a field in a |
||
| 609 | * new language!). After that, field IDs need to stay the same, only the |
||
| 610 | * version number changes. |
||
| 611 | * |
||
| 612 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 613 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
| 614 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
| 615 | * |
||
| 616 | * @return int ID |
||
| 617 | */ |
||
| 618 | public function insertNewField(Content $content, Field $field, StorageFieldValue $value) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Inserts an existing field. |
||
| 639 | * |
||
| 640 | * Used to insert a field with an exsting ID but a new version number. |
||
| 641 | * |
||
| 642 | * @param Content $content |
||
| 643 | * @param Field $field |
||
| 644 | * @param StorageFieldValue $value |
||
| 645 | */ |
||
| 646 | public function insertExistingField(Content $content, Field $field, StorageFieldValue $value) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Sets field (ezcontentobject_attribute) values to the given query. |
||
| 662 | * |
||
| 663 | * @param \eZ\Publish\Core\Persistence\Database\InsertQuery $q |
||
| 664 | * @param Content $content |
||
| 665 | * @param Field $field |
||
| 666 | * @param StorageFieldValue $value |
||
| 667 | */ |
||
| 668 | protected function setInsertFieldValues(InsertQuery $q, Content $content, Field $field, StorageFieldValue $value) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Checks if $languageCode is always available in $content. |
||
| 717 | * |
||
| 718 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 719 | * @param string $languageCode |
||
| 720 | * |
||
| 721 | * @return bool |
||
| 722 | */ |
||
| 723 | protected function isLanguageAlwaysAvailable(Content $content, $languageCode) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Updates an existing field. |
||
| 733 | * |
||
| 734 | * @param Field $field |
||
| 735 | * @param StorageFieldValue $value |
||
| 736 | */ |
||
| 737 | public function updateField(Field $field, StorageFieldValue $value) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Sets update fields for $value on $q. |
||
| 760 | * |
||
| 761 | * @param \eZ\Publish\Core\Persistence\Database\UpdateQuery $q |
||
| 762 | * @param StorageFieldValue $value |
||
| 763 | */ |
||
| 764 | protected function setFieldUpdateValues(UpdateQuery $q, StorageFieldValue $value) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Updates an existing, non-translatable field. |
||
| 788 | * |
||
| 789 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
| 790 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
| 791 | * @param int $contentId |
||
| 792 | */ |
||
| 793 | public function updateNonTranslatableField( |
||
| 820 | |||
| 821 | /** |
||
| 822 | * {@inheritdoc} |
||
| 823 | */ |
||
| 824 | public function load($contentId, $version = null, array $translations = null) |
||
| 825 | { |
||
| 828 | |||
| 829 | /** |
||
| 830 | * {@inheritdoc} |
||
| 831 | */ |
||
| 832 | public function loadContentList(array $contentIds, array $translations = null): array |
||
| 836 | |||
| 837 | /** |
||
| 838 | * @see load(), loadContentList() |
||
| 839 | * |
||
| 840 | * @param array $contentIds |
||
| 841 | * @param int|null $version |
||
| 842 | * @param string[]|null $translations |
||
| 843 | * |
||
| 844 | * @return array |
||
| 845 | */ |
||
| 846 | private function internalLoadContent(array $contentIds, int $version = null, array $translations = null): array |
||
| 932 | |||
| 933 | /** |
||
| 934 | * Get query builder to load Content Info data. |
||
| 935 | * |
||
| 936 | * @see loadContentInfo(), loadContentInfoByRemoteId(), loadContentInfoList(), loadContentInfoByLocationId() |
||
| 937 | * |
||
| 938 | * @param bool $joinMainLocation |
||
| 939 | * |
||
| 940 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
| 941 | */ |
||
| 942 | private function createLoadContentInfoQueryBuilder(bool $joinMainLocation = true): DoctrineQueryBuilder |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Loads info for content identified by $contentId. |
||
| 971 | * Will basically return a hash containing all field values for ezcontentobject table plus some additional keys: |
||
| 972 | * - always_available => Boolean indicating if content's language mask contains alwaysAvailable bit field |
||
| 973 | * - main_language_code => Language code for main (initial) language. E.g. "eng-GB". |
||
| 974 | * |
||
| 975 | * @param int $contentId |
||
| 976 | * |
||
| 977 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 978 | * |
||
| 979 | * @return array |
||
| 980 | */ |
||
| 981 | View Code Duplication | public function loadContentInfo($contentId) |
|
| 995 | |||
| 996 | public function loadContentInfoList(array $contentIds) |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Loads info for a content object identified by its remote ID. |
||
| 1008 | * |
||
| 1009 | * Returns an array with the relevant data. |
||
| 1010 | * |
||
| 1011 | * @param mixed $remoteId |
||
| 1012 | * |
||
| 1013 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 1014 | * |
||
| 1015 | * @return array |
||
| 1016 | */ |
||
| 1017 | View Code Duplication | public function loadContentInfoByRemoteId($remoteId) |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Loads info for a content object identified by its location ID (node ID). |
||
| 1034 | * |
||
| 1035 | * Returns an array with the relevant data. |
||
| 1036 | * |
||
| 1037 | * @param int $locationId |
||
| 1038 | * |
||
| 1039 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 1040 | * |
||
| 1041 | * @return array |
||
| 1042 | */ |
||
| 1043 | View Code Duplication | public function loadContentInfoByLocationId($locationId) |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * Loads version info for content identified by $contentId and $versionNo. |
||
| 1060 | * Will basically return a hash containing all field values from ezcontentobject_version table plus following keys: |
||
| 1061 | * - names => Hash of content object names. Key is the language code, value is the name. |
||
| 1062 | * - 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. |
||
| 1063 | * - initial_language_code => Language code for initial language in this version. |
||
| 1064 | * |
||
| 1065 | * @param int $contentId |
||
| 1066 | * @param int|null $versionNo |
||
| 1067 | * |
||
| 1068 | * @return array |
||
| 1069 | */ |
||
| 1070 | public function loadVersionInfo($contentId, $versionNo = null) |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Returns the number of all versions with given status created by the given $userId for content which is not in Trash. |
||
| 1085 | * |
||
| 1086 | * @param int $userId |
||
| 1087 | * @param int $status |
||
| 1088 | * |
||
| 1089 | * @return int |
||
| 1090 | */ |
||
| 1091 | public function countVersionsForUser(int $userId, int $status = VersionInfo::STATUS_DRAFT): int |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Returns data for all versions with given status created by the given $userId. |
||
| 1122 | * |
||
| 1123 | * @param int $userId |
||
| 1124 | * @param int $status |
||
| 1125 | * |
||
| 1126 | * @return string[][] |
||
| 1127 | */ |
||
| 1128 | public function listVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT) |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * {@inheritdoc} |
||
| 1149 | */ |
||
| 1150 | public function loadVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT, int $offset = 0, int $limit = -1): array |
||
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Returns all version data for the given $contentId, optionally filtered by status. |
||
| 1177 | * |
||
| 1178 | * Result is returned with oldest version first (using version id as it has index and is auto increment). |
||
| 1179 | * |
||
| 1180 | * @param mixed $contentId |
||
| 1181 | * @param mixed|null $status Optional argument to filter versions by status, like {@see VersionInfo::STATUS_ARCHIVED}. |
||
| 1182 | * @param int $limit Limit for items returned, -1 means none. |
||
| 1183 | * |
||
| 1184 | * @return string[][] |
||
| 1185 | */ |
||
| 1186 | public function listVersions($contentId, $status = null, $limit = -1) |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Helper for {@see listVersions()} and {@see listVersionsForUser()} that filters duplicates |
||
| 1216 | * that are the result of the cartesian product performed by createVersionInfoFindQuery(). |
||
| 1217 | * |
||
| 1218 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
||
| 1219 | * |
||
| 1220 | * @return string[][] |
||
| 1221 | */ |
||
| 1222 | private function listVersionsHelper(SelectQuery $query) |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * Returns all version numbers for the given $contentId. |
||
| 1247 | * |
||
| 1248 | * @param mixed $contentId |
||
| 1249 | * |
||
| 1250 | * @return int[] |
||
| 1251 | */ |
||
| 1252 | View Code Duplication | public function listVersionNumbers($contentId) |
|
| 1253 | { |
||
| 1254 | $query = $this->dbHandler->createSelectQuery(); |
||
| 1255 | $query->selectDistinct( |
||
| 1256 | $this->dbHandler->quoteColumn('version') |
||
| 1257 | )->from( |
||
| 1258 | $this->dbHandler->quoteTable('ezcontentobject_version') |
||
| 1259 | )->where( |
||
| 1260 | $query->expr->eq( |
||
| 1261 | $this->dbHandler->quoteColumn('contentobject_id'), |
||
| 1262 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1263 | ) |
||
| 1264 | ); |
||
| 1265 | |||
| 1266 | $statement = $query->prepare(); |
||
| 1267 | $statement->execute(); |
||
| 1268 | |||
| 1269 | return $statement->fetchAll(\PDO::FETCH_COLUMN); |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Returns last version number for content identified by $contentId. |
||
| 1274 | * |
||
| 1275 | * @param int $contentId |
||
| 1276 | * |
||
| 1277 | * @return int |
||
| 1278 | */ |
||
| 1279 | View Code Duplication | public function getLastVersionNumber($contentId) |
|
| 1280 | { |
||
| 1281 | $query = $this->dbHandler->createSelectQuery(); |
||
| 1282 | $query->select( |
||
| 1283 | $query->expr->max($this->dbHandler->quoteColumn('version')) |
||
| 1284 | )->from( |
||
| 1285 | $this->dbHandler->quoteTable('ezcontentobject_version') |
||
| 1286 | )->where( |
||
| 1287 | $query->expr->eq( |
||
| 1288 | $this->dbHandler->quoteColumn('contentobject_id'), |
||
| 1289 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1290 | ) |
||
| 1291 | ); |
||
| 1292 | |||
| 1293 | $statement = $query->prepare(); |
||
| 1294 | $statement->execute(); |
||
| 1295 | |||
| 1296 | return (int)$statement->fetchColumn(); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * Returns all IDs for locations that refer to $contentId. |
||
| 1301 | * |
||
| 1302 | * @param int $contentId |
||
| 1303 | * |
||
| 1304 | * @return int[] |
||
| 1305 | */ |
||
| 1306 | View Code Duplication | public function getAllLocationIds($contentId) |
|
| 1307 | { |
||
| 1308 | $query = $this->dbHandler->createSelectQuery(); |
||
| 1309 | $query->select( |
||
| 1310 | $this->dbHandler->quoteColumn('node_id') |
||
| 1311 | )->from( |
||
| 1312 | $this->dbHandler->quoteTable('ezcontentobject_tree') |
||
| 1313 | )->where( |
||
| 1314 | $query->expr->eq( |
||
| 1315 | $this->dbHandler->quoteColumn('contentobject_id'), |
||
| 1316 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1317 | ) |
||
| 1318 | ); |
||
| 1319 | |||
| 1320 | $statement = $query->prepare(); |
||
| 1321 | $statement->execute(); |
||
| 1322 | |||
| 1323 | return $statement->fetchAll(\PDO::FETCH_COLUMN); |
||
| 1324 | } |
||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Returns all field IDs of $contentId grouped by their type. |
||
| 1328 | * If $versionNo is set only field IDs for that version are returned. |
||
| 1329 | * If $languageCode is set, only field IDs for that language are returned. |
||
| 1330 | * |
||
| 1331 | * @param int $contentId |
||
| 1332 | * @param int|null $versionNo |
||
| 1333 | * @param string|null $languageCode |
||
| 1334 | * |
||
| 1335 | * @return int[][] |
||
| 1336 | */ |
||
| 1337 | public function getFieldIdsByType($contentId, $versionNo = null, $languageCode = null) |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Deletes relations to and from $contentId. |
||
| 1386 | * If $versionNo is set only relations for that version are deleted. |
||
| 1387 | * |
||
| 1388 | * @param int $contentId |
||
| 1389 | * @param int|null $versionNo |
||
| 1390 | */ |
||
| 1391 | public function deleteRelations($contentId, $versionNo = null) |
||
| 1392 | { |
||
| 1393 | $query = $this->dbHandler->createDeleteQuery(); |
||
| 1394 | $query->deleteFrom( |
||
| 1395 | $this->dbHandler->quoteTable('ezcontentobject_link') |
||
| 1396 | ); |
||
| 1397 | |||
| 1398 | if (isset($versionNo)) { |
||
| 1399 | $query->where( |
||
| 1400 | $query->expr->lAnd( |
||
| 1401 | $query->expr->eq( |
||
| 1402 | $this->dbHandler->quoteColumn('from_contentobject_id'), |
||
| 1403 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1404 | ), |
||
| 1405 | $query->expr->eq( |
||
| 1406 | $this->dbHandler->quoteColumn('from_contentobject_version'), |
||
| 1407 | $query->bindValue($versionNo, null, \PDO::PARAM_INT) |
||
| 1408 | ) |
||
| 1409 | ) |
||
| 1410 | ); |
||
| 1411 | View Code Duplication | } else { |
|
| 1412 | $query->where( |
||
| 1413 | $query->expr->lOr( |
||
| 1414 | $query->expr->eq( |
||
| 1415 | $this->dbHandler->quoteColumn('from_contentobject_id'), |
||
| 1416 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1417 | ), |
||
| 1418 | $query->expr->eq( |
||
| 1419 | $this->dbHandler->quoteColumn('to_contentobject_id'), |
||
| 1420 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1421 | ) |
||
| 1422 | ) |
||
| 1423 | ); |
||
| 1424 | } |
||
| 1425 | |||
| 1426 | $query->prepare()->execute(); |
||
| 1427 | } |
||
| 1428 | |||
| 1429 | /** |
||
| 1430 | * Removes relations to Content with $contentId from Relation and RelationList field type fields. |
||
| 1431 | * |
||
| 1432 | * @param int $contentId |
||
| 1433 | */ |
||
| 1434 | public function removeReverseFieldRelations($contentId) |
||
| 1484 | |||
| 1485 | /** |
||
| 1486 | * Updates field value of RelationList field type identified by given $row data, |
||
| 1487 | * removing relations toward given $contentId. |
||
| 1488 | * |
||
| 1489 | * @param int $contentId |
||
| 1490 | * @param array $row |
||
| 1491 | */ |
||
| 1492 | protected function removeRelationFromRelationListField($contentId, array $row) |
||
| 1527 | |||
| 1528 | /** |
||
| 1529 | * Updates field value of Relation field type identified by given $row data, |
||
| 1530 | * removing relation data. |
||
| 1531 | * |
||
| 1532 | * @param array $row |
||
| 1533 | */ |
||
| 1534 | protected function removeRelationFromRelationField(array $row) |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Deletes the field with the given $fieldId. |
||
| 1559 | * |
||
| 1560 | * @param int $fieldId |
||
| 1561 | */ |
||
| 1562 | View Code Duplication | public function deleteField($fieldId) |
|
| 1563 | { |
||
| 1564 | $query = $this->dbHandler->createDeleteQuery(); |
||
| 1565 | $query->deleteFrom( |
||
| 1566 | $this->dbHandler->quoteTable('ezcontentobject_attribute') |
||
| 1567 | )->where( |
||
| 1568 | $query->expr->eq( |
||
| 1569 | $this->dbHandler->quoteColumn('id'), |
||
| 1570 | $query->bindValue($fieldId, null, \PDO::PARAM_INT) |
||
| 1571 | ) |
||
| 1572 | ); |
||
| 1573 | |||
| 1574 | $query->prepare()->execute(); |
||
| 1575 | } |
||
| 1576 | |||
| 1577 | /** |
||
| 1578 | * Deletes all fields of $contentId in all versions. |
||
| 1579 | * If $versionNo is set only fields for that version are deleted. |
||
| 1580 | * |
||
| 1581 | * @param int $contentId |
||
| 1582 | * @param int|null $versionNo |
||
| 1583 | */ |
||
| 1584 | View Code Duplication | public function deleteFields($contentId, $versionNo = null) |
|
| 1585 | { |
||
| 1586 | $query = $this->dbHandler->createDeleteQuery(); |
||
| 1587 | $query->deleteFrom('ezcontentobject_attribute') |
||
| 1588 | ->where( |
||
| 1589 | $query->expr->eq( |
||
| 1590 | $this->dbHandler->quoteColumn('contentobject_id'), |
||
| 1591 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1592 | ) |
||
| 1593 | ); |
||
| 1594 | |||
| 1595 | if (isset($versionNo)) { |
||
| 1596 | $query->where( |
||
| 1597 | $query->expr->eq( |
||
| 1598 | $this->dbHandler->quoteColumn('version'), |
||
| 1599 | $query->bindValue($versionNo, null, \PDO::PARAM_INT) |
||
| 1600 | ) |
||
| 1601 | ); |
||
| 1602 | } |
||
| 1603 | |||
| 1604 | $query->prepare()->execute(); |
||
| 1605 | } |
||
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Deletes all versions of $contentId. |
||
| 1609 | * If $versionNo is set only that version is deleted. |
||
| 1610 | * |
||
| 1611 | * @param int $contentId |
||
| 1612 | * @param int|null $versionNo |
||
| 1613 | */ |
||
| 1614 | View Code Duplication | public function deleteVersions($contentId, $versionNo = null) |
|
| 1615 | { |
||
| 1616 | $query = $this->dbHandler->createDeleteQuery(); |
||
| 1617 | $query->deleteFrom('ezcontentobject_version') |
||
| 1618 | ->where( |
||
| 1619 | $query->expr->eq( |
||
| 1620 | $this->dbHandler->quoteColumn('contentobject_id'), |
||
| 1621 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1622 | ) |
||
| 1623 | ); |
||
| 1624 | |||
| 1625 | if (isset($versionNo)) { |
||
| 1626 | $query->where( |
||
| 1627 | $query->expr->eq( |
||
| 1628 | $this->dbHandler->quoteColumn('version'), |
||
| 1629 | $query->bindValue($versionNo, null, \PDO::PARAM_INT) |
||
| 1630 | ) |
||
| 1631 | ); |
||
| 1632 | } |
||
| 1633 | |||
| 1634 | $query->prepare()->execute(); |
||
| 1635 | } |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Deletes all names of $contentId. |
||
| 1639 | * If $versionNo is set only names for that version are deleted. |
||
| 1640 | * |
||
| 1641 | * @param int $contentId |
||
| 1642 | * @param int|null $versionNo |
||
| 1643 | */ |
||
| 1644 | View Code Duplication | public function deleteNames($contentId, $versionNo = null) |
|
| 1645 | { |
||
| 1646 | $query = $this->dbHandler->createDeleteQuery(); |
||
| 1647 | $query->deleteFrom('ezcontentobject_name') |
||
| 1648 | ->where( |
||
| 1649 | $query->expr->eq( |
||
| 1650 | $this->dbHandler->quoteColumn('contentobject_id'), |
||
| 1651 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
| 1652 | ) |
||
| 1653 | ); |
||
| 1654 | |||
| 1655 | if (isset($versionNo)) { |
||
| 1656 | $query->where( |
||
| 1657 | $query->expr->eq( |
||
| 1658 | $this->dbHandler->quoteColumn('content_version'), |
||
| 1659 | $query->bindValue($versionNo, null, \PDO::PARAM_INT) |
||
| 1660 | ) |
||
| 1661 | ); |
||
| 1662 | } |
||
| 1663 | |||
| 1664 | $query->prepare()->execute(); |
||
| 1665 | } |
||
| 1666 | |||
| 1667 | /** |
||
| 1668 | * Sets the name for Content $contentId in version $version to $name in $language. |
||
| 1669 | * |
||
| 1670 | * @param int $contentId |
||
| 1671 | * @param int $version |
||
| 1672 | * @param string $name |
||
| 1673 | * @param string $language |
||
| 1674 | */ |
||
| 1675 | public function setName($contentId, $version, $name, $language) |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Returns a language sub select query for setName. |
||
| 1739 | * |
||
| 1740 | * Return sub select query which gets proper language mask for alwaysAvailable Content. |
||
| 1741 | * |
||
| 1742 | * @return \eZ\Publish\Core\Persistence\Database\SelectQuery |
||
| 1743 | */ |
||
| 1744 | private function getLanguageQuery() |
||
| 1783 | |||
| 1784 | /** |
||
| 1785 | * Deletes the actual content object referred to by $contentId. |
||
| 1786 | * |
||
| 1787 | * @param int $contentId |
||
| 1788 | */ |
||
| 1789 | View Code Duplication | public function deleteContent($contentId) |
|
| 1790 | { |
||
| 1791 | $query = $this->dbHandler->createDeleteQuery(); |
||
| 1802 | |||
| 1803 | /** |
||
| 1804 | * Loads relations from $contentId to published content, optionally only from $contentVersionNo. |
||
| 1805 | * |
||
| 1806 | * $relationType can also be filtered. |
||
| 1807 | * |
||
| 1808 | * @param int $contentId |
||
| 1809 | * @param int $contentVersionNo |
||
| 1810 | * @param int $relationType |
||
| 1811 | * |
||
| 1812 | * @return string[][] array of relation data |
||
| 1813 | */ |
||
| 1814 | public function loadRelations($contentId, $contentVersionNo = null, $relationType = null) |
||
| 1882 | |||
| 1883 | /** |
||
| 1884 | * Loads data that related to $toContentId. |
||
| 1885 | * |
||
| 1886 | * @param int $toContentId |
||
| 1887 | * @param int $relationType |
||
| 1888 | * |
||
| 1889 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
| 1890 | */ |
||
| 1891 | public function loadReverseRelations($toContentId, $relationType = null) |
||
| 1940 | |||
| 1941 | /** |
||
| 1942 | * Inserts a new relation database record. |
||
| 1943 | * |
||
| 1944 | * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct |
||
| 1945 | * |
||
| 1946 | * @return int ID the inserted ID |
||
| 1947 | */ |
||
| 1948 | public function insertRelation(RelationCreateStruct $createStruct) |
||
| 1979 | |||
| 1980 | /** |
||
| 1981 | * Deletes the relation with the given $relationId. |
||
| 1982 | * |
||
| 1983 | * @param int $relationId |
||
| 1984 | * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON, |
||
| 1985 | * \eZ\Publish\API\Repository\Values\Content\Relation::EMBED, |
||
| 1986 | * \eZ\Publish\API\Repository\Values\Content\Relation::LINK, |
||
| 1987 | * \eZ\Publish\API\Repository\Values\Content\Relation::FIELD} |
||
| 1988 | */ |
||
| 1989 | public function deleteRelation($relationId, $type) |
||
| 2051 | |||
| 2052 | /** |
||
| 2053 | * Returns all Content IDs for a given $contentTypeId. |
||
| 2054 | * |
||
| 2055 | * @param int $contentTypeId |
||
| 2056 | * |
||
| 2057 | * @return int[] |
||
| 2058 | */ |
||
| 2059 | View Code Duplication | public function getContentIdsByContentTypeId($contentTypeId) |
|
| 2077 | |||
| 2078 | /** |
||
| 2079 | * Load name data for set of content id's and corresponding version number. |
||
| 2080 | * |
||
| 2081 | * @param array[] $rows array of hashes with 'id' and 'version' to load names for |
||
| 2082 | * |
||
| 2083 | * @return array |
||
| 2084 | */ |
||
| 2085 | public function loadVersionedNameData($rows) |
||
| 2108 | |||
| 2109 | /** |
||
| 2110 | * Batch method for copying all relation meta data for copied Content object. |
||
| 2111 | * |
||
| 2112 | * {@inheritdoc} |
||
| 2113 | * |
||
| 2114 | * @param int $originalContentId |
||
| 2115 | * @param int $copiedContentId |
||
| 2116 | * @param int|null $versionNo If specified only copy for a given version number, otherwise all. |
||
| 2117 | */ |
||
| 2118 | public function copyRelations($originalContentId, $copiedContentId, $versionNo = null) |
||
| 2138 | |||
| 2139 | /** |
||
| 2140 | * Remove the specified translation from the Content Object Version. |
||
| 2141 | * |
||
| 2142 | * @param int $contentId |
||
| 2143 | * @param string $languageCode language code of the translation |
||
| 2144 | * @throws \Doctrine\DBAL\DBALException |
||
| 2145 | */ |
||
| 2146 | View Code Duplication | public function deleteTranslationFromContent($contentId, $languageCode) |
|
| 2162 | |||
| 2163 | /** |
||
| 2164 | * Delete Content fields (attributes) for the given Translation. |
||
| 2165 | * If $versionNo is given, fields for that Version only will be deleted. |
||
| 2166 | * |
||
| 2167 | * @param string $languageCode |
||
| 2168 | * @param int $contentId |
||
| 2169 | * @param int $versionNo (optional) filter by versionNo |
||
| 2170 | */ |
||
| 2171 | View Code Duplication | public function deleteTranslatedFields($languageCode, $contentId, $versionNo = null) |
|
| 2195 | |||
| 2196 | /** |
||
| 2197 | * Delete the specified Translation from the given Version. |
||
| 2198 | * |
||
| 2199 | * @param int $contentId |
||
| 2200 | * @param int $versionNo |
||
| 2201 | * @param string $languageCode |
||
| 2202 | * @throws \Doctrine\DBAL\DBALException |
||
| 2203 | */ |
||
| 2204 | View Code Duplication | public function deleteTranslationFromVersion($contentId, $versionNo, $languageCode) |
|
| 2219 | |||
| 2220 | /** |
||
| 2221 | * Delete translation from the ezcontentobject_name table. |
||
| 2222 | * |
||
| 2223 | * @param int $contentId |
||
| 2224 | * @param string $languageCode |
||
| 2225 | * @param int $versionNo optional, if specified, apply to this Version only. |
||
| 2226 | */ |
||
| 2227 | View Code Duplication | private function deleteTranslationFromContentNames($contentId, $languageCode, $versionNo = null) |
|
| 2251 | |||
| 2252 | /** |
||
| 2253 | * Remove language from language_mask of ezcontentobject. |
||
| 2254 | * |
||
| 2255 | * @param int $contentId |
||
| 2256 | * @param int $languageId |
||
| 2257 | * @throws \eZ\Publish\Core\Base\Exceptions\BadStateException |
||
| 2258 | */ |
||
| 2259 | private function deleteTranslationFromContentObject($contentId, $languageId) |
||
| 2288 | |||
| 2289 | /** |
||
| 2290 | * Remove language from language_mask of ezcontentobject_version and update initialLanguageId |
||
| 2291 | * if it matches the removed one. |
||
| 2292 | * |
||
| 2293 | * @param int $contentId |
||
| 2294 | * @param int $languageId |
||
| 2295 | * @param int $versionNo optional, if specified, apply to this Version only. |
||
| 2296 | * @throws \eZ\Publish\Core\Base\Exceptions\BadStateException |
||
| 2297 | */ |
||
| 2298 | private function deleteTranslationFromContentVersions($contentId, $languageId, $versionNo = null) |
||
| 2342 | |||
| 2343 | /** |
||
| 2344 | * Get query builder for content version objects, used for version loading w/o fields. |
||
| 2345 | * |
||
| 2346 | * Creates a select query with all necessary joins to fetch a complete |
||
| 2347 | * content object. Does not apply any WHERE conditions, and does not contain |
||
| 2348 | * name data as it will lead to large result set {@see createNamesQuery}. |
||
| 2349 | * |
||
| 2350 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
| 2351 | */ |
||
| 2352 | private function createVersionInfoFindQueryBuilder(): DoctrineQueryBuilder |
||
| 2405 | } |
||
| 2406 |
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.