typo3_core_add_uniqueInGroups_option.patch

Johannes Ender, 2020-01-10 10:46

Download (4.17 KB)

View differences:

Classes/DataHandling/SlugHelper.php (date 1578649322835)
327 327
        return true;
328 328
    }
329 329

  
330
    /**
331
     * Check if there are other records with the same slug that are located in defined pid groups.
332
     *
333
     * @param string $slug
334
     * @param RecordState $state
335
     * @return bool
336
     * @throws \TYPO3\CMS\Core\Exception\SiteNotFoundException
337
     */
338
    public function isUniqueInGroups(string $slug, RecordState $state): bool
339
    {
340
        $pageId = (int)$state->resolveNodeIdentifier();
341
        $recordId = $state->getSubject()->getIdentifier();
342
        $languageId = $state->getContext()->getLanguageId();
343

  
344
        if ($pageId < 0) {
345
            $pageId = $this->resolveLivePageId($recordId);
346
        }
347

  
348
        $queryBuilder = $this->createPreparedQueryBuilder();
349
        $this->applySlugConstraint($queryBuilder, $slug);
350
        $relevantPids = [$pageId];
351
        foreach ($this->configuration["generatorOptions"]["pidGroups"] as $pidGroup) {
352
            if (in_array($pageId, $pidGroup)) {
353
                foreach ($pidGroup as $pid) {
354
                    if (!in_array($pid, $relevantPids)) {
355
                        $relevantPids[] = $pid;
356
                    }
357
                }
358
            }
359
        }
360
        $this->applyPageIdConstraint($queryBuilder, $relevantPids);
361
        $this->applyRecordConstraint($queryBuilder, $recordId);
362
        if ($languageId != -1) {
363
            $this->applyLanguageConstraint($queryBuilder, [-1, $languageId]);
364
        }
365
        $this->applyWorkspaceConstraint($queryBuilder);
366
        $statement = $queryBuilder->execute();
367

  
368
        $records = $this->resolveVersionOverlays(
369
            $statement->fetchAll()
370
        );
371
        return count($records) === 0;
372
    }
373

  
330 374
    /**
331 375
     * Generate a slug with a suffix "/mytitle-1" if that is in use already.
332 376
     *
......
380 424
        return $newValue;
381 425
    }
382 426

  
427
    /**
428
     * Generate a slug with a suffix "/mytitle-1" if the suggested slug is in use already.
429
     *
430
     * @param string $slug proposed slug
431
     * @param RecordState $state
432
     * @return string
433
     */
434
    public function buildSlugForUniqueInGroups(string $slug, RecordState $state): string
435
    {
436
        $slug = $this->sanitize($slug);
437
        $rawValue = $this->extract($slug);
438
        $newValue = $slug;
439
        $counter = 0;
440
        while (!$this->isUniqueInGroups(
441
            $newValue,
442
            $state
443
        ) && $counter++ < 100
444
        ) {
445
            $newValue = $this->sanitize($rawValue . '-' . $counter);
446
        }
447
        if ($counter === 100) {
448
            $newValue = $this->sanitize($rawValue . '-' . GeneralUtility::shortMD5($rawValue));
449
        }
450
        return $newValue;
451
    }
452

  
383 453
    /**
384 454
     * @return QueryBuilder
385 455
     */
......
478 548

  
479 549
    /**
480 550
     * @param QueryBuilder $queryBuilder
481
     * @param int $pageId
551
     * @param int|array $pageId
482 552
     */
483
    protected function applyPageIdConstraint(QueryBuilder $queryBuilder, int $pageId)
553
    protected function applyPageIdConstraint(QueryBuilder $queryBuilder, $pageId)
484 554
    {
485 555
        if ($pageId < 0) {
486 556
            throw new \RuntimeException(
......
492 562
            );
493 563
        }
494 564

  
495
        $queryBuilder->andWhere(
496
            $queryBuilder->expr()->eq(
497
                'pid',
498
                $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
499
            )
500
        );
565
        if (is_array($pageId)) {
566
            $queryBuilder->andWhere(
567
                $queryBuilder->expr()->in(
568
                    'pid',
569
                    $queryBuilder->createNamedParameter($pageId, Connection::PARAM_INT_ARRAY)
570
                )
571
            );
572
        } else {
573
            $queryBuilder->andWhere(
574
                $queryBuilder->expr()->eq(
575
                    'pid',
576
                    $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
577
                )
578
            );
579
        }
501 580
    }
502 581

  
503 582
    /**