| 1 | <?php |
||
| 2 | |||
| 3 | class action_plugin_pwaoffline extends DokuWiki_Action_Plugin |
||
| 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) |
||
| 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) |
||
| 33 | { |
||
| 34 | $event->data['icons'][] = [ |
||
| 35 | 'src' => DOKU_BASE . 'lib/plugins/pwaoffline/144.png', |
||
| 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; |
||
| 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
Comprehensibility
Best Practice
introduced
by
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']), |
||
| 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; |
||
| 86 | if (act_clean($ACT) === 'pwaoffline_serviceworker') { |
||
| 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
|
|||
| 94 | } |
||
| 95 | |||
| 96 | global $JSINFO; |
||
| 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 |