Passed
Push — master ( 7f044c...429ae6 )
by Guillaume
05:11
created

Tiki::insertOnlineRedirection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Docsets;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Collection;
7
use Wa72\HtmlPageDom\HtmlPageCrawler;
8
use Illuminate\Support\Facades\Storage;
9
10
class Tiki extends BaseDocset
11
{
12
    public const CODE = 'tiki';
13
    public const NAME = 'Tiki';
14
    public const URL = 'doc.tiki.org';
15
    public const INDEX = 'All-the-Documentation.html';
16
    public const PLAYGROUND = 'https://demo.tiki.org/';
17
    public const ICON_16 = '../../icons/icon.png';
18
    public const ICON_32 = '../../icons/[email protected]';
19
    public const EXTERNAL_DOMAINS = [
20
        'themes.tiki.org',
21
    ];
22
23
24
    public function grab(): bool
25
    {
26
        $toIgnore = implode('|', [
27
            '\?refresh',
28
            '\?session_filters',
29
            '\?sort_mode',
30
            '\?todate',
31
            '\?viewmode',
32
            '/Plugins-',
33
            'comzone=',
34
            'fullscreen=',
35
            'offset=',
36
            'PDF\.js',
37
            'Plugins\.html',
38
            'structure=',
39
            'tikiversion=',
40
            'wp_files_sort_mode[0-9]=',
41
        ]);
42
43
        $toGet = implode('|', [
44
            '\.css',
45
            '\.gif',
46
            '\.ico',
47
            '\.jpg',
48
            '\.js',
49
            '\.png',
50
            '\.svg',
51
            '\.webmanifest',
52
            '/LIST',
53
            '/Module-',
54
            '/Plugin',
55
            '-Tracker-Field',
56
            'Tiki_org_family',
57
        ]);
58
59
        system(
60
            "echo; wget doc.tiki.org/All-the-Documentation \
61
                --mirror \
62
                --trust-server-names \
63
                --header 'Cookie: javascript_enabled_detect=true' \
64
                --reject-regex='{$toIgnore}' \
65
                --accept-regex='{$toGet}' \
66
                --ignore-case \
67
                --page-requisites \
68
                --adjust-extension \
69
                --convert-links \
70
                --span-hosts \
71
                --domains={$this->externalDomains()} \
72
                --directory-prefix=storage/{$this->downloadedDirectory()} \
73
                -e robots=off \
74
                --quiet \
75
                --show-progress",
76
            $result
77
        );
78
79
        return $result === 0;
80
    }
81
82 1
    public function entries(string $file): Collection
83
    {
84 1
        $crawler = HtmlPageCrawler::create(Storage::get($file));
85
86 1
        $entries = collect();
87 1
        $entries = $entries->merge($this->pluginEntries($crawler, $file));
88 1
        $entries = $entries->merge($this->moduleEntries($crawler, $file));
89 1
        $entries = $entries->merge($this->fieldEntries($crawler, $file));
90
91 1
        return $entries;
92
    }
93
94 1
    protected function pluginEntries(HtmlPageCrawler $crawler, string $file)
95
    {
96 1
        $entries = collect();
97
98 1
        if (preg_match('/Plugin/i', $file)) {
99 1
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
100
101
            $crawler->filter('#top h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
102 1
                $entries->push([
103 1
                        'name' => $node->text(),
104 1
                        'type' => 'Plugin',
105 1
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
106
                    ]);
107 1
            });
108
        }
109
110 1
        return $entries;
111
    }
112
113 1
    protected function moduleEntries(HtmlPageCrawler $crawler, string $file)
114
    {
115 1
        $entries = collect();
116
117 1
        if (preg_match('/Module/i', $file)) {
118
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
119
120
            $crawler->filter('#top h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
121
                $entries->push([
122
                        'name' => $node->text(),
123
                        'type' => 'Module',
124
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
125
                    ]);
126
            });
127
        }
128
129 1
        return $entries;
130
    }
131
132 1
    protected function fieldEntries(HtmlPageCrawler $crawler, string $file)
133
    {
134 1
        $entries = collect();
135
136 1
        if (preg_match('/Tracker-Field/i', $file)) {
137
            $path = $crawler->filter('link[rel=canonical]')->attr('href');
138
139
            $crawler->filter('#top h1:first-of-type')->each(function (HtmlPageCrawler $node) use ($entries, $file, $path) {
140
                $entries->push([
141
                        'name' => $node->text(),
142
                        'type' => 'Field',
143
                        'path' => Str::after($file . '#' . Str::slug($path), $this->innerDirectory()),
144
                    ]);
145
            });
146
        }
147
148 1
        return $entries;
149
    }
150
151 1
    public function format(string $html): string
152
    {
153 1
        $crawler = HtmlPageCrawler::create($html);
154
155 1
        $this->removeNavbar($crawler);
156 1
        $this->removeLeftSidebarButton($crawler);
157 1
        $this->removeFullscreenButton($crawler);
158 1
        $this->removePageTopModules($crawler);
159 1
        $this->removeWikiActionsWrapper($crawler);
160 1
        $this->removeBreadcrumb($crawler);
161 1
        $this->removeTopbar($crawler);
162 1
        $this->removeLeftSidebar($crawler);
163 1
        $this->removeRightSidebar($crawler);
164 1
        $this->removePagebar($crawler);
165 1
        $this->removeFooter($crawler);
166 1
        $this->removeUnwantedJavaScript($crawler);
167
168 1
        $this->updateCss($crawler);
169
170 1
        $this->insertOnlineRedirection($crawler);
171 1
        $this->insertDashTableOfContents($crawler);
172
173 1
        return $crawler->saveHTML();
174
    }
175
176 1
    protected function removeNavbar(HtmlPageCrawler $crawler)
177
    {
178 1
        $crawler->filter('nav.navbar')->remove();
179 1
    }
180
181 1
    protected function removeLeftSidebarButton(HtmlPageCrawler $crawler)
182
    {
183 1
        $crawler->filter('#row-middle > div.side-col-toggle-container')->remove();
184 1
    }
185
186 1
    protected function removeFullscreenButton(HtmlPageCrawler $crawler)
187
    {
188 1
        $crawler->filter('#fullscreenbutton')->remove();
189 1
    }
190
191 1
    protected function removePageTopModules(HtmlPageCrawler $crawler)
192
    {
193 1
        $crawler->filter('#pagetop_modules')->remove();
194 1
    }
195
196 1
    protected function removeWikiActionsWrapper(HtmlPageCrawler $crawler)
197
    {
198 1
        $crawler->filter('#col1 > div.wikiactions_wrapper')->remove();
199 1
    }
200
201 1
    protected function removeBreadcrumb(HtmlPageCrawler $crawler)
202
    {
203 1
        $crawler->filter('nav.nav-breadcrumb')->remove();
204 1
    }
205
206 1
    protected function removeTopbar(HtmlPageCrawler $crawler)
207
    {
208 1
        $crawler->filter('#topbar')->remove();
209 1
    }
210
211 1
    protected function removeLeftSidebar(HtmlPageCrawler $crawler)
212
    {
213 1
        $crawler->filter('#col2')->remove();
214 1
    }
215
216 1
    protected function removeRightSidebar(HtmlPageCrawler $crawler)
217
    {
218 1
        $crawler->filter('script[src*="autoToc.js"]')->remove();
219 1
    }
220
221 1
    protected function removePagebar(HtmlPageCrawler $crawler)
222
    {
223 1
        $crawler->filter('#page-bar')->remove();
224 1
    }
225
226 1
    protected function removeFooter(HtmlPageCrawler $crawler)
227
    {
228 1
        $crawler->filter('#footer')->remove();
229 1
    }
230
231 1
    protected function removeUnwantedJavaScript(HtmlPageCrawler $crawler)
232
    {
233 1
        $crawler->filter('script[src*=autosave]')->remove();
234 1
        $crawler->filter('script[src*=gtag]')->remove();
235 1
        $crawler->filter('noscript')->remove();
236 1
        $crawler->filterXPath("//script[text()[contains(.,'piwik.tiki.org')]]")->remove();
237 1
        $crawler->filterXPath("//script[text()[contains(.,'gtag')]]")->remove();
238 1
    }
239
240 1
    protected function updateCSS(HtmlPageCrawler $crawler)
241
    {
242 1
        $this->updateTopPadding($crawler);
243 1
        $this->updateArticlePadding($crawler);
244 1
    }
245
246 1
    protected function updateTopPadding(HtmlPageCrawler $crawler)
247
    {
248 1
        $crawler->filter('body')
249 1
            ->removeClass('navbar-padding')
250 1
            ->addClass('hide_zone_left')
251 1
            ->css('padding-top', '0')
252
        ;
253 1
    }
254
255 1
    protected function updateArticlePadding(HtmlPageCrawler $crawler)
256
    {
257 1
        $crawler->filter('article#top')
258 1
            ->css('padding-top', '44px')
259
        ;
260 1
    }
261
262 1
    protected function insertOnlineRedirection(HtmlPageCrawler $crawler)
263
    {
264 1
        $onlineUrl = '';
265 1
        $meta = $crawler->filter('meta[property="og:url"]');
266
267 1
        if ($meta->getDOMDocument()) {
268 1
            $onlineUrl = $meta->attr('content');
269
        }
270
271 1
        $crawler->filter('html')->prepend("<!-- Online page at $onlineUrl -->");
272 1
    }
273
274 1
    protected function insertDashTableOfContents(HtmlPageCrawler $crawler)
275
    {
276 1
        $crawler->filter('#top h1:first-of-type')
277 1
            ->before('<a name="//apple_ref/cpp/Section/Top" class="dashAnchor"></a>');
278
279
        $crawler->filter('h2')->each(static function (HtmlPageCrawler $node) {
280 1
            $node->before(
281 1
                '<a id="' . Str::slug($node->text()) . '" name="//apple_ref/cpp/Section/' . rawurlencode($node->text()) . '" class="dashAnchor"></a>'
282
            );
283 1
        });
284 1
    }
285
}
286