commit d52503fcbd0b45d410e67236801ddd7bca36499c Author: Kirk Mayo Date: Mon Aug 26 09:49:49 2013 +1200 NEW: Start of external broken links module diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a5ad1a --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# External links + +## Introduction + +The external links module is a task and ModelAdmin to track and to report on broken external links. + +## Maintainer Contact + + * Kirk Mayo kirk (at) silverstripe (dot) com + +## Requirements + + * SilverStripe 3.0 + + +## Features + +* Add external links to broken links reports +* Add a model admin for external broken links +* Add a task to track external broken links + +## Installation + + 1. Download the module form GitHub (Composer support to be added) + 2. Extract the file (if you are on windows try 7-zip for extracting tar.gz files + 3. Make sure the folder after being extracted is named 'externallinks' + 4. Place this directory in your sites root directory. This is the one with framework and cms in it. + 5. Run in your browser - `/dev/build` to rebuild the database. + 6. You should see a new menu called *Broken Ext. Links* + +## Disable the Broken external link menu + +To disable the *Broken Ext. Links* menu add the following code to mysite/_config.php + +`CMSMenu::remove_menu_item('BrokenExternalLinksAdmin');` diff --git a/_config.php b/_config.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/_config.php @@ -0,0 +1 @@ + 'Varchar', + 'HTTPCode' =>'Int' + ); + + private static $has_one = array( + 'Page' => 'Page' + ); + + public static $summary_fields = array( + 'Page.Title' => 'Page', + 'HTTPCode' => 'HTTP Code', + 'Created' => 'Created' + ); + + public static $searchable_fields = array( + 'HTTPCode' + ); + + function canEdit($member = false) { + return false; + } + +} + +class BrokenExternalLinksAdmin extends ModelAdmin { + + public static $url_segment = 'broken-external-links-admin'; + + public static $managed_models = array( + 'BrokenExternalLinks' + ); + + public static $menu_title = 'Broken Ext. links'; + + public function init() { + parent::init(); + } + +} diff --git a/tasks/.CheckExternalLinks.php.swp b/tasks/.CheckExternalLinks.php.swp new file mode 100644 index 0000000..1544884 Binary files /dev/null and b/tasks/.CheckExternalLinks.php.swp differ diff --git a/tasks/CheckExternalLinks.php b/tasks/CheckExternalLinks.php new file mode 100644 index 0000000..8996ea4 --- /dev/null +++ b/tasks/CheckExternalLinks.php @@ -0,0 +1,66 @@ +clearTable($table); + else DB::query("TRUNCATE \"$table\""); + $pages = SiteTree::get(); + foreach ($pages as $page) { + $htmlValue = Injector::inst()->create('HTMLValue', $page->Content); + + // Populate link tracking for internal links & links to asset files. + if($links = $htmlValue->getElementsByTagName('a')) foreach($links as $link) { + $href = Director::makeRelative($link->getAttribute('href')); + if ($href == 'admin/') continue; + + // ignore SiteTree and assets links as they will be caught by SiteTreeLinkTracking + if(preg_match('/\[sitetree_link,id=([0-9]+)\]/i', $href, $matches)) { + return; + } else if(substr($href, 0, strlen(ASSETS_DIR) + 1) == ASSETS_DIR.'/') { + return; + } + if($href && function_exists('curl_init')) { + $handle = curl_init($href); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); + $response = curl_exec($handle); + $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); + curl_close($handle); + if (($httpCode < 200 || $httpCode > 302) + || ($href == '' || $href[0] == '/')) + { + $brokenLink = new BrokenExternalLinks(); + $brokenLink->PageID = $page->ID; + $brokenLink->Link = $href; + $brokenLink->HTTPCode = $httpCode; + $brokenLink->write(); + + // set the broken link class + /* + $class = $link->getAttribute('class'); + $class = ($class) ? $class . 'ss-broken' : 'ss-broken'; + $link->setAttribute('class', ($class ? "$class ss-broken" : 'ss-broken')); + */ + + // use raw sql query to set broken link as calling the dataobject write + // method will reset the links if no broken internal links are found + $query = 'UPDATE SiteTree SET HasBrokenLink = 1 '; + $query .= 'WHERE ID = ' . (int)$page->ID; + $result = DB::query($query); + if (!$result) { + // error updating hasBrokenLink + } + + } + } + } + } + } +}