1<?php
2/**
3 * Delete Page Button plugin
4 *
5 * @copyright (c) 2020 Damien Regad
6 * @license GPLv2 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
7 * @author  Damien Regad
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13use dokuwiki\plugin\deletepagebutton\DeletePageButton;
14
15/**
16 * Class action_plugin_deletepagebutton
17 *
18 * @package dokuwiki\plugin\deletepagebutton
19 */
20class action_plugin_deletepagebutton extends DokuWiki_Action_Plugin {
21
22    /**
23     * Register event handlers.
24     *
25     * @param Doku_Event_Handler $controller The plugin controller
26     */
27    public function register(Doku_Event_Handler $controller) {
28        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addJsInfo' );
29        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addButton' );
30        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'deletePage' );
31    }
32
33    /**
34     * Hook for DOKUWIKI_STARTED event.
35     *
36     * Adds current template to $JSINFO
37     */
38    public function addJsInfo() {
39        global $JSINFO, $conf;
40        $JSINFO['deletepagebutton_template'] = $conf['template'];
41    }
42
43    /**
44     * Hook for MENU_ITEMS_ASSEMBLY event.
45     *
46     * Adds 'Delete' button to DokuWiki's PageMenu.
47     *
48     * @param Doku_Event $event
49     */
50    public function addButton(Doku_Event $event) {
51        global $ID;
52
53        if (
54            $event->data['view'] !== 'page'
55            || !$this->canDelete($ID)
56        ) {
57            return;
58        }
59
60        array_splice($event->data['items'], -1, 0, array(new DeletePageButton()));
61    }
62
63    /**
64     * Determines whether the Delete button should be shown.
65     *
66     * @param $id
67     * @return bool
68     */
69    protected function canDelete($id) {
70        global $ACT;
71
72        return ($ACT == 'show' || empty($ACT))
73            && page_exists($id)
74            && auth_quickaclcheck($id) >= AUTH_EDIT
75            && checklock($id) === false && !@file_exists(wikiLockFN($id));
76    }
77
78    /**
79     * Hook for ACTION_ACT_PREPROCESS event.
80     *
81     * Handles the plugin's custom page deletion action: deletes the page and
82     * redirects to page view ('show' action).
83     *
84     * @param Doku_Event $event
85     */
86    public function deletePage(Doku_Event $event) {
87        global $ID, $INFO, $lang;
88
89        // Ignore other actions
90        if ($event->data != 'deletepagebutton') {
91            return;
92        };
93
94        if(checkSecurityToken() && $INFO['exists']) {
95            // Save the page with empty contents to delete it
96            saveWikiText($ID, null, $lang['deleted']);
97
98            // Display confirmation message
99            msg($this->getLang('deleted_ok'), 1);
100        }
101
102        // Redirect to page view
103        $event->data = 'redirect';
104    }
105
106}
107