This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c0a3465
Showing
6 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# v1.0.0 | ||
## 11/14/2016 | ||
|
||
1. [](#new) | ||
* ChangeLog started... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Aaron Dalton | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Backlinks Plugin | ||
|
||
The **Backlinks** Plugin is for [Grav CMS](http://github.com/getgrav/grav). For each page in your site, it records all other pages that point to it. In wikis, these are commonly called "backlinks." | ||
|
||
## Installation | ||
|
||
Installing the Backlinks plugin can be done in one of two ways. The GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file. | ||
|
||
### GPM Installation (Preferred) | ||
|
||
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's terminal (also called the command line). From the root of your Grav install type: | ||
|
||
bin/gpm install backlinks | ||
|
||
This will install the Backlinks plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/backlinks`. | ||
|
||
### Manual Installation | ||
|
||
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `backlinks`. You can find these files on [GitHub](https://github.com/aaron-dalton/grav-plugin-backlinks) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). | ||
|
||
You should now have all the plugin files under | ||
|
||
/your/site/grav/user/plugins/backlinks | ||
|
||
> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav) and the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) to operate. | ||
## Configuration | ||
|
||
Before configuring this plugin, you should copy the `user/plugins/backlinks/backlinks.yaml` to `user/config/plugins/backlinks.yaml` and only edit that copy. | ||
|
||
Here is the default configuration and an explanation of available options: | ||
|
||
```yaml | ||
enabled: true | ||
datafile: 'backlinks.yaml' # relative to `user/data` | ||
``` | ||
* The `enabled` flag turns the plugin off and on. | ||
|
||
* `datafile` points to a file relative to the `user/data` folder where you want the backlinks data to live. | ||
|
||
## Usage | ||
|
||
**Describe how to use the plugin.** | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
namespace Grav\Plugin; | ||
|
||
use Grav\Common\Plugin; | ||
use RocketTheme\Toolbox\Event\Event; | ||
use Grav\Common\Utils; | ||
use RocketTheme\Toolbox\File\File; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* Class BacklinksPlugin | ||
* @package Grav\Plugin | ||
*/ | ||
class BacklinksPlugin extends Plugin | ||
{ | ||
/** | ||
* @return array | ||
* | ||
* The getSubscribedEvents() gives the core a list of events | ||
* that the plugin wants to listen to. The key of each | ||
* array section is the event that the plugin listens to | ||
* and the value (in the form of an array) contains the | ||
* callable (or function) as well as the priority. The | ||
* higher the number the higher the priority. | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'onPluginsInitialized' => ['onPluginsInitialized', 0] | ||
]; | ||
} | ||
|
||
/** | ||
* Initialize the plugin | ||
*/ | ||
public function onPluginsInitialized() | ||
{ | ||
// Don't proceed if we are in the admin plugin | ||
if ($this->isAdmin()) { | ||
return; | ||
} | ||
|
||
$this->enable([ | ||
'onShutdown' => ['onShutdown', 0] | ||
]); | ||
} | ||
|
||
public function onShutdown() | ||
{ | ||
/** @var Cache $cache */ | ||
$cache = $this->grav['cache']; | ||
$cache_id = md5('backlink'.$cache->getKey()); | ||
$backlinked = $cache->fetch($cache_id); | ||
|
||
if (!$backlinked) { | ||
// Commenting the following check out because it caused the whole function | ||
// to fail on my system. I'm open to suggestions on how to fix this. | ||
|
||
// check if this function is available, if so use it to stop any timeouts | ||
// try { | ||
// if (!Utils::isFunctionDisabled('set_time_limit') && !ini_get('safe_mode') && function_exists('set_time_limit')) { | ||
// set_time_limit(0); | ||
// } | ||
// } catch (\Exception $e) {} | ||
|
||
$backlinks = []; | ||
/** @var Pages $pages */ | ||
$pages = $this->grav['pages']; | ||
$uri = $this->grav['uri']; | ||
$root = $uri->host() . $uri->rootUrl(); | ||
$routes = $pages->routes(); | ||
|
||
foreach ($routes as $route => $path) { | ||
try { | ||
$page = $pages->get($path); | ||
// get the content and parse it for backlinks | ||
$content = $page->rawMarkdown(); | ||
$matches = array(); | ||
preg_match_all('/(?<!\!)\[.*?\]\((.*?)[\s\)]/', $content, $matches, PREG_PATTERN_ORDER); | ||
foreach ($matches[1] as $link) { | ||
// Ignore absolute links and named anchors | ||
if ( (!Utils::startsWith($link, 'http://')) && (!Utils::startsWith($link, 'https://')) && (!Utils::startsWith($link, '#')) ) { | ||
// resolve to absolute path | ||
$abspath = ''; | ||
if (Utils::startsWith($link, '/')) { | ||
$abspath = $link; | ||
} else { | ||
$abspath = $route.DS.$link; | ||
} | ||
$abspath = static::resolvePath($abspath); | ||
|
||
// Record backlink | ||
if ($route !== $abspath) { | ||
if (array_key_exists($abspath, $backlinks)) { | ||
if (!in_array($route, $backlinks[$abspath])) { | ||
$backlinks[$abspath][] = $route; | ||
} | ||
} else { | ||
$backlinks[$abspath] = [$route]; | ||
} | ||
} | ||
} | ||
} | ||
} catch (\Exception $e) { | ||
// do nothing on error | ||
} | ||
} | ||
$cache->save($cache_id, true); | ||
$path = $this->grav['locator']->findResource('user://data', true); | ||
$path .= DS.static::sanitize($this->grav['config']->get('plugins.backlinks.datafile')); | ||
$datafh = File::instance($path); | ||
$datafh->lock(); | ||
$datafh->save(YAML::dump($backlinks)); | ||
$datafh->free(); | ||
} | ||
} | ||
|
||
private static function resolvePath($path) { | ||
$elements = explode(DS, $path); | ||
$parents = array(); | ||
foreach ($elements as $dir) { | ||
switch ($dir) { | ||
case '.': | ||
break; | ||
case '..': | ||
array_pop($parents); | ||
break; | ||
default: | ||
$parents[] = $dir; | ||
break; | ||
} | ||
} | ||
if (count($parents) === 0) { | ||
return DS; | ||
} else { | ||
return implode(DS, $parents); | ||
} | ||
} | ||
|
||
private static function sanitize($fn) { | ||
$fn = trim($fn); | ||
$fn = str_replace('..', '', $fn); | ||
$fn = ltrim($fn, DS); | ||
$fn = str_replace(DS.DS, DS, $fn); | ||
return $fn; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
enabled: true | ||
datafile: 'backlinks.yaml' # relative to `user/data` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Backlinks | ||
version: 1.0.0 | ||
description: Find pages that point to another page | ||
icon: plug | ||
author: | ||
name: Aaron Dalton | ||
email: aaron@daltons.ca | ||
homepage: https://github.com/perlkonig/grav-plugin-backlinks | ||
demo: https://perlkonig.com/demos/backlinks | ||
keywords: grav, plugin, backlinks, links | ||
bugs: https://github.com/perlkonig/grav-plugin-backlinks/issues | ||
docs: https://github.com/perlkonig/grav-plugin-backlinks/blob/master/README.md | ||
license: MIT | ||
|
||
form: | ||
validation: strict | ||
fields: | ||
enabled: | ||
type: toggle | ||
label: Plugin status | ||
highlight: 1 | ||
default: 0 | ||
options: | ||
1: Enabled | ||
0: Disabled | ||
validate: | ||
type: bool |