From 6830ede0d10187da5199a9ba6a297e831b03085b Mon Sep 17 00:00:00 2001 From: Dennis Kerzig Date: Mon, 13 Nov 2017 16:23:10 +0200 Subject: [PATCH] v0.2.0 (see releases page) --- README.md | 6 +++-- broken-links-widget.php | 2 +- broken-links/broken-links.php | 48 ++++++++++++++++++++++------------- package.json | 2 +- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 99f6b8a..c0f138a 100644 --- a/README.md +++ b/README.md @@ -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.** @@ -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) diff --git a/broken-links-widget.php b/broken-links-widget.php index 6c1d8c2..6c2bcd9 100644 --- a/broken-links-widget.php +++ b/broken-links-widget.php @@ -5,7 +5,7 @@ * * @package Kirby CMS * @author Dennis Kerzig -* @version 0.1.0 +* @version 0.2.0 * */ diff --git a/broken-links/broken-links.php b/broken-links/broken-links.php index 2aa2f55..4e88289 100644 --- a/broken-links/broken-links.php +++ b/broken-links/broken-links.php @@ -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; } } } diff --git a/package.json b/package.json index 6a3bdc7..55884b4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "broken-links-widget", "description": "A Kirby panel-widget which shows broken links within the pages.", "author": "Dennis Kerzig (https://wottpal.com)", - "version": "0.1.0", + "version": "0.2.0", "type": "kirby-plugin", "license": "MIT" }