1<?php
2require_once "System.php";
3require_once('SyntaxConverter.php');
4require_once('ImportUtils.php');
5
6/**
7 * Action Plugin
8 *
9 * @license     GPL 3 (http://www.gnu.org/licenses/gpl.html)
10 * @author      Thibault Dory (thibault.dory@gmail.com)
11 * @version     1.0
12 */
13
14if(!defined('DOKU_INC')) die();
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
16
17class action_plugin_docimporter extends DokuWiki_Action_Plugin {
18  function register($controller) {
19    $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_importer', array());
20  }
21
22  function _importer(&$event, $param) {
23    if ( auth_quickaclcheck( $ID ) < AUTH_EDIT ) return;
24    if ( $event->data != 'doc2dw' ) return;
25    if ( $_FILES['doc'] ) {
26
27      $pageName = $_POST["title"];
28      $nameSpace = $_POST["ns"];
29      if (!$nameSpace) {
30	$nameSpace = "wiki";
31      }
32      #Create temporary directory
33      $tempDir = System::mktemp("-d word_upload");
34
35      $fileName = basename($_FILES['doc']['name']);
36      $fileName = preg_replace("/ /", "_", $fileName);
37      $agnosticFileName = preg_split("/\./", $fileName);
38      $agnosticFileName = $agnosticFileName[0];
39      if(move_uploaded_file($_FILES['doc']['tmp_name'], $tempDir."/". $fileName)){
40
41        #Convert the doc to HTML and MediaWiki
42        $result = exec("export HOME=/tmp && cd ".$tempDir." && convert_to_mediawiki ".$fileName);
43
44        #Load html and MediaWiki
45        $myWikiContent = file_get_contents($tempDir."/".$agnosticFileName.".txt");
46        $myHTMLContent = file_get_contents($tempDir."/".$agnosticFileName.".html");
47
48        #Replace nbsp by space
49        $myWikiContent = preg_replace("/&nbsp;/", " ",$myWikiContent);
50        #Remove center tags
51        $myWikiContent = preg_replace("/<center>/", " ",$myWikiContent);
52        $myWikiContent = preg_replace("/<\/center>/", " \n",$myWikiContent);
53
54        #Remove buggy Toc tags
55        $myWikiContent = preg_replace("/\\[#_Toc.*\\]/", " \n",$myWikiContent);
56
57        #Convert from MediaWiki to dokuwiki
58        $converter = new MediaWiki2DokuWiki_MediaWiki_SyntaxConverter($myWikiContent);
59        $myWikiContent = $converter->convert();
60
61        #Remove buggy //
62        $myWikiContent = preg_replace("/\/\//", "",$myWikiContent);
63
64        #Get undernlined sentences from html and replace them in the wiki text
65        $underlined_sentences = get_tagged_from_html($myHTMLContent, "underlined");
66        $myWikiContent = replace_from_list($myWikiContent, $underlined_sentences, "underlined");
67
68        #Get italic sentences from html and replace them in the wiki text
69        $italic_sentences = get_tagged_from_html($myHTMLContent, "italic");
70        $myWikiContent = replace_from_list($myWikiContent, $italic_sentences, "italic");
71
72        #Get all the images from the html
73        $myWikiContent = get_images_from_html($myHTMLContent, $myWikiContent);
74
75        #Convert tables
76        $raw_tables = array();
77        $good_tables = array();
78        preg_match_all("/\{\|(.*)\|\}/sU", $myWikiContent, $tables);
79        foreach ($tables[1] as $match) {
80            array_push($raw_tables, '{|'.$match.'|}');
81            array_push($good_tables, convert_table($match));
82        }
83        $myWikiContent = str_replace($raw_tables, $good_tables, $myWikiContent);
84
85        #Transform references into footnotes
86        $myWikiContent = preg_replace_callback("/<ref.*>(.*)<\/ref>/sU", "convert_footnote",$myWikiContent);
87
88        #Remove junk <br/> tags
89        $myWikiContent = preg_replace("/<br\/>/", "\n",$myWikiContent);
90
91        #Remove junk <references/>
92        $myWikiContent = preg_replace("/----\n<references\/>/", "",$myWikiContent);
93
94        #Remove ** that does not have the corresponding closing **
95        $content_lines = explode(PHP_EOL, $myWikiContent);
96        $myWikiContent = "";
97        foreach ($content_lines as $line) {
98            $first = strpos($line, "**");
99            if ($first) {
100                $second = strpos($line, "**", $first+2);
101                if (!$second) {
102                    $line = preg_replace("/\*\*/U","",$line);
103                }
104            }
105            $myWikiContent = $myWikiContent.$line."\n";
106        }
107
108        #Send the wiki page
109        $username = $this->getConf('api_username');
110        $password = $this->getConf('api_password');
111        $client = new IXR_Client('http://localhost/dokuwiki/lib/exe/xmlrpc.php');
112        $ok = $client->query('dokuwiki.login', $username, $password);
113        if ($ok) {
114          $attrs = array('sum' => 'First try', 'minor' => false);
115          $result = $client->query('wiki.putPage', $nameSpace.':'.$pageName, $myWikiContent, $attrs);
116
117          #Send all the images as attachements
118          if ($handle = opendir($tempDir)) {
119
120            while (false !== ($entry = readdir($handle))) {
121                if (strpos($entry,'.png') !== false || strpos($entry,'.jpg') !== false || strpos($entry,'.gif') !== false) {
122                     $image_data = file_get_contents($tempDir."/".$entry, true);
123                     //$image_data = base64_encode($image_data);
124                     $image_data = new IXR_Base64($image_data);
125                     $attrs = array('ow' => true);
126                     $client->query('wiki.putAttachment', $entry, $image_data, $attrs);
127                }
128            }
129
130            closedir($handle);
131
132          }
133        }
134      }
135
136      $event->data = $this->getConf('parserPostDisplay');
137      send_redirect(wl($nameSpace.':'.$pageName, '', true, '&'));
138    }
139  }
140}
141
142?>
143