1 | <?php |
||
40 | class RemoveOrphanedPagesTask extends Controller { |
||
41 | |||
42 | private static $allowed_actions = array( |
||
|
|||
43 | 'index' => 'ADMIN', |
||
44 | 'Form' => 'ADMIN', |
||
45 | 'run' => 'ADMIN', |
||
46 | 'handleAction' => 'ADMIN', |
||
47 | ); |
||
48 | |||
49 | protected $title = 'Removed orphaned pages without existing parents from both stage and live'; |
||
50 | |||
51 | protected $description = " |
||
52 | <p> |
||
53 | Identify 'orphaned' pages which point to a parent |
||
54 | that no longer exists in a specific stage. |
||
55 | </p> |
||
56 | <p> |
||
57 | Caution: Pages also count as orphans if they don't |
||
58 | have parents in this stage, even if the parent has a representation |
||
59 | in the other stage:<br /> |
||
60 | - A live child is orphaned if its parent was deleted from live, but still exists on stage<br /> |
||
61 | - A stage child is orphaned if its parent was deleted from stage, but still exists on live |
||
62 | </p> |
||
63 | "; |
||
64 | |||
65 | protected $orphanedSearchClass = 'SilverStripe\\CMS\\Model\\SiteTree'; |
||
66 | |||
67 | public function init() { |
||
68 | parent::init(); |
||
69 | |||
70 | if(!Permission::check('ADMIN')) { |
||
71 | Security::permissionFailure($this); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | public function Link($action = null) |
||
76 | { |
||
77 | /** @skipUpgrade */ |
||
78 | return Controller::join_links('RemoveOrphanedPagesTask', $action, '/'); |
||
79 | } |
||
80 | |||
81 | public function index() { |
||
88 | |||
89 | public function Form() { |
||
90 | $fields = new FieldList(); |
||
91 | $source = array(); |
||
92 | |||
93 | $fields->push(new HeaderField( |
||
94 | 'Header', |
||
95 | _t('RemoveOrphanedPagesTask.HEADER', 'Remove all orphaned pages task') |
||
96 | )); |
||
97 | $fields->push(new LiteralField( |
||
98 | 'Description', |
||
99 | $this->description |
||
100 | )); |
||
101 | |||
102 | $orphans = $this->getOrphanedPages($this->orphanedSearchClass); |
||
103 | if($orphans) foreach($orphans as $orphan) { |
||
104 | /** @var SiteTree $latestVersion */ |
||
105 | $latestVersion = Versioned::get_latest_version($this->orphanedSearchClass, $orphan->ID); |
||
106 | $latestAuthor = DataObject::get_by_id('SilverStripe\\Security\\Member', $latestVersion->AuthorID); |
||
107 | $orphanBaseTable = DataObject::getSchema()->baseDataTable($this->orphanedSearchClass); |
||
108 | $liveRecord = Versioned::get_one_by_stage( |
||
109 | $this->orphanedSearchClass, |
||
110 | 'Live', |
||
111 | array("\"$orphanBaseTable\".\"ID\"" => $orphan->ID) |
||
112 | ); |
||
113 | $label = sprintf( |
||
114 | '<a href="admin/pages/edit/show/%d">%s</a> <small>(#%d, Last Modified Date: %s, Last Modifier: %s, %s)</small>', |
||
115 | $orphan->ID, |
||
116 | $orphan->Title, |
||
117 | $orphan->ID, |
||
118 | $orphan->dbObject('LastEdited')->Nice(), |
||
119 | ($latestAuthor) ? $latestAuthor->Title : 'unknown', |
||
120 | ($liveRecord) ? 'is published' : 'not published' |
||
121 | ); |
||
122 | $source[$orphan->ID] = $label; |
||
123 | } |
||
124 | |||
125 | if($orphans && $orphans->count()) { |
||
126 | $fields->push(new CheckboxSetField('OrphanIDs', false, $source)); |
||
127 | $fields->push(new LiteralField( |
||
128 | 'SelectAllLiteral', |
||
129 | sprintf( |
||
130 | '<p><a href="#" onclick="javascript:jQuery(\'#Form_Form_OrphanIDs :checkbox\').attr(\'checked\', \'checked\'); return false;">%s</a> ', |
||
131 | _t('RemoveOrphanedPagesTask.SELECTALL', 'select all') |
||
132 | ) |
||
133 | )); |
||
134 | $fields->push(new LiteralField( |
||
135 | 'UnselectAllLiteral', |
||
136 | sprintf( |
||
137 | '<a href="#" onclick="javascript:jQuery(\'#Form_Form_OrphanIDs :checkbox\').attr(\'checked\', \'\'); return false;">%s</a></p>', |
||
138 | _t('RemoveOrphanedPagesTask.UNSELECTALL', 'unselect all') |
||
139 | ) |
||
140 | )); |
||
141 | $fields->push(new OptionsetField( |
||
142 | 'OrphanOperation', |
||
143 | _t('RemoveOrphanedPagesTask.CHOOSEOPERATION', 'Choose operation:'), |
||
144 | array( |
||
145 | 'rebase' => _t( |
||
146 | 'RemoveOrphanedPagesTask.OPERATION_REBASE', |
||
147 | sprintf( |
||
148 | 'Rebase selected to a new holder page "%s" and unpublish. None of these pages will show up for website visitors.', |
||
149 | $this->rebaseHolderTitle() |
||
150 | ) |
||
151 | ), |
||
152 | 'remove' => _t('RemoveOrphanedPagesTask.OPERATION_REMOVE', 'Remove selected from all stages (WARNING: Will destroy all selected pages from both stage and live)'), |
||
153 | ), |
||
154 | 'rebase' |
||
155 | )); |
||
156 | $fields->push(new LiteralField( |
||
157 | 'Warning', |
||
158 | sprintf('<p class="message">%s</p>', |
||
159 | _t( |
||
160 | 'RemoveOrphanedPagesTask.DELETEWARNING', |
||
161 | 'Warning: These operations are not reversible. Please handle with care.' |
||
162 | ) |
||
163 | ) |
||
164 | )); |
||
165 | } else { |
||
166 | $fields->push(new LiteralField( |
||
167 | 'NotFoundLabel', |
||
168 | sprintf( |
||
169 | '<p class="message">%s</p>', |
||
170 | _t('RemoveOrphanedPagesTask.NONEFOUND', 'No orphans found') |
||
171 | ) |
||
172 | )); |
||
173 | } |
||
174 | |||
175 | $form = new Form( |
||
176 | $this, |
||
177 | 'SilverStripe\\Forms\\Form', |
||
178 | $fields, |
||
179 | new FieldList( |
||
180 | new FormAction('doSubmit', _t('RemoveOrphanedPagesTask.BUTTONRUN', 'Run')) |
||
181 | ) |
||
182 | ); |
||
183 | |||
184 | if(!$orphans || !$orphans->count()) { |
||
185 | $form->makeReadonly(); |
||
186 | } |
||
187 | |||
188 | return $form; |
||
189 | } |
||
190 | |||
191 | public function run($request) { |
||
194 | |||
195 | public function doSubmit($data, $form) { |
||
228 | |||
229 | protected function removeOrphans($orphanIDs) { |
||
261 | |||
262 | protected function rebaseHolderTitle() { |
||
265 | |||
266 | protected function rebaseOrphans($orphanIDs) { |
||
267 | $holder = new SiteTree(); |
||
268 | $holder->ShowInMenus = 0; |
||
319 | |||
320 | /** |
||
321 | * Gets all orphans from "Stage" and "Live" stages. |
||
322 | * |
||
323 | * @param string $class |
||
324 | * @param array $filter |
||
325 | * @param string $sort |
||
326 | * @param string $join |
||
327 | * @param int|array $limit |
||
328 | * @return SS_List |
||
329 | */ |
||
330 | public function getOrphanedPages($class = 'SilverStripe\\CMS\\Model\\SiteTree', $filter = array(), $sort = null, $join = null, $limit = null) { |
||
361 | } |
||
362 |