includes/render.php
changeset 1098 be6cfe79128c
parent 1081 745200a9cc2a
child 1108 c1be67a50d81
equal deleted inserted replaced
1097:4252e4d83102 1098:be6cfe79128c
   615    */
   615    */
   616   
   616   
   617   public static function parse_internal_links($text, $tplcode = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
   617   public static function parse_internal_links($text, $tplcode = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
   618   {
   618   {
   619     global $db, $session, $paths, $template, $plugins; // Common objects
   619     global $db, $session, $paths, $template, $plugins; // Common objects
   620     
   620   
   621     if ( is_string($tplcode) )
   621     $parser = is_string($tplcode) ? $template->makeParserText($tplcode) : false;
   622     {
       
   623       $parser = $template->makeParserText($tplcode);
       
   624     }
       
   625     
   622     
   626     // stage 1 - links with alternate text
   623     // stage 1 - links with alternate text
   627     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   624     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   628     foreach ( $matches[0] as $i => $match )
   625     foreach ( $matches[0] as $i => $match )
   629     {
   626     {
   630       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   627       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   631       if ( ($pos = strrpos($page_id, '#')) !== false )
   628       $link = self::generate_internal_link($namespace, $page_id, $matches[2][$i], $match, $parser, $do_exist_check, $match_page_id, $match_namespace);
   632       {
       
   633         $hash = substr($page_id, $pos);
       
   634         $page_id = substr($page_id, 0, $pos);
       
   635       }
       
   636       else
       
   637       {
       
   638         $hash = '';
       
   639       }
       
   640       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
       
   641       
       
   642       $url = makeUrl($pid_clean, false, true) . $hash;
       
   643       $inner_text = $matches[2][$i];
       
   644       $quot = '"';
       
   645       $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
       
   646       
       
   647       if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
       
   648         $exists .= ' class="currentpage"';
       
   649       
       
   650       if ( $tplcode )
       
   651       {
       
   652         $parser->assign_vars(array(
       
   653             'HREF' => $url,
       
   654             'FLAGS' => $exists,
       
   655             'TEXT' => $inner_text
       
   656           ));
       
   657         $link = $parser->run();
       
   658       }
       
   659       else
       
   660       {
       
   661         $omatch = self::escape_parser_hint_attrib($match);
       
   662         $link = "<!--#internallink src=\"$omatch\" --><a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
       
   663       }
       
   664       
       
   665       $text = str_replace($match, $link, $text);
   629       $text = str_replace($match, $link, $text);
   666     }
   630     }
   667     
   631     
   668     // stage 2 - links with no alternate text
   632     // stage 2 - links with no alternate text
   669     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\]\]/', $text, $matches);
   633     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\]\]/', $text, $matches);
   670     foreach ( $matches[0] as $i => $match )
   634     foreach ( $matches[0] as $i => $match )
   671     {
   635     {
   672       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   636       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   673       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   637       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   674       
       
   675       $url = makeUrl($pid_clean, false, true);
       
   676       $inner_text = ( isPage($pid_clean) ) ? htmlspecialchars(get_page_title($pid_clean)) : htmlspecialchars($matches[1][$i]);
   638       $inner_text = ( isPage($pid_clean) ) ? htmlspecialchars(get_page_title($pid_clean)) : htmlspecialchars($matches[1][$i]);
   677       $quot = '"';
   639       
   678       $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
   640       $link = self::generate_internal_link($namespace, $page_id, $inner_text, $match, $parser, $do_exist_check, $match_page_id, $match_namespace);
   679       
       
   680       if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
       
   681         $exists .= ' class="currentpage"';
       
   682       
       
   683       if ( $tplcode )
       
   684       {
       
   685         $parser->assign_vars(array(
       
   686             'HREF' => $url,
       
   687             'FLAGS' => $exists,
       
   688             'TEXT' => $inner_text
       
   689           ));
       
   690         $link = $parser->run();
       
   691       }
       
   692       else
       
   693       {
       
   694         $omatch = self::escape_parser_hint_attrib($match);
       
   695         $link = "<!--#internallink src=\"$omatch\" --><a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
       
   696       }
       
   697       
   641       
   698       $text = str_replace($match, $link, $text);
   642       $text = str_replace($match, $link, $text);
   699     }
   643     }
   700     
   644     
   701     return $text;
   645     return $text;
       
   646   }
       
   647   
       
   648   /**
       
   649    * Internal link generation function
       
   650    * @access private
       
   651    * @return string HTML
       
   652    */
       
   653   
       
   654   private static function generate_internal_link($namespace, $page_id, $inner_text, $match, $parser = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
       
   655   {
       
   656     global $db, $session, $paths, $template, $plugins; // Common objects
       
   657     
       
   658     if ( ($pos = strrpos($page_id, '#')) !== false )
       
   659     {
       
   660       $hash = substr($page_id, $pos);
       
   661       $page_id = substr($page_id, 0, $pos);
       
   662     }
       
   663     else
       
   664     {
       
   665       $hash = '';
       
   666     }
       
   667     
       
   668     if ( $namespace == 'Admin' )
       
   669     {
       
   670       // No linking directly to Admin pages!
       
   671       $get = 'module=' . $paths->nslist[$namespace] . sanitize_page_id($page_id);
       
   672       $pid_clean = $paths->nslist['Special'] . 'Administration';
       
   673       $onclick = ' onclick="ajaxLoginNavTo(\'Special\', \'Administration\', USER_LEVEL_ADMIN, \'' . addslashes($get) . '\'); return false;"';
       
   674     }
       
   675     else
       
   676     {
       
   677       $get = false;
       
   678       $onclick = '';
       
   679       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
       
   680     }
       
   681     
       
   682     $url = makeUrl($pid_clean, $get, true) . $hash;
       
   683     $quot = '"';
       
   684     $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
       
   685     
       
   686     if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
       
   687       $exists .= ' class="currentpage"';
       
   688     
       
   689     if ( $parser )
       
   690     {
       
   691       $parser->assign_vars(array(
       
   692           'HREF' => $url,
       
   693           'FLAGS' => $exists,
       
   694           'TEXT' => $inner_text
       
   695         ));
       
   696       $link = $parser->run();
       
   697     }
       
   698     else
       
   699     {
       
   700       $omatch = self::escape_parser_hint_attrib($match);
       
   701       $link = "<!--#internallink src=\"$omatch\" --><a{$onclick} href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
       
   702     }
       
   703     
       
   704     return $link;
   702   }
   705   }
   703   
   706   
   704   /**
   707   /**
   705    * Parses a partial template tag in wikitext, and return an array with the parameters.
   708    * Parses a partial template tag in wikitext, and return an array with the parameters.
   706    * @param string The portion of the template tag that contains the parameters.
   709    * @param string The portion of the template tag that contains the parameters.