1<?php
2// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
3/**
4 * Wikilink rule end renderer for Xhtml
5 *
6 * PHP versions 4 and 5
7 *
8 * @category   Text
9 * @package    Text_Wiki
10 * @author     Paul M. Jones <pmjones@php.net>
11 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
12 * @version    CVS: $Id: Wikilink.php 224670 2006-12-08 21:25:24Z justinpatrin $
13 * @link       http://pear.php.net/package/Text_Wiki
14 */
15
16/**
17 * This class renders wiki links in XHTML.
18 *
19 * @category   Text
20 * @package    Text_Wiki
21 * @author     Paul M. Jones <pmjones@php.net>
22 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
23 * @version    Release: @package_version@
24 * @link       http://pear.php.net/package/Text_Wiki
25 */
26class Text_Wiki_Render_Xhtml_Wikilink extends Text_Wiki_Render {
27
28    var $conf = array(
29        'pages' => array(), // set to null or false to turn off page checks
30        'view_url' => 'http://example.com/index.php?page=%s',
31        'new_url'  => 'http://example.com/new.php?page=%s',
32        'new_text' => '?',
33        'new_text_pos' => 'after', // 'before', 'after', or null/false
34        'css' => null,
35        'css_new' => null,
36        'exists_callback' => null // call_user_func() callback
37    );
38
39
40    /**
41    *
42    * Renders a token into XHTML.
43    *
44    * @access public
45    *
46    * @param array $options The "options" portion of the token (second
47    * element).
48    *
49    * @return string The text rendered from the token options.
50    *
51    */
52
53    function token($options)
54    {
55        // make nice variable names (page, anchor, text)
56        extract($options);
57
58        // is there a "page existence" callback?
59        // we need to access it directly instead of through
60        // getConf() because we'll need a reference (for
61        // object instance method callbacks).
62        if (isset($this->conf['exists_callback'])) {
63            $callback =& $this->conf['exists_callback'];
64        } else {
65        	$callback = false;
66        }
67
68        if ($callback) {
69            // use the callback function
70            $exists = call_user_func($callback, $page);
71        } else {
72            // no callback, go to the naive page array.
73            $list = $this->getConf('pages');
74            if (is_array($list)) {
75                // yes, check against the page list
76                $exists = in_array($page, $list);
77            } else {
78                // no, assume it exists
79                $exists = true;
80            }
81        }
82
83        $anchor = '#'.$this->urlEncode(substr($anchor, 1));
84
85        // does the page exist?
86        if ($exists) {
87
88            // PAGE EXISTS.
89
90            // link to the page view, but we have to build
91            // the HREF.  we support both the old form where
92            // the page always comes at the end, and the new
93            // form that uses %s for sprintf()
94            $href = $this->getConf('view_url');
95
96            if (strpos($href, '%s') === false) {
97                // use the old form (page-at-end)
98                $href = $href . $this->urlEncode($page) . $anchor;
99            } else {
100                // use the new form (sprintf format string)
101                $href = sprintf($href, $this->urlEncode($page)) . $anchor;
102            }
103
104            // get the CSS class and generate output
105            $css = ' class="'.$this->textEncode($this->getConf('css')).'"';
106
107            $start = '<a'.$css.' href="'.$this->textEncode($href).'">';
108            $end = '</a>';
109        } else {
110
111            // PAGE DOES NOT EXIST.
112
113            // link to a create-page url, but only if new_url is set
114            $href = $this->getConf('new_url', null);
115
116            // set the proper HREF
117            if (! $href || trim($href) == '') {
118
119                // no useful href, return the text as it is
120                //TODO: This is no longer used, need to look closer into this branch
121                $output = $text;
122
123            } else {
124
125                // yes, link to the new-page href, but we have to build
126                // it.  we support both the old form where
127                // the page always comes at the end, and the new
128                // form that uses sprintf()
129                if (strpos($href, '%s') === false) {
130                    // use the old form
131                    $href = $href . $this->urlEncode($page);
132                } else {
133                    // use the new form
134                    $href = sprintf($href, $this->urlEncode($page));
135                }
136            }
137
138            // get the appropriate CSS class and new-link text
139            $css = ' class="'.$this->textEncode($this->getConf('css_new')).'"';
140            $new = $this->getConf('new_text');
141
142            // what kind of linking are we doing?
143            $pos = $this->getConf('new_text_pos');
144            if (! $pos || ! $new) {
145                // no position (or no new_text), use css only on the page name
146
147                $start = '<a'.$css.' href="'.$this->textEncode($href).'">';
148                $end = '</a>';
149            } elseif ($pos == 'before') {
150                // use the new_text BEFORE the page name
151                $start = '<a'.$css.' href="'.$this->textEncode($href).'">'.$this->textEncode($new).'</a>';
152                $end = '';
153            } else {
154                // default, use the new_text link AFTER the page name
155                $start = '';
156                $end = '<a'.$css.' href="'.$this->textEncode($href).'">'.$this->textEncode($new).'</a>';
157            }
158        }
159        if (!strlen($text)) {
160            $start .= $this->textEncode($page);
161        }
162        if (isset($type)) {
163            switch ($type) {
164            case 'start':
165                $output = $start;
166                break;
167            case 'end':
168                $output = $end;
169                break;
170            }
171        } else {
172            $output = $start.$this->textEncode($text).$end;
173        }
174        return $output;
175    }
176}
177?>
178