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