Completed
Branch master (ae6477)
by Michael
01:35
created

action_plugin_pwaoffline   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
dl 0
loc 102
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeConfigToJSINFO() 0 22 3
A add144pxImageToManifest() 0 5 1
B collectPagesToCache() 0 29 5
A register() 0 5 1
1
<?php
2
3
class action_plugin_pwaoffline extends DokuWiki_Action_Plugin
0 ignored issues
show
Bug introduced by
The type DokuWiki_Action_Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    /**
7
     * Registers a callback function for a given event
8
     *
9
     * @param Doku_Event_Handler $controller DokuWiki's event controller object
10
     *
11
     * @return void
12
     */
13
    public function register(Doku_Event_Handler $controller)
0 ignored issues
show
Bug introduced by
The type Doku_Event_Handler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
    {
15
        $controller->register_hook('MANIFEST_SEND', 'BEFORE', $this, 'add144pxImageToManifest');
16
        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'collectPagesToCache');
17
        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'writeConfigToJSINFO');
18
19
    }
20
21
    /**
22
     * [Custom event handler which performs action]
23
     *
24
     * Event: MANIFEST_SEND
25
     *
26
     * @param Doku_Event $event  event object by reference
27
     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
28
     *                           handler was registered]
29
     *
30
     * @return void
31
     */
32
    public function add144pxImageToManifest(Doku_Event $event, $param)
0 ignored issues
show
Bug introduced by
The type Doku_Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
    {
34
        $event->data['icons'][] = [
35
            'src' => DOKU_BASE . 'lib/plugins/pwaoffline/144.png',
0 ignored issues
show
Bug introduced by
The constant DOKU_BASE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
            'sizes' => '144x144',
37
        ];
38
    }
39
40
    /**
41
     * Event: AJAX_CALL_UNKNOWN
42
     *
43
     * @param Doku_Event $event
44
     * @param            $param
45
     */
46
    public function collectPagesToCache(Doku_Event $event, $param)
47
    {
48
        if ($event->data !== 'plugin_pwaoffline') {
49
            return;
50
        }
51
52
        global $conf, $INPUT;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
53
54
        // fixme do a full resync if the config was saved?
55
        $ts = $INPUT->has('ts') ? $INPUT->int('ts') : 0;
56
57
        search($pages, $conf['datadir'], 'search_allpages', ['skipacl' => false]);
0 ignored issues
show
Bug introduced by
The function search was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        search($pages, $conf['datadir'], 'search_allpages', ['skipacl' => false]);
Loading history...
Comprehensibility Best Practice introduced by
The variable $pages seems to be never defined.
Loading history...
58
59
        $pagesToCache = [];
60
        foreach ($pages as $pageData) {
61
            if ($pageData['mtime'] < $ts) {
62
                continue;
63
            }
64
            $pagesToCache[] = [
65
                'link' => wl($pageData['id']),
0 ignored issues
show
Bug introduced by
The function wl was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
                'link' => /** @scrutinizer ignore-call */ wl($pageData['id']),
Loading history...
66
                'lastmod' => $pageData['mtime'],
67
            ];
68
        }
69
70
        header('Content-Type:application/json');
71
        echo json_encode($pagesToCache);
72
73
        $event->preventDefault();
74
        $event->stopPropagation();
75
    }
76
77
    /**
78
     * Event: DOKUWIKI_STARTED
79
     *
80
     * @param Doku_Event $event
81
     * @param            $param
82
     */
83
    public function writeConfigToJSINFO(Doku_Event $event, $param)
84
    {
85
        global $ACT;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
86
        if (act_clean($ACT) === 'pwaoffline_serviceworker') {
0 ignored issues
show
Bug introduced by
The function act_clean was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        if (/** @scrutinizer ignore-call */ act_clean($ACT) === 'pwaoffline_serviceworker') {
Loading history...
87
            header('Content-Type:application/javascript');
88
            $swjs = file_get_contents(__DIR__ . '/sw.js');
89
            echo $swjs;
90
            echo "const swHashVersion = '" . md5($swjs) . "';\n";
91
            $idbKeyVal = file_get_contents(__DIR__ . '/node_modules/idb-keyval/dist/idb-keyval-iife.min.js');
92
            echo $idbKeyVal;
93
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
94
        }
95
96
        global $JSINFO;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
97
        header('X-DWPLUGIN-PWAOFFLINE-ACT:' . act_clean($ACT));
98
        if (empty($JSINFO['plugins'])) {
99
            $JSINFO['plugins'] = [];
100
        }
101
102
        $JSINFO['plugins']['pwaoffline'] = [
103
            'ts' => time(),
104
            'swHashVersion' => md5(file_get_contents(__DIR__ . '/sw.js')),
105
        ];
106
    }
107
108
}
109