NEW: Start of external broken links module

This commit is contained in:
Kirk Mayo 2013-08-26 09:49:49 +12:00
commit d52503fcbd
6 changed files with 145 additions and 0 deletions

34
README.md Normal file
View File

@ -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');`

1
_config.php Normal file
View File

@ -0,0 +1 @@
<?php

Binary file not shown.

View File

@ -0,0 +1,44 @@
<?php
class BrokenExternalLinks extends DataObject {
private static $db = array(
'Link' => '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();
}
}

Binary file not shown.

View File

@ -0,0 +1,66 @@
<?php
class CheckExternalLinks extends BuildTask {
protected $title = 'Checking broken External links in the SiteTree';
protected $description = 'A task that records external broken links in the SiteTree';
protected $enabled = true;
function run($request) {
// clear broken external link table
$table = 'BrokenExternalLinks';
if(method_exists(DB::getConn(), 'clearTable')) DB::getConn()->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
}
}
}
}
}
}
}