b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/_wikilinks.plugin.php - 350 lines - 11808 bytes - Summary - Text - Print

Description: This file implements the Wiki links plugin for b2evolution Creates wiki links

   1  <?php
   2  /**
   3   * This file implements the Wiki links plugin for b2evolution
   4   *
   5   * Creates wiki links
   6   *
   7   * b2evolution - {@link http://b2evolution.net/}
   8   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
   9   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
  10   *
  11   * @package plugins
  12   * @ignore
  13   */
  14  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  15  
  16  
  17  /**
  18   * @package plugins
  19   */
  20  class wikilinks_plugin extends Plugin
  21  {
  22      var $code = 'b2evWiLi';
  23      var $name = 'Wiki Links';
  24      var $priority = 35;
  25      var $version = '5.0.0';
  26      var $group = 'rendering';
  27      var $short_desc;
  28      var $long_desc;
  29      var $help_url = 'http://b2evolution.net/man/technical-reference/renderer-plugins/wiki-links-plugin';
  30      var $number_of_installs = 1;
  31  
  32      /**
  33       * Init
  34       */
  35  	function PluginInit( & $params )
  36      {
  37          $this->short_desc = T_('Wiki Links converter');
  38          $this->long_desc = T_('You can create links with [[CamelCase]] or ((CamelCase)) which will try to link to the category or the post with the slug "camel-case". See manual for more.');
  39      }
  40  
  41  
  42      /**
  43       * Define here default collection/blog settings that are to be made available in the backoffice.
  44       *
  45       * @param array Associative array of parameters.
  46       * @return array See {@link Plugin::get_coll_setting_definitions()}.
  47       */
  48  	function get_coll_setting_definitions( & $params )
  49      {
  50          $default_values = array(
  51                  'default_post_rendering' => 'opt-in'
  52              );
  53  
  54          if( !empty( $params['blog_type'] ) && $params['blog_type'] != 'forum' )
  55          {    // Set the default settings depends on blog type
  56              $default_values['default_comment_rendering'] = 'never';
  57          }
  58  
  59          $default_params = array_merge( $params, $default_values );
  60          return array_merge( parent::get_coll_setting_definitions( $default_params ),
  61              array(
  62                  'link_without_brackets' => array(
  63                      'label' => $this->T_('Links without brackets'),
  64                      'type' => 'checkbox',
  65                      'defaultvalue' => 0,
  66                      'note' => $this->T_('Enable this to create the links from words like WikiWord without brackets [[]]'),
  67                  )
  68              ) );
  69      }
  70  
  71  
  72      /**
  73       * Perform rendering
  74       *
  75       * @param array Associative array of parameters
  76       *   'data': the data (by reference). You probably want to modify this.
  77       *   'format': see {@link format_to_output()}. Only 'htmlbody' and 'entityencoded' will arrive here.
  78       * @return boolean true if we can render something for the required output format
  79       */
  80  	function RenderItemAsHtml( & $params )
  81      {
  82          $content = & $params['data'];
  83  
  84          if( !empty( $params['Item'] ) )
  85          { // Get Item from params
  86              $Item = & $params['Item'];
  87          }
  88          elseif( !empty( $params['Comment'] ) )
  89          { // Get Item from Comment
  90              $Comment = & $params['Comment'];
  91              $Item = & $Comment->get_Item();
  92          }
  93          else
  94          { // Item and Comment are not defined, Exit here
  95              return;
  96          }
  97          $item_Blog = & $Item->get_Blog();
  98  
  99          return $this->render_content( $content, $item_Blog );
 100      }
 101  
 102  
 103      /**
 104       * Render content of Item, Comment, Message
 105       *
 106       * @todo get rid of global $blog
 107       * 
 108       * @param string Content
 109       * @param object Blog
 110       * @param boolean Allow empty Blog
 111       * return boolean
 112       */
 113  	function render_content( & $content, $item_Blog = NULL, $allow_null_blog = false )
 114      {
 115          global $ItemCache, $admin_url, $blog, $evo_charset;
 116  
 117          $regexp_modifier = '';
 118          if( $evo_charset == 'utf-8' )
 119          { // Add this modifier to work with UTF-8 strings correctly
 120              $regexp_modifier = 'u';
 121          }
 122  
 123          // Regular links:
 124          $search = array(
 125              // [[http://url]] :
 126              '#\[\[((https?|mailto)://((?:[^<>{}\s\]]|,(?!\s))+?))\]\]#i',
 127              // [[http://url text]] :
 128              '#\[\[((https?|mailto)://([^<>{}\s\]]+)) ([^\n\r]+?)\]\]#i',
 129              // ((http://url)) :
 130              '#\(\(((https?|mailto)://((?:[^<>{}\s\]]|,(?!\s))+?))\)\)#i',
 131              // ((http://url text)) :
 132              '#\(\(((https?|mailto)://([^<>{}\s\]]+)) ([^\n\r]+?)\)\)#i',
 133          );
 134          $replace = array(
 135              '<a href="$1">$1</a>',
 136              '<a href="$1">$4</a>',
 137              '<a href="$1">$1</a>',
 138              '<a href="$1">$4</a>'
 139          );
 140  
 141          $content = replace_content_outcode( $search, $replace, $content );
 142  
 143  /* QUESTION: fplanque, implementation of this planned? then use make_clickable() - or remove this comment
 144      $ret = preg_replace("#([\n ])aim:([^,< \n\r]+)#i", "\\1<a href=\"aim:goim?screenname=\\2\\3&message=Hello\">\\2\\3</a>", $ret);
 145  
 146      $ret = preg_replace("#([\n ])icq:([^,< \n\r]+)#i", "\\1<a href=\"http://wwp.icq.com/scripts/search.dll?to=\\2\\3\">\\2\\3</a>", $ret);
 147  
 148      $ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^,< \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\">www.\\2.\\3\\4</a>", $ret);
 149  
 150      $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^,< \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret); */
 151  
 152          // To use function replace_special_chars()
 153          load_funcs('locales/_charset.funcs.php');
 154  
 155          // WIKIWORDS:
 156  
 157          $search_wikiwords = array();
 158          $replace_links = array();
 159  
 160          if( $this->get_coll_setting( 'link_without_brackets', $item_Blog, $allow_null_blog ) )
 161          {    // Create the links from standalone WikiWords
 162  
 163              // STANDALONE WIKIWORDS:
 164              $search = '/
 165                      (?<= \s | ^ )                                                    # Lookbehind for whitespace
 166                      ([\p{Lu}]+[\p{Ll}0-9_]+([\p{Lu}]+[\p{L}0-9_]+)+)    # WikiWord or WikiWordLong
 167                      (?= [\.,:;!\?] \s | \s | $ )                                            # Lookahead for whitespace or punctuation
 168                  /x'.$regexp_modifier;    // x = extended (spaces + comments allowed)
 169  
 170              if( preg_match_all( $search, $content, $matches, PREG_SET_ORDER) )
 171              {
 172                  // Construct array of wikiwords to look up in post urltitles
 173                  $wikiwords = array();
 174                  foreach( $matches as $match )
 175                  {
 176                      // Convert the WikiWord to an urltitle
 177                      $WikiWord = $match[0];
 178                      $Wiki_Word = preg_replace( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '$1-$2', $WikiWord );
 179                      $wiki_word = evo_strtolower( $Wiki_Word );
 180                      // echo '<br />Match: [', $WikiWord, '] -> [', $wiki_word, ']';
 181                      $wiki_word = replace_special_chars( $wiki_word );
 182                      $wikiwords[ $WikiWord ] = $wiki_word;
 183                  }
 184  
 185                  // Lookup all urltitles at once in DB and preload cache:
 186                  $ItemCache = & get_ItemCache();
 187                  $ItemCache->load_urltitle_array( $wikiwords );
 188  
 189                  // Construct arrays for replacing wikiwords by links:
 190                  foreach( $wikiwords as $WikiWord => $wiki_word )
 191                  {
 192                      // WikiWord
 193                      $search_wikiwords[] = '/
 194                          (?<= \s | ^ )                         # Lookbehind for whitespace or start
 195                          (?<! <span\ class="NonExistentWikiWord"> )
 196                          '.$WikiWord.'                            # Specific WikiWord to replace
 197                          (?= [\.,:;!\?] \s | \s | $ )                            # Lookahead for whitespace or end of string
 198                          /sx';    // s = dot matches newlines, x = extended (spaces + comments allowed)
 199  
 200  
 201                      // Find matching Item:
 202                      if( ($Item = & $ItemCache->get_by_urltitle( $wiki_word, false )) !== false )
 203                      { // Item Found
 204                          $permalink = $Item->get_permanent_url();
 205  
 206                          // WikiWord
 207                          $replace_links[] = '<a href="'.$permalink.'">'.$Item->get( 'title' ).'</a>';
 208  
 209                      }
 210                      else
 211                      { // Item not found
 212  
 213                          $create_link = isset($blog) ? ('<a href="'.$admin_url.'?ctrl=items&amp;action=new&amp;blog='.$blog.'&amp;post_title='.preg_replace( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '$1%20$2', $WikiWord ).'&amp;post_urltitle='.$wiki_word.'" title="Create...">?</a>') : '';
 214  
 215                          // WikiWord
 216                          $replace_links[] = '<span class="NonExistentWikiWord">'.$WikiWord.$create_link.'</span>';
 217  
 218                      }
 219                  }
 220              }
 221          }
 222  
 223          // BRACKETED WIKIWORDS:
 224          $search = '/
 225                  (?<= \(\( | \[\[ )                                        # Lookbehind for (( or [[
 226                  ([\p{L}0-9]+[\p{L}0-9_\-]*)                                    # Anything from Wikiword to WikiWordLong
 227                  (?= ( \s .*? )? ( \)\) | \]\] ) )            # Lookahead for )) or ]]
 228              /x'.$regexp_modifier;    // x = extended (spaces + comments allowed)
 229  
 230          if( preg_match_all( $search, $content, $matches, PREG_SET_ORDER) )
 231          {
 232              // Construct array of wikiwords to look up in post urltitles
 233              $wikiwords = array();
 234              foreach( $matches as $match )
 235              {
 236                  // Convert the WikiWord to an urltitle
 237                  $WikiWord = $match[0];
 238                  if( preg_match( '/^[\p{Ll}0-9_\-]+$/'.$regexp_modifier, $WikiWord ) )
 239                  {    // This WikiWord already matches a slug format
 240                      $Wiki_Word = $WikiWord;
 241                      $wiki_word = $Wiki_Word;
 242                  }
 243                  else
 244                  {    // Convert WikiWord to slug format
 245                      $Wiki_Word = preg_replace( array( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '*([^0-9])([0-9])*'.$regexp_modifier ), '$1-$2', $WikiWord );
 246                      $wiki_word = evo_strtolower( $Wiki_Word );
 247                  }
 248                  // echo '<br />Match: [', $WikiWord, '] -> [', $wiki_word, ']';
 249                  $wiki_word = replace_special_chars( $wiki_word );
 250                  $wikiwords[ $WikiWord ] = $wiki_word;
 251              }
 252  
 253              // Lookup all urltitles at once in DB and preload cache:
 254              $ChapterCache = & get_ChapterCache();
 255              $ChapterCache->load_urlname_array( $wikiwords );
 256              $ItemCache = & get_ItemCache();
 257              $ItemCache->load_urltitle_array( $wikiwords );
 258  
 259              // Construct arrays for replacing wikiwords by links:
 260              foreach( $wikiwords as $WikiWord => $wiki_word )
 261              {
 262                  // [[WikiWord text]]
 263                  $search_wikiwords[] = '*
 264                      \[\[
 265                      '.$WikiWord.'                            # Specific WikiWord to replace
 266                      \s (.+?)
 267                      \]\]
 268                      *sx';    // s = dot matches newlines, x = extended (spaces + comments allowed)
 269  
 270                  // ((WikiWord text))
 271                  $search_wikiwords[] = '*
 272                      \(\(
 273                      '.$WikiWord.'                            # Specific WikiWord to replace
 274                      \s (.+?)
 275                      \)\)
 276                      *sx';    // s = dot matches newlines, x = extended (spaces + comments allowed)
 277  
 278                  // [[Wikiword]]
 279                  $search_wikiwords[] = '*
 280                      \[\[
 281                      '.$WikiWord.'                            # Specific WikiWord to replace
 282                      \]\]
 283                      *sx';    // s = dot matches newlines, x = extended (spaces + comments allowed)
 284  
 285                  // ((Wikiword))
 286                  $search_wikiwords[] = '*
 287                      \(\(
 288                      '.$WikiWord.'                            # Specific WikiWord to replace
 289                      \)\)
 290                      *sx';    // s = dot matches newlines, x = extended (spaces + comments allowed)
 291  
 292  
 293                  // Find matching Chapter or Item:
 294                  $permalink = '';
 295                  $link_text = preg_replace( array( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '*([^0-9])([0-9])*'.$regexp_modifier ), '$1 $2', $WikiWord );
 296                  $link_text = ucwords( str_replace( '-', ' ', $link_text ) );
 297                  if( ($Chapter = & $ChapterCache->get_by_urlname( $wiki_word, false )) !== false )
 298                  { // Chapter is found
 299                      $permalink = $Chapter->get_permanent_url();
 300                      $existing_link_text = $Chapter->get( 'name' );
 301                  }
 302                  elseif( ($Item = & $ItemCache->get_by_urltitle( $wiki_word, false )) !== false )
 303                  { // Item is found
 304                      $permalink = $Item->get_permanent_url();
 305                      $existing_link_text = $Item->get( 'title' );
 306                  }
 307  
 308                  if( !empty( $permalink ) )
 309                  {    // Chapter or Item are found
 310                      // [[WikiWord text]]
 311                      $replace_links[] = '<a href="'.$permalink.'">$1</a>';
 312  
 313                      // ((WikiWord text))
 314                      $replace_links[] = '<a href="'.$permalink.'">$1</a>';
 315  
 316                      // [[Wikiword]]
 317                      $replace_links[] = '<a href="'.$permalink.'">'.$existing_link_text.'</a>';
 318  
 319                      // ((Wikiword))
 320                      $replace_links[] = '<a href="'.$permalink.'">'.$link_text.'</a>';
 321                  }
 322                  else
 323                  {    // Chapter and Item are not found
 324                      $create_link = isset($blog) ? ('<a href="'.$admin_url.'?ctrl=items&amp;action=new&amp;blog='.$blog.'&amp;post_title='.preg_replace( '*([^\p{Lu}_])([\p{Lu}])*'.$regexp_modifier, '$1%20$2', $WikiWord ).'&amp;post_urltitle='.$wiki_word.'" title="Create...">?</a>') : '';
 325  
 326                      // [[WikiWord text]]
 327                      $replace_links[] = '<span class="NonExistentWikiWord">$1'.$create_link.'</span>';
 328  
 329                      // ((WikiWord text))
 330                      $replace_links[] = '<span class="NonExistentWikiWord">$1'.$create_link.'</span>';
 331  
 332                      // [[Wikiword]]
 333                      $replace_links[] = '<span class="NonExistentWikiWord">'.$link_text.$create_link.'</span>';
 334  
 335                      // ((Wikiword))
 336                      $replace_links[] = '<span class="NonExistentWikiWord">'.$link_text.$create_link.'</span>';
 337                  }
 338              }
 339          }
 340  
 341          // echo '<br />---';
 342  
 343          // pre_dump( $search_wikiwords );
 344  
 345          $content = replace_content_outcode( $search_wikiwords, $replace_links, $content );
 346  
 347          return true;
 348      }
 349  }
 350  ?>

title

Description

title

Description

title

Description

title

title

Body