Total Complexity | 86 |
Total Lines | 524 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SiteTreeSubsites often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SiteTreeSubsites, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class SiteTreeSubsites extends DataExtension |
||
35 | { |
||
36 | private static $has_one = [ |
||
37 | 'Subsite' => Subsite::class, // The subsite that this page belongs to |
||
38 | ]; |
||
39 | |||
40 | private static $many_many = [ |
||
41 | 'CrossSubsiteLinkTracking' => SiteTree::class // Stored separately, as the logic for URL rewriting is different |
||
42 | ]; |
||
43 | |||
44 | private static $many_many_extraFields = [ |
||
45 | 'CrossSubsiteLinkTracking' => ['FieldName' => 'Varchar'] |
||
46 | ]; |
||
47 | |||
48 | public function isMainSite() |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Update any requests to limit the results to the current site |
||
55 | * @param SQLSelect $query |
||
56 | * @param DataQuery $dataQuery |
||
57 | */ |
||
58 | public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) |
||
59 | { |
||
60 | if (Subsite::$disable_subsite_filter) { |
||
61 | return; |
||
62 | } |
||
63 | if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) { |
||
64 | return; |
||
65 | } |
||
66 | |||
67 | // If you're querying by ID, ignore the sub-site - this is a bit ugly... |
||
68 | // if(!$query->where |
||
69 | // || (strpos($query->where[0], ".\"ID\" = ") === false |
||
70 | // && strpos($query->where[0], ".`ID` = ") === false && strpos($query->where[0], ".ID = ") === false |
||
71 | // && strpos($query->where[0], "ID = ") !== 0)) { |
||
72 | if ($query->filtersOnID()) { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | $subsiteID = null; |
||
77 | if (Subsite::$force_subsite) { |
||
78 | $subsiteID = Subsite::$force_subsite; |
||
79 | } else { |
||
80 | $subsiteID = SubsiteState::singleton()->getSubsiteId(); |
||
81 | } |
||
82 | |||
83 | if ($subsiteID === null) { |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | // The foreach is an ugly way of getting the first key :-) |
||
88 | foreach ($query->getFrom() as $tableName => $info) { |
||
89 | // The tableName should be SiteTree or SiteTree_Live... |
||
90 | $siteTreeTableName = SiteTree::getSchema()->tableName(SiteTree::class); |
||
91 | if (strpos($tableName, $siteTreeTableName) === false) { |
||
92 | break; |
||
93 | } |
||
94 | $query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)"); |
||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public function onBeforeWrite() |
||
106 | } |
||
107 | |||
108 | public function updateCMSFields(FieldList $fields) |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Does the basic duplication, but doesn't write anything this means we can subclass this easier and do more |
||
174 | * complex relation duplication. |
||
175 | * |
||
176 | * Note that when duplicating including children, everything is written. |
||
177 | * |
||
178 | * @param Subsite|int $subsiteID |
||
179 | * @param bool $includeChildren |
||
180 | * @return SiteTree |
||
181 | */ |
||
182 | public function duplicateToSubsitePrep($subsiteID, $includeChildren) |
||
183 | { |
||
184 | if (is_object($subsiteID)) { |
||
185 | $subsiteID = $subsiteID->ID; |
||
186 | } |
||
187 | |||
188 | return SubsiteState::singleton() |
||
189 | ->withState(function (SubsiteState $newState) use ($subsiteID, $includeChildren) { |
||
190 | $newState->setSubsiteId($subsiteID); |
||
191 | |||
192 | /** @var SiteTree $page */ |
||
193 | $page = $this->owner; |
||
194 | |||
195 | try { |
||
196 | // We have no idea what the ParentID should be, but it shouldn't be the same as it was since |
||
197 | // we're now in a different subsite. As a workaround use the url-segment and subsite ID. |
||
198 | if ($page->Parent()) { |
||
199 | $parentSeg = $page->Parent()->URLSegment; |
||
200 | $newParentPage = Page::get()->filter('URLSegment', $parentSeg)->first(); |
||
201 | $originalParentID = $page->ParentID; |
||
202 | if ($newParentPage) { |
||
203 | $page->ParentID = (int) $newParentPage->ID; |
||
204 | } else { |
||
205 | // reset it to the top level, so the user can decide where to put it |
||
206 | $page->ParentID = 0; |
||
207 | } |
||
208 | } |
||
209 | |||
210 | // Disable query filtering by subsite during actual duplication |
||
211 | $originalFilter = Subsite::$disable_subsite_filter; |
||
212 | Subsite::disable_subsite_filter(true); |
||
213 | |||
214 | return $includeChildren ? $page->duplicateWithChildren() : $page->duplicate(false); |
||
215 | } finally { |
||
216 | Subsite::disable_subsite_filter($originalFilter); |
||
217 | |||
218 | // Re-set the original parent ID for the current page |
||
219 | $page->ParentID = $originalParentID; |
||
220 | } |
||
221 | }); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * When duplicating a page, assign the current subsite ID from the state |
||
226 | */ |
||
227 | public function onBeforeDuplicate() |
||
228 | { |
||
229 | $subsiteId = SubsiteState::singleton()->getSubsiteId(); |
||
230 | if ($subsiteId !== null) { |
||
231 | $this->owner->SubsiteID = $subsiteId; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Create a duplicate of this page and save it to another subsite |
||
237 | * |
||
238 | * @param Subsite|int $subsiteID The Subsite to copy to, or its ID |
||
239 | * @param boolean $includeChildren Whether to duplicate child pages too |
||
240 | * @return SiteTree The duplicated page |
||
241 | */ |
||
242 | public function duplicateToSubsite($subsiteID = null, $includeChildren = false) |
||
243 | { |
||
244 | $clone = $this->owner->duplicateToSubsitePrep($subsiteID, $includeChildren); |
||
245 | $clone->invokeWithExtensions('onBeforeDuplicateToSubsite', $this->owner); |
||
246 | |||
247 | if (!$includeChildren) { |
||
248 | // Write the new page if "include children" is false, because it is written by default when it's true. |
||
249 | $clone->write(); |
||
250 | } |
||
251 | // Deprecated: manually duplicate any configured relationships |
||
252 | $clone->duplicateSubsiteRelations($this->owner); |
||
253 | |||
254 | $clone->invokeWithExtensions('onAfterDuplicateToSubsite', $this->owner); |
||
255 | |||
256 | return $clone; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Duplicate relations using a static property to define |
||
261 | * which ones we want to duplicate |
||
262 | * |
||
263 | * It may be that some relations are not diostinct to sub site so can stay |
||
264 | * whereas others may need to be duplicated |
||
265 | * |
||
266 | * @deprecated 2.2..3.0 Use the "cascade_duplicates" config API instead |
||
267 | * @param SiteTree $originalPage |
||
268 | */ |
||
269 | public function duplicateSubsiteRelations($originalPage) |
||
270 | { |
||
271 | $thisClass = $originalPage->ClassName; |
||
272 | $relations = Config::inst()->get($thisClass, 'duplicate_to_subsite_relations'); |
||
273 | |||
274 | if ($relations && !empty($relations)) { |
||
275 | foreach ($relations as $relation) { |
||
276 | $items = $originalPage->$relation(); |
||
277 | foreach ($items as $item) { |
||
278 | $duplicateItem = $item->duplicate(false); |
||
279 | $duplicateItem->{$thisClass.'ID'} = $this->owner->ID; |
||
280 | $duplicateItem->write(); |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @return SiteConfig |
||
288 | */ |
||
289 | public function alternateSiteConfig() |
||
290 | { |
||
291 | if (!$this->owner->SubsiteID) { |
||
292 | return false; |
||
293 | } |
||
294 | $sc = DataObject::get_one(SiteConfig::class, '"SubsiteID" = ' . $this->owner->SubsiteID); |
||
295 | if (!$sc) { |
||
296 | $sc = new SiteConfig(); |
||
297 | $sc->SubsiteID = $this->owner->SubsiteID; |
||
298 | $sc->Title = _t('SilverStripe\\Subsites\\Model\\Subsite.SiteConfigTitle', 'Your Site Name'); |
||
299 | $sc->Tagline = _t('SilverStripe\\Subsites\\Model\\Subsite.SiteConfigSubtitle', 'Your tagline here'); |
||
300 | $sc->write(); |
||
301 | } |
||
302 | return $sc; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Only allow editing of a page if the member satisfies one of the following conditions: |
||
307 | * - Is in a group which has access to the subsite this page belongs to |
||
308 | * - Is in a group with edit permissions on the "main site" |
||
309 | * |
||
310 | * If there are no subsites configured yet, this logic is skipped. |
||
311 | * |
||
312 | * @param Member|null $member |
||
313 | * @return bool|null |
||
314 | */ |
||
315 | public function canEdit($member = null) |
||
316 | { |
||
317 | if (!$member) { |
||
318 | $member = Security::getCurrentUser(); |
||
319 | } |
||
320 | |||
321 | // Do not provide any input if there are no subsites configured |
||
322 | if (!Subsite::get()->exists()) { |
||
323 | return null; |
||
324 | } |
||
325 | |||
326 | // Find the sites that this user has access to |
||
327 | $goodSites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true, 'all', $member)->column('ID'); |
||
328 | |||
329 | if (!is_null($this->owner->SubsiteID)) { |
||
330 | $subsiteID = $this->owner->SubsiteID; |
||
331 | } else { |
||
332 | // The relationships might not be available during the record creation when using a GridField. |
||
333 | // In this case the related objects will have empty fields, and SubsiteID will not be available. |
||
334 | // |
||
335 | // We do the second best: fetch the likely SubsiteID from the session. The drawback is this might |
||
336 | // make it possible to force relations to point to other (forbidden) subsites. |
||
337 | $subsiteID = SubsiteState::singleton()->getSubsiteId(); |
||
338 | } |
||
339 | |||
340 | // Return true if they have access to this object's site |
||
341 | if (!(in_array(0, $goodSites) || in_array($subsiteID, $goodSites))) { |
||
342 | return false; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @param null $member |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function canDelete($member = null) |
||
351 | { |
||
352 | if (!$member && $member !== false) { |
||
353 | $member = Security::getCurrentUser(); |
||
354 | } |
||
355 | |||
356 | return $this->canEdit($member); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param null $member |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function canAddChildren($member = null) |
||
364 | { |
||
365 | if (!$member && $member !== false) { |
||
366 | $member = Security::getCurrentUser(); |
||
367 | } |
||
368 | |||
369 | return $this->canEdit($member); |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @param Member|null $member |
||
374 | * @return bool|null |
||
375 | */ |
||
376 | public function canPublish($member = null) |
||
377 | { |
||
378 | if (!$member && $member !== false) { |
||
379 | $member = Security::getCurrentUser(); |
||
380 | } |
||
381 | |||
382 | return $this->canEdit($member); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Called by ContentController::init(); |
||
387 | * @param $controller |
||
388 | */ |
||
389 | public static function contentcontrollerInit($controller) |
||
390 | { |
||
391 | $subsite = Subsite::currentSubsite(); |
||
392 | |||
393 | if ($subsite && $subsite->Theme) { |
||
394 | SSViewer::add_themes([$subsite->Theme]); |
||
395 | } |
||
396 | |||
397 | if ($subsite && i18n::getData()->validate($subsite->Language)) { |
||
398 | i18n::set_locale($subsite->Language); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @param null $action |
||
404 | * @return string |
||
405 | */ |
||
406 | public function alternateAbsoluteLink($action = null) |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Use the CMS domain for iframed CMS previews to prevent single-origin violations |
||
419 | * and SSL cert problems. Always set SubsiteID to avoid errors because a page doesn't |
||
420 | * exist on the CMS domain. |
||
421 | * |
||
422 | * @param string &$link |
||
423 | * @param string|null $action |
||
424 | * @return string |
||
425 | */ |
||
426 | public function updatePreviewLink(&$link, $action = null) |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * This function is marked as deprecated for removal in 5.0.0 in silverstripe/cms |
||
435 | * so now simply passes execution to where the functionality exists for backwards compatiblity. |
||
436 | * CMS 4.0.0 SiteTree already throws a SilverStripe deprecation error before calling this function. |
||
437 | * @deprecated 2.2...3.0 use updatePreviewLink instead |
||
438 | * |
||
439 | * @param string|null $action |
||
440 | * @return string |
||
441 | */ |
||
442 | public function alternatePreviewLink($action = null) |
||
443 | { |
||
444 | $link = ''; |
||
445 | return $this->updatePreviewLink($link, $action); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Inject the subsite ID into the content so it can be used by frontend scripts. |
||
450 | * @param $tags |
||
451 | * @return string |
||
452 | */ |
||
453 | public function MetaTags(&$tags) |
||
454 | { |
||
455 | if ($this->owner->SubsiteID) { |
||
456 | $tags .= '<meta name="x-subsite-id" content="' . $this->owner->SubsiteID . "\" />\n"; |
||
457 | } |
||
458 | |||
459 | return $tags; |
||
460 | } |
||
461 | |||
462 | public function augmentSyncLinkTracking() |
||
463 | { |
||
464 | // Set LinkTracking appropriately |
||
465 | $links = HTTP::getLinksIn($this->owner->Content); |
||
466 | $linkedPages = []; |
||
467 | |||
468 | if ($links) { |
||
469 | foreach ($links as $link) { |
||
470 | if (substr($link, 0, strlen('http://')) == 'http://') { |
||
471 | $withoutHttp = substr($link, strlen('http://')); |
||
472 | if (strpos($withoutHttp, '/') && strpos($withoutHttp, '/') < strlen($withoutHttp)) { |
||
473 | $domain = substr($withoutHttp, 0, strpos($withoutHttp, '/')); |
||
474 | $rest = substr($withoutHttp, strpos($withoutHttp, '/') + 1); |
||
475 | |||
476 | $subsiteID = Subsite::getSubsiteIDForDomain($domain); |
||
477 | if ($subsiteID == 0) { |
||
478 | continue; |
||
479 | } // We have no idea what the domain for the main site is, so cant track links to it |
||
480 | |||
481 | $origDisableSubsiteFilter = Subsite::$disable_subsite_filter; |
||
482 | Subsite::disable_subsite_filter(true); |
||
483 | $candidatePage = DataObject::get_one( |
||
484 | SiteTree::class, |
||
485 | "\"URLSegment\" = '" |
||
486 | . Convert::raw2sql(urldecode($rest)) |
||
487 | . "' AND \"SubsiteID\" = " |
||
488 | . $subsiteID, |
||
489 | false |
||
490 | ); |
||
491 | Subsite::disable_subsite_filter($origDisableSubsiteFilter); |
||
492 | |||
493 | if ($candidatePage) { |
||
494 | $linkedPages[] = $candidatePage->ID; |
||
495 | } else { |
||
496 | $this->owner->HasBrokenLink = true; |
||
497 | } |
||
498 | } |
||
499 | } |
||
500 | } |
||
501 | } |
||
502 | |||
503 | $this->owner->CrossSubsiteLinkTracking()->setByIDList($linkedPages); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Ensure that valid url segments are checked within the correct subsite of the owner object, |
||
508 | * even if the current subsiteID is set to some other subsite. |
||
509 | * |
||
510 | * @return null|bool Either true or false, or null to not influence result |
||
511 | */ |
||
512 | public function augmentValidURLSegment() |
||
513 | { |
||
514 | // If this page is being filtered in the current subsite, then no custom validation query is required. |
||
515 | $subsite = Subsite::$force_subsite ?: SubsiteState::singleton()->getSubsiteId(); |
||
516 | if (empty($this->owner->SubsiteID) || $subsite == $this->owner->SubsiteID) { |
||
517 | return null; |
||
518 | } |
||
519 | |||
520 | // Backup forced subsite |
||
521 | $prevForceSubsite = Subsite::$force_subsite; |
||
522 | Subsite::$force_subsite = $this->owner->SubsiteID; |
||
523 | |||
524 | // Repeat validation in the correct subsite |
||
525 | $isValid = $this->owner->validURLSegment(); |
||
526 | |||
527 | // Restore |
||
528 | Subsite::$force_subsite = $prevForceSubsite; |
||
529 | |||
530 | return (bool)$isValid; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Return a piece of text to keep DataObject cache keys appropriately specific |
||
535 | */ |
||
536 | public function cacheKeyComponent() |
||
539 | } |
||
540 | |||
541 | /** |
||
542 | * @param Member $member |
||
543 | * @return boolean|null |
||
544 | */ |
||
545 | public function canCreate($member = null) |
||
558 | } |
||
559 | } |
||
560 | } |
||
561 | } |
||
562 |
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