Created
August 5, 2013 01:01
-
-
Save ss23/6152787 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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