Skip to content

Instantly share code, notes, and snippets.

@stojg
Forked from ss23/FindIncorrectLinksTask.php
Created August 5, 2013 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stojg/6152813 to your computer and use it in GitHub Desktop.
Save stojg/6152813 to your computer and use it in GitHub Desktop.
<?php
/**
* A task to identify any pages on the site that are referencing absolute URLs
* (that is, URLs like http://site.com, instead of [sitetree])
* Can't easily be automated due to https://github.com/silverstripe/silverstripe-cms/issues/812
*/
class FindIncorrectLinksTask extends BuildTask {
protected $title = 'Find Incorrect Links Tasks';
protected $description = 'Tries to find all bad links to your site, instead of using a proper SiteTree link.';
public function run($request) {
echo "<h2>The following URLs need updating:</h2>";
$pages = SiteTree::get();
if($pages) foreach($pages as $page) {
$content = $page->Content;
// Find the href attribute of every a element.
$pattern = '/<a [^>]*href="([^"]*)/';
$matches = array();
if (!preg_match_all($pattern, $page->Content, $matches)) continue;
// Loop through all the matches to ([^"]*), which captures the link href.
foreach ($matches[1] as $match) {
// We need to report on URL's that match, but aren't being rewritten
// TODO: Fill in with your URLs
if ((strpos($match, '://yoursite.com/') !== FALSE) or (strpos($match, '://www.yoursite/'))) {
echo htmlentities($match) . " : <a href='" . $page->Link() . "'>" . $page->Title . "</a>\r\n<br>";
}
continue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment