home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / wiki / tools / tavidoc.php
PHP Script  |  2004-03-08  |  7KB  |  257 lines

  1. <?php
  2. // $Id: tavidoc.php,v 1.1 2004/01/12 22:14:08 comsubvie Exp $
  3.  
  4. ?><html>
  5.  <head>
  6.   <title>TaviDoc</title>    
  7.  </head>
  8.  <body><?php
  9.  
  10. //////////////////////////////////////////////////////////////////////////
  11. //
  12. //    S t a t i c
  13. //
  14.  
  15. // Required for database connectivity
  16. require_once('config.php');
  17.  
  18. // Required for rendering
  19. require_once('lib/defaults.php');
  20. require_once('parse/main.php');
  21. require_once('parse/html.php');
  22. require_once('parse/transforms.php');
  23.  
  24. // TaviDoc defaults
  25. static    $TDOutputPath        = 'tavidoc/';
  26. static    $TDTemplateFile        = 'tavidoc.template.html';
  27. $TDPageQueue            = array();
  28.  
  29. // Tavi defaults
  30. static     $CustomParseEngine     = array('parse_elem_flag',
  31.                     'parse_raw_html',
  32.                     'custom_parse_hyperlink_description',
  33.                     'parse_hyperlink',
  34.                     'custom_parse_freelink',
  35.                     'parse_bold',
  36.                     'parse_italic',
  37.                     'parse_teletype',
  38.                     'parse_heading',
  39.                     'parse_table',
  40.                     'parse_horiz',
  41.                     'parse_indents',
  42.                     'parse_newline',
  43.                     'parse_elements');
  44. $DisplayEngine['hr']        = 'custom_display_html_hr';
  45. $DisplayEngine['ref']        = 'custom_display_html_ref';
  46. $FlgChr                        = chr(255);
  47.  
  48. //////////////////////////////////////////////////////////////////////////
  49. //
  50. //    M a i n l i n e
  51. //
  52.  
  53. // Report
  54. echo '<h1>TaviDoc</h1>';
  55.  
  56. // Check if the user has properly set a configuration page name
  57. parse_str($QUERY_STRING);
  58. if (!isset($page))
  59.   die('No page specified');
  60.  
  61. // Load the template
  62. $TDTemplate = join('', file($TDTemplateFile));
  63.  
  64. // Open the database
  65. mysql_connect($DBServer, $DBUser, $DBPasswd);
  66. mysql_select_db($DBName);
  67.  
  68. // Report
  69. echo 'Processing configuration page <a href="',
  70.   "index.php?page=$page", '">', $page, "</a>.<br />\n";
  71. echo 'Using template <a href="', $TDTemplateFile, '">',
  72.   $TDTemplateFile, "</a>.<br />\n";
  73. echo 'Output into path <a href="', $TDOutputPath, '">',
  74.   $TDOutputPath, "</a>.<br />\n",
  75.   '<ol>';
  76.  
  77. $TDPageQueue[] = $page;
  78. $i = 0;
  79. while ($i < count($TDPageQueue))
  80. {
  81.   $Title    = $TDPageQueue[$i];
  82.   $Messages    = array();
  83.   $PageCount    = count($TDPageQueue);
  84.  
  85.   // Render using Tavi
  86.   $Entity    = array();
  87.   $body        = parseText(query_page_body($Title),
  88.                 $CustomParseEngine, '');
  89.  
  90.   // Macros
  91.   $body        = str_replace(array('[TITLE]',
  92.                     '[BODY]',
  93.                     '[TIMESTAMP]',
  94.                     '[YEAR]'),
  95.                   array($Title,
  96.                     $body,
  97.                     date('w, F jS Y, G:i'),
  98.                     date('Y')),
  99.                   $TDTemplate);
  100.  
  101.   // Write file
  102.   $filename    = $TDOutputPath.filename($Title);
  103.   $fid         = fopen($filename, 'w');
  104.   fwrite($fid, $body);
  105.   fclose($fid);
  106.  
  107.   // Report
  108.   $dif = (count($TDPageQueue) - $PageCount);
  109.   echo '<li>',
  110.     'Writing page <a href="index.php?page=', $Title, '">', $Title, '</a>',
  111.     ' to file <a href="'.$filename.'">', $filename, '</a>',
  112.     ($dif > 0 ? ", $dif new pages found" : '');
  113.  
  114.   if (count($Messages) > 0)
  115.   {
  116.     echo '<ol>';
  117.     foreach ($Messages as $Message)
  118.       echo '<li>'.$Message.'</li>';
  119.     echo '</ol>';
  120.   }
  121.  
  122.   echo '</li>';
  123.   flush();
  124.   ++$i;
  125. }
  126.  
  127. // Close the database
  128. mysql_close();
  129.  
  130. echo "</ol>\n", "Succesfully completed.\n";
  131.  
  132. //////////////////////////////////////////////////////////////////////////
  133. //
  134. //    F u n c t i o n s
  135. //
  136.  
  137. //------------------------------------------------------------------------
  138. // Create the filename from a page title
  139. function filename($Title)
  140. {
  141.   return strtolower(urlencode(str_replace(' ', '_', $Title))).'.html';
  142. }
  143.  
  144. //------------------------------------------------------------------------
  145. // Die with a nice error
  146. function query_error($sql)
  147. {
  148.   die('<big>MySQL Error '.mysql_errno().': '.mysql_error()
  149.     .'</big><br /><code>'.nl2br($sql).'</code>');
  150. }
  151.  
  152. //------------------------------------------------------------------------
  153. // Extract the body from a page
  154. function query_page_body($Title)
  155. {
  156.   global $DBTablePrefix;
  157.  
  158.   $Title = mysql_escape_string($Title);
  159.  
  160.   $sql    = 'select body
  161.        from '.$DBTablePrefix."pages
  162.        where title = '$Title'
  163.        order by version desc";
  164.   $qid    = mysql_query($sql)    or query_error($sql);
  165.   $row    = mysql_fetch_row($qid)    or query_error($sql);
  166.   mysql_free_result($qid)    or query_error($sql);
  167.  
  168.   return $row[0];
  169. }
  170.  
  171. //------------------------------------------------------------------------
  172. // Query the Wiki whether a page exists with given title
  173. function query_page_exists($Title)
  174. {
  175.   global $DBTablePrefix;
  176.  
  177.   $Title = mysql_escape_string($Title);
  178.  
  179.   $sql    = 'select count(*)
  180.        from '.$DBTablePrefix."pages
  181.        where title = '$Title'";
  182.   $qid    = mysql_query($sql)    or query_error($sql);
  183.   $row    = mysql_fetch_row($qid) or query_error($sql);
  184.   mysql_free_result($qid)    or query_error($sql);
  185.  
  186.   return($row[0] > 0? TRUE : FALSE);
  187. }
  188.  
  189. //------------------------------------------------------------------------
  190. // Custom renderer for horizontal rulers
  191. function custom_display_html_hr()
  192. {
  193.   return "<hr noshade size=\"2\" />\n";
  194. }
  195.  
  196. //------------------------------------------------------------------------
  197. // Custom parser which strips of the braces from links
  198. function custom_parse_hyperlink_description($text)
  199. {
  200.   global $UrlPtn;
  201.  
  202.   return preg_replace("/\\[($UrlPtn) ([^]]+)]/e",
  203.     "url_token(q1('\\1'), q1('\\4'))", $text, -1);
  204. }
  205.  
  206. //------------------------------------------------------------------------
  207. // Custom display engine which nullifies non-existing pages
  208. function custom_display_html_ref($page, $appearance, $hover = '',
  209.                  $anchor = '', $anchor_appearance = '')
  210. {
  211.   global $SeparateLinkWords, $TDPageQueue, $Messages;
  212.  
  213.   if ($hover != '')
  214.     $hover = ' title="' . $hover . '"';
  215.  
  216.   if (query_page_exists($page)) {
  217.     $found = FALSE;
  218.     foreach($TDPageQueue as $t)
  219.       if ($t == $page)
  220.     $found = TRUE;
  221.  
  222.     if (!$found)
  223.       $TDPageQueue[] = $page;
  224.  
  225.     if ($SeparateLinkWords && $page == $appearance)
  226.       $appearance = html_split_name($page);
  227.  
  228.     return '<a href="'.viewURL($page).$anchor.'"'.$hover.'>'
  229.       .$appearance.$anchor_appearance.'</a>';
  230.   } else {
  231.     $Messages[] = 'Page <a href="index.php?action=edit&page='.$page.'">'
  232.       .$page.'</a> missing from Wiki.';
  233.  
  234.     return $appearance;
  235.   }
  236. }
  237.  
  238. //------------------------------------------------------------------------
  239. // Custom parser which strips of the braces from links
  240. function custom_parse_freelink($text)
  241. {
  242.   return preg_replace(
  243.     "/\\(\\(([-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+)()()\\)\\)/e",
  244.     "freelink_token(q1('\\1'), q1('\\2'), '\\3', '')",
  245.     $text, -1);
  246. }
  247.  
  248. //------------------------------------------------------------------------
  249. // Overwrite the normal viewURL code to return the filename
  250. function viewURL($page, $version = '', $full = '')
  251. {
  252.   return filename($page);
  253. }
  254.  
  255. ?> </body>
  256. </html>
  257.