sunnysideup /
silverstripe-duplicate-urlsegments
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Sunnysideup\DuplicateURLSegments; |
||
| 4 | |||
| 5 | use SilverStripe\CMS\Model\SiteTree; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use SilverStripe\Control\Director; |
||
| 7 | use SilverStripe\Dev\BuildTask; |
||
| 8 | use SilverStripe\ORM\DB; |
||
| 9 | use SilverStripe\Versioned\Versioned; |
||
|
0 ignored issues
–
show
The type
SilverStripe\Versioned\Versioned was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 10 | use SilverStripe\View\Parsers\URLSegmentFilter; |
||
| 11 | |||
| 12 | class UrlSegmentFixer extends BuildTask |
||
| 13 | { |
||
| 14 | protected $title = 'Remove -2, -3, -4, -5, etc... from URLSegment'; |
||
| 15 | |||
| 16 | protected $description = 'Removes unnecessary appendixes from Page URLSegments'; |
||
| 17 | |||
| 18 | protected $enabled = true; |
||
| 19 | |||
| 20 | protected $forReal = false; |
||
| 21 | protected $max = 9; |
||
| 22 | |||
| 23 | private static $segment = 'urlsegmentfixer'; |
||
| 24 | |||
| 25 | public function setForReal(bool $b) |
||
| 26 | { |
||
| 27 | $this->forReal = $b; |
||
| 28 | return $this; |
||
| 29 | } |
||
| 30 | |||
| 31 | public function setMax(int $max) |
||
| 32 | { |
||
| 33 | $this->max = $max; |
||
| 34 | return $this; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function run($request) |
||
| 38 | { |
||
| 39 | if ($request->getVar('go')) { |
||
| 40 | $this->forReal = true; |
||
| 41 | } |
||
| 42 | |||
| 43 | if ($this->forReal) { |
||
| 44 | echo '<h4>Running for real!</h4>'; |
||
| 45 | } else { |
||
| 46 | echo '<h4>Test Only - <a href="?go=1">run for real</a></h4>'; |
||
| 47 | } |
||
| 48 | |||
| 49 | $i = 1; |
||
| 50 | while ($i < $this->max) { |
||
| 51 | ++$i; |
||
| 52 | $appendix = '-' . $i; |
||
| 53 | $list = SiteTree::get()->filter(['URLSegment:EndsWith' => $appendix]); |
||
| 54 | foreach ($list as $page) { |
||
| 55 | $this->fixOnePage($page); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | public function fixOnePage($page) |
||
| 61 | { |
||
| 62 | $old = $page->URLSegment; |
||
| 63 | $cleanUrlSegment = $page->generateURLSegment($page->Title); |
||
| 64 | if ($cleanUrlSegment !== $old) { |
||
| 65 | DB::alteration_message($this->pageObjectToLink($page)); |
||
| 66 | |||
| 67 | if ($this->forReal) { |
||
| 68 | $page->URLSegment = $cleanUrlSegment; |
||
| 69 | $isPublished = $page->isPublished(); |
||
| 70 | $page->writeToStage(Versioned::DRAFT); |
||
| 71 | if ($isPublished) { |
||
| 72 | $page->publishSingle(); |
||
| 73 | } |
||
| 74 | $page = SiteTree::get()->byID($page->ID); |
||
| 75 | if ($page->URLSegment === $cleanUrlSegment) { |
||
| 76 | DB::alteration_message('... FIXED! from ' . $old . ' to ' . $cleanUrlSegment, 'created'); |
||
| 77 | } else { |
||
| 78 | DB::alteration_message('... COULD NOT FIX from ' . $old . ' to ' . $cleanUrlSegment, 'deleted'); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | protected function pageObjectToLink($page): string |
||
| 85 | { |
||
| 86 | if (Director::is_cli()) { |
||
| 87 | $v = $page->Link(); |
||
| 88 | } else { |
||
| 89 | $v = '<a href="' . $page->CMSEditLink() . '">✎</a> <a href="' . $page->Link() . '">' . $page->Link() . ': ' . $page->Title . '</a>'; |
||
| 90 | } |
||
| 91 | return str_replace('?stage=Stage', '', $v); |
||
| 92 | } |
||
| 93 | |||
| 94 | } |
||
| 95 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths