Skip to content

Commit

Permalink
v0.2.0 (see releases page)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Kerzig committed Nov 13, 2017
1 parent ddb18d6 commit 6830ede
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ The following options can be set globally in your `config.php` with `c::set($key
key | default | description
----------------- | ------- | ------------------------------------------------
`include-external` | `false` | Not only test for internal links but also for external ones.*
`include-fields` | `['text']` | Use other fields than `text` to search for broken links.
`exclude-pages` | `[]` | Page-IDs to exclude from broken link search.

\* **Not recommended yet because it only works synchonously with the page-load of your panel which will slow everything quite down a bit.**

Expand All @@ -41,9 +43,9 @@ Have a look at the [releases page](https://github.com/wottpal/kirby-anchor-headi

# Roadmap

- [x] Make pages excludable
- [x] Make fields user-definable
- [ ] Do checks asynchronously
- [ ] Make pages/links excludable
- [ ] At the moment it only looks at fields with the name `text`. Make this user-definable per page.
- [ ] Check for internet-connection (if external links are enabled)


Expand Down
2 changes: 1 addition & 1 deletion broken-links-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @package Kirby CMS
* @author Dennis Kerzig <hi@wottpal.com>
* @version 0.1.0
* @version 0.2.0
*
*/

Expand Down
48 changes: 30 additions & 18 deletions broken-links/broken-links.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,47 @@
// ],

'html' => function () {
// Gather Plugin-Options
$include_external = c::get('broken-links.include-external', false);
$include_fields = c::get('broken-links.include-fields', ['text']);
$exclude_pages = c::get('broken-links.exclude-pages', []);

// Initialization
$site = panel()->site();
$base_url = $site->url();
$all_links = [];
$broken_links = [];
$regex = '/https?\:\/\/[^\" \n]+/i';
$base_url = $site->url();

// Gather Plugin-Options
$include_external = c::get('broken-links.include-external', false);
// Filter out excluded pages
$pages = $site->index()->filter(function ($page) use ($exclude_pages) {
return !in_array($page->id(), $exclude_pages);
});

// Look through all pages and fields
foreach($pages as $page) {
foreach($include_fields as $field) {

if ($page->$field()->isNotEmpty()) {
$text = $page->text()->kt();
preg_match_all($regex, $text, $matches);

// Determine all links in all pages
foreach($site->index() as $page) {
if ($page->text()->isNotEmpty()) {
$text = $page->text()->kt();
preg_match_all($regex, $text, $matches);
// Determine internal & broken links
foreach($matches[0] as $link) {
$is_internal = substr($link, 0, strlen($base_url)) === $base_url;
if (!$is_internal && !$include_external) continue;

// Determine internal & broken links
foreach($matches[0] as $link) {
$is_internal = substr($link, 0, strlen($base_url)) === $base_url;
if (!$is_internal && !$include_external) continue;
$is_broken = get_http_response_code($link) == '404';
if (!$is_broken) continue;

$is_broken = get_http_response_code($link) == '404';
if (!$is_broken) continue;
// Remove Base-URL Prefix (internal)
if ($is_internal) {
$link = substr($link, strlen($base_url));
}

// Remove Base-URL Prefix (internal)
if ($is_internal) {
$link = substr($link, strlen($base_url));
$broken_links[$page->id()][] = $link;
}

$broken_links[$page->id()][] = $link;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "broken-links-widget",
"description": "A Kirby panel-widget which shows broken links within the pages.",
"author": "Dennis Kerzig <hi@wottpal.com> (https://wottpal.com)",
"version": "0.1.0",
"version": "0.2.0",
"type": "kirby-plugin",
"license": "MIT"
}

0 comments on commit 6830ede

Please # to comment.