Wikulator.php
author Dan
Wed, 24 Dec 2008 09:53:25 -0500
changeset 0 1783be241488
child 1 616d046e3bd9
permissions -rwxr-xr-x
First commit; half-done and a bit tempermental.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     1
<?php
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     2
/*
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     3
Plugin Name: Mediafier
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     4
Plugin URI: http://enanocms.org/Mediafier
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     5
Description: Several parser extensions that provide MediaWiki-like support for references, search highlighting, and a table of contents to Enano
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     6
Author: Dan Fuhry
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     7
Version: 0.1 beta 1
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     8
Author URI: http://enanocms.org/
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
     9
*/
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    10
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    11
/*
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    12
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    13
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    14
 *
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    15
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    16
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    17
 */
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    18
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    19
$plugins->attachHook('render_wikiformat_post', 'mediafy($result);');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    20
$plugins->attachHook('compile_template', 'mediafier_add_headers();');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    21
$plugins->attachHook('html_attribute_whitelist', '$whitelist["ref"] = array(); $whitelist["references"] = array("/");');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    22
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    23
function mediafy(&$text)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    24
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    25
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    26
  mediafy_highlight_search_words($text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    27
  mediafy_process_references($text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    28
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    29
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    30
function mediafier_add_headers()
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    31
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    32
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    33
  $template->add_header("<style type=\"text/css\">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    34
                        .highlight  { background-color: #FFFFC0; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    35
                        .refbak     { background-color: #E0E0FF; font-weight: bold; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    36
                        .references { font-size: smaller; }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    37
                      </style>");
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    38
  $ref_script = <<<EOF
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    39
      <enano:no-opt>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    40
      <script type="text/javascript">
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    41
      // <![CDATA[
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    42
        function refsOff()
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    43
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    44
          var divs = getElementsByClassName(document, '*', 'refbottom');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    45
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    46
          {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    47
            $(divs[i]).rmClass('refbak');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    48
          }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    49
          divs = getElementsByClassName(document, '*', 'reftop');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    50
          for ( var i in divs )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    51
          {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    52
            $(divs[i]).rmClass('refbak');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    53
          }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    54
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    55
        function refToBottom(id)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    56
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    57
          refsOff();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    58
          $('ref_'+id+'_b').addClass('refbak');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    59
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    60
        function refToTop(id)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    61
        {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    62
          refsOff();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    63
          $('cite_'+id).addClass('refbak');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    64
        }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    65
        // ]]>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    66
      </script>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    67
      </enano:no-opt>
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    68
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    69
EOF;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    70
  $template->add_header($ref_script);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    71
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    72
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    73
function mediafy_highlight_search_words(&$result)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    74
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    75
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    76
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    77
  if ( !isset($_SERVER['HTTP_REFERER']) && !isset($_GET['highlight']) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    78
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    79
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    80
  $referer = ( isset($_GET['highlight']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['HTTP_REFERER'];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    81
  $term = ( isset($_GET['highlight']) ) ? 'highlight' : 'q';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    82
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    83
  preg_match('/(\?|&)'.$term.'=(.+?)(&|$)/', $referer, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    84
  if ( !isset($match[2]) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    85
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    86
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    87
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    88
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    89
  $words = $match[2];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    90
  $words = urldecode($words);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    91
  if ( $term == 'q' )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    92
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    93
    // it's from a search query - extract terms
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    94
    require_once(ENANO_ROOT . '/includes/search.php');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    95
    $words = parse_search_query($words, $warnings);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    96
    $words = array_merge($words['any'], $words['req']);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    97
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    98
  else
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
    99
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   100
    $words = explode(' ', $words);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   101
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   102
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   103
  // strip HTML out of the rendered text
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   104
  $rand_seed = $session->dss_rand();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   105
  preg_match_all('/<.+?>/', $result, $html_matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   106
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   107
  foreach ( $html_matches[0] as $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   108
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   109
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   110
    $result = str_replace($match, "{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   111
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   112
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   113
  // highlight matches
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   114
  foreach ( $words as $word )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   115
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   116
    $result = preg_replace('/([\W]|^)(' . preg_quote($word) . ')([\W])/i', "\\1<span class=\"highlight\">\\2</span>\\3", $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   117
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   118
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   119
  // restore HTML
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   120
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   121
  foreach ( $html_matches[0] as $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   122
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   123
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   124
    $result = str_replace("{HIGHLIGHT_PLACEHOLDER:$i:$rand_seed}", $match, $result);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   125
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   126
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   127
  // add "remove highlighting" link
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   128
  $result = '<div style="float: right; text-align: right;"><a href="' . makeUrl($paths->page) . '" onclick="ajaxReset(); return false;">Turn off <span class="highlight">highlighting</span></a></div>' .
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   129
            $result;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   130
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   131
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   132
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   133
function mediafy_process_references(&$text)
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   134
{
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   135
  global $db, $session, $paths, $template, $plugins; // Common objects
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   136
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   137
  // Is there a <references /> in the wikitext? If not, just return out to avoid empty processing
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   138
  if ( !preg_match('#<references(([ ]*)/)?>#', $text) )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   139
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   140
    //die('no match');
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   141
    return false;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   142
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   143
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   144
  // Retrieve references
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   145
  preg_match_all('#<ref>(.+?)</ref>#s', $text, $matches);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   146
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   147
  // Init counter
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   148
  $i = 0;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   149
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   150
  // Init refs array
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   151
  $refs = array();
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   152
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   153
  // main parser loop
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   154
  foreach ( $matches[0] as $j => $match )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   155
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   156
    $i++;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   157
    $inner =& $matches[1][$j];
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   158
    $refs[$i] = $inner;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   159
    $reflink = '<sup><a class="reftop" id="cite_' . $i . '" name="cite_' . $i . '" href="#ref_' . $i . '" onclick="refToBottom(\'' . $i . '\');">[' . $i . ']</a></sup>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   160
    $text = str_replace($match, $reflink, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   161
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   162
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   163
  // compile refs div
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   164
  $refsdiv = '<div class="references">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   165
  $refsdiv .= '<table border="0" width="100%"><tr><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   166
  $count = ( count($refs) >= 20 ) ? floor(count($refs) / 2) : 99;
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   167
  foreach ( $refs as $i => $ref )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   168
  {
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   169
    $reflink = '<span id="ref_' . $i . '" name="ref_' . $i . '"><sup><b><a onclick="refToTop(\'' . $i . '\');" href="#cite_' . $i . '">^</a></b></sup> </span>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   170
    $refsdiv .= "<div class=\"refbottom\" id=\"ref_{$i}_b\">$reflink $i. $ref</div>";
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   171
    if ( $i == $count )
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   172
      $refsdiv .= '</td><td valign="top">';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   173
  }
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   174
  $refsdiv .= '</td></tr></table>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   175
  $refsdiv .= '</div>';
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   176
  
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   177
  preg_match('#<references(([ ]*)/)?>#', $text, $match);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   178
  $text = str_replace_once($match[0], $refsdiv, $text);
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   179
}
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   180
1783be241488 First commit; half-done and a bit tempermental.
Dan
parents:
diff changeset
   181
?>