Passed
Pull Request — master (#27)
by
unknown
01:54
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
Indentation   +207 added lines, -207 removed lines patch added patch discarded remove patch
@@ -16,211 +16,211 @@
 block discarded – undo
16 16
 class CheckExternalLinksTask extends BuildTask
17 17
 {
18 18
 
19
-    private static $dependencies = [
20
-        'LinkChecker' => '%$' . LinkChecker::class
21
-    ];
22
-
23
-    /**
24
-     * @var bool
25
-     */
26
-    protected $silent = false;
27
-
28
-    /**
29
-     * @var LinkChecker
30
-     */
31
-    protected $linkChecker;
32
-
33
-    protected $title = 'Checking broken External links in the SiteTree';
34
-
35
-    protected $description = 'A task that records external broken links in the SiteTree';
36
-
37
-    protected $enabled = true;
38
-
39
-    /**
40
-     * Log a message
41
-     *
42
-     * @param string $message
43
-     */
44
-    protected function log($message)
45
-    {
46
-        if (!$this->silent) {
47
-            Debug::message($message);
48
-        }
49
-    }
50
-
51
-    public function run($request)
52
-    {
53
-        $this->runLinksCheck();
54
-    }
55
-    /**
56
-     * Turn on or off message output
57
-     *
58
-     * @param bool $silent
59
-     */
60
-    public function setSilent($silent)
61
-    {
62
-        $this->silent = $silent;
63
-    }
64
-
65
-    /**
66
-     * @param LinkChecker $linkChecker
67
-     */
68
-    public function setLinkChecker(LinkChecker $linkChecker)
69
-    {
70
-        $this->linkChecker = $linkChecker;
71
-    }
72
-
73
-    /**
74
-     * @return LinkChecker
75
-     */
76
-    public function getLinkChecker()
77
-    {
78
-        return $this->linkChecker;
79
-    }
80
-
81
-    /**
82
-     * Check the status of a single link on a page
83
-     *
84
-     * @param BrokenExternalPageTrack $pageTrack
85
-     * @param DOMNode $link
86
-     */
87
-    protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
88
-    {
89
-        $class = $link->getAttribute('class');
90
-        $href = $link->getAttribute('href');
91
-        $markedBroken = preg_match('/\b(ss-broken)\b/', $class);
92
-
93
-        // Check link
94
-        $httpCode = $this->linkChecker->checkLink($href);
95
-        if ($httpCode === null) {
96
-            return; // Null link means uncheckable, such as an internal link
97
-        }
98
-
99
-        // If this code is broken then mark as such
100
-        if ($foundBroken = $this->isCodeBroken($httpCode)) {
101
-            // Create broken record
102
-            $brokenLink = new BrokenExternalLink();
103
-            $brokenLink->Link = $href;
104
-            $brokenLink->HTTPCode = $httpCode;
105
-            $brokenLink->TrackID = $pageTrack->ID;
106
-            $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
107
-            $brokenLink->write();
108
-        }
109
-
110
-        // Check if we need to update CSS class, otherwise return
111
-        if ($markedBroken == $foundBroken) {
112
-            return;
113
-        }
114
-        if ($foundBroken) {
115
-            $class .= ' ss-broken';
116
-        } else {
117
-            $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
118
-        }
119
-        $link->setAttribute('class', trim($class));
120
-    }
121
-
122
-    /**
123
-     * Determine if the given HTTP code is "broken"
124
-     *
125
-     * @param int $httpCode
126
-     * @return bool True if this is a broken code
127
-     */
128
-    protected function isCodeBroken($httpCode)
129
-    {
130
-        // Null represents no request attempted
131
-        if ($httpCode === null) {
132
-            return false;
133
-        }
134
-
135
-        // do we have any whitelisted codes
136
-        $ignoreCodes = $this->config()->get('IgnoreCodes');
137
-        if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
138
-            return false;
139
-        }
140
-
141
-        // Check if code is outside valid range
142
-        return $httpCode < 200 || $httpCode > 302;
143
-    }
144
-
145
-    /**
146
-     * Runs the links checker and returns the track used
147
-     *
148
-     * @param int $limit Limit to number of pages to run, or null to run all
149
-     * @return BrokenExternalPageTrackStatus
150
-     */
151
-    public function runLinksCheck($limit = null)
152
-    {
153
-        // Check the current status
154
-        $status = BrokenExternalPageTrackStatus::get_or_create();
155
-
156
-        // Calculate pages to run
157
-        $pageTracks = $status->getIncompleteTracks();
158
-        if ($limit) {
159
-            $pageTracks = $pageTracks->limit($limit);
160
-        }
161
-
162
-        // Check each page
163
-        foreach ($pageTracks as $pageTrack) {
164
-            // Flag as complete
165
-            $pageTrack->Processed = 1;
166
-            $pageTrack->write();
167
-
168
-            // Check value of html area
169
-            $page = $pageTrack->Page();
170
-            $this->log("Checking {$page->Title}");
171
-            $htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
172
-            if (!$htmlValue->isValid()) {
173
-                continue;
174
-            }
175
-
176
-            // Check each link
177
-            $links = $htmlValue->getElementsByTagName('a');
178
-            foreach ($links as $link) {
179
-                $this->checkPageLink($pageTrack, $link);
180
-            }
181
-
182
-            // Update content of page based on link fixes / breakages
183
-            $htmlValue->saveHTML();
184
-            $page->Content = $htmlValue->getContent();
185
-            $page->write();
186
-
187
-            // Once all links have been created for this page update HasBrokenLinks
188
-            $count = $pageTrack->BrokenLinks()->count();
189
-            $this->log("Found {$count} broken links");
190
-            if ($count) {
191
-                // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
192
-                DB::query(sprintf(
193
-                    'UPDATE "SiteTree" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
194
-                    intval($pageTrack->ID)
195
-                ));
196
-            }
197
-        }
198
-
199
-        $status->updateJobInfo('Updating completed pages');
200
-        $status->updateStatus();
201
-        return $status;
202
-    }
203
-
204
-    private function updateCompletedPages($trackID = 0)
205
-    {
206
-        $noPages = BrokenExternalPageTrack::get()
207
-            ->filter(array(
208
-                'TrackID' => $trackID,
209
-                'Processed' => 1
210
-            ))
211
-            ->count();
212
-        $track = BrokenExternalPageTrackStatus::get_latest();
213
-        $track->CompletedPages = $noPages;
214
-        $track->write();
215
-        return $noPages;
216
-    }
217
-
218
-    private function updateJobInfo($message)
219
-    {
220
-        $track = BrokenExternalPageTrackStatus::get_latest();
221
-        if ($track) {
222
-            $track->JobInfo = $message;
223
-            $track->write();
224
-        }
225
-    }
19
+	private static $dependencies = [
20
+		'LinkChecker' => '%$' . LinkChecker::class
21
+	];
22
+
23
+	/**
24
+	 * @var bool
25
+	 */
26
+	protected $silent = false;
27
+
28
+	/**
29
+	 * @var LinkChecker
30
+	 */
31
+	protected $linkChecker;
32
+
33
+	protected $title = 'Checking broken External links in the SiteTree';
34
+
35
+	protected $description = 'A task that records external broken links in the SiteTree';
36
+
37
+	protected $enabled = true;
38
+
39
+	/**
40
+	 * Log a message
41
+	 *
42
+	 * @param string $message
43
+	 */
44
+	protected function log($message)
45
+	{
46
+		if (!$this->silent) {
47
+			Debug::message($message);
48
+		}
49
+	}
50
+
51
+	public function run($request)
52
+	{
53
+		$this->runLinksCheck();
54
+	}
55
+	/**
56
+	 * Turn on or off message output
57
+	 *
58
+	 * @param bool $silent
59
+	 */
60
+	public function setSilent($silent)
61
+	{
62
+		$this->silent = $silent;
63
+	}
64
+
65
+	/**
66
+	 * @param LinkChecker $linkChecker
67
+	 */
68
+	public function setLinkChecker(LinkChecker $linkChecker)
69
+	{
70
+		$this->linkChecker = $linkChecker;
71
+	}
72
+
73
+	/**
74
+	 * @return LinkChecker
75
+	 */
76
+	public function getLinkChecker()
77
+	{
78
+		return $this->linkChecker;
79
+	}
80
+
81
+	/**
82
+	 * Check the status of a single link on a page
83
+	 *
84
+	 * @param BrokenExternalPageTrack $pageTrack
85
+	 * @param DOMNode $link
86
+	 */
87
+	protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
88
+	{
89
+		$class = $link->getAttribute('class');
90
+		$href = $link->getAttribute('href');
91
+		$markedBroken = preg_match('/\b(ss-broken)\b/', $class);
92
+
93
+		// Check link
94
+		$httpCode = $this->linkChecker->checkLink($href);
95
+		if ($httpCode === null) {
96
+			return; // Null link means uncheckable, such as an internal link
97
+		}
98
+
99
+		// If this code is broken then mark as such
100
+		if ($foundBroken = $this->isCodeBroken($httpCode)) {
101
+			// Create broken record
102
+			$brokenLink = new BrokenExternalLink();
103
+			$brokenLink->Link = $href;
104
+			$brokenLink->HTTPCode = $httpCode;
105
+			$brokenLink->TrackID = $pageTrack->ID;
106
+			$brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
107
+			$brokenLink->write();
108
+		}
109
+
110
+		// Check if we need to update CSS class, otherwise return
111
+		if ($markedBroken == $foundBroken) {
112
+			return;
113
+		}
114
+		if ($foundBroken) {
115
+			$class .= ' ss-broken';
116
+		} else {
117
+			$class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
118
+		}
119
+		$link->setAttribute('class', trim($class));
120
+	}
121
+
122
+	/**
123
+	 * Determine if the given HTTP code is "broken"
124
+	 *
125
+	 * @param int $httpCode
126
+	 * @return bool True if this is a broken code
127
+	 */
128
+	protected function isCodeBroken($httpCode)
129
+	{
130
+		// Null represents no request attempted
131
+		if ($httpCode === null) {
132
+			return false;
133
+		}
134
+
135
+		// do we have any whitelisted codes
136
+		$ignoreCodes = $this->config()->get('IgnoreCodes');
137
+		if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
138
+			return false;
139
+		}
140
+
141
+		// Check if code is outside valid range
142
+		return $httpCode < 200 || $httpCode > 302;
143
+	}
144
+
145
+	/**
146
+	 * Runs the links checker and returns the track used
147
+	 *
148
+	 * @param int $limit Limit to number of pages to run, or null to run all
149
+	 * @return BrokenExternalPageTrackStatus
150
+	 */
151
+	public function runLinksCheck($limit = null)
152
+	{
153
+		// Check the current status
154
+		$status = BrokenExternalPageTrackStatus::get_or_create();
155
+
156
+		// Calculate pages to run
157
+		$pageTracks = $status->getIncompleteTracks();
158
+		if ($limit) {
159
+			$pageTracks = $pageTracks->limit($limit);
160
+		}
161
+
162
+		// Check each page
163
+		foreach ($pageTracks as $pageTrack) {
164
+			// Flag as complete
165
+			$pageTrack->Processed = 1;
166
+			$pageTrack->write();
167
+
168
+			// Check value of html area
169
+			$page = $pageTrack->Page();
170
+			$this->log("Checking {$page->Title}");
171
+			$htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
172
+			if (!$htmlValue->isValid()) {
173
+				continue;
174
+			}
175
+
176
+			// Check each link
177
+			$links = $htmlValue->getElementsByTagName('a');
178
+			foreach ($links as $link) {
179
+				$this->checkPageLink($pageTrack, $link);
180
+			}
181
+
182
+			// Update content of page based on link fixes / breakages
183
+			$htmlValue->saveHTML();
184
+			$page->Content = $htmlValue->getContent();
185
+			$page->write();
186
+
187
+			// Once all links have been created for this page update HasBrokenLinks
188
+			$count = $pageTrack->BrokenLinks()->count();
189
+			$this->log("Found {$count} broken links");
190
+			if ($count) {
191
+				// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
192
+				DB::query(sprintf(
193
+					'UPDATE "SiteTree" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
194
+					intval($pageTrack->ID)
195
+				));
196
+			}
197
+		}
198
+
199
+		$status->updateJobInfo('Updating completed pages');
200
+		$status->updateStatus();
201
+		return $status;
202
+	}
203
+
204
+	private function updateCompletedPages($trackID = 0)
205
+	{
206
+		$noPages = BrokenExternalPageTrack::get()
207
+			->filter(array(
208
+				'TrackID' => $trackID,
209
+				'Processed' => 1
210
+			))
211
+			->count();
212
+		$track = BrokenExternalPageTrackStatus::get_latest();
213
+		$track->CompletedPages = $noPages;
214
+		$track->write();
215
+		return $noPages;
216
+	}
217
+
218
+	private function updateJobInfo($message)
219
+	{
220
+		$track = BrokenExternalPageTrackStatus::get_latest();
221
+		if ($track) {
222
+			$track->JobInfo = $message;
223
+			$track->write();
224
+		}
225
+	}
226 226
 }
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
 
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.
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.