Passed
Pull Request — master (#27)
by
unknown
01:53
created
tests/ExternalLinksTestPage.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -7,9 +7,9 @@
 block discarded – undo
7 7
 
8 8
 class ExternalLinksTestPage extends Page implements TestOnly
9 9
 {
10
-    private static $table_name = 'ExternalLinksTestPage';
10
+	private static $table_name = 'ExternalLinksTestPage';
11 11
 
12
-    private static $db = array(
13
-        'ExpectedContent' => 'HTMLText'
14
-    );
12
+	private static $db = array(
13
+		'ExpectedContent' => 'HTMLText'
14
+	);
15 15
 }
Please login to merge, or discard this patch.
src/Model/BrokenExternalPageTrack.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -13,27 +13,27 @@
 block discarded – undo
13 13
  */
14 14
 class BrokenExternalPageTrack extends DataObject
15 15
 {
16
-    private static $table_name = 'BrokenExternalPageTrack';
16
+	private static $table_name = 'BrokenExternalPageTrack';
17 17
 
18
-    private static $db = array(
19
-        'Processed' => 'Boolean'
20
-    );
18
+	private static $db = array(
19
+		'Processed' => 'Boolean'
20
+	);
21 21
 
22
-    private static $has_one = array(
23
-        'Page' => SiteTree::class,
24
-        'Status' => BrokenExternalPageTrackStatus::class
25
-    );
22
+	private static $has_one = array(
23
+		'Page' => SiteTree::class,
24
+		'Status' => BrokenExternalPageTrackStatus::class
25
+	);
26 26
 
27
-    private static $has_many = array(
28
-        'BrokenLinks' => BrokenExternalLink::class
29
-    );
27
+	private static $has_many = array(
28
+		'BrokenLinks' => BrokenExternalLink::class
29
+	);
30 30
 
31
-    /**
32
-     * @return SiteTree
33
-     */
34
-    public function Page()
35
-    {
36
-        return Versioned::get_by_stage(SiteTree::class, 'Stage')
37
-            ->byID($this->PageID);
38
-    }
31
+	/**
32
+	 * @return SiteTree
33
+	 */
34
+	public function Page()
35
+	{
36
+		return Versioned::get_by_stage(SiteTree::class, 'Stage')
37
+			->byID($this->PageID);
38
+	}
39 39
 }
Please login to merge, or discard this patch.
src/Tasks/LinkChecker.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -8,11 +8,11 @@
 block discarded – undo
8 8
 interface LinkChecker
9 9
 {
10 10
     
11
-    /**
12
-     * Determine the http status code for a given link
13
-     *
14
-     * @param string $href URL to check
15
-     * @return int HTTP status code, or null if not checkable (not a link)
16
-     */
17
-    public function checkLink($href);
11
+	/**
12
+	 * Determine the http status code for a given link
13
+	 *
14
+	 * @param string $href URL to check
15
+	 * @return int HTTP status code, or null if not checkable (not a link)
16
+	 */
17
+	public function checkLink($href);
18 18
 }
Please login to merge, or discard this patch.
src/Tasks/CurlLinkChecker.php 2 patches
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -10,47 +10,47 @@
 block discarded – undo
10 10
 class CurlLinkChecker implements LinkChecker
11 11
 {
12 12
 
13
-    /**
14
-     * Return cache
15
-     *
16
-     * @return Zend_Cache_Frontend
17
-     */
18
-    protected function getCache()
19
-    {
20
-        return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker');
21
-    }
22
-
23
-    /**
24
-     * Determine the http status code for a given link
25
-     *
26
-     * @param string $href URL to check
27
-     * @return int HTTP status code, or null if not checkable (not a link)
28
-     */
29
-    public function checkLink($href)
30
-    {
31
-        // Skip non-external links
32
-        if (!preg_match('/^https?[^:]*:\/\//', $href)) {
33
-            return null;
34
-        }
35
-
36
-        // Check if we have a cached result
37
-        $cacheKey = md5($href);
38
-        $result = $this->getCache()->get($cacheKey);
39
-        if ($result !== false) {
40
-            return $result;
41
-        }
42
-
43
-        // No cached result so just request
44
-        $handle = curl_init($href);
45
-        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
46
-        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
47
-        curl_setopt($handle, CURLOPT_TIMEOUT, 10);
48
-        curl_exec($handle);
49
-        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
50
-        curl_close($handle);
51
-
52
-        // Cache result
53
-        $this->getCache()->set($httpCode, $cacheKey);
54
-        return $httpCode;
55
-    }
13
+	/**
14
+	 * Return cache
15
+	 *
16
+	 * @return Zend_Cache_Frontend
17
+	 */
18
+	protected function getCache()
19
+	{
20
+		return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker');
21
+	}
22
+
23
+	/**
24
+	 * Determine the http status code for a given link
25
+	 *
26
+	 * @param string $href URL to check
27
+	 * @return int HTTP status code, or null if not checkable (not a link)
28
+	 */
29
+	public function checkLink($href)
30
+	{
31
+		// Skip non-external links
32
+		if (!preg_match('/^https?[^:]*:\/\//', $href)) {
33
+			return null;
34
+		}
35
+
36
+		// Check if we have a cached result
37
+		$cacheKey = md5($href);
38
+		$result = $this->getCache()->get($cacheKey);
39
+		if ($result !== false) {
40
+			return $result;
41
+		}
42
+
43
+		// No cached result so just request
44
+		$handle = curl_init($href);
45
+		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
46
+		curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
47
+		curl_setopt($handle, CURLOPT_TIMEOUT, 10);
48
+		curl_exec($handle);
49
+		$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
50
+		curl_close($handle);
51
+
52
+		// Cache result
53
+		$this->getCache()->set($httpCode, $cacheKey);
54
+		return $httpCode;
55
+	}
56 56
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@
 block discarded – undo
17 17
      */
18 18
     protected function getCache()
19 19
     {
20
-        return Injector::inst()->get(CacheInterface::class . '.CurlLinkChecker');
20
+        return Injector::inst()->get(CacheInterface::class.'.CurlLinkChecker');
21 21
     }
22 22
 
23 23
     /**
Please login to merge, or discard this patch.
src/Jobs/CheckExternalLinksJob.php 2 patches
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
8 8
 
9 9
 if (!class_exists(AbstractQueuedJob::class)) {
10
-    return;
10
+	return;
11 11
 }
12 12
 
13 13
 /**
@@ -17,30 +17,30 @@  discard block
 block discarded – undo
17 17
 class CheckExternalLinksJob extends AbstractQueuedJob implements QueuedJob
18 18
 {
19 19
 
20
-    public function getTitle()
21
-    {
22
-        return _t(__CLASS__ . '.TITLE', 'Checking for external broken links');
23
-    }
24
-
25
-    public function getJobType()
26
-    {
27
-        return QueuedJob::QUEUED;
28
-    }
29
-
30
-    public function getSignature()
31
-    {
32
-        return md5(get_class($this));
33
-    }
34
-
35
-    /**
36
-     * Check an individual page
37
-     */
38
-    public function process()
39
-    {
40
-        $task = CheckExternalLinksTask::create();
41
-        $track = $task->runLinksCheck(1);
42
-        $this->currentStep = $track->CompletedPages;
43
-        $this->totalSteps = $track->TotalPages;
44
-        $this->isComplete = $track->Status === 'Completed';
45
-    }
20
+	public function getTitle()
21
+	{
22
+		return _t(__CLASS__ . '.TITLE', 'Checking for external broken links');
23
+	}
24
+
25
+	public function getJobType()
26
+	{
27
+		return QueuedJob::QUEUED;
28
+	}
29
+
30
+	public function getSignature()
31
+	{
32
+		return md5(get_class($this));
33
+	}
34
+
35
+	/**
36
+	 * Check an individual page
37
+	 */
38
+	public function process()
39
+	{
40
+		$task = CheckExternalLinksTask::create();
41
+		$track = $task->runLinksCheck(1);
42
+		$this->currentStep = $track->CompletedPages;
43
+		$this->totalSteps = $track->TotalPages;
44
+		$this->isComplete = $track->Status === 'Completed';
45
+	}
46 46
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@
 block discarded – undo
19 19
 
20 20
     public function getTitle()
21 21
     {
22
-        return _t(__CLASS__ . '.TITLE', 'Checking for external broken links');
22
+        return _t(__CLASS__.'.TITLE', 'Checking for external broken links');
23 23
     }
24 24
 
25 25
     public function getJobType()
Please login to merge, or discard this patch.
tests/Stubs/PretendLinkChecker.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -5,24 +5,24 @@
 block discarded – undo
5 5
 
6 6
 class PretendLinkChecker implements LinkChecker
7 7
 {
8
-    public function checkLink($href)
9
-    {
10
-        switch ($href) {
11
-            case 'http://www.working.com':
12
-                return 200;
13
-            case 'http://www.broken.com':
14
-                return 403;
15
-            case 'http://www.nodomain.com':
16
-                return 0;
17
-            case '/internal/link':
18
-            case '[sitetree_link,id=9999]':
19
-            case 'home':
20
-            case 'broken-internal':
21
-            case '[sitetree_link,id=1]':
22
-                return null;
23
-            case 'http://www.broken.com/url/thing':
24
-            default:
25
-                return 404;
26
-        }
27
-    }
8
+	public function checkLink($href)
9
+	{
10
+		switch ($href) {
11
+			case 'http://www.working.com':
12
+				return 200;
13
+			case 'http://www.broken.com':
14
+				return 403;
15
+			case 'http://www.nodomain.com':
16
+				return 0;
17
+			case '/internal/link':
18
+			case '[sitetree_link,id=9999]':
19
+			case 'home':
20
+			case 'broken-internal':
21
+			case '[sitetree_link,id=1]':
22
+				return null;
23
+			case 'http://www.broken.com/url/thing':
24
+			default:
25
+				return 404;
26
+		}
27
+	}
28 28
 }
Please login to merge, or discard this patch.
tests/ExternalLinksTest.php 1 patch
Indentation   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -16,95 +16,95 @@
 block discarded – undo
16 16
 class ExternalLinksTest extends SapphireTest
17 17
 {
18 18
 
19
-    protected static $fixture_file = 'ExternalLinksTest.yml';
19
+	protected static $fixture_file = 'ExternalLinksTest.yml';
20 20
 
21
-    protected static $extra_dataobjects = array(
22
-        ExternalLinksTestPage::class
23
-    );
21
+	protected static $extra_dataobjects = array(
22
+		ExternalLinksTestPage::class
23
+	);
24 24
 
25
-    protected function setUp()
26
-    {
27
-        parent::setUp();
25
+	protected function setUp()
26
+	{
27
+		parent::setUp();
28 28
 
29
-        // Stub link checker
30
-        $checker = new PretendLinkChecker;
31
-        Injector::inst()->registerService($checker, LinkChecker::class);
32
-    }
29
+		// Stub link checker
30
+		$checker = new PretendLinkChecker;
31
+		Injector::inst()->registerService($checker, LinkChecker::class);
32
+	}
33 33
 
34
-    public function testLinks()
35
-    {
36
-        // Run link checker
37
-        $task = CheckExternalLinksTask::create();
38
-        $task->setSilent(true); // Be quiet during the test!
39
-        $task->runLinksCheck();
34
+	public function testLinks()
35
+	{
36
+		// Run link checker
37
+		$task = CheckExternalLinksTask::create();
38
+		$task->setSilent(true); // Be quiet during the test!
39
+		$task->runLinksCheck();
40 40
 
41
-        // Get all links checked
42
-        $status = BrokenExternalPageTrackStatus::get_latest();
43
-        $this->assertEquals('Completed', $status->Status);
44
-        $this->assertEquals(5, $status->TotalPages);
45
-        $this->assertEquals(5, $status->CompletedPages);
41
+		// Get all links checked
42
+		$status = BrokenExternalPageTrackStatus::get_latest();
43
+		$this->assertEquals('Completed', $status->Status);
44
+		$this->assertEquals(5, $status->TotalPages);
45
+		$this->assertEquals(5, $status->CompletedPages);
46 46
 
47
-        // Check all pages have had the correct HTML adjusted
48
-        for ($i = 1; $i <= 5; $i++) {
49
-            $page = $this->objFromFixture(ExternalLinksTestPage::class, 'page'.$i);
50
-            $this->assertNotEmpty($page->Content);
51
-            $this->assertEquals(
52
-                $page->ExpectedContent,
53
-                $page->Content,
54
-                "Assert that the content of page{$i} has been updated"
55
-            );
56
-        }
47
+		// Check all pages have had the correct HTML adjusted
48
+		for ($i = 1; $i <= 5; $i++) {
49
+			$page = $this->objFromFixture(ExternalLinksTestPage::class, 'page'.$i);
50
+			$this->assertNotEmpty($page->Content);
51
+			$this->assertEquals(
52
+				$page->ExpectedContent,
53
+				$page->Content,
54
+				"Assert that the content of page{$i} has been updated"
55
+			);
56
+		}
57 57
 
58
-        // Check that the correct report of broken links is generated
59
-        $links = $status
60
-            ->BrokenLinks()
61
-            ->sort('Link');
58
+		// Check that the correct report of broken links is generated
59
+		$links = $status
60
+			->BrokenLinks()
61
+			->sort('Link');
62 62
 
63
-        $this->assertEquals(4, $links->count());
64
-        $this->assertEquals(
65
-            array(
66
-                'http://www.broken.com',
67
-                'http://www.broken.com/url/thing',
68
-                'http://www.broken.com/url/thing',
69
-                'http://www.nodomain.com'
70
-            ),
71
-            array_values($links->map('ID', 'Link')->toArray())
72
-        );
63
+		$this->assertEquals(4, $links->count());
64
+		$this->assertEquals(
65
+			array(
66
+				'http://www.broken.com',
67
+				'http://www.broken.com/url/thing',
68
+				'http://www.broken.com/url/thing',
69
+				'http://www.nodomain.com'
70
+			),
71
+			array_values($links->map('ID', 'Link')->toArray())
72
+		);
73 73
 
74
-        // Check response codes are correct
75
-        $expected = array(
76
-            'http://www.broken.com' => 403,
77
-            'http://www.broken.com/url/thing' => 404,
78
-            'http://www.nodomain.com' => 0
79
-        );
80
-        $actual = $links->map('Link', 'HTTPCode')->toArray();
81
-        $this->assertEquals($expected, $actual);
74
+		// Check response codes are correct
75
+		$expected = array(
76
+			'http://www.broken.com' => 403,
77
+			'http://www.broken.com/url/thing' => 404,
78
+			'http://www.nodomain.com' => 0
79
+		);
80
+		$actual = $links->map('Link', 'HTTPCode')->toArray();
81
+		$this->assertEquals($expected, $actual);
82 82
 
83
-        // Check response descriptions are correct
84
-        i18n::set_locale('en_NZ');
85
-        $expected = array(
86
-            'http://www.broken.com' => '403 (Forbidden)',
87
-            'http://www.broken.com/url/thing' => '404 (Not Found)',
88
-            'http://www.nodomain.com' => '0 (Server Not Available)'
89
-        );
90
-        $actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
91
-        $this->assertEquals($expected, $actual);
92
-    }
83
+		// Check response descriptions are correct
84
+		i18n::set_locale('en_NZ');
85
+		$expected = array(
86
+			'http://www.broken.com' => '403 (Forbidden)',
87
+			'http://www.broken.com/url/thing' => '404 (Not Found)',
88
+			'http://www.nodomain.com' => '0 (Server Not Available)'
89
+		);
90
+		$actual = $links->map('Link', 'HTTPCodeDescription')->toArray();
91
+		$this->assertEquals($expected, $actual);
92
+	}
93 93
 
94
-    /**
95
-     * Test that broken links appears in the reports list
96
-     */
97
-    public function testReportExists()
98
-    {
99
-        $reports = Report::get_reports();
100
-        $reportNames = array();
101
-        foreach ($reports as $report) {
102
-            $reportNames[] = get_class($report);
103
-        }
104
-        $this->assertContains(
105
-            BrokenExternalLinksReport::class,
106
-            $reportNames,
107
-            'BrokenExternalLinksReport is in reports list'
108
-        );
109
-    }
94
+	/**
95
+	 * Test that broken links appears in the reports list
96
+	 */
97
+	public function testReportExists()
98
+	{
99
+		$reports = Report::get_reports();
100
+		$reportNames = array();
101
+		foreach ($reports as $report) {
102
+			$reportNames[] = get_class($report);
103
+		}
104
+		$this->assertContains(
105
+			BrokenExternalLinksReport::class,
106
+			$reportNames,
107
+			'BrokenExternalLinksReport is in reports list'
108
+		);
109
+	}
110 110
 }
Please login to merge, or discard this patch.
src/Tasks/CheckExternalLinksTask.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@
 block discarded – undo
17 17
 {
18 18
 
19 19
     private static $dependencies = [
20
-        'LinkChecker' => '%$' . LinkChecker::class
20
+        'LinkChecker' => '%$'.LinkChecker::class
21 21
     ];
22 22
 
23 23
     /**
Please login to merge, or discard this patch.
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -18,213 +18,213 @@
 block discarded – undo
18 18
 class CheckExternalLinksTask extends BuildTask
19 19
 {
20 20
 
21
-    private static $dependencies = [
22
-        'LinkChecker' => '%$' . LinkChecker::class
23
-    ];
24
-
25
-    /**
26
-     * @var bool
27
-     */
28
-    protected $silent = false;
29
-
30
-    /**
31
-     * @var LinkChecker
32
-     */
33
-    protected $linkChecker;
34
-
35
-    protected $title = 'Checking broken External links in the SiteTree';
36
-
37
-    protected $description = 'A task that records external broken links in the SiteTree';
38
-
39
-    protected $enabled = true;
40
-
41
-    /**
42
-     * Log a message
43
-     *
44
-     * @param string $message
45
-     */
46
-    protected function log($message)
47
-    {
48
-        if (!$this->silent) {
49
-            Debug::message($message);
50
-        }
51
-    }
52
-
53
-    public function run($request)
54
-    {
55
-        $this->runLinksCheck();
56
-    }
57
-    /**
58
-     * Turn on or off message output
59
-     *
60
-     * @param bool $silent
61
-     */
62
-    public function setSilent($silent)
63
-    {
64
-        $this->silent = $silent;
65
-    }
66
-
67
-    /**
68
-     * @param LinkChecker $linkChecker
69
-     */
70
-    public function setLinkChecker(LinkChecker $linkChecker)
71
-    {
72
-        $this->linkChecker = $linkChecker;
73
-    }
74
-
75
-    /**
76
-     * @return LinkChecker
77
-     */
78
-    public function getLinkChecker()
79
-    {
80
-        return $this->linkChecker;
81
-    }
82
-
83
-    /**
84
-     * Check the status of a single link on a page
85
-     *
86
-     * @param BrokenExternalPageTrack $pageTrack
87
-     * @param DOMNode $link
88
-     */
89
-    protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
90
-    {
91
-        $class = $link->getAttribute('class');
92
-        $href = $link->getAttribute('href');
93
-        $markedBroken = preg_match('/\b(ss-broken)\b/', $class);
94
-
95
-        // Check link
96
-        $httpCode = $this->linkChecker->checkLink($href);
97
-        if ($httpCode === null) {
98
-            return; // Null link means uncheckable, such as an internal link
99
-        }
100
-
101
-        // If this code is broken then mark as such
102
-        if ($foundBroken = $this->isCodeBroken($httpCode)) {
103
-            // Create broken record
104
-            $brokenLink = new BrokenExternalLink();
105
-            $brokenLink->Link = $href;
106
-            $brokenLink->HTTPCode = $httpCode;
107
-            $brokenLink->TrackID = $pageTrack->ID;
108
-            $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
109
-            $brokenLink->write();
110
-        }
111
-
112
-        // Check if we need to update CSS class, otherwise return
113
-        if ($markedBroken == $foundBroken) {
114
-            return;
115
-        }
116
-        if ($foundBroken) {
117
-            $class .= ' ss-broken';
118
-        } else {
119
-            $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
120
-        }
121
-        $link->setAttribute('class', trim($class));
122
-    }
123
-
124
-    /**
125
-     * Determine if the given HTTP code is "broken"
126
-     *
127
-     * @param int $httpCode
128
-     * @return bool True if this is a broken code
129
-     */
130
-    protected function isCodeBroken($httpCode)
131
-    {
132
-        // Null represents no request attempted
133
-        if ($httpCode === null) {
134
-            return false;
135
-        }
136
-
137
-        // do we have any whitelisted codes
138
-        $ignoreCodes = $this->config()->get('IgnoreCodes');
139
-        if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
140
-            return false;
141
-        }
142
-
143
-        // Check if code is outside valid range
144
-        return $httpCode < 200 || $httpCode > 302;
145
-    }
146
-
147
-    /**
148
-     * Runs the links checker and returns the track used
149
-     *
150
-     * @param int $limit Limit to number of pages to run, or null to run all
151
-     * @return BrokenExternalPageTrackStatus
152
-     */
153
-    public function runLinksCheck($limit = null)
154
-    {
155
-        // Check the current status
156
-        $status = BrokenExternalPageTrackStatus::get_or_create();
157
-
158
-        // Calculate pages to run
159
-        $pageTracks = $status->getIncompleteTracks();
160
-        if ($limit) {
161
-            $pageTracks = $pageTracks->limit($limit);
162
-        }
163
-
164
-        // Check each page
165
-        foreach ($pageTracks as $pageTrack) {
166
-            // Flag as complete
167
-            $pageTrack->Processed = 1;
168
-            $pageTrack->write();
169
-
170
-            // Check value of html area
171
-            $page = $pageTrack->Page();
172
-            $this->log("Checking {$page->Title}");
173
-            $htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
174
-            if (!$htmlValue->isValid()) {
175
-                continue;
176
-            }
177
-
178
-            // Check each link
179
-            $links = $htmlValue->getElementsByTagName('a');
180
-            foreach ($links as $link) {
181
-                $this->checkPageLink($pageTrack, $link);
182
-            }
183
-
184
-            // Update content of page based on link fixes / breakages
185
-            $htmlValue->saveHTML();
186
-            $page->Content = $htmlValue->getContent();
187
-            $page->write();
188
-
189
-            // Once all links have been created for this page update HasBrokenLinks
190
-            $count = $pageTrack->BrokenLinks()->count();
191
-            $this->log("Found {$count} broken links");
192
-            if ($count) {
193
-                $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class);
194
-                // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
195
-                DB::query(sprintf(
196
-                    'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
197
-                    $siteTreeTable,
198
-                    intval($pageTrack->ID)
199
-                ));
200
-            }
201
-        }
202
-
203
-        $status->updateJobInfo('Updating completed pages');
204
-        $status->updateStatus();
205
-        return $status;
206
-    }
207
-
208
-    private function updateCompletedPages($trackID = 0)
209
-    {
210
-        $noPages = BrokenExternalPageTrack::get()
211
-            ->filter(array(
212
-                'TrackID' => $trackID,
213
-                'Processed' => 1
214
-            ))
215
-            ->count();
216
-        $track = BrokenExternalPageTrackStatus::get_latest();
217
-        $track->CompletedPages = $noPages;
218
-        $track->write();
219
-        return $noPages;
220
-    }
221
-
222
-    private function updateJobInfo($message)
223
-    {
224
-        $track = BrokenExternalPageTrackStatus::get_latest();
225
-        if ($track) {
226
-            $track->JobInfo = $message;
227
-            $track->write();
228
-        }
229
-    }
21
+	private static $dependencies = [
22
+		'LinkChecker' => '%$' . LinkChecker::class
23
+	];
24
+
25
+	/**
26
+	 * @var bool
27
+	 */
28
+	protected $silent = false;
29
+
30
+	/**
31
+	 * @var LinkChecker
32
+	 */
33
+	protected $linkChecker;
34
+
35
+	protected $title = 'Checking broken External links in the SiteTree';
36
+
37
+	protected $description = 'A task that records external broken links in the SiteTree';
38
+
39
+	protected $enabled = true;
40
+
41
+	/**
42
+	 * Log a message
43
+	 *
44
+	 * @param string $message
45
+	 */
46
+	protected function log($message)
47
+	{
48
+		if (!$this->silent) {
49
+			Debug::message($message);
50
+		}
51
+	}
52
+
53
+	public function run($request)
54
+	{
55
+		$this->runLinksCheck();
56
+	}
57
+	/**
58
+	 * Turn on or off message output
59
+	 *
60
+	 * @param bool $silent
61
+	 */
62
+	public function setSilent($silent)
63
+	{
64
+		$this->silent = $silent;
65
+	}
66
+
67
+	/**
68
+	 * @param LinkChecker $linkChecker
69
+	 */
70
+	public function setLinkChecker(LinkChecker $linkChecker)
71
+	{
72
+		$this->linkChecker = $linkChecker;
73
+	}
74
+
75
+	/**
76
+	 * @return LinkChecker
77
+	 */
78
+	public function getLinkChecker()
79
+	{
80
+		return $this->linkChecker;
81
+	}
82
+
83
+	/**
84
+	 * Check the status of a single link on a page
85
+	 *
86
+	 * @param BrokenExternalPageTrack $pageTrack
87
+	 * @param DOMNode $link
88
+	 */
89
+	protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
90
+	{
91
+		$class = $link->getAttribute('class');
92
+		$href = $link->getAttribute('href');
93
+		$markedBroken = preg_match('/\b(ss-broken)\b/', $class);
94
+
95
+		// Check link
96
+		$httpCode = $this->linkChecker->checkLink($href);
97
+		if ($httpCode === null) {
98
+			return; // Null link means uncheckable, such as an internal link
99
+		}
100
+
101
+		// If this code is broken then mark as such
102
+		if ($foundBroken = $this->isCodeBroken($httpCode)) {
103
+			// Create broken record
104
+			$brokenLink = new BrokenExternalLink();
105
+			$brokenLink->Link = $href;
106
+			$brokenLink->HTTPCode = $httpCode;
107
+			$brokenLink->TrackID = $pageTrack->ID;
108
+			$brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
109
+			$brokenLink->write();
110
+		}
111
+
112
+		// Check if we need to update CSS class, otherwise return
113
+		if ($markedBroken == $foundBroken) {
114
+			return;
115
+		}
116
+		if ($foundBroken) {
117
+			$class .= ' ss-broken';
118
+		} else {
119
+			$class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
120
+		}
121
+		$link->setAttribute('class', trim($class));
122
+	}
123
+
124
+	/**
125
+	 * Determine if the given HTTP code is "broken"
126
+	 *
127
+	 * @param int $httpCode
128
+	 * @return bool True if this is a broken code
129
+	 */
130
+	protected function isCodeBroken($httpCode)
131
+	{
132
+		// Null represents no request attempted
133
+		if ($httpCode === null) {
134
+			return false;
135
+		}
136
+
137
+		// do we have any whitelisted codes
138
+		$ignoreCodes = $this->config()->get('IgnoreCodes');
139
+		if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
140
+			return false;
141
+		}
142
+
143
+		// Check if code is outside valid range
144
+		return $httpCode < 200 || $httpCode > 302;
145
+	}
146
+
147
+	/**
148
+	 * Runs the links checker and returns the track used
149
+	 *
150
+	 * @param int $limit Limit to number of pages to run, or null to run all
151
+	 * @return BrokenExternalPageTrackStatus
152
+	 */
153
+	public function runLinksCheck($limit = null)
154
+	{
155
+		// Check the current status
156
+		$status = BrokenExternalPageTrackStatus::get_or_create();
157
+
158
+		// Calculate pages to run
159
+		$pageTracks = $status->getIncompleteTracks();
160
+		if ($limit) {
161
+			$pageTracks = $pageTracks->limit($limit);
162
+		}
163
+
164
+		// Check each page
165
+		foreach ($pageTracks as $pageTrack) {
166
+			// Flag as complete
167
+			$pageTrack->Processed = 1;
168
+			$pageTrack->write();
169
+
170
+			// Check value of html area
171
+			$page = $pageTrack->Page();
172
+			$this->log("Checking {$page->Title}");
173
+			$htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
174
+			if (!$htmlValue->isValid()) {
175
+				continue;
176
+			}
177
+
178
+			// Check each link
179
+			$links = $htmlValue->getElementsByTagName('a');
180
+			foreach ($links as $link) {
181
+				$this->checkPageLink($pageTrack, $link);
182
+			}
183
+
184
+			// Update content of page based on link fixes / breakages
185
+			$htmlValue->saveHTML();
186
+			$page->Content = $htmlValue->getContent();
187
+			$page->write();
188
+
189
+			// Once all links have been created for this page update HasBrokenLinks
190
+			$count = $pageTrack->BrokenLinks()->count();
191
+			$this->log("Found {$count} broken links");
192
+			if ($count) {
193
+				$siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class);
194
+				// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
195
+				DB::query(sprintf(
196
+					'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
197
+					$siteTreeTable,
198
+					intval($pageTrack->ID)
199
+				));
200
+			}
201
+		}
202
+
203
+		$status->updateJobInfo('Updating completed pages');
204
+		$status->updateStatus();
205
+		return $status;
206
+	}
207
+
208
+	private function updateCompletedPages($trackID = 0)
209
+	{
210
+		$noPages = BrokenExternalPageTrack::get()
211
+			->filter(array(
212
+				'TrackID' => $trackID,
213
+				'Processed' => 1
214
+			))
215
+			->count();
216
+		$track = BrokenExternalPageTrackStatus::get_latest();
217
+		$track->CompletedPages = $noPages;
218
+		$track->write();
219
+		return $noPages;
220
+	}
221
+
222
+	private function updateJobInfo($message)
223
+	{
224
+		$track = BrokenExternalPageTrackStatus::get_latest();
225
+		if ($track) {
226
+			$track->JobInfo = $message;
227
+			$track->write();
228
+		}
229
+	}
230 230
 }
Please login to merge, or discard this patch.
src/Reports/BrokenExternalLinksReport.php 2 patches
Indentation   +74 added lines, -74 removed lines patch added patch discarded remove patch
@@ -18,85 +18,85 @@
 block discarded – undo
18 18
 class BrokenExternalLinksReport extends Report
19 19
 {
20 20
 
21
-    /**
22
-     * Returns the report title
23
-     *
24
-     * @return string
25
-     */
26
-    public function title()
27
-    {
28
-        return _t(__CLASS__ . '.EXTERNALBROKENLINKS', "External broken links report");
29
-    }
21
+	/**
22
+	 * Returns the report title
23
+	 *
24
+	 * @return string
25
+	 */
26
+	public function title()
27
+	{
28
+		return _t(__CLASS__ . '.EXTERNALBROKENLINKS', "External broken links report");
29
+	}
30 30
 
31
-    public function columns()
32
-    {
33
-        return array(
34
-            "Created" => "Checked",
35
-            'Link' => array(
36
-                'title' => 'External Link',
37
-                'formatting' => function ($value, $item) {
38
-                    return sprintf(
39
-                        '<a target="_blank" href="%s">%s</a>',
40
-                        Convert::raw2att($item->Link),
41
-                        Convert::raw2xml($item->Link)
42
-                    );
43
-                }
44
-            ),
45
-            'HTTPCodeDescription' => 'HTTP Error Code',
46
-            "Title" => array(
47
-                "title" => 'Page link is on',
48
-                'formatting' => function ($value, $item) {
49
-                    $page = $item->Page();
50
-                    return sprintf(
51
-                        '<a href="%s">%s</a>',
52
-                        Convert::raw2att($page->CMSEditLink()),
53
-                        Convert::raw2xml($page->Title)
54
-                    );
55
-                }
56
-            )
57
-        );
58
-    }
31
+	public function columns()
32
+	{
33
+		return array(
34
+			"Created" => "Checked",
35
+			'Link' => array(
36
+				'title' => 'External Link',
37
+				'formatting' => function ($value, $item) {
38
+					return sprintf(
39
+						'<a target="_blank" href="%s">%s</a>',
40
+						Convert::raw2att($item->Link),
41
+						Convert::raw2xml($item->Link)
42
+					);
43
+				}
44
+			),
45
+			'HTTPCodeDescription' => 'HTTP Error Code',
46
+			"Title" => array(
47
+				"title" => 'Page link is on',
48
+				'formatting' => function ($value, $item) {
49
+					$page = $item->Page();
50
+					return sprintf(
51
+						'<a href="%s">%s</a>',
52
+						Convert::raw2att($page->CMSEditLink()),
53
+						Convert::raw2xml($page->Title)
54
+					);
55
+				}
56
+			)
57
+		);
58
+	}
59 59
 
60
-    /**
61
-     * Alias of columns(), to support the export to csv action
62
-     * in {@link GridFieldExportButton} generateExportFileData method.
63
-     * @return array
64
-     */
65
-    public function getColumns()
66
-    {
67
-        return $this->columns();
68
-    }
60
+	/**
61
+	 * Alias of columns(), to support the export to csv action
62
+	 * in {@link GridFieldExportButton} generateExportFileData method.
63
+	 * @return array
64
+	 */
65
+	public function getColumns()
66
+	{
67
+		return $this->columns();
68
+	}
69 69
 
70
-    public function sourceRecords()
71
-    {
72
-        $track = BrokenExternalPageTrackStatus::get_latest();
73
-        if ($track) {
74
-            return $track->BrokenLinks();
75
-        }
76
-        return ArrayList::create();
77
-    }
70
+	public function sourceRecords()
71
+	{
72
+		$track = BrokenExternalPageTrackStatus::get_latest();
73
+		if ($track) {
74
+			return $track->BrokenLinks();
75
+		}
76
+		return ArrayList::create();
77
+	}
78 78
 
79
-    public function getCMSFields()
80
-    {
81
-        Requirements::javascript('silverstripe/externallinks: javascript/BrokenExternalLinksReport.js');
82
-        $fields = parent::getCMSFields();
79
+	public function getCMSFields()
80
+	{
81
+		Requirements::javascript('silverstripe/externallinks: javascript/BrokenExternalLinksReport.js');
82
+		$fields = parent::getCMSFields();
83 83
 
84
-        $reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
85
-        $reportResult = LiteralField::create('ResultTitle', $reportResultSpan);
86
-        $fields->push($reportResult);
84
+		$reportResultSpan = '</ br></ br><h3 id="ReportHolder"></h3>';
85
+		$reportResult = LiteralField::create('ResultTitle', $reportResultSpan);
86
+		$fields->push($reportResult);
87 87
 
88
-        $button = HTML::createTag(
89
-            'button',
90
-            [
91
-                'id' => 'externalLinksReport',
92
-                'type' => 'button',
93
-                'class' => 'btn btn-primary'
94
-            ],
95
-            _t(__CLASS__ . '.RUNREPORT', 'Create new report')
96
-        );
97
-        $runReportButton = LiteralField::create('runReport', $button);
98
-        $fields->push($runReportButton);
88
+		$button = HTML::createTag(
89
+			'button',
90
+			[
91
+				'id' => 'externalLinksReport',
92
+				'type' => 'button',
93
+				'class' => 'btn btn-primary'
94
+			],
95
+			_t(__CLASS__ . '.RUNREPORT', 'Create new report')
96
+		);
97
+		$runReportButton = LiteralField::create('runReport', $button);
98
+		$fields->push($runReportButton);
99 99
 
100
-        return $fields;
101
-    }
100
+		return $fields;
101
+	}
102 102
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
      */
26 26
     public function title()
27 27
     {
28
-        return _t(__CLASS__ . '.EXTERNALBROKENLINKS', "External broken links report");
28
+        return _t(__CLASS__.'.EXTERNALBROKENLINKS', "External broken links report");
29 29
     }
30 30
 
31 31
     public function columns()
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
             "Created" => "Checked",
35 35
             'Link' => array(
36 36
                 'title' => 'External Link',
37
-                'formatting' => function ($value, $item) {
37
+                'formatting' => function($value, $item) {
38 38
                     return sprintf(
39 39
                         '<a target="_blank" href="%s">%s</a>',
40 40
                         Convert::raw2att($item->Link),
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
             'HTTPCodeDescription' => 'HTTP Error Code',
46 46
             "Title" => array(
47 47
                 "title" => 'Page link is on',
48
-                'formatting' => function ($value, $item) {
48
+                'formatting' => function($value, $item) {
49 49
                     $page = $item->Page();
50 50
                     return sprintf(
51 51
                         '<a href="%s">%s</a>',
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
                 'type' => 'button',
93 93
                 'class' => 'btn btn-primary'
94 94
             ],
95
-            _t(__CLASS__ . '.RUNREPORT', 'Create new report')
95
+            _t(__CLASS__.'.RUNREPORT', 'Create new report')
96 96
         );
97 97
         $runReportButton = LiteralField::create('runReport', $button);
98 98
         $fields->push($runReportButton);
Please login to merge, or discard this patch.