1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\DuplicateURLSegments; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
|
|
|
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Dev\BuildTask; |
8
|
|
|
use SilverStripe\ORM\DB; |
9
|
|
|
use SilverStripe\Versioned\Versioned; |
|
|
|
|
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
|
|
|
$cleanUrlSegment = $page->generateURLSegment($page->Title); |
63
|
|
|
if ($cleanUrlSegment !== $page->URLSegment) { |
64
|
|
|
DB::alteration_message($this->pageObjectToLink($page)); |
65
|
|
|
|
66
|
|
|
if ($this->forReal) { |
67
|
|
|
$page->URLSegment = $cleanUrlSegment; |
68
|
|
|
$isPublished = $page->isPublished(); |
69
|
|
|
$page->writeToStage(Versioned::DRAFT); |
70
|
|
|
if ($isPublished) { |
71
|
|
|
$page->publishSingle(); |
72
|
|
|
} |
73
|
|
|
DB::alteration_message('... FIXED! '); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function pageObjectToLink($page): string |
79
|
|
|
{ |
80
|
|
|
if (Director::is_cli()) { |
81
|
|
|
$v = $page->Link(); |
82
|
|
|
} else { |
83
|
|
|
$v = '<a href="' . $page->CMSEditLink() . '">✎</a> <a href="' . $page->Link() . '">' . $page->Link() . ': ' . $page->Title . '</a>'; |
84
|
|
|
} |
85
|
|
|
return str_replace('?stage=Stage', '', $v); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
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