1<?php
2
3    /**
4     * Exports the saved information of the plugin to an csv file
5     *
6     * Parameters:
7     * - id        The id of the plugin instance which should be exported
8     */
9
10
11    // Farmpatch:
12    // $tmp = explode('/',$_SERVER['SCRIPT_FILENAME']); array_pop($tmp); array_pop($tmp);
13    // if(!defined('DOKU_INC')) define('DOKU_INC',dirname(join('/',$tmp)).'/../');
14
15    if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
16    if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
17    if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
18
19    require_once(DOKU_CONF."dokuwiki.php");
20    require_once(DOKU_INC."inc/utf8.php");
21
22    // set metadir acording to config (see inc/init.php)
23    $conf['metadir'] = $conf['savedir'].'/meta';
24
25
26    // read parameters
27    $dID = $_GET['id'];
28
29
30    // get doodle file contents
31    $file = DOKU_INC.metaFN(md5($dID), '.btable');
32    $content = unserialize(@file_get_contents($file));
33
34    // write out header
35	  header("Content-type: text/csv");
36	  header("Content-Disposition: attachment; filename=".$dID.".csv");
37
38    // write out content
39    if (is_array($content)) {
40
41        $rows = array_keys($content);
42        $columns = array_keys($content[$rows[0]]);
43
44        if (count($columns) > 0) {
45
46            foreach($columns as $column) {
47                echo(",".$column);
48            }
49
50            echo("\n");
51
52            foreach ($rows as $row) {
53
54                echo($row);
55
56                foreach ($columns as $column) {
57
58                    if ($content[$row][$column]) {
59                        echo(",1");
60                    } else {
61                        echo(",0");
62                    }
63                }
64
65                echo("\n");
66            }
67        }
68    }
69
70
71
72
73    /**
74     * Remove unwanted chars from ID
75     *
76     * Cleans a given ID to only use allowed characters. Accented characters are
77     * converted to unaccented ones
78     *
79     * @author Andreas Gohr <andi@splitbrain.org>
80     * @param  string  $raw_id    The pageid to clean
81     * @param  boolean $ascii     Force ASCII
82     * @see inc/pageutils.php
83     */
84    function cleanID($raw_id,$ascii=false){
85        global $conf;
86        static $sepcharpat = null;
87
88        global $cache_cleanid;
89        $cache = & $cache_cleanid;
90
91        // check if it's already in the memory cache
92        if (isset($cache[$raw_id])) {
93            return $cache[$raw_id];
94        }
95
96        $sepchar = $conf['sepchar'];
97        if($sepcharpat == null) // build string only once to save clock cycles
98            $sepcharpat = '#\\'.$sepchar.'+#';
99
100        $id = trim($raw_id);
101        $id = utf8_strtolower($id);
102
103        //alternative namespace seperator
104        $id = strtr($id,';',':');
105        if($conf['useslash']) {
106            $id = strtr($id,'/',':');
107        }else{
108            $id = strtr($id,'/',$sepchar);
109        }
110
111        if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
112        if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
113
114        //remove specials
115        $id = utf8_stripspecials($id,$sepchar,'\*');
116
117        if($ascii) $id = utf8_strip($id);
118
119        //clean up
120        $id = preg_replace($sepcharpat,$sepchar,$id);
121        $id = preg_replace('#:+#',':',$id);
122        $id = trim($id,':._-');
123        $id = preg_replace('#:[:\._\-]+#',':',$id);
124
125        $cache[$raw_id] = $id;
126
127        return($id);
128    }
129
130
131    /**
132     * returns the full path to the meta file specified by ID and extension
133     *
134     * The filename is URL encoded to protect Unicode chars
135     *
136     * @author Steven Danz <steven-danz@kc.rr.com>
137     * @see inc/pageutils.php
138     */
139    function metaFN($id,$ext){
140        global $conf;
141
142        $id = cleanID($id);
143        $id = str_replace(':','/',$id);
144        $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
145
146        return $fn;
147    }
148?>
149