| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\DevToolkit\Tasks; |
||
| 4 | |||
| 5 | use SilverStripe\ORM\DB; |
||
| 6 | use SilverStripe\Dev\BuildTask; |
||
| 7 | use SilverStripe\Security\Group; |
||
| 8 | use LeKoala\DevToolkit\BuildTaskTools; |
||
| 9 | use SilverStripe\Subsites\Model\Subsite; |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | /** |
||
| 12 | * @author LeKoala <[email protected]> |
||
| 13 | */ |
||
| 14 | class RemoveEmptyGroupsTask extends BuildTask |
||
| 15 | { |
||
| 16 | use BuildTaskTools; |
||
| 17 | |||
| 18 | protected $title = "Remove empty/duplicate groups from the cms"; |
||
| 19 | private static $segment = 'RemoveEmptyGroupsTask'; |
||
|
0 ignored issues
–
show
|
|||
| 20 | |||
| 21 | public function run($request) |
||
| 22 | { |
||
| 23 | $this->request = $request; |
||
| 24 | $groups = Group::get(); |
||
| 25 | echo 'Pass ?drop=1 to drop groups without members<br/>'; |
||
| 26 | echo 'Want more dropping? Pass ?permission=1 to also drop groups without permissions even if they have members<br/>'; |
||
| 27 | echo 'Pass ?merge=1 to merge groups with the same code<br/>'; |
||
| 28 | echo 'Want to merge across subsites ? Pass ?subsite=1 to disable subsite filters<br/>'; |
||
| 29 | |||
| 30 | echo '<hr/>'; |
||
| 31 | $merge = $request->getVar('merge'); |
||
| 32 | $drop = $request->getVar('drop'); |
||
| 33 | $dropNoPermission = $request->getVar('permission'); |
||
| 34 | $subsite = $request->getVar('subsite'); |
||
| 35 | |||
| 36 | if (class_exists(Subsite::class) && $subsite) { |
||
| 37 | Subsite::$disable_subsite_filter = true; |
||
| 38 | } |
||
| 39 | |||
| 40 | if ($drop) { |
||
| 41 | DB::alteration_message("Dropping groups with no members"); |
||
| 42 | if ($dropNoPermission) { |
||
| 43 | DB::alteration_message("Also dropping groups with no permissions"); |
||
| 44 | } |
||
| 45 | foreach ($groups as $group) { |
||
| 46 | if (!$group->Members()->count()) { |
||
| 47 | DB::alteration_message( |
||
| 48 | "Removing group {$group->ID} because it has no members", |
||
| 49 | "deleted" |
||
| 50 | ); |
||
| 51 | $group->delete(); |
||
| 52 | } |
||
| 53 | if ($dropNoPermission) { |
||
| 54 | $c = $group->Permissions()->count(); |
||
| 55 | if (!$c) { |
||
| 56 | DB::alteration_message( |
||
| 57 | "Removing group {$group->ID} because it has no permissions", |
||
| 58 | "deleted" |
||
| 59 | ); |
||
| 60 | $group->delete(); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | if ($merge) { |
||
| 66 | DB::alteration_message("Merging groups with duplicated codes"); |
||
| 67 | $index = array(); |
||
| 68 | |||
| 69 | /* @var $group Group */ |
||
| 70 | foreach ($groups as $group) { |
||
| 71 | DB::alteration_message("Found group " . $group->Code); |
||
| 72 | if (!isset($index[$group->Code])) { |
||
| 73 | $index[$group->Code] = $group; |
||
| 74 | DB::alteration_message("First instance of group, do not merge"); |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | $mergeGroup = $index[$group->Code]; |
||
| 79 | |||
| 80 | |||
| 81 | DB::alteration_message( |
||
| 82 | 'Merge group ' . $group->ID . ' with ' . $mergeGroup->ID, |
||
| 83 | 'repaired' |
||
| 84 | ); |
||
| 85 | |||
| 86 | $i = 0; |
||
| 87 | foreach ($group->Members() as $m) { |
||
| 88 | $i++; |
||
| 89 | $mergeGroup->Members()->add($m); |
||
| 90 | } |
||
| 91 | DB::alteration_message( |
||
| 92 | 'Added ' . $i . ' members to group', |
||
| 93 | 'created' |
||
| 94 | ); |
||
| 95 | |||
| 96 | DB::alteration_message( |
||
| 97 | "Group " . $group->ID . ' was deleted', |
||
| 98 | 'deleted' |
||
| 99 | ); |
||
| 100 | $group->delete(); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | DB::alteration_message('All done!'); |
||
| 105 | } |
||
| 106 | } |
||
| 107 |
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