<?php
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
/**
 * Wikilink rule end renderer for Xhtml
 *
 * PHP versions 4 and 5
 *
 * @category   Text
 * @package    Text_Wiki
 * @author     Paul M. Jones <pmjones@php.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    CVS: $Id: Wikilink.php,v 1.22 2006/12/08 21:25:24 justinpatrin Exp $
 * @link       http://pear.php.net/package/Text_Wiki
 */

/**
 * This class renders wiki links in XHTML.
 *
 * @category   Text
 * @package    Text_Wiki
 * @author     Paul M. Jones <pmjones@php.net>
 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 * @version    Release: @package_version@
 * @link       http://pear.php.net/package/Text_Wiki
 */
class Text_Wiki_Render_Xhtml_Wikilink extends Text_Wiki_Render {

    var $conf = array(
        'pages' => array(), // set to null or false to turn off page checks
        'view_url' => '%s',
        'new_url'  => '%s',
        'new_text' => '',
        'new_text_pos' => 'after', // 'before', 'after', or null/false
        'css' => 'wiki_exists',
        'css_new' => 'wiki_newpage',
        'exists_callback' => null // call_user_func() callback
    );
    
    function pageExists($page) {
        if (ereg(":",$page)) {
            $nazov = substr($page,strrpos($page,":")+1);
            $ns = substr($page,0,strrpos($page,":"));
            $sql = "SELECT id_namespace FROM namespace
                WHERE namespace = '".$ns."'";
            $nsid = $GLOBALS['wiki']['oDb']->getKey("id_namespace",$sql, 0);
        } else {
            $nazov = $page;
            $nsid = 0;
        }
        $sql = "SELECT id_wiki FROM wiki
            WHERE nazov = '".$nazov."'
            AND id_namespace = '".$nsid."'";
        $q = $GLOBALS['wiki']['oDb']->query($sql,FALSE);
        if (!$q)
	        return FALSE;
        return $GLOBALS['wiki']['oDb']->numrows($q) > 0 ? TRUE : FALSE;
        
    }

    /**
    *
    * Renders a token into XHTML.
    *
    * @access public
    *
    * @param array $options The "options" portion of the token (second
    * element).
    *
    * @return string The text rendered from the token options.
    *
    */

    function token($options)
    {
        // make nice variable names (page, anchor, text)
        extract($options);

        // File
        $url = $GLOBALS['wiki']['oUrl'];
        if (eregi("File:",$page)) {
            $page = substr($page, 5);
            return '<a href="{PAGEFILE}{'.$page.'}" class="w_file">' . $page . '</a>';
        }

        // Exist?
        $exists = $this->pageExists($page);
        if ($exists) {

            // PAGE EXISTS.

            // link to the page view, but we have to build
            // the HREF.  we support both the old form where
            // the page always comes at the end, and the new
            // form that uses %s for sprintf()
            $href = "{PAGEWIKI}{" . $this->getConf('view_url') . "}";

            if (strpos($href, '%s') === false) {
                // use the old form (page-at-end)
                // $href = $href . $this->urlEncode($page) . $anchor;
                $href = $href . $page . $anchor;
            } else {
                // use the new form (sprintf format string)
                // $href = sprintf($href, $this->urlEncode($page)) . $anchor;
                $href = sprintf($href, $page) . $anchor;
            }

            // get the CSS class and generate output
            $css = ' class="'.$this->textEncode($this->getConf('css')).'"';

            $start = '<a'.$css.' href="'.$this->textEncode($href).'">';
            $end = '</a>';
        } else {

            // PAGE DOES NOT EXIST.

            // link to a create-page url, but only if new_url is set
            $href = "{PAGEWIKI}{" . $this->getConf('new_url',null) . "}";

            // set the proper HREF
            if (! $href || trim($href) == '') {

                // no useful href, return the text as it is
                //TODO: This is no longer used, need to look closer into this branch
                $output = $text;

            } else {

                // yes, link to the new-page href, but we have to build
                // it.  we support both the old form where
                // the page always comes at the end, and the new
                // form that uses sprintf()
                if (strpos($href, '%s') === false) {
                    // use the old form
                    // $href = $href . $this->urlEncode($page);
                    $href = $href . $page;
                } else {
                    // use the new form
                    // $href = sprintf($href, $this->urlEncode($page));
                    $href = sprintf($href, $page);
                }
            }

            // get the appropriate CSS class and new-link text
            $css = ' class="'.$this->textEncode($this->getConf('css_new')).'"';
            $new = $this->getConf('new_text');

            // what kind of linking are we doing?
            $pos = $this->getConf('new_text_pos');
            // $href = $this->textEncode($href);
            if (! $pos || ! $new) {
                // no position (or no new_text), use css only on the page name

                $start = '<a'.$css.' href="'.$href.'">';
                $end = '</a>';
            } elseif ($pos == 'before') {
                // use the new_text BEFORE the page name
                $start = '<a'.$css.' href="'.$href.'">'.$this->textEncode($new).'</a>';
                $end = '';
            } else {
                // default, use the new_text link AFTER the page name
                $start = '';
                $end = '<a'.$css.' href="'.$href.'">'.$this->textEncode($new).'</a>';
            }
        }
        if (!strlen($text)) {
            $start .= $this->textEncode($page);
        }
        if (isset($type)) {
            switch ($type) {
            case 'start':
                $output = $start;
                break;
            case 'end':
                $output = $end;
                break;
            }
        } else {
            $output = $start.$this->textEncode($text).$end;
        }
        return $output;
    }
}
?>
