1<?php
2
3use dokuwiki\ChangeLog\PageChangeLog;
4
5/**
6 * All DokuWiki plugins to extend the admin function
7 * need to inherit from this class
8 */
9class admin_plugin_revert extends DokuWiki_Admin_Plugin
10{
11    protected $cmd;
12    // some vars which might need tuning later
13    protected $max_lines = 800; // lines to read from changelog
14    protected $max_revs  = 20;  // numer of old revisions to check
15
16
17    /**
18     * Constructor
19     */
20    public function __construct()
21    {
22        $this->setupLocale();
23    }
24
25    /**
26     * access for managers
27     */
28    public function forAdminOnly()
29    {
30        return false;
31    }
32
33    /**
34     * return sort order for position in admin menu
35     */
36    public function getMenuSort()
37    {
38        return 40;
39    }
40
41    /**
42     * handle user request
43     */
44    public function handle()
45    {
46    }
47
48    /**
49     * output appropriate html
50     */
51    public function html()
52    {
53        global $INPUT;
54
55        echo $this->locale_xhtml('intro');
56
57        $this->printSearchForm();
58
59        if (is_array($INPUT->param('revert')) && checkSecurityToken()) {
60            $this->revertEdits($INPUT->arr('revert'), $INPUT->str('filter'));
61        } elseif ($INPUT->has('filter')) {
62            $this->listEdits($INPUT->str('filter'));
63        }
64    }
65
66    /**
67     * Display the form for searching spam pages
68     */
69    protected function printSearchForm()
70    {
71        global $lang, $INPUT;
72        echo '<form action="" method="post"><div class="no">';
73        echo '<label>'.$this->getLang('filter').': </label>';
74        echo '<input type="text" name="filter" class="edit" value="'.hsc($INPUT->str('filter')).'" /> ';
75        echo '<button type="submit">'.$lang['btn_search'].'</button> ';
76        echo '<span>'.$this->getLang('note1').'</span>';
77        echo '</div></form><br /><br />';
78    }
79
80    /**
81     * Start the reversion process
82     */
83    protected function revertEdits($revert, $filter)
84    {
85        echo '<hr /><br />';
86        echo '<p>'.$this->getLang('revstart').'</p>';
87
88        echo '<ul>';
89        foreach ($revert as $id) {
90            global $REV;
91
92            // find the last non-spammy revision
93            $data = '';
94            $pagelog = new PageChangeLog($id);
95            $old  = $pagelog->getRevisions(0, $this->max_revs);
96            if (count($old)) {
97                foreach ($old as $REV) {
98                    $data = rawWiki($id, $REV);
99                    if (strpos($data, $filter) === false) break;
100                }
101            }
102
103            if ($data) {
104                saveWikiText($id, $data, 'old revision restored', false);
105                printf('<li><div class="li">'.$this->getLang('reverted').'</div></li>', $id, $REV);
106            } else {
107                saveWikiText($id, '', '', false);
108                printf('<li><div class="li">'.$this->getLang('removed').'</div></li>', $id);
109            }
110            @set_time_limit(10);
111            flush();
112        }
113        echo '</ul>';
114
115        echo '<p>'.$this->getLang('revstop').'</p>';
116    }
117
118    /**
119     * List recent edits matching the given filter
120     */
121    protected function listEdits($filter)
122    {
123        global $conf;
124        global $lang;
125        echo '<hr /><br />';
126        echo '<form action="" method="post"><div class="no">';
127        echo '<input type="hidden" name="filter" value="'.hsc($filter).'" />';
128        formSecurityToken();
129
130        $recents = getRecents(0, $this->max_lines);
131        echo '<ul>';
132
133        $cnt = 0;
134        foreach ($recents as $recent) {
135            if ($filter) {
136                if (strpos(rawWiki($recent['id']), $filter) === false) continue;
137            }
138
139            $cnt++;
140            $date = dformat($recent['date']);
141
142            echo ($recent['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) ? '<li class="minor">' : '<li>';
143            echo '<div class="li">';
144            echo '<input type="checkbox" name="revert[]" value="'.hsc($recent['id']).
145                '" checked="checked" id="revert__'.$cnt.'" />';
146            echo ' <label for="revert__'.$cnt.'">'.$date.'</label> ';
147
148            echo '<a href="'.wl($recent['id'], "do=diff").'">';
149            $p = array();
150            $p['src']    = DOKU_BASE.'lib/images/diff.png';
151            $p['width']  = 15;
152            $p['height'] = 11;
153            $p['title']  = $lang['diff'];
154            $p['alt']    = $lang['diff'];
155            $att = buildAttributes($p);
156            echo "<img $att />";
157            echo '</a> ';
158
159            echo '<a href="'.wl($recent['id'], "do=revisions").'">';
160            $p = array();
161            $p['src']    = DOKU_BASE.'lib/images/history.png';
162            $p['width']  = 12;
163            $p['height'] = 14;
164            $p['title']  = $lang['btn_revs'];
165            $p['alt']    = $lang['btn_revs'];
166            $att = buildAttributes($p);
167            echo "<img $att />";
168            echo '</a> ';
169
170            echo html_wikilink(':'.$recent['id'], (useHeading('navigation'))?null:$recent['id']);
171            echo ' – '.htmlspecialchars($recent['sum']);
172
173            echo ' <span class="user">';
174                echo $recent['user'].' '.$recent['ip'];
175            echo '</span>';
176
177            echo '</div>';
178            echo '</li>';
179
180            @set_time_limit(10);
181            flush();
182        }
183        echo '</ul>';
184
185        echo '<p>';
186        echo '<button type="submit">'.$this->getLang('revert').'</button> ';
187        printf($this->getLang('note2'), hsc($filter));
188        echo '</p>';
189
190        echo '</div></form>';
191    }
192}
193//Setup VIM: ex: et ts=4 :
194