|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpTek\Exodus\Tool; |
|
4
|
|
|
|
|
5
|
|
|
use phpQuery; |
|
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* Helper class for rewriting links using phpQuery. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Sam Minnee <[email protected]> |
|
14
|
|
|
* @author Russell Michell <[email protected]> |
|
15
|
|
|
* @author Michael Parkhill <[email protected]> |
|
16
|
|
|
* @package phptek/silverstripe-exodus |
|
17
|
|
|
* @see {@link StaticSiteRewriteLinksTask} |
|
18
|
|
|
*/ |
|
19
|
|
|
class StaticSiteLinkRewriter |
|
20
|
|
|
{ |
|
21
|
|
|
use Injectable; |
|
22
|
|
|
use Configurable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Simple map of tags to count as "linkable" |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tagMap = [ |
|
30
|
|
|
'a' => 'href', |
|
31
|
|
|
'img' => 'src', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The callback function to run over each link. |
|
36
|
|
|
* |
|
37
|
|
|
* @var Object |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $callback; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @param $callback |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($callback) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->callback = $callback; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set a map of tags & attributes to search for URls. |
|
53
|
|
|
* Each key is a tagname, and each value is an array of attribute names. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $tagMap |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setTagMap($tagMap) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->tagMap = $tagMap; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return the tagmap |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getTagMap() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->tagMap; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @param phpQueryObject $pq |
|
|
|
|
|
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function rewriteInPQ($pq): void |
|
79
|
|
|
{ |
|
80
|
|
|
$callback = $this->callback; |
|
81
|
|
|
|
|
82
|
|
|
// Make URLs absolute |
|
83
|
|
|
foreach ($this->tagMap as $tag => $attribute) { |
|
84
|
|
|
foreach ($pq[$tag] as $tagObj) { |
|
85
|
|
|
if ($url = pq($tagObj)->attr($attribute)) { |
|
86
|
|
|
$newURL = $callback($url); |
|
87
|
|
|
pq($tagObj)->attr($attribute, $newURL); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Rewrite URLs in the given content snippet. Returns the updated content. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $content The content containing the links to rewrite. |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function rewriteInContent($content) |
|
100
|
|
|
{ |
|
101
|
|
|
$pq = phpQuery::newDocument($content); |
|
|
|
|
|
|
102
|
|
|
$this->rewriteInPQ($pq); |
|
103
|
|
|
|
|
104
|
|
|
return $pq->html(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|