Passed
Pull Request — master (#38)
by Robbie
02:34
created
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/Jobs/CheckExternalLinksJob.php 1 patch
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.
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/Model/BrokenExternalPageTrackStatus.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -18,136 +18,136 @@
 block discarded – undo
18 18
  */
19 19
 class BrokenExternalPageTrackStatus extends DataObject
20 20
 {
21
-    private static $table_name = 'BrokenExternalPageTrackStatus';
22
-
23
-    private static $db = array(
24
-        'Status' => 'Enum("Completed, Running", "Running")',
25
-        'JobInfo' => 'Varchar(255)'
26
-    );
27
-
28
-    private static $has_many = array(
29
-        'TrackedPages' => BrokenExternalPageTrack::class,
30
-        'BrokenLinks' => BrokenExternalLink::class
31
-    );
32
-
33
-    /**
34
-     * Get the latest track status
35
-     *
36
-     * @return BrokenExternalPageTrackStatus
37
-     */
38
-    public static function get_latest()
39
-    {
40
-        return self::get()
41
-            ->sort('ID', 'DESC')
42
-            ->first();
43
-    }
44
-
45
-    /**
46
-     * Gets the list of Pages yet to be checked
47
-     *
48
-     * @return DataList
49
-     */
50
-    public function getIncompletePageList()
51
-    {
52
-        $pageIDs = $this
53
-            ->getIncompleteTracks()
54
-            ->column('PageID');
55
-        if ($pageIDs) {
56
-            return Versioned::get_by_stage(SiteTree::class, 'Stage')
57
-            ->byIDs($pageIDs);
58
-        }
59
-    }
60
-
61
-    /**
62
-     * Get the list of incomplete BrokenExternalPageTrack
63
-     *
64
-     * @return DataList
65
-     */
66
-    public function getIncompleteTracks()
67
-    {
68
-        return $this
69
-            ->TrackedPages()
70
-            ->filter('Processed', 0);
71
-    }
72
-
73
-    /**
74
-     * Get total pages count
75
-     *
76
-     * @return int
77
-     */
78
-    public function getTotalPages()
79
-    {
80
-        return $this->TrackedPages()->count();
81
-    }
82
-
83
-    /**
84
-     * Get completed pages count
85
-     *
86
-     * @return int
87
-     */
88
-    public function getCompletedPages()
89
-    {
90
-        return $this
91
-            ->TrackedPages()
92
-            ->filter('Processed', 1)
93
-            ->count();
94
-    }
95
-
96
-    /**
97
-     * Returns the latest run, or otherwise creates a new one
98
-     *
99
-     * @return BrokenExternalPageTrackStatus
100
-     */
101
-    public static function get_or_create()
102
-    {
103
-        // Check the current status
104
-        $status = self::get_latest();
105
-        if ($status && $status->Status == 'Running') {
106
-            $status->updateStatus();
107
-            return $status;
108
-        }
109
-
110
-        return self::create_status();
111
-    }
112
-
113
-    /**
114
-     * Create and prepare a new status
115
-     *
116
-     * @return BrokenExternalPageTrackStatus
117
-     */
118
-    public static function create_status()
119
-    {
120
-        // If the script is to be started create a new status
121
-        $status = self::create();
122
-        $status->updateJobInfo('Creating new tracking object');
123
-
124
-        // Setup all pages to test
125
-        $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
126
-            ->column('ID');
127
-        foreach ($pageIDs as $pageID) {
128
-            $trackPage = BrokenExternalPageTrack::create();
129
-            $trackPage->PageID = $pageID;
130
-            $trackPage->StatusID = $status->ID;
131
-            $trackPage->write();
132
-        }
133
-
134
-        return $status;
135
-    }
136
-
137
-    public function updateJobInfo($message)
138
-    {
139
-        $this->JobInfo = $message;
140
-        $this->write();
141
-    }
142
-
143
-    /**
144
-     * Self check status
145
-     */
146
-    public function updateStatus()
147
-    {
148
-        if ($this->CompletedPages == $this->TotalPages) {
149
-            $this->Status = 'Completed';
150
-            $this->updateJobInfo('Setting to completed');
151
-        }
152
-    }
21
+	private static $table_name = 'BrokenExternalPageTrackStatus';
22
+
23
+	private static $db = array(
24
+		'Status' => 'Enum("Completed, Running", "Running")',
25
+		'JobInfo' => 'Varchar(255)'
26
+	);
27
+
28
+	private static $has_many = array(
29
+		'TrackedPages' => BrokenExternalPageTrack::class,
30
+		'BrokenLinks' => BrokenExternalLink::class
31
+	);
32
+
33
+	/**
34
+	 * Get the latest track status
35
+	 *
36
+	 * @return BrokenExternalPageTrackStatus
37
+	 */
38
+	public static function get_latest()
39
+	{
40
+		return self::get()
41
+			->sort('ID', 'DESC')
42
+			->first();
43
+	}
44
+
45
+	/**
46
+	 * Gets the list of Pages yet to be checked
47
+	 *
48
+	 * @return DataList
49
+	 */
50
+	public function getIncompletePageList()
51
+	{
52
+		$pageIDs = $this
53
+			->getIncompleteTracks()
54
+			->column('PageID');
55
+		if ($pageIDs) {
56
+			return Versioned::get_by_stage(SiteTree::class, 'Stage')
57
+			->byIDs($pageIDs);
58
+		}
59
+	}
60
+
61
+	/**
62
+	 * Get the list of incomplete BrokenExternalPageTrack
63
+	 *
64
+	 * @return DataList
65
+	 */
66
+	public function getIncompleteTracks()
67
+	{
68
+		return $this
69
+			->TrackedPages()
70
+			->filter('Processed', 0);
71
+	}
72
+
73
+	/**
74
+	 * Get total pages count
75
+	 *
76
+	 * @return int
77
+	 */
78
+	public function getTotalPages()
79
+	{
80
+		return $this->TrackedPages()->count();
81
+	}
82
+
83
+	/**
84
+	 * Get completed pages count
85
+	 *
86
+	 * @return int
87
+	 */
88
+	public function getCompletedPages()
89
+	{
90
+		return $this
91
+			->TrackedPages()
92
+			->filter('Processed', 1)
93
+			->count();
94
+	}
95
+
96
+	/**
97
+	 * Returns the latest run, or otherwise creates a new one
98
+	 *
99
+	 * @return BrokenExternalPageTrackStatus
100
+	 */
101
+	public static function get_or_create()
102
+	{
103
+		// Check the current status
104
+		$status = self::get_latest();
105
+		if ($status && $status->Status == 'Running') {
106
+			$status->updateStatus();
107
+			return $status;
108
+		}
109
+
110
+		return self::create_status();
111
+	}
112
+
113
+	/**
114
+	 * Create and prepare a new status
115
+	 *
116
+	 * @return BrokenExternalPageTrackStatus
117
+	 */
118
+	public static function create_status()
119
+	{
120
+		// If the script is to be started create a new status
121
+		$status = self::create();
122
+		$status->updateJobInfo('Creating new tracking object');
123
+
124
+		// Setup all pages to test
125
+		$pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
126
+			->column('ID');
127
+		foreach ($pageIDs as $pageID) {
128
+			$trackPage = BrokenExternalPageTrack::create();
129
+			$trackPage->PageID = $pageID;
130
+			$trackPage->StatusID = $status->ID;
131
+			$trackPage->write();
132
+		}
133
+
134
+		return $status;
135
+	}
136
+
137
+	public function updateJobInfo($message)
138
+	{
139
+		$this->JobInfo = $message;
140
+		$this->write();
141
+	}
142
+
143
+	/**
144
+	 * Self check status
145
+	 */
146
+	public function updateStatus()
147
+	{
148
+		if ($this->CompletedPages == $this->TotalPages) {
149
+			$this->Status = 'Completed';
150
+			$this->updateJobInfo('Setting to completed');
151
+		}
152
+	}
153 153
 }
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
@@ -6,24 +6,24 @@
 block discarded – undo
6 6
 
7 7
 class PretendLinkChecker implements LinkChecker, TestOnly
8 8
 {
9
-    public function checkLink($href)
10
-    {
11
-        switch ($href) {
12
-            case 'http://www.working.com':
13
-                return 200;
14
-            case 'http://www.broken.com':
15
-                return 403;
16
-            case 'http://www.nodomain.com':
17
-                return 0;
18
-            case '/internal/link':
19
-            case '[sitetree_link,id=9999]':
20
-            case 'home':
21
-            case 'broken-internal':
22
-            case '[sitetree_link,id=1]':
23
-                return null;
24
-            case 'http://www.broken.com/url/thing':
25
-            default:
26
-                return 404;
27
-        }
28
-    }
9
+	public function checkLink($href)
10
+	{
11
+		switch ($href) {
12
+			case 'http://www.working.com':
13
+				return 200;
14
+			case 'http://www.broken.com':
15
+				return 403;
16
+			case 'http://www.nodomain.com':
17
+				return 0;
18
+			case '/internal/link':
19
+			case '[sitetree_link,id=9999]':
20
+			case 'home':
21
+			case 'broken-internal':
22
+			case '[sitetree_link,id=1]':
23
+				return null;
24
+			case 'http://www.broken.com/url/thing':
25
+			default:
26
+				return 404;
27
+		}
28
+	}
29 29
 }
Please login to merge, or discard this patch.
tests/Stubs/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/Tasks/CheckExternalLinksTask.php 1 patch
Indentation   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -17,224 +17,224 @@
 block discarded – undo
17 17
 
18 18
 class CheckExternalLinksTask extends BuildTask
19 19
 {
20
-    private static $dependencies = [
21
-        'LinkChecker' => '%$' . LinkChecker::class
22
-    ];
23
-
24
-    private static $segment = 'CheckExternalLinksTask';
25
-
26
-    /**
27
-     * Define a list of HTTP response codes that should not be treated as "broken", where they usually
28
-     * might be.
29
-     *
30
-     * @config
31
-     * @var array
32
-     */
33
-    private static $ignore_codes = [];
34
-
35
-    /**
36
-     * @var bool
37
-     */
38
-    protected $silent = false;
39
-
40
-    /**
41
-     * @var LinkChecker
42
-     */
43
-    protected $linkChecker;
44
-
45
-    protected $title = 'Checking broken External links in the SiteTree';
46
-
47
-    protected $description = 'A task that records external broken links in the SiteTree';
48
-
49
-    protected $enabled = true;
50
-
51
-    /**
52
-     * Log a message
53
-     *
54
-     * @param string $message
55
-     */
56
-    protected function log($message)
57
-    {
58
-        if (!$this->silent) {
59
-            Debug::message($message);
60
-        }
61
-    }
62
-
63
-    public function run($request)
64
-    {
65
-        $this->runLinksCheck();
66
-    }
67
-    /**
68
-     * Turn on or off message output
69
-     *
70
-     * @param bool $silent
71
-     */
72
-    public function setSilent($silent)
73
-    {
74
-        $this->silent = $silent;
75
-    }
76
-
77
-    /**
78
-     * @param LinkChecker $linkChecker
79
-     */
80
-    public function setLinkChecker(LinkChecker $linkChecker)
81
-    {
82
-        $this->linkChecker = $linkChecker;
83
-    }
84
-
85
-    /**
86
-     * @return LinkChecker
87
-     */
88
-    public function getLinkChecker()
89
-    {
90
-        return $this->linkChecker;
91
-    }
92
-
93
-    /**
94
-     * Check the status of a single link on a page
95
-     *
96
-     * @param BrokenExternalPageTrack $pageTrack
97
-     * @param DOMNode $link
98
-     */
99
-    protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
100
-    {
101
-        $class = $link->getAttribute('class');
102
-        $href = $link->getAttribute('href');
103
-        $markedBroken = preg_match('/\b(ss-broken)\b/', $class);
104
-
105
-        // Check link
106
-        $httpCode = $this->linkChecker->checkLink($href);
107
-        if ($httpCode === null) {
108
-            return; // Null link means uncheckable, such as an internal link
109
-        }
110
-
111
-        // If this code is broken then mark as such
112
-        if ($foundBroken = $this->isCodeBroken($httpCode)) {
113
-            // Create broken record
114
-            $brokenLink = new BrokenExternalLink();
115
-            $brokenLink->Link = $href;
116
-            $brokenLink->HTTPCode = $httpCode;
117
-            $brokenLink->TrackID = $pageTrack->ID;
118
-            $brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
119
-            $brokenLink->write();
120
-        }
121
-
122
-        // Check if we need to update CSS class, otherwise return
123
-        if ($markedBroken == $foundBroken) {
124
-            return;
125
-        }
126
-        if ($foundBroken) {
127
-            $class .= ' ss-broken';
128
-        } else {
129
-            $class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
130
-        }
131
-        $link->setAttribute('class', trim($class));
132
-    }
133
-
134
-    /**
135
-     * Determine if the given HTTP code is "broken"
136
-     *
137
-     * @param int $httpCode
138
-     * @return bool True if this is a broken code
139
-     */
140
-    protected function isCodeBroken($httpCode)
141
-    {
142
-        // Null represents no request attempted
143
-        if ($httpCode === null) {
144
-            return false;
145
-        }
146
-
147
-        // do we have any whitelisted codes
148
-        $ignoreCodes = $this->config()->get('ignore_codes');
149
-        if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
150
-            return false;
151
-        }
152
-
153
-        // Check if code is outside valid range
154
-        return $httpCode < 200 || $httpCode > 302;
155
-    }
156
-
157
-    /**
158
-     * Runs the links checker and returns the track used
159
-     *
160
-     * @param int $limit Limit to number of pages to run, or null to run all
161
-     * @return BrokenExternalPageTrackStatus
162
-     */
163
-    public function runLinksCheck($limit = null)
164
-    {
165
-        // Check the current status
166
-        $status = BrokenExternalPageTrackStatus::get_or_create();
167
-
168
-        // Calculate pages to run
169
-        $pageTracks = $status->getIncompleteTracks();
170
-        if ($limit) {
171
-            $pageTracks = $pageTracks->limit($limit);
172
-        }
173
-
174
-        // Check each page
175
-        foreach ($pageTracks as $pageTrack) {
176
-            // Flag as complete
177
-            $pageTrack->Processed = 1;
178
-            $pageTrack->write();
179
-
180
-            // Check value of html area
181
-            $page = $pageTrack->Page();
182
-            $this->log("Checking {$page->Title}");
183
-            $htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
184
-            if (!$htmlValue->isValid()) {
185
-                continue;
186
-            }
187
-
188
-            // Check each link
189
-            $links = $htmlValue->getElementsByTagName('a');
190
-            foreach ($links as $link) {
191
-                $this->checkPageLink($pageTrack, $link);
192
-            }
193
-
194
-            // Update content of page based on link fixes / breakages
195
-            $htmlValue->saveHTML();
196
-            $page->Content = $htmlValue->getContent();
197
-            $page->write();
198
-
199
-            // Once all links have been created for this page update HasBrokenLinks
200
-            $count = $pageTrack->BrokenLinks()->count();
201
-            $this->log("Found {$count} broken links");
202
-            if ($count) {
203
-                $siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class);
204
-                // Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
205
-                DB::query(sprintf(
206
-                    'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
207
-                    $siteTreeTable,
208
-                    intval($pageTrack->ID)
209
-                ));
210
-            }
211
-        }
212
-
213
-        $status->updateJobInfo('Updating completed pages');
214
-        $status->updateStatus();
215
-        return $status;
216
-    }
217
-
218
-    private function updateCompletedPages($trackID = 0)
219
-    {
220
-        $noPages = BrokenExternalPageTrack::get()
221
-            ->filter(array(
222
-                'TrackID' => $trackID,
223
-                'Processed' => 1
224
-            ))
225
-            ->count();
226
-        $track = BrokenExternalPageTrackStatus::get_latest();
227
-        $track->CompletedPages = $noPages;
228
-        $track->write();
229
-        return $noPages;
230
-    }
231
-
232
-    private function updateJobInfo($message)
233
-    {
234
-        $track = BrokenExternalPageTrackStatus::get_latest();
235
-        if ($track) {
236
-            $track->JobInfo = $message;
237
-            $track->write();
238
-        }
239
-    }
20
+	private static $dependencies = [
21
+		'LinkChecker' => '%$' . LinkChecker::class
22
+	];
23
+
24
+	private static $segment = 'CheckExternalLinksTask';
25
+
26
+	/**
27
+	 * Define a list of HTTP response codes that should not be treated as "broken", where they usually
28
+	 * might be.
29
+	 *
30
+	 * @config
31
+	 * @var array
32
+	 */
33
+	private static $ignore_codes = [];
34
+
35
+	/**
36
+	 * @var bool
37
+	 */
38
+	protected $silent = false;
39
+
40
+	/**
41
+	 * @var LinkChecker
42
+	 */
43
+	protected $linkChecker;
44
+
45
+	protected $title = 'Checking broken External links in the SiteTree';
46
+
47
+	protected $description = 'A task that records external broken links in the SiteTree';
48
+
49
+	protected $enabled = true;
50
+
51
+	/**
52
+	 * Log a message
53
+	 *
54
+	 * @param string $message
55
+	 */
56
+	protected function log($message)
57
+	{
58
+		if (!$this->silent) {
59
+			Debug::message($message);
60
+		}
61
+	}
62
+
63
+	public function run($request)
64
+	{
65
+		$this->runLinksCheck();
66
+	}
67
+	/**
68
+	 * Turn on or off message output
69
+	 *
70
+	 * @param bool $silent
71
+	 */
72
+	public function setSilent($silent)
73
+	{
74
+		$this->silent = $silent;
75
+	}
76
+
77
+	/**
78
+	 * @param LinkChecker $linkChecker
79
+	 */
80
+	public function setLinkChecker(LinkChecker $linkChecker)
81
+	{
82
+		$this->linkChecker = $linkChecker;
83
+	}
84
+
85
+	/**
86
+	 * @return LinkChecker
87
+	 */
88
+	public function getLinkChecker()
89
+	{
90
+		return $this->linkChecker;
91
+	}
92
+
93
+	/**
94
+	 * Check the status of a single link on a page
95
+	 *
96
+	 * @param BrokenExternalPageTrack $pageTrack
97
+	 * @param DOMNode $link
98
+	 */
99
+	protected function checkPageLink(BrokenExternalPageTrack $pageTrack, DOMNode $link)
100
+	{
101
+		$class = $link->getAttribute('class');
102
+		$href = $link->getAttribute('href');
103
+		$markedBroken = preg_match('/\b(ss-broken)\b/', $class);
104
+
105
+		// Check link
106
+		$httpCode = $this->linkChecker->checkLink($href);
107
+		if ($httpCode === null) {
108
+			return; // Null link means uncheckable, such as an internal link
109
+		}
110
+
111
+		// If this code is broken then mark as such
112
+		if ($foundBroken = $this->isCodeBroken($httpCode)) {
113
+			// Create broken record
114
+			$brokenLink = new BrokenExternalLink();
115
+			$brokenLink->Link = $href;
116
+			$brokenLink->HTTPCode = $httpCode;
117
+			$brokenLink->TrackID = $pageTrack->ID;
118
+			$brokenLink->StatusID = $pageTrack->StatusID; // Slight denormalisation here for performance reasons
119
+			$brokenLink->write();
120
+		}
121
+
122
+		// Check if we need to update CSS class, otherwise return
123
+		if ($markedBroken == $foundBroken) {
124
+			return;
125
+		}
126
+		if ($foundBroken) {
127
+			$class .= ' ss-broken';
128
+		} else {
129
+			$class = preg_replace('/\s*\b(ss-broken)\b\s*/', ' ', $class);
130
+		}
131
+		$link->setAttribute('class', trim($class));
132
+	}
133
+
134
+	/**
135
+	 * Determine if the given HTTP code is "broken"
136
+	 *
137
+	 * @param int $httpCode
138
+	 * @return bool True if this is a broken code
139
+	 */
140
+	protected function isCodeBroken($httpCode)
141
+	{
142
+		// Null represents no request attempted
143
+		if ($httpCode === null) {
144
+			return false;
145
+		}
146
+
147
+		// do we have any whitelisted codes
148
+		$ignoreCodes = $this->config()->get('ignore_codes');
149
+		if (is_array($ignoreCodes) && in_array($httpCode, $ignoreCodes)) {
150
+			return false;
151
+		}
152
+
153
+		// Check if code is outside valid range
154
+		return $httpCode < 200 || $httpCode > 302;
155
+	}
156
+
157
+	/**
158
+	 * Runs the links checker and returns the track used
159
+	 *
160
+	 * @param int $limit Limit to number of pages to run, or null to run all
161
+	 * @return BrokenExternalPageTrackStatus
162
+	 */
163
+	public function runLinksCheck($limit = null)
164
+	{
165
+		// Check the current status
166
+		$status = BrokenExternalPageTrackStatus::get_or_create();
167
+
168
+		// Calculate pages to run
169
+		$pageTracks = $status->getIncompleteTracks();
170
+		if ($limit) {
171
+			$pageTracks = $pageTracks->limit($limit);
172
+		}
173
+
174
+		// Check each page
175
+		foreach ($pageTracks as $pageTrack) {
176
+			// Flag as complete
177
+			$pageTrack->Processed = 1;
178
+			$pageTrack->write();
179
+
180
+			// Check value of html area
181
+			$page = $pageTrack->Page();
182
+			$this->log("Checking {$page->Title}");
183
+			$htmlValue = Injector::inst()->create('HTMLValue', $page->Content);
184
+			if (!$htmlValue->isValid()) {
185
+				continue;
186
+			}
187
+
188
+			// Check each link
189
+			$links = $htmlValue->getElementsByTagName('a');
190
+			foreach ($links as $link) {
191
+				$this->checkPageLink($pageTrack, $link);
192
+			}
193
+
194
+			// Update content of page based on link fixes / breakages
195
+			$htmlValue->saveHTML();
196
+			$page->Content = $htmlValue->getContent();
197
+			$page->write();
198
+
199
+			// Once all links have been created for this page update HasBrokenLinks
200
+			$count = $pageTrack->BrokenLinks()->count();
201
+			$this->log("Found {$count} broken links");
202
+			if ($count) {
203
+				$siteTreeTable = DataObject::getSchema()->tableName(SiteTree::class);
204
+				// Bypass the ORM as syncLinkTracking does not allow you to update HasBrokenLink to true
205
+				DB::query(sprintf(
206
+					'UPDATE "%s" SET "HasBrokenLink" = 1 WHERE "ID" = \'%d\'',
207
+					$siteTreeTable,
208
+					intval($pageTrack->ID)
209
+				));
210
+			}
211
+		}
212
+
213
+		$status->updateJobInfo('Updating completed pages');
214
+		$status->updateStatus();
215
+		return $status;
216
+	}
217
+
218
+	private function updateCompletedPages($trackID = 0)
219
+	{
220
+		$noPages = BrokenExternalPageTrack::get()
221
+			->filter(array(
222
+				'TrackID' => $trackID,
223
+				'Processed' => 1
224
+			))
225
+			->count();
226
+		$track = BrokenExternalPageTrackStatus::get_latest();
227
+		$track->CompletedPages = $noPages;
228
+		$track->write();
229
+		return $noPages;
230
+	}
231
+
232
+	private function updateJobInfo($message)
233
+	{
234
+		$track = BrokenExternalPageTrackStatus::get_latest();
235
+		if ($track) {
236
+			$track->JobInfo = $message;
237
+			$track->write();
238
+		}
239
+	}
240 240
 }
Please login to merge, or discard this patch.
src/Model/BrokenExternalLink.php 1 patch
Indentation   +54 added lines, -54 removed lines patch added patch discarded remove patch
@@ -17,68 +17,68 @@
 block discarded – undo
17 17
  */
18 18
 class BrokenExternalLink extends DataObject
19 19
 {
20
-    private static $table_name = 'BrokenExternalLink';
20
+	private static $table_name = 'BrokenExternalLink';
21 21
 
22
-    private static $db = array(
23
-        'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
24
-        'HTTPCode' =>'Int'
25
-    );
22
+	private static $db = array(
23
+		'Link' => 'Varchar(2083)', // 2083 is the maximum length of a URL in Internet Explorer.
24
+		'HTTPCode' =>'Int'
25
+	);
26 26
 
27
-    private static $has_one = array(
28
-        'Track' => BrokenExternalPageTrack::class,
29
-        'Status' => BrokenExternalPageTrackStatus::class
30
-    );
27
+	private static $has_one = array(
28
+		'Track' => BrokenExternalPageTrack::class,
29
+		'Status' => BrokenExternalPageTrackStatus::class
30
+	);
31 31
 
32
-    private static $summary_fields = array(
33
-        'Created' => 'Checked',
34
-        'Link' => 'External Link',
35
-        'HTTPCodeDescription' => 'HTTP Error Code',
36
-        'Page.Title' => 'Page link is on'
37
-    );
32
+	private static $summary_fields = array(
33
+		'Created' => 'Checked',
34
+		'Link' => 'External Link',
35
+		'HTTPCodeDescription' => 'HTTP Error Code',
36
+		'Page.Title' => 'Page link is on'
37
+	);
38 38
 
39
-    private static $searchable_fields = array(
40
-        'HTTPCode' => array('title' => 'HTTP Code')
41
-    );
39
+	private static $searchable_fields = array(
40
+		'HTTPCode' => array('title' => 'HTTP Code')
41
+	);
42 42
 
43
-    /**
44
-     * @return SiteTree
45
-     */
46
-    public function Page()
47
-    {
48
-        return $this->Track()->Page();
49
-    }
43
+	/**
44
+	 * @return SiteTree
45
+	 */
46
+	public function Page()
47
+	{
48
+		return $this->Track()->Page();
49
+	}
50 50
 
51
-    public function canEdit($member = false)
52
-    {
53
-        return false;
54
-    }
51
+	public function canEdit($member = false)
52
+	{
53
+		return false;
54
+	}
55 55
 
56
-    public function canView($member = false)
57
-    {
58
-        $member = $member ? $member : Security::getCurrentUser();
59
-        $codes = array('content-authors', 'administrators');
60
-        return Permission::checkMember($member, $codes);
61
-    }
56
+	public function canView($member = false)
57
+	{
58
+		$member = $member ? $member : Security::getCurrentUser();
59
+		$codes = array('content-authors', 'administrators');
60
+		return Permission::checkMember($member, $codes);
61
+	}
62 62
 
63
-    /**
64
-     * Retrieve a human readable description of a response code
65
-     *
66
-     * @return string
67
-     */
68
-    public function getHTTPCodeDescription()
69
-    {
70
-        $code = $this->HTTPCode;
63
+	/**
64
+	 * Retrieve a human readable description of a response code
65
+	 *
66
+	 * @return string
67
+	 */
68
+	public function getHTTPCodeDescription()
69
+	{
70
+		$code = $this->HTTPCode;
71 71
 
72
-        try {
73
-            $response = HTTPResponse::create('', $code);
74
-            // Assume that $code = 0 means there was no response
75
-            $description = $code ?
76
-                $response->getStatusDescription() :
77
-                _t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available');
78
-        } catch (InvalidArgumentException $e) {
79
-            $description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code');
80
-        }
72
+		try {
73
+			$response = HTTPResponse::create('', $code);
74
+			// Assume that $code = 0 means there was no response
75
+			$description = $code ?
76
+				$response->getStatusDescription() :
77
+				_t(__CLASS__ . '.NOTAVAILABLE', 'Server Not Available');
78
+		} catch (InvalidArgumentException $e) {
79
+			$description = _t(__CLASS__ . '.UNKNOWNRESPONSE', 'Unknown Response Code');
80
+		}
81 81
 
82
-        return sprintf("%d (%s)", $code, $description);
83
-    }
82
+		return sprintf("%d (%s)", $code, $description);
83
+	}
84 84
 }
Please login to merge, or discard this patch.