1<?php
2// created by Dave H - The Life Ninja : https://newlife.ninja
3// Sept-2024
4
5ini_set('display_errors','1');
6error_reporting(E_ALL);
7
8echo "CSV TO DOKUWIKI EXPLODER";
9echo "<br/>";
10echo "<br/>";
11
12// SET THESE VALUES...
13// ----------------------
14
15$namespace 		= 'csvtest'; 	// THIS IS THE ONLY VALUE YOU REALLY NEED TO CHANGE
16								// (except the next one if your filename is not in first column)
17
18$pagename_column = array(8,9);			// which column in the csv file contains
19								// the wiki page name to be created (starting from 0)
20								// IMPORTANT - MAKE SURE THIS IS CORRECT!
21								// Note: two columns can be combined to make unique filenames
22								// 		 by using an array such as: $pagename_column = array(8,9);
23								//		 where data from columns 8 and 9 are combined
24
25$OVERWRITE_FILES = FALSE;		// you can set this to TRUE if you want to allow existing pages
26								// to be overwritten
27
28								// ---------------------------------------------------
29
30$indexpage 		= 'start';  	// the default dokuwiki page is 'start' but you can (if you wish)
31								// change this value so that a different index page is created
32
33$csvfile 		= 'csvdata';	// the dokuwiki page in which you pasted the csv data
34$templatefile	= 'csvtemplate';// the dokuwiki page you created holding the template
35
36// ----------------------
37// DO NOT EDIT BELOW HERE
38
39$outputfilepath = '../data/pages/'.$namespace.'/'; // where the page files are going to be created
40
41$indexpagecontent = "=====REMINDER: DELETE THE CSVTODW FOLDER OFF YOUR SERVER!!!=====\r\n\r\n" .
42					"==== INDEX ====\r\n\r\n Pages created from csv data:\r\n\r\n----\r\n\r\n";
43
44$dr = rtrim( $_SERVER['DOCUMENT_ROOT'], "/" ) .'/';
45
46// get the template file contents
47$template 	= file_get_contents($outputfilepath.$templatefile.'.txt');
48
49// open the csv data file
50$file = fopen($outputfilepath.$csvfile.'.txt', 'r');
51
52// get field names from first row
53$fieldnames = fgetcsv($file);
54
55// if we have some fieldnames lets rock'n'roll
56if ($fieldnames !== FALSE) {
57
58	if (array_count_values($fieldnames) >= 1) {
59
60		while (( $line = fgetcsv($file) ) !== FALSE) {
61
62			if ( !empty(array_filter($line)) ) {
63
64				// get clean copy of the template
65				$thispage = $template;
66
67				// set the output filename
68				if ( ! is_array($pagename_column) ) {
69					$outputfile 	   = trim($line[$pagename_column]).'.txt';
70				} else {
71					$outputfile = '';
72					// combine column data into filename
73					foreach ($pagename_column as $col) {
74						$outputfile	   .= trim($line[$col]).'_';
75					}
76					$outputfile = rtrim($outputfile, "_").'.txt';
77				}
78
79				// clean output filename to dokuwiki standards
80				$ofclean = strtolower(preg_replace('/\s+/', '', $outputfile));
81
82				$indexpagecontent .= '  * [['.substr($ofclean, 0, -4)."|".substr($outputfile, 0, -4)."]]\r\n";
83
84				// replace the template [placeholder] names with content
85				foreach ($fieldnames as $key => $field) {
86					$thispage = str_replace('['.strtolower($field).']', $line[$key], $thispage);
87				}
88
89				echo "<hr/>";
90
91				$doit = FALSE;  // failsafe
92				IF ( !file_exists($dr.ltrim($outputfilepath, "./").$outputfile) || $OVERWRITE_FILES == TRUE ) {
93					$doit = TRUE;
94				}
95
96				if ( $doit ) {
97					if ( $doit && file_put_contents($outputfilepath.$ofclean, $thispage) !== FALSE ) {
98						echo 'Creating wiki page: '.$outputfilepath.$ofclean."\r\n";
99					} else {
100						echo "ERROR creating file: ".$outputfilepath.$ofclean."\r\n";
101					}
102				} else {
103					echo 'SKIPPING: Wiki page: '.$outputfilepath.$ofclean." already exists\r\n";
104				}
105
106				echo "<hr/>";
107			}
108		}
109
110		// create index page
111		if ( file_put_contents($outputfilepath.$indexpage.'.txt', $indexpagecontent) !== FALSE ) {
112			echo 'Creating wiki index page: '.$outputfilepath.$indexpage.'.txt'."\r\n";
113		} else {
114			echo "ERROR creating index file: ".$outputfilepath.$indexpage.'.txt'."\r\n";
115		}
116	}
117}
118
119fclose($file);
120
121echo "<h2><bold>REMINDER: DELETE THIS CSVTODW FOLDER OFF YOUR SERVER!!!</bold></h2>";
122echo "<br/>";
123echo 'Go to '.$namespace.' <a href="/'.$namespace.'/'.$indexpage.'">index</a> page.';
124?>