| 1 | <?php |
| 2 | |
| 3 | class Text_Wiki_Render_Xhtml_Wikilink extends Text_Wiki_Render { |
| 4 | |
| 5 | var $conf = array( |
| 6 | 'pages' => array(), // set to null or false to turn off page checks |
| 7 | 'view_url' => 'http://example.com/index.php?page=%s', |
| 8 | 'new_url' => 'http://example.com/new.php?page=%s', |
| 9 | 'new_text' => '?', |
| 10 | 'new_text_pos' => 'after', // 'before', 'after', or null/false |
| 11 | 'css' => null, |
| 12 | 'css_new' => null, |
| 13 | 'exists_callback' => null // call_user_func() callback |
| 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 | // is there a "page existence" callback? |
| 36 | // we need to access it directly instead of through |
| 37 | // getConf() because we'll need a reference (for |
| 38 | // object instance method callbacks). |
| 39 | if (isset($this->conf['exists_callback'])) { |
| 40 | $callback =& $this->conf['exists_callback']; |
| 41 | } else { |
| 42 | $callback = false; |
| 43 | } |
| 44 | |
| 45 | if ($callback) { |
| 46 | // use the callback function |
| 47 | $exists = call_user_func($callback, $page); |
| 48 | } else { |
| 49 | // no callback, go to the naive page array. |
| 50 | $list =& $this->getConf('pages'); |
| 51 | if (is_array($list)) { |
| 52 | // yes, check against the page list |
| 53 | $exists = in_array($page, $list); |
| 54 | } else { |
| 55 | // no, assume it exists |
| 56 | $exists = true; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // convert *after* checking against page names so as not to mess |
| 61 | // up what the user typed and what we're checking. |
| 62 | $page = htmlspecialchars($page); |
| 63 | $anchor = htmlspecialchars($anchor); |
| 64 | $text = htmlspecialchars($text); |
| 65 | |
| 66 | // does the page exist? |
| 67 | if ($exists) { |
| 68 | |
| 69 | // PAGE EXISTS. |
| 70 | |
| 71 | // link to the page view, but we have to build |
| 72 | // the HREF. we support both the old form where |
| 73 | // the page always comes at the end, and the new |
| 74 | // form that uses %s for sprintf() |
| 75 | $href = $this->getConf('view_url'); |
| 76 | |
| 77 | if (strpos($href, '%s') === false) { |
| 78 | // use the old form (page-at-end) |
| 79 | $href = $href . $page . $anchor; |
| 80 | } else { |
| 81 | // use the new form (sprintf format string) |
| 82 | $href = sprintf($href, $page . $anchor); |
| 83 | } |
| 84 | |
| 85 | // get the CSS class and generate output |
| 86 | $css = $this->formatConf(' class="%s"', 'css'); |
| 87 | $output = "<a$css href=\"$href\">$text</a>"; |
| 88 | |
| 89 | } else { |
| 90 | |
| 91 | // PAGE DOES NOT EXIST. |
| 92 | |
| 93 | // link to a create-page url, but only if new_url is set |
| 94 | $href = $this->getConf('new_url', null); |
| 95 | |
| 96 | // set the proper HREF |
| 97 | if (! $href || trim($href) == '') { |
| 98 | |
| 99 | // no useful href, return the text as it is |
| 100 | $output = $text; |
| 101 | |
| 102 | } else { |
| 103 | |
| 104 | // yes, link to the new-page href, but we have to build |
| 105 | // it. we support both the old form where |
| 106 | // the page always comes at the end, and the new |
| 107 | // form that uses sprintf() |
| 108 | if (strpos($href, '%s') === false) { |
| 109 | // use the old form |
| 110 | $href = $href . $page; |
| 111 | } else { |
| 112 | // use the new form |
| 113 | $href = sprintf($href, $page); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // get the appropriate CSS class and new-link text |
| 118 | $css = $this->formatConf(' class="%s"', 'css_new'); |
| 119 | $new = $this->getConf('new_text'); |
| 120 | |
| 121 | // what kind of linking are we doing? |
| 122 | $pos = $this->getConf('new_text_pos'); |
| 123 | if (! $pos || ! $new) { |
| 124 | // no position (or no new_text), use css only on the page name |
| 125 | $output = "<a$css href=\"$href\">$page</a>"; |
| 126 | } elseif ($pos == 'before') { |
| 127 | // use the new_text BEFORE the page name |
| 128 | $output = "<a$css href=\"$href\">$new</a>$text"; |
| 129 | } else { |
| 130 | // default, use the new_text link AFTER the page name |
| 131 | $output = "$text<a$css href=\"$href\">$new</a>"; |
| 132 | } |
| 133 | } |
| 134 | return $output; |
| 135 | } |
| 136 | } |
| 137 | ?> |