1<?php
2
3/**
4 * Plugin BatchEdit
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10require_once(DOKU_PLUGIN . 'batchedit/config.php');
11require_once(DOKU_PLUGIN . 'batchedit/engine.php');
12require_once(DOKU_PLUGIN . 'batchedit/interface.php');
13require_once(DOKU_PLUGIN . 'batchedit/request.php');
14
15class admin_plugin_batchedit extends DokuWiki_Admin_Plugin {
16
17    private static $instance = NULL;
18
19    private $command;
20    private $config;
21    private $session;
22
23    public static function getInstance() {
24        return self::$instance;
25    }
26
27    public function __construct() {
28        $this->command = BatcheditRequest::COMMAND_WELCOME;
29        $this->config = new BatcheditConfig();
30        $this->session = new BatcheditSession($this->getConf('sessionexp'));
31
32        self::$instance = $this;
33    }
34
35    /**
36     *
37     */
38    public function getConfig() {
39        return $this->config;
40    }
41
42    /**
43     * Handle user request
44     */
45    public function handle() {
46        try {
47            $this->handleRequest();
48        }
49        catch (BatcheditException $error) {
50            $this->session->setError($error);
51            $this->session->expire();
52        }
53    }
54
55    /**
56     * Output appropriate html
57     */
58    public function html() {
59        $interface = new BatcheditInterface($this);
60
61        $interface->configure($this->config);
62
63        $interface->printBeginning($this->session->getId());
64        $interface->printMessages($this->session->getMessages());
65
66        if ($this->session->getMatchCount() > 0) {
67            $interface->printTotalStats($this->command, $this->session->getMatchCount(),
68                    $this->session->getPageCount(), $this->session->getEditCount());
69            $interface->printMatches($this->session->getPages());
70        }
71
72        $interface->printMainForm($this->session->getMatchCount() > 0);
73        $interface->printEnding();
74    }
75
76    /**
77     *
78     */
79    private function handleRequest() {
80        $request = new BatcheditRequest($this->config);
81
82        $this->command = $request->getCommand();
83
84        switch ($this->command) {
85            case BatcheditRequest::COMMAND_PREVIEW:
86                $this->handlePreview($request);
87                break;
88
89            case BatcheditRequest::COMMAND_APPLY:
90                $this->handleApply($request);
91                break;
92        }
93    }
94
95    /**
96     *
97     */
98    private function handlePreview($request) {
99        $engine = $this->createEngine();
100
101        $this->session->setId($request->getSessionId());
102        $this->findMatches($engine, $request);
103        $this->markMatches($engine, $request);
104        $this->session->save($request, $this->config);
105    }
106
107    /**
108     *
109     */
110    private function handleApply($request) {
111        $engine = $this->createEngine();
112
113        if (!$this->session->load($request, $this->config)) {
114            $this->findMatches($engine, $request);
115        }
116
117        $this->applyMatches($engine, $request);
118        $this->session->save($request, $this->config);
119    }
120
121    /**
122     *
123     */
124    private function createEngine() {
125        if ($this->getConf('timelimit') > 0) {
126            set_time_limit($this->getConf('timelimit'));
127        }
128
129        return new BatcheditEngine($this->session);
130    }
131
132    /**
133     *
134     */
135    private function findMatches($engine, $request) {
136        $engine->findMatches($request->getNamespace(), $request->getRegexp(), $request->getReplacement(),
137                $this->config->getConf('searchlimit') ? $this->config->getConf('searchmax') : -1,
138                $this->config->getConf('matchctx') ? $this->config->getConf('ctxchars') : 0,
139                $this->config->getConf('ctxlines'), $this->config->getConf('tplpatterns'));
140    }
141
142    /**
143     *
144     */
145    private function markMatches($engine, $request) {
146        if (!$this->config->getConf('keepmarks') || $this->session->getMatchCount() == 0 || empty($request->getAppliedMatches())) {
147            return;
148        }
149
150        $engine->markRequestedMatches($request->getAppliedMatches(), $this->config->getConf('markpolicy'));
151    }
152
153    /**
154     *
155     */
156    private function applyMatches($engine, $request) {
157        if ($this->session->getMatchCount() == 0 || empty($request->getAppliedMatches())) {
158            return;
159        }
160
161        $engine->markRequestedMatches($request->getAppliedMatches());
162        $engine->applyMatches($request->getSummary(), $request->getMinorEdit());
163    }
164}
165