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