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 Handler 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 Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Handler implements UrlAliasHandlerInterface |
||
| 32 | { |
||
| 33 | const ROOT_LOCATION_ID = 1; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * This is intentionally hardcoded for now as: |
||
| 37 | * 1. We don't implement this configuration option. |
||
| 38 | * 2. Such option should not be in this layer, should be handled higher up. |
||
| 39 | * |
||
| 40 | * @deprecated |
||
| 41 | */ |
||
| 42 | const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The maximum level of alias depth. |
||
| 46 | */ |
||
| 47 | const MAX_URL_ALIAS_DEPTH_LEVEL = 60; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * UrlAlias Gateway. |
||
| 51 | * |
||
| 52 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
| 53 | */ |
||
| 54 | protected $gateway; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Gateway for handling location data. |
||
| 58 | * |
||
| 59 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 60 | */ |
||
| 61 | protected $locationGateway; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * UrlAlias Mapper. |
||
| 65 | * |
||
| 66 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper |
||
| 67 | */ |
||
| 68 | protected $mapper; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Caching language handler. |
||
| 72 | * |
||
| 73 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
||
| 74 | */ |
||
| 75 | protected $languageHandler; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * URL slug converter. |
||
| 79 | * |
||
| 80 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
| 81 | */ |
||
| 82 | protected $slugConverter; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gateway for handling content data. |
||
| 86 | * |
||
| 87 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway |
||
| 88 | */ |
||
| 89 | protected $contentGateway; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Language mask generator. |
||
| 93 | * |
||
| 94 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator |
||
| 95 | */ |
||
| 96 | protected $maskGenerator; |
||
| 97 | |||
| 98 | /** @var \eZ\Publish\SPI\Persistence\TransactionHandler */ |
||
| 99 | private $transactionHandler; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Creates a new UrlAlias Handler. |
||
| 103 | * |
||
| 104 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway |
||
| 105 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper |
||
| 106 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 107 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 108 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter |
||
| 109 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway |
||
| 110 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $maskGenerator |
||
| 111 | * @param \eZ\Publish\SPI\Persistence\TransactionHandler $transactionHandler |
||
| 112 | */ |
||
| 113 | public function __construct( |
||
| 132 | |||
| 133 | public function publishUrlAliasForLocation( |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Internal publish method, accepting language ID instead of language code and optionally |
||
| 155 | * new alias ID (used when swapping Locations). |
||
| 156 | * |
||
| 157 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 158 | * |
||
| 159 | * @param int $locationId |
||
| 160 | * @param int $parentLocationId |
||
| 161 | * @param string $name |
||
| 162 | * @param int $languageId |
||
| 163 | * @param bool $alwaysAvailable |
||
| 164 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
| 165 | * @param int $newId |
||
| 166 | */ |
||
| 167 | private function internalPublishUrlAliasForLocation( |
||
| 168 | $locationId, |
||
| 169 | $parentLocationId, |
||
| 170 | $name, |
||
| 171 | $languageId, |
||
| 172 | $alwaysAvailable = false, |
||
| 173 | $updatePathIdentificationString = false, |
||
| 174 | $newId = null |
||
| 175 | ) { |
||
| 176 | $parentId = $this->getRealAliasId($parentLocationId); |
||
| 177 | $name = $this->slugConverter->convert($name, 'location_' . $locationId); |
||
| 178 | $uniqueCounter = $this->slugConverter->getUniqueCounterValue($name, $parentId == 0); |
||
| 179 | $languageMask = $languageId | (int)$alwaysAvailable; |
||
| 180 | $action = 'eznode:' . $locationId; |
||
| 181 | $cleanup = false; |
||
| 182 | |||
| 183 | // Exiting the loop with break; |
||
| 184 | while (true) { |
||
| 185 | $newText = ''; |
||
| 186 | if ($locationId != self::CONTENT_REPOSITORY_ROOT_LOCATION_ID) { |
||
| 187 | $newText = $name . ($name == '' || $uniqueCounter > 1 ? $uniqueCounter : ''); |
||
| 188 | } |
||
| 189 | $newTextMD5 = $this->getHash($newText); |
||
| 190 | |||
| 191 | // Try to load existing entry |
||
| 192 | $row = $this->gateway->loadRow($parentId, $newTextMD5); |
||
| 193 | |||
| 194 | // If nothing was returned insert new entry |
||
| 195 | if (empty($row)) { |
||
| 196 | // Check for existing active location entry on this level and reuse it's id |
||
| 197 | $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
||
| 198 | if (!empty($existingLocationEntry)) { |
||
| 199 | $cleanup = true; |
||
| 200 | $newId = $existingLocationEntry['id']; |
||
| 201 | } |
||
| 202 | |||
| 203 | try { |
||
| 204 | $newId = $this->gateway->insertRow( |
||
| 205 | [ |
||
| 206 | 'id' => $newId, |
||
| 207 | 'link' => $newId, |
||
| 208 | 'parent' => $parentId, |
||
| 209 | 'action' => $action, |
||
| 210 | 'lang_mask' => $languageMask, |
||
| 211 | 'text' => $newText, |
||
| 212 | 'text_md5' => $newTextMD5, |
||
| 213 | ] |
||
| 214 | ); |
||
| 215 | } catch (\RuntimeException $e) { |
||
| 216 | while ($e->getPrevious() !== null) { |
||
| 217 | $e = $e->getPrevious(); |
||
| 218 | if ($e instanceof UniqueConstraintViolationException) { |
||
| 219 | // Concurrency! someone else inserted the same row that we where going to. |
||
| 220 | // let's do another loop pass |
||
| 221 | ++$uniqueCounter; |
||
| 222 | continue 2; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | throw $e; |
||
| 227 | } |
||
| 228 | |||
| 229 | break; |
||
| 230 | } |
||
| 231 | |||
| 232 | // Row exists, check if it is reusable. There are 3 cases when this is possible: |
||
| 233 | // 1. NOP entry |
||
| 234 | // 2. existing location or custom alias entry |
||
| 235 | // 3. history entry |
||
| 236 | if ( |
||
| 237 | $row['action'] === Gateway::NOP_ACTION || |
||
| 238 | $row['action'] === $action || |
||
| 239 | (int)$row['is_original'] === 0 |
||
| 240 | ) { |
||
| 241 | // Check for existing location entry on this level, if it exists and it's id differs from reusable |
||
| 242 | // entry id then reusable entry should be updated with the existing location entry id. |
||
| 243 | // Note: existing location entry may be downgraded and relinked later, depending on its language. |
||
| 244 | $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
||
| 245 | |||
| 246 | if (!empty($existingLocationEntry)) { |
||
| 247 | // Always cleanup when active autogenerated entry exists on the same level |
||
| 248 | $cleanup = true; |
||
| 249 | $newId = $existingLocationEntry['id']; |
||
| 250 | if ($existingLocationEntry['id'] == $row['id']) { |
||
| 251 | // If we are reusing existing location entry merge existing language mask |
||
| 252 | $languageMask |= ($row['lang_mask'] & ~1); |
||
| 253 | } |
||
| 254 | } elseif ($newId === null) { |
||
| 255 | // Use reused row ID only if publishing normally, else use given $newId |
||
| 256 | $newId = $row['id']; |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->gateway->updateRow( |
||
| 260 | $parentId, |
||
| 261 | $newTextMD5, |
||
| 262 | [ |
||
| 263 | 'action' => $action, |
||
| 264 | // In case when NOP row was reused |
||
| 265 | 'action_type' => 'eznode', |
||
| 266 | 'lang_mask' => $languageMask, |
||
| 267 | // Updating text ensures that letter case changes are stored |
||
| 268 | 'text' => $newText, |
||
| 269 | // Set "id" and "link" for case when reusable entry is history |
||
| 270 | 'id' => $newId, |
||
| 271 | 'link' => $newId, |
||
| 272 | // Entry should be active location entry (original and not alias). |
||
| 273 | // Note: this takes care of taking over custom alias entry for the location on the same level |
||
| 274 | // and with same name and action. |
||
| 275 | 'alias_redirects' => 1, |
||
| 276 | 'is_original' => 1, |
||
| 277 | 'is_alias' => 0, |
||
| 278 | ] |
||
| 279 | ); |
||
| 280 | |||
| 281 | break; |
||
| 282 | } |
||
| 283 | |||
| 284 | // If existing row is not reusable, increment $uniqueCounter and try again |
||
| 285 | ++$uniqueCounter; |
||
| 286 | } |
||
| 287 | |||
| 288 | /* @var $newText */ |
||
| 289 | if ($updatePathIdentificationString) { |
||
| 290 | $this->locationGateway->updatePathIdentificationString( |
||
| 291 | $locationId, |
||
| 292 | $parentLocationId, |
||
| 293 | $this->slugConverter->convert($newText, 'node_' . $locationId, 'urlalias_compat') |
||
| 294 | ); |
||
| 295 | } |
||
| 296 | |||
| 297 | /* @var $newId */ |
||
| 298 | /* @var $newTextMD5 */ |
||
| 299 | // Note: cleanup does not touch custom and global entries |
||
| 300 | if ($cleanup) { |
||
| 301 | $this->gateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $newTextMD5); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
| 307 | * |
||
| 308 | * If $languageCode is null the $alias is created in the system's default |
||
| 309 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 310 | * |
||
| 311 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 312 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 313 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
| 314 | * |
||
| 315 | * @param mixed $locationId |
||
| 316 | * @param string $path |
||
| 317 | * @param bool $forwarding |
||
| 318 | * @param string $languageCode |
||
| 319 | * @param bool $alwaysAvailable |
||
| 320 | * |
||
| 321 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 322 | */ |
||
| 323 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 324 | { |
||
| 325 | return $this->createUrlAlias( |
||
| 326 | 'eznode:' . $locationId, |
||
| 327 | $path, |
||
| 328 | $forwarding, |
||
| 329 | $languageCode, |
||
| 330 | $alwaysAvailable |
||
| 331 | ); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 336 | * This method does not handle location resources - if a user enters a location target |
||
| 337 | * the createCustomUrlAlias method has to be used. |
||
| 338 | * |
||
| 339 | * If $languageCode is null the $alias is created in the system's default |
||
| 340 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 341 | * |
||
| 342 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given resource |
||
| 343 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if the path is broken |
||
| 344 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 345 | * |
||
| 346 | * @param string $resource |
||
| 347 | * @param string $path |
||
| 348 | * @param bool $forwarding |
||
| 349 | * @param string $languageCode |
||
| 350 | * @param bool $alwaysAvailable |
||
| 351 | * |
||
| 352 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 353 | */ |
||
| 354 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 355 | { |
||
| 356 | return $this->createUrlAlias( |
||
| 357 | $resource, |
||
| 358 | $path, |
||
| 359 | $forwarding, |
||
| 360 | $languageCode, |
||
| 361 | $alwaysAvailable |
||
| 362 | ); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
| 367 | * |
||
| 368 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given context |
||
| 369 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 370 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 371 | * |
||
| 372 | * @param string $action |
||
| 373 | * @param string $path |
||
| 374 | * @param bool $forward |
||
| 375 | * @param string|null $languageCode |
||
| 376 | * @param bool $alwaysAvailable |
||
| 377 | * |
||
| 378 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 379 | */ |
||
| 380 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable): UrlAlias |
||
| 381 | { |
||
| 382 | $pathElements = explode('/', $path); |
||
| 383 | $topElement = array_pop($pathElements); |
||
| 384 | $languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id; |
||
| 385 | $parentId = 0; |
||
| 386 | |||
| 387 | // Handle all path elements except topmost one |
||
| 388 | $isPathNew = false; |
||
| 389 | foreach ($pathElements as $level => $pathElement) { |
||
| 390 | $pathElement = $this->slugConverter->convert($pathElement, 'noname' . ($level + 1)); |
||
| 391 | $pathElementMD5 = $this->getHash($pathElement); |
||
| 392 | if (!$isPathNew) { |
||
| 393 | $row = $this->gateway->loadRow($parentId, $pathElementMD5); |
||
| 394 | if (empty($row)) { |
||
| 395 | $isPathNew = true; |
||
| 396 | } else { |
||
| 397 | $parentId = $row['link']; |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | if ($isPathNew) { |
||
| 402 | $parentId = $this->insertNopEntry($parentId, $pathElement, $pathElementMD5); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | // Handle topmost path element |
||
| 407 | $topElement = $this->slugConverter->convert( |
||
| 408 | $topElement, |
||
| 409 | 'noname' . (count($pathElements) + 1) |
||
| 410 | ); |
||
| 411 | |||
| 412 | // If last (next to topmost) entry parent is special root entry we handle topmost entry as first level entry |
||
| 413 | // That is why we need to reset $parentId to 0 |
||
| 414 | if ($parentId !== 0 && $this->gateway->isRootEntry($parentId)) { |
||
| 415 | $parentId = 0; |
||
| 416 | } |
||
| 417 | |||
| 418 | $topElementMD5 = $this->getHash($topElement); |
||
| 419 | // Set common values for two cases below |
||
| 420 | $data = [ |
||
| 421 | 'action' => $action, |
||
| 422 | 'is_alias' => 1, |
||
| 423 | 'alias_redirects' => $forward ? 1 : 0, |
||
| 424 | 'parent' => $parentId, |
||
| 425 | 'text' => $topElement, |
||
| 426 | 'text_md5' => $topElementMD5, |
||
| 427 | 'is_original' => 1, |
||
| 428 | ]; |
||
| 429 | // Try to load topmost element |
||
| 430 | if (!$isPathNew) { |
||
| 431 | $row = $this->gateway->loadRow($parentId, $topElementMD5); |
||
| 432 | } |
||
| 433 | |||
| 434 | // If nothing was returned perform insert |
||
| 435 | if ($isPathNew || empty($row)) { |
||
| 436 | $data['lang_mask'] = $languageId | (int)$alwaysAvailable; |
||
| 437 | $id = $this->gateway->insertRow($data); |
||
| 438 | } elseif ($row['action'] === Gateway::NOP_ACTION || (int)$row['is_original'] === 0) { |
||
| 439 | // Row exists, check if it is reusable. There are 2 cases when this is possible: |
||
| 440 | // 1. NOP entry |
||
| 441 | // 2. history entry |
||
| 442 | $data['lang_mask'] = $languageId | (int)$alwaysAvailable; |
||
| 443 | // If history is reused move link to id |
||
| 444 | $data['link'] = $id = $row['id']; |
||
| 445 | $this->gateway->updateRow( |
||
| 446 | $parentId, |
||
| 447 | $topElementMD5, |
||
| 448 | $data |
||
| 449 | ); |
||
| 450 | } elseif ( |
||
| 451 | $row['action'] === $action && |
||
| 452 | (int)$row['is_alias'] === 1 && |
||
| 453 | 0 === ((int)$row['lang_mask'] & $languageId) |
||
| 454 | ) { |
||
| 455 | // add another language to the same custom alias |
||
| 456 | $data['link'] = $id = $row['id']; |
||
| 457 | $data['lang_mask'] = $row['lang_mask'] | $languageId | (int)$alwaysAvailable; |
||
| 458 | $this->gateway->updateRow( |
||
| 459 | $parentId, |
||
| 460 | $topElementMD5, |
||
| 461 | $data |
||
| 462 | ); |
||
| 463 | } else { |
||
| 464 | throw new ForbiddenException( |
||
| 465 | "Path '%path%' already exists for the given context", |
||
| 466 | ['%path%' => $path] |
||
| 467 | ); |
||
| 468 | } |
||
| 469 | |||
| 470 | $data['raw_path_data'] = $this->gateway->loadPathData($id); |
||
| 471 | |||
| 472 | return $this->mapper->extractUrlAliasFromData($data); |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Convenience method for inserting nop type row. |
||
| 477 | * |
||
| 478 | * @param mixed $parentId |
||
| 479 | * @param string $text |
||
| 480 | * @param string $textMD5 |
||
| 481 | * |
||
| 482 | * @return mixed |
||
| 483 | */ |
||
| 484 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
| 485 | { |
||
| 486 | return $this->gateway->insertRow( |
||
| 487 | [ |
||
| 488 | 'lang_mask' => 1, |
||
| 489 | 'action' => Gateway::NOP_ACTION, |
||
| 490 | 'parent' => $parentId, |
||
| 491 | 'text' => $text, |
||
| 492 | 'text_md5' => $textMD5, |
||
| 493 | ] |
||
| 494 | ); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
| 499 | * |
||
| 500 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 501 | * |
||
| 502 | * @param mixed $locationId |
||
| 503 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 504 | * |
||
| 505 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 506 | */ |
||
| 507 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
| 508 | { |
||
| 509 | $data = $this->gateway->loadLocationEntries($locationId, $custom); |
||
| 510 | foreach ($data as &$entry) { |
||
| 511 | $entry['raw_path_data'] = $this->gateway->loadPathData($entry['id']); |
||
| 512 | } |
||
| 513 | |||
| 514 | return $this->mapper->extractUrlAliasListFromData($data); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * List global aliases. |
||
| 519 | * |
||
| 520 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 521 | * |
||
| 522 | * @param string|null $languageCode |
||
| 523 | * @param int $offset |
||
| 524 | * @param int $limit |
||
| 525 | * |
||
| 526 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 527 | */ |
||
| 528 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
| 529 | { |
||
| 530 | $data = $this->gateway->listGlobalEntries($languageCode, $offset, $limit); |
||
| 531 | foreach ($data as &$entry) { |
||
| 532 | $entry['raw_path_data'] = $this->gateway->loadPathData($entry['id']); |
||
| 533 | } |
||
| 534 | |||
| 535 | return $this->mapper->extractUrlAliasListFromData($data); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Removes url aliases. |
||
| 540 | * |
||
| 541 | * Autogenerated aliases are not removed by this method. |
||
| 542 | * |
||
| 543 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
| 544 | * |
||
| 545 | * @return bool |
||
| 546 | */ |
||
| 547 | public function removeURLAliases(array $urlAliases) |
||
| 548 | { |
||
| 549 | foreach ($urlAliases as $urlAlias) { |
||
| 550 | if ($urlAlias->isCustom) { |
||
| 551 | list($parentId, $textMD5) = explode('-', $urlAlias->id); |
||
| 552 | if (!$this->gateway->removeCustomAlias($parentId, $textMD5)) { |
||
| 553 | return false; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | } |
||
| 557 | |||
| 558 | return true; |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Looks up a url alias for the given url. |
||
| 563 | * |
||
| 564 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 565 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 566 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 567 | * |
||
| 568 | * @param string $url |
||
| 569 | * |
||
| 570 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 571 | */ |
||
| 572 | public function lookup($url) |
||
| 573 | { |
||
| 574 | $urlHashes = []; |
||
| 575 | foreach (explode('/', $url) as $level => $text) { |
||
| 576 | $urlHashes[$level] = $this->getHash($text); |
||
| 577 | } |
||
| 578 | |||
| 579 | $pathDepth = count($urlHashes); |
||
| 580 | if ($pathDepth > self::MAX_URL_ALIAS_DEPTH_LEVEL) { |
||
| 581 | throw new InvalidArgumentException('$urlHashes', 'Exceeded maximum depth level of content url alias.'); |
||
| 582 | } |
||
| 583 | |||
| 584 | $data = $this->gateway->loadUrlAliasData($urlHashes); |
||
| 585 | if (empty($data)) { |
||
| 586 | throw new NotFoundException('URLAlias', $url); |
||
| 587 | } |
||
| 588 | |||
| 589 | $hierarchyData = []; |
||
| 590 | $isPathHistory = false; |
||
| 591 | for ($level = 0; $level < $pathDepth; ++$level) { |
||
| 592 | $prefix = $level === $pathDepth - 1 ? '' : 'ezurlalias_ml' . $level . '_'; |
||
| 593 | $isPathHistory = $isPathHistory ?: ($data[$prefix . 'link'] != $data[$prefix . 'id']); |
||
| 594 | $hierarchyData[$level] = [ |
||
| 595 | 'id' => $data[$prefix . 'id'], |
||
| 596 | 'parent' => $data[$prefix . 'parent'], |
||
| 597 | 'action' => $data[$prefix . 'action'], |
||
| 598 | ]; |
||
| 599 | } |
||
| 600 | |||
| 601 | $data['is_path_history'] = $isPathHistory; |
||
| 602 | $data['raw_path_data'] = ($data['action_type'] == 'eznode' && !$data['is_alias']) |
||
| 603 | ? $this->gateway->loadPathDataByHierarchy($hierarchyData) |
||
| 604 | : $this->gateway->loadPathData($data['id']); |
||
| 605 | |||
| 606 | return $this->mapper->extractUrlAliasFromData($data); |
||
| 607 | } |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Loads URL alias by given $id. |
||
| 611 | * |
||
| 612 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 613 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 614 | * |
||
| 615 | * @param string $id |
||
| 616 | * |
||
| 617 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 618 | */ |
||
| 619 | public function loadUrlAlias($id) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Notifies the underlying engine that a location has moved. |
||
| 635 | * |
||
| 636 | * This method triggers the change of the autogenerated aliases. |
||
| 637 | * |
||
| 638 | * @param mixed $locationId |
||
| 639 | * @param mixed $oldParentId |
||
| 640 | * @param mixed $newParentId |
||
| 641 | */ |
||
| 642 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Notifies the underlying engine that a location was copied. |
||
| 665 | * |
||
| 666 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
| 667 | * |
||
| 668 | * @param mixed $locationId |
||
| 669 | * @param mixed $newLocationId |
||
| 670 | * @param mixed $newParentId |
||
| 671 | */ |
||
| 672 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Notify the underlying engine that a Location has been swapped. |
||
| 688 | * |
||
| 689 | * This method triggers the change of the autogenerated aliases. |
||
| 690 | * |
||
| 691 | * @param int $location1Id |
||
| 692 | * @param int $location1ParentId |
||
| 693 | * @param int $location2Id |
||
| 694 | * @param int $location2ParentId |
||
| 695 | * |
||
| 696 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 697 | */ |
||
| 698 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @param array $contentInfo |
||
| 754 | * |
||
| 755 | * @return array |
||
| 756 | */ |
||
| 757 | private function getNamesForAllLanguages(array $contentInfo) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Historizes given existing active entries for two swapped Locations. |
||
| 777 | * |
||
| 778 | * This should be done before republishing URL aliases, in order to avoid unnecessary |
||
| 779 | * conflicts when swapped Locations are siblings. |
||
| 780 | * |
||
| 781 | * We need to historize everything separately per language (mask), in case the entries |
||
| 782 | * remain history future publishing reusages need to be able to take them over cleanly. |
||
| 783 | * |
||
| 784 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 785 | * |
||
| 786 | * @param array $location1Entries |
||
| 787 | * @param array $location2Entries |
||
| 788 | */ |
||
| 789 | private function historizeBeforeSwap($location1Entries, $location2Entries) |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Decides if UrlAlias for $location2 should be published first. |
||
| 802 | * |
||
| 803 | * The order in which Locations are published only matters if swapped Locations are siblings and they have the same |
||
| 804 | * name in a given language. In this case, the UrlAlias for Location which previously had lower number at the end of |
||
| 805 | * its UrlAlias text (or no number at all) should be published first. This ensures that the number still stays lower |
||
| 806 | * for this Location after the swap. If it wouldn't stay lower, then swapping Locations in conjunction with swapping |
||
| 807 | * UrlAliases would effectively cancel each other. |
||
| 808 | * |
||
| 809 | * @param array $location1Entries |
||
| 810 | * @param int $location1ParentId |
||
| 811 | * @param string $name1 |
||
| 812 | * @param array $location2Entries |
||
| 813 | * @param int $location2ParentId |
||
| 814 | * @param string $name2 |
||
| 815 | * @param int $languageId |
||
| 816 | * |
||
| 817 | * @return bool |
||
| 818 | */ |
||
| 819 | private function shouldUrlAliasForSecondLocationBePublishedFirst( |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Get in a proper order - to be published - a list of URL aliases for swapped Locations. |
||
| 846 | * |
||
| 847 | * @see shouldUrlAliasForSecondLocationBePublishedFirst |
||
| 848 | * |
||
| 849 | * @param \eZ\Publish\SPI\Persistence\Content\Language $language |
||
| 850 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties $location1 |
||
| 851 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties $location2 |
||
| 852 | * |
||
| 853 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\UrlAliasForSwappedLocation[] |
||
| 854 | */ |
||
| 855 | private function getUrlAliasesForSwappedLocations( |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @param array $locationEntries |
||
| 902 | * @param int $languageId |
||
| 903 | * |
||
| 904 | * @return array|null |
||
| 905 | */ |
||
| 906 | private function getLocationEntryInLanguage(array $locationEntries, $languageId) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
| 920 | * |
||
| 921 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
| 922 | * There are two cases when alias id needs to be corrected: |
||
| 923 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
| 924 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
| 925 | * in standard installation) |
||
| 926 | * |
||
| 927 | * @param mixed $locationId |
||
| 928 | * |
||
| 929 | * @return mixed |
||
| 930 | */ |
||
| 931 | protected function getRealAliasId($locationId) |
||
| 949 | |||
| 950 | /** |
||
| 951 | * Recursively copies aliases from old parent under new parent. |
||
| 952 | * |
||
| 953 | * @param array $actionMap |
||
| 954 | * @param mixed $oldParentAliasId |
||
| 955 | * @param mixed $newParentAliasId |
||
| 956 | */ |
||
| 957 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
| 981 | |||
| 982 | /** |
||
| 983 | * @param mixed $oldParentId |
||
| 984 | * @param mixed $newParentId |
||
| 985 | * |
||
| 986 | * @return array |
||
| 987 | */ |
||
| 988 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
| 1000 | |||
| 1001 | public function locationDeleted($locationId): array |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Notifies the underlying engine that Locations Content Translation was removed. |
||
| 1022 | * |
||
| 1023 | * @param int[] $locationIds all Locations of the Content that got Translation removed |
||
| 1024 | * @param string $languageCode language code of the removed Translation |
||
| 1025 | */ |
||
| 1026 | public function translationRemoved(array $locationIds, $languageCode) |
||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Recursively removes aliases by given $id and $action. |
||
| 1039 | * |
||
| 1040 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
| 1041 | * |
||
| 1042 | * @param mixed $id |
||
| 1043 | * @param string $action |
||
| 1044 | * @param mixed $original |
||
| 1045 | */ |
||
| 1046 | protected function removeSubtree($id, $action, $original) |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * @param string $text |
||
| 1069 | * |
||
| 1070 | * @return string |
||
| 1071 | */ |
||
| 1072 | protected function getHash($text) |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * {@inheritdoc} |
||
| 1079 | */ |
||
| 1080 | public function archiveUrlAliasesForDeletedTranslations($locationId, $parentLocationId, array $languageCodes) |
||
| 1105 | |||
| 1106 | /** |
||
| 1107 | * Remove corrupted URL aliases (global, custom and system). |
||
| 1108 | * |
||
| 1109 | * @return int Number of removed URL aliases |
||
| 1110 | * |
||
| 1111 | * @throws \Exception |
||
| 1112 | */ |
||
| 1113 | public function deleteCorruptedUrlAliases() |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Attempt repairing auto-generated URL aliases for the given Location (including history). |
||
| 1133 | * |
||
| 1134 | * Note: it is assumed that at this point original, working, URL Alias for Location is published. |
||
| 1135 | * |
||
| 1136 | * @param int $locationId |
||
| 1137 | * |
||
| 1138 | * @throws \eZ\Publish\Core\Base\Exceptions\BadStateException |
||
| 1139 | */ |
||
| 1140 | public function repairBrokenUrlAliasesForLocation(int $locationId) |
||
| 1148 | |||
| 1149 | private function insertAliasEntryAsNop(array $aliasEntry): void |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Internal publish custom aliases method, accepting language mask to set correct language mask on url aliases |
||
| 1159 | * new alias ID (used when swapping Locations). |
||
| 1160 | */ |
||
| 1161 | private function internalPublishCustomUrlAliasForLocation(SwappedLocationProperties $location, int $languageMask) |
||
| 1186 | } |
||
| 1187 |
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.