Source of file CheckAllTemplates.php
Size: 4,309 Bytes - Last Modified: 2021-12-23T10:47:10+00:00
/var/www/docs.ssmods.com/process/src/src/Tasks/CheckAllTemplates.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | <?php namespace Sunnysideup\TemplateOverview\Tasks; use SilverStripe\Control\Director; use SilverStripe\Core\Environment; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\BuildTask; use SilverStripe\ORM\ArrayList; use SilverStripe\Security\Permission; use SilverStripe\View\ArrayData; use SilverStripe\View\Requirements; use SilverStripe\View\SSViewer; use SilverStripe\View\ViewableData; use Sunnysideup\TemplateOverview\Api\AllLinks; class CheckAllTemplates extends BuildTask { protected $title = 'Check URLs for HTTP errors'; protected $description = 'Will go through main URLs (all page types (e.g Page, MyPageTemplate), all page types in CMS (e.g. edit Page, edit HomePage, new MyPage) and all models being edited in ModelAdmin, checking for HTTP response errors (e.g. 404). Click start to run.'; private static $segment = 'smoketest'; /** * Main function * has two streams: * 1. check on url specified in GET variable. * 2. create a list of urls to check. * * @param \SilverStripe\Control\HTTPRequest $request */ public function run($request) { ini_set('max_execution_time', 3000); //we have this check here so that even in dev mode you have to log in. //because if you do not log in, the test will not work. if (! Permission::check('ADMIN')) { die('Please <a href="/Security/login/?BackURL=/dev/tasks/smoketest/">log in</a> first.'); return; } $allLinks = Injector::inst()->get(AllLinks::class)->getAllLinks(); if (! empty($_GET['htmllist'])) { $this->htmlListOutput($allLinks); return; } $this->defaultOutput($allLinks); } protected function defaultOutput(array $allLinks) { $count = 0; $sections = ['allNonCMSLinks', 'allCMSLinks']; $links = ArrayList::create(); foreach ($sections as $isCMSLink => $sectionVariable) { foreach ($allLinks[$sectionVariable] as $link) { ++$count; $links->push(ArrayData::create([ 'IsCMSLink' => $isCMSLink, 'Link' => $link, 'ItemCount' => $count, ])); } } $otherLinks = ''; $className = ''; foreach ($allLinks['otherLinks'] as $linkArray) { if ($linkArray['ClassName'] !== $className) { $className = $linkArray['ClassName']; $otherLinks .= '</ul><h2>' . $className . '</h2><ul>'; } $otherLinks .= '<li><a href="' . $linkArray['Link'] . '">' . $linkArray['Link'] . '</a></li>'; } Requirements::javascript('https://code.jquery.com/jquery-3.3.1.min.js'); Requirements::javascript('sunnysideup/templateoverview:client/javascript/checkalltemplates.js'); Requirements::themedCSS('client/css/checkalltemplates'); $template = new SSViewer('CheckAllTemplates'); echo $template->process( ViewableData::create(), [ 'Title' => $this->title, 'Links' => $links, 'OtherLinks' => $otherLinks, 'AbsoluteBaseURLMinusSlash' => $this->baseURL(), 'HasEnvironmentVariable' => ((bool) Environment::getEnv('SS_ALLOW_SMOKE_TEST')), ] ); } protected function baseURL() { return trim(Director::absoluteBaseURL(), '/'); } protected function htmlListOutput(array $allLinks) { $base = $this->baseURL(); $array = []; foreach ($allLinks as $key => $list) { foreach ($list as $item) { if ('otherLinks' === $key) { $link = $base . $item['Link']; $title = $base . $item['Link']; } else { $link = $base . $item; $title = $base . $item; } $array[$link] = '<a href="' . $link . '">' . $title . '</a>'; } } if (count($array) > 0) { ksort($array); echo '<ol><li>' . implode('</li><li>', $array) . '</li></ol>'; } else { echo 'No links available'; } } } |