1<?php
2// dokuwiki2mediawiki.php (c) 2010-2015 by Thorsten Staerk
3// This program reads files containing dokuwiki syntax and converts them into files
4// containing mediawiki syntax.
5// The source file is given by parameter, the target file is the source file plus a
6// ".mod" suffix.
7
8// TODO:
9// filename: not only one file name!
10// test if we overwrite a file
11// test if file exists
12// allow rowspan (::: in dokuwiki syntax)
13
14$in_table=false;
15
16if ($argc==1)
17{
18  echo "dokuwiki2mediawiki.php (c) 2010-2012 by Thorsten Staerk\n";
19  echo "This program converts dokuwiki syntax to mediawiki syntax.\n";
20  echo "The source file is given as an argument, the target file is the same plus the suffix \".mod\"\n";
21  echo "Usage: php dokuwiki2mediawiki <file>\n";
22  echo "Example: php dokuwiki2mediawiki start.txt\n";
23}
24else
25for ($argument=1;$argument<$argc;$argument++)
26{
27  $filename=$argv[$argument];
28  $inputfile=fopen($filename,"r");
29  $i=0;
30  $output="";
31  if ($inputfile)
32  {
33    while (!feof($inputfile))
34    {
35      $lines[$i++]=fgets($inputfile); //we start counting a 0
36    }
37    fclose($inputfile);
38  }
39  $linecount=$i;
40  $i=-1;
41
42  while (++$i<$linecount)
43  {
44    if ($in_table) $row++;
45    // replace headings
46    $line=$lines[$i];
47    if (preg_match('/^ *======.*====== *$/',$line))
48    {
49      $line=preg_replace('/^ *======/','=',$line);
50      $line=preg_replace('/====== *$/','=',$line);
51    }
52    elseif (preg_match('/^ *=====.*===== *$/',$line))
53    {
54      $line=preg_replace('/^ *=====/','==',$line);
55      $line=preg_replace('/===== *$/','==',$line);
56    }
57    elseif (preg_match('/^ *====.*==== *$/',$line))
58    {
59      $line=preg_replace('/^ *====/','===',$line);
60      $line=preg_replace('/==== *$/','===',$line);
61    }
62    elseif (preg_match('/^ *===.*=== *$/',$line))
63    {
64      $line=preg_replace('/^ *===/','====',$line);
65      $line=preg_replace('/=== *$/','====',$line);
66    }
67    elseif (preg_match('/^ *==.*== *$/',$line))
68    {
69      $line=preg_replace('/^ *==/','=====',$line);
70      $line=preg_replace('/== *$/','=====',$line);
71    }
72    // end of replace headings
73
74    // replace bulletpoints
75    $level=0; // level of bulletpoints, e.g. * is level 1, *** is level 3.
76    while (preg_match('/^(  )+\*/',$line))
77    {
78      $line=preg_replace("/^  /","",$line);
79      $level++;
80    }
81    while ($level>1)
82    {
83      $line="*".$line;
84      $level--;
85    }
86    // end of replace bulletpoints
87
88    // replace ordered list items
89    $level=0; // level of list items, e.g. - is level 1, --- is level 3.
90    while (preg_match('/^(  )+\-/',$line))
91    {
92      $line=preg_replace("/^  /","",$line);
93      $level++;
94      $line=preg_replace("/^-/","#",$line);
95    }
96
97    while ($level>1)
98    {
99      $line="#".$line;
100      $level--;
101    }
102    // end of replace ordered list items
103
104    // replace //
105    $line=preg_replace("/\/\//","''",$line);
106    // end of replace //
107
108    // replace **
109    $line=preg_replace("/\*\*/","'''",$line);
110    // end of replace **
111
112    // replace \\
113    // thanks to Rakete Kalle
114    $line=preg_replace("/\\\\\\\\ /","<br />",$line);
115    // end of replace \\
116
117    // begin care for tables
118    if (preg_match("/^\|/",$line))
119    {
120      $line=preg_replace("/\| *$/","",$line);
121      $line=preg_replace("/\n/","",$line);
122      if (!$in_table)
123      {
124        $in_table=true;
125        $row=1;
126      }
127      $cells[$row]=explode("|",preg_replace("/^\|/","",$line));
128    }
129
130    // have we left a table?
131    if ((!preg_match("/^\|/",$line)) && $in_table)
132    {
133      $in_table=false;
134      if ($headers!="")
135      for ($n=0;$n<count($headers);$n=$n+1)
136      {
137        echo $headers[$n]; echo "|";
138      }
139      echo "\n";
140      for ($y=1;$y<count($cells)+1;$y=$y+1)
141      {
142        for ($x=0;$x<count($cells[$y]);$x=$x+1)
143        {
144          echo $cells[$y][$x]; echo "|";
145        }
146        echo "\n";
147      }
148      echo "***** END *****\n";
149      $rowspancells=$cells;
150
151      // each cell's rowspan value is 1
152      for ($y=1;$y<count($cells)+1;$y=$y+1)
153        for ($x=0;$x<count($cells[$y]);$x=$x+1)
154          $rowspancells[$y][$x]=1;
155
156      // every cell that needs an attribute rowspan=x gets x as its rowspan value
157      for ($y=1;$y<count($cells);$y=$y+1)
158      {
159        for ($x=0;$x<count($cells[$y]);$x=$x+1)
160        {
161          $z=1;
162          while (($y+$z<=count($cells)) && (preg_match("/ *::: */",$cells[$y+$z][$x])))
163          {
164            $rowspancells[$y][$x]+=1;
165            $z+=1;
166          }
167        }
168      }
169
170      // if the cell itself if :::, then its rowspan value is 0
171      for ($y=1;$y<count($cells)+1;$y=$y+1)
172        for ($x=0;$x<count($cells[$y]);$x=$x+1)
173          if (preg_match("/ *::: */",$cells[$y][$x])) $rowspancells[$y][$x]=0;
174
175      // display them
176      for ($y=1;$y<count($cells)+1;$y=$y+1)
177      {
178        for ($x=0;$x<count($cells[$y]);$x=$x+1)
179          echo $rowspancells[$y][$x];
180        echo "\n";
181      }
182
183      // begin display the mediawiki table
184      $tablesource="{| class=\"wikitable sortable\" border=1\n";
185      if ($headers!="")
186      {
187        $tablesource.="!";
188        for ($n=0;$n<count($headers);$n=$n+1)
189        {
190          $tablesource.=$headers[$n];
191          if ($n<count($headers)-1) $tablesource.="!!";
192        }
193        $tablesource.="\n|-\n";
194      }
195      for ($y=1;$y<count($cells)+1;$y=$y+1)
196      {
197        $tablesource.="| ";
198        for ($x=0;$x<count($cells[$y]);$x=$x+1)
199        {
200          if ($rowspancells[$y][$x]>=1)
201	  {
202            if ($rowspancells[$y][$x]>1) $tablesource.="rowspan=".$rowspancells[$y][$x]."|";
203	    $tablesource.=$cells[$y][$x];
204	    if ($x<count($cells[$y])-1) $tablesource.=" || ";
205	  }
206        }
207        $tablesource.="\n|-\n";
208      }
209      $tablesource.="|}\n";
210      echo $tablesource;
211      $output.=$tablesource;
212      //end display mediawiki table
213
214      $headers="";
215      unset($cells);
216      $row=0;
217    } // endif have we left a table
218
219    // replace tables
220    if (preg_match("/^\^/",$line))
221    {
222      $in_table=true;
223      $row=0; // the header row is row 0. It may exist or not.
224      $line=preg_replace("/^\^/","",$line);
225      $line=preg_replace("/\n/","",$line);
226      $line=preg_replace("/\^$/","",$line);
227      $headers=explode("^",$line);
228    }
229
230    if ($in_table) $line=""; // if this is in a table then the table's content will be stored in $headers and $cells
231    // end care for tables
232
233    $output.=$line;
234  } //while (++$i<$linecount)
235  // is the end of file also an end of table?
236  $outputfile=fopen($filename.".mod","w");
237  fwrite($outputfile,$output);
238  fclose ($outputfile);
239}
240?>
241