248 |
jpm |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class Text_Wiki_Render_Xhtml_Wikilink extends Text_Wiki_Render {
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
var $conf = array(
|
|
|
7 |
'pages' => array(), // set to null or false to turn off page checks
|
|
|
8 |
'view_url' => 'http://example.com/index.php?page=%s',
|
|
|
9 |
'new_url' => 'http://example.com/new.php?page=%s',
|
|
|
10 |
'new_text' => '?',
|
|
|
11 |
'new_text_pos' => 'after', // 'before', 'after', or null/false
|
|
|
12 |
'css' => null,
|
|
|
13 |
'css_new' => null
|
|
|
14 |
);
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
*
|
|
|
19 |
* Renders a token into XHTML.
|
|
|
20 |
*
|
|
|
21 |
* @access public
|
|
|
22 |
*
|
|
|
23 |
* @param array $options The "options" portion of the token (second
|
|
|
24 |
* element).
|
|
|
25 |
*
|
|
|
26 |
* @return string The text rendered from the token options.
|
|
|
27 |
*
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
function token($options)
|
|
|
31 |
{
|
|
|
32 |
// make nice variable names (page, anchor, text)
|
|
|
33 |
extract($options);
|
|
|
34 |
|
|
|
35 |
// are we checking page existence?
|
|
|
36 |
$list =& $this->getConf('pages');
|
|
|
37 |
if (is_array($list)) {
|
|
|
38 |
// yes, check against the page list
|
|
|
39 |
$exists = in_array($page, $list);
|
|
|
40 |
} else {
|
|
|
41 |
// no, assume it exists
|
|
|
42 |
$exists = true;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// convert *after* checking against page names so as not to mess
|
|
|
46 |
// up what the user typed and what we're checking.
|
|
|
47 |
$page = htmlspecialchars($page);
|
|
|
48 |
$anchor = htmlspecialchars($anchor);
|
|
|
49 |
$text = htmlspecialchars($text);
|
|
|
50 |
|
|
|
51 |
// does the page exist?
|
|
|
52 |
if ($exists) {
|
|
|
53 |
|
|
|
54 |
// PAGE EXISTS.
|
|
|
55 |
|
|
|
56 |
// yes, link to the page view, but we have to build
|
|
|
57 |
// the HREF. we support both the old form where
|
|
|
58 |
// the page always comes at the end, and the new
|
|
|
59 |
// form that uses %s for sprintf()
|
|
|
60 |
$href = $this->getConf('view_url');
|
|
|
61 |
|
|
|
62 |
if (strpos($href, '%s') === false) {
|
|
|
63 |
// use the old form (page-at-end)
|
|
|
64 |
$href = $href . $page . $anchor;
|
|
|
65 |
} else {
|
|
|
66 |
// use the new form (sprintf format string)
|
|
|
67 |
$href = sprintf($href, $page . $anchor);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// get the CSS class and generate output
|
|
|
71 |
$css = $this->formatConf(' class="%s"', 'css');
|
|
|
72 |
$output = "<a$css href=\"$href\">$text</a>";
|
|
|
73 |
|
|
|
74 |
} else {
|
|
|
75 |
|
|
|
76 |
// PAGE DOES NOT EXIST.
|
|
|
77 |
|
|
|
78 |
// link to a create-page url, but only if new_url is set
|
|
|
79 |
$href = $this->getConf('new_url', null);
|
|
|
80 |
|
|
|
81 |
// set the proper HREF
|
|
|
82 |
if (! $href || trim($href) == '') {
|
|
|
83 |
|
|
|
84 |
// no useful href, return the text as it is
|
|
|
85 |
$output = $text;
|
|
|
86 |
|
|
|
87 |
} else {
|
|
|
88 |
|
|
|
89 |
// yes, link to the new-page href, but we have to build
|
|
|
90 |
// it. we support both the old form where
|
|
|
91 |
// the page always comes at the end, and the new
|
|
|
92 |
// form that uses sprintf()
|
|
|
93 |
if (strpos($href, '%s') === false) {
|
|
|
94 |
// use the old form
|
|
|
95 |
$href = $href . $page;
|
|
|
96 |
} else {
|
|
|
97 |
// use the new form
|
|
|
98 |
$href = sprintf($href, $page);
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// get the appropriate CSS class and new-link text
|
|
|
103 |
$css = $this->formatConf(' class="%s"', 'css');
|
|
|
104 |
$new = $this->getConf('new_text');
|
|
|
105 |
|
|
|
106 |
// what kind of linking are we doing?
|
|
|
107 |
$pos = $this->getConf('new_text_pos');
|
|
|
108 |
if (! $pos || ! $new) {
|
|
|
109 |
// no position (or no new_text), use css only on the page name
|
|
|
110 |
$output = "<a$css href=\"$href\">$page</a>";
|
|
|
111 |
} elseif ($pos == 'before') {
|
|
|
112 |
// use the new_text BEFORE the page name
|
|
|
113 |
$output = "<a$css href=\"$href\">$new</a>$text";
|
|
|
114 |
} else {
|
|
|
115 |
// default, use the new_text link AFTER the page name
|
|
|
116 |
$output = "$text<a$css href=\"$href\">$new</a>";
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
return $output;
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
?>
|