interwiki.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: interwiki.php,v 1.3 2004/01/31 15:52:08 pmjones Exp $
  20. /**
  21. *
  22. * This class implements a Text_Wiki_Rule to find source text marked as
  23. * an Interwiki link. See the regex for a detailed explanation of the
  24. * text matching procedure; e.g., "InterWikiName:PageName".
  25. *
  26. * @author Paul M. Jones <pmjones@ciaweb.net>
  27. *
  28. * @package Text_Wiki
  29. *
  30. */
  31. class Text_Wiki_Rule_interwiki extends Text_Wiki_Rule {
  32. /**
  33. *
  34. * Constructor. We override the Text_Wiki_Rule constructor so we can
  35. * explicitly comment each part of the $regex property.
  36. *
  37. * @access public
  38. *
  39. * @param object &$obj The calling "parent" Text_Wiki object.
  40. *
  41. * @param string $name The token name to use for this rule.
  42. *
  43. */
  44. function Text_Wiki_Rule_interwiki(&$obj, $name)
  45. {
  46. parent::Text_Wiki_Rule($obj, $name);
  47. $this->regex = '/([A-Za-z0-9]+):([\/=&~#A-Za-z0-9]+)/';
  48. }
  49. /**
  50. *
  51. * Generates a replacement for the matched text. Token options are:
  52. *
  53. * 'site' => The key name for the Text_Wiki interwiki array map, usually
  54. * the name of the interwiki site.
  55. *
  56. * 'page' => The page on the target interwiki to link to.
  57. *
  58. * @access public
  59. *
  60. * @param array &$matches The array of matches from parse().
  61. *
  62. * @return A delimited token to be used as a placeholder in
  63. * the source text, plus any text priot to the match.
  64. *
  65. */
  66. function process(&$matches)
  67. {
  68. $options = array(
  69. 'site' => $matches[1],
  70. 'page' => $matches[2]
  71. );
  72. return $this->addToken($options);
  73. }
  74. /**
  75. *
  76. * Renders a token into text matching the requested format.
  77. *
  78. * @access public
  79. *
  80. * @param array $options The "options" portion of the token (second
  81. * element).
  82. *
  83. * @return string The text rendered from the token options.
  84. *
  85. */
  86. function renderXhtml($options)
  87. {
  88. $site = $options['site'];
  89. $page = $options['page'];
  90. if (isset($this->_wiki->interwiki[$site])) {
  91. $href = $this->_wiki->interwiki[$site];
  92. } else {
  93. $href = '';
  94. }
  95. if ($href != '') {
  96. return "<a href=\"$href$page\">$site:$page</a>";
  97. } else {
  98. return "$site:$page";
  99. }
  100. }
  101. }
  102. ?>