Top/PukiWiki/1.4/ちょっと便利に/make_link()の改造実験/パッチ

/lib/link.php

--- 146/lib/link.php	Fri Apr 29 20:24:20 2005
+++ 146exp1/lib/link.php	Sun Feb 26 12:37:54 2006
@@ -66,14 +66,14 @@
 	$rel_auto = array(); // オートリンクしている参照先
 	$links    = links_get_objects($page, TRUE);
 	foreach ($links as $_obj) {
-		if (! isset($_obj->type) || $_obj->type != 'pagename' ||
-		    $_obj->name == $page || $_obj->name == '')
-			continue;
-
-		if (is_a($_obj, 'Link_autolink')) { // 行儀が悪い
-			$rel_auto[] = $_obj->name;
-		} else {
-			$rel_new[]  = $_obj->name;
+//	この条件は$_obj['type'] == 'LocalLink'に変えたつもり
+//		if ($_obj['name'] == $page || $_obj['name'] == '')
+//			continue;
+
+		if ($_obj['type'] == 'AutoLink') {
+			$rel_auto[] = $_obj['name'];
+		} else if ($_obj['type'] == 'PageLink') {
+			$rel_new[]  = $_obj['name'];
 		}
 	}
 	$rel_new = array_unique($rel_new);
@@ -151,15 +151,14 @@
 		$rel   = array(); // 参照先
 		$links = links_get_objects($page);
 		foreach ($links as $_obj) {
-			if (! isset($_obj->type) || $_obj->type != 'pagename' ||
-			    $_obj->name == $page || $_obj->name == '')
+			if (! preg_match('/^(Auto|Page)Link$/', $_obj['type']))
 				continue;
 
-			$rel[] = $_obj->name;
-			if (! is_a($_obj, 'Link_autolink')) {
-				$ref_notauto[$_obj->name][$page] = TRUE;
+			$rel[] = $_obj['name'];
+			if ($_obj['type'] != 'AutoLink') {
+				$ref_notauto[$_obj['name']][$page] = TRUE;
 			}
-			$ref[$_obj->name][] = $page;
+			$ref[$_obj['name']][] = $page;
 		}
 		$rel = array_unique($rel);
 		if (! empty($rel)) {
@@ -234,7 +233,7 @@
 			}
 		}
 		unlink($ref_file);
-		if ($is_page && ! $all_auto && $ref != '') {
+		if ($is_page && ! $all_auto && $ref != '') {	//	ここ変
 			$fp = fopen($ref_file, 'w')
 				or die_message('cannot write ' . htmlspecialchars($ref_file));
 			fputs($fp, $ref);
@@ -243,7 +242,7 @@
 	}
 }
 
-function & links_get_objects($page, $refresh = FALSE)
+function links_get_objects($page, $refresh = FALSE)
 {
 	static $obj;

/lib/make_link.php

--- 146/lib/make_link.php	Thu Jul 14 23:45:08 2005
+++ 146exp1/lib/make_link.php	Sun Feb 26 20:54:10 2006
@@ -16,9 +16,7 @@
 
 	if (! isset($converter)) $converter = new InlineConverter();
 
-	$clone = $converter->get_clone($converter);
-
-	return $clone->convert($string, ($page != '') ? $page : $vars['page']);
+	return $converter->convert($string, ($page != '') ? $page : $vars['page']);
 }
 
 // Converters of inline element
@@ -26,30 +24,9 @@
 {
 	var $converters; // as array()
 	var $pattern;
-	var $pos;
+	var $page;
 	var $result;
 
-	function get_clone($obj) {
-		static $clone_func;
-
-		if (! isset($clone_func)) {
-			if (version_compare(PHP_VERSION, '5.0.0', '<')) {
-				$clone_func = create_function('$a', 'return $a;');
-			} else {
-				$clone_func = create_function('$a', 'return clone $a;');
-			}
-		}
-		return $clone_func($obj);
-	}
-
-	function __clone() {
-		$converters = array();
-		foreach ($this->converters as $key=>$converter) {
-			$converters[$key] = $this->get_clone($converter);
-		}
-		$this->converters = $converters;
-	}
-
 	function InlineConverter($converters = NULL, $excludes = NULL)
 	{
 		if ($converters === NULL) {
@@ -89,6 +66,7 @@
 
 	function convert($string, $page)
 	{
+		$stack = array($this->page, $this->result);
 		$this->page   = $page;
 		$this->result = array();
 
@@ -96,19 +74,21 @@
 			array(& $this, 'replace'), $string);
 
 		$arr = explode("\x08", make_line_rules(htmlspecialchars($string)));
-		$retval = '';
-		while (! empty($arr)) {
-			$retval .= array_shift($arr) . array_shift($this->result);
-		}
+		$retval = array_shift($arr);
+		while (! empty($arr))
+			$retval .= array_shift($this->result) . array_shift($arr);
+
+		list($this->page, $this->result) = $stack;
 		return $retval;
 	}
 
 	function replace($arr)
 	{
-		$obj = $this->get_converter($arr);
+		$obj = & $this->get_converter($arr);
 
-		$this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
-			$obj->toString() : make_line_rules(htmlspecialchars($arr[0]));
+		$this->result[] =
+			is_array($param = $obj->set($obj->splice($arr), $this->page)) ?
+			$obj->toString($param) : make_line_rules(htmlspecialchars($arr[0]));
 
 		return "\x08"; // Add a mark into latest processed part
 	}
@@ -118,41 +98,35 @@
 		$matches = $arr = array();
 		preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
 		foreach ($matches as $match) {
-			$obj = $this->get_converter($match);
-			if ($obj->set($match, $page) !== FALSE) {
-				$arr[] = $this->get_clone($obj);
-				if ($obj->body != '')
-					$arr = array_merge($arr, $this->get_objects($obj->body, $page));
+			$obj = & $this->get_converter($match);
+			if (is_array($param = $obj->set($obj->splice($match), $page))) {
+				$arr[] = $param;
+				if (isset($param['body']))
+					$arr = array_merge($arr, $this->get_objects($param['body'], $page));
 			}
 		}
 		return $arr;
 	}
 
-	function & get_converter(& $arr)
+	function & get_converter($arr)
 	{
-		foreach (array_keys($this->converters) as $start) {
-			if ($arr[$start] == $arr[0])
-				return $this->converters[$start];
-		}
-		return NULL;
+		$start = array_search($arr[0], array_slice($arr, 1));
+		if (! is_int($start))
+			die_message('inline converter error!!');
+		return $this->converters[$start + 1];
 	}
 }
 
 // Base class of inline elements
 class Link
 {
+	var $type;    // Object type (for link.php)
 	var $start;   // Origin number of parentheses (0 origin)
-	var $text;    // Matched string
-
-	var $type;
-	var $page;
-	var $name;
-	var $body;
-	var $alias;
 
 	// Constructor
-	function Link($start)
+	function Link($start, $type)
 	{
+		$this->type  = $type;
 		$this->start = $start;
 	}
 
@@ -163,43 +137,46 @@
 	function get_count() {}
 
 	// Set pattern that matches
+	//	戻り値は連想配列か、元の文字列(誤ったマッチングをした場合)
 	function set($arr, $page) {}
 
-	function toString() {}
+	//	setの戻り値を受けてhtml生成は極力ここで行なう
+	function toString($param) {}
 
 	// Private: Get needed parts from a matched array()
+	//	preg_splitには使えない
 	function splice($arr)
 	{
 		$count = $this->get_count() + 1;
-		$arr   = array_pad(array_splice($arr, $this->start, $count), $count, '');
-		$this->text = $arr[0];
-		return $arr;
+		return array_pad(array_splice($arr, $this->start, $count), $count, '');
 	}
 
 	// Set basic parameters
-	function setParam($page, $name, $body, $type = '', $alias = '')
+	function setParam($param)
 	{
 		static $converter = NULL;
 
-		$this->page = $page;
-		$this->name = $name;
-		$this->body = $body;
-		$this->type = $type;
-		if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
-			is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
-			$alias = '<img src="' . htmlspecialchars($alias) . '" alt="' . $name . '" />';
-		} else if ($alias != '') {
-			if ($converter === NULL)
-				$converter = new InlineConverter(array('plugin'));
+		//	必須パラメータを埋め込む
+		$param = array_merge(
+			array('type' => $this->type, 'name' => '', 'alias' => ''),
+			$param
+		);
+
+		if ($param['alias'] != '') {
+			if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
+				is_url($param['alias']) && preg_match('/\.(gif|png|jpe?g)$/i', $param['alias'])) {
+				$param['alias'] = '<img src="' . htmlspecialchars($param['alias']) . '" alt="' . $param['name'] . '" />';
+			} else {
+				if ($converter === NULL)
+					$converter = new InlineConverter(array('plugin'));
 
-			$alias = make_line_rules($converter->convert($alias, $page));
+				$param['alias'] = make_line_rules($converter->convert($param['alias'], $param['page']));
 
-			// BugTrack/669: A hack removing anchor tags added by AutoLink
-			$alias = preg_replace('#</?a[^>]*>#i', '', $alias);
+				// BugTrack/669: A hack removing anchor tags added by AutoLink
+				$param['alias'] = preg_replace('#</?a[^>]*>#i', '', $param['alias']);
+			}
 		}
-		$this->alias = $alias;
-
-		return TRUE;
+		return $param;
 	}
 }
 
@@ -207,11 +184,10 @@
 class Link_plugin extends Link
 {
 	var $pattern;
-	var $plain,$param;
 
 	function Link_plugin($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'Plugin');
 	}
 
 	function get_pattern()
@@ -245,32 +221,32 @@
 
 	function set($arr, $page)
 	{
-		list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
+		@list($all, $plain, $name, $param, $body) = $arr;
 
 		// Re-get true plugin name and patameters (for PHP 4.1.2)
 		$matches = array();
 		if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
-			&& $matches[1] != $this->plain) 
-			list(, $this->plain, $name, $this->param) = $matches;
+			&& $matches[1] != $plain) 
+			list(, $plain, $name, $param) = $matches;
 
-		return parent::setParam($page, $name, $body, 'plugin');
+		return parent::setParam(compact('name', 'body', 'plain', 'param', 'page'));
 	}
 
-	function toString()
+	function toString($param)
 	{
-		$body = ($this->body == '') ? '' : make_link($this->body);
+		$body = ($param['body'] == '') ? '' : make_link($param['body']);
 		$str = FALSE;
 
 		// Try to call the plugin
-		if (exist_plugin_inline($this->name))
-			$str = do_plugin_inline($this->name, $this->param, $body);
+		if (exist_plugin_inline($param['name']))
+			$str = do_plugin_inline($param['name'], $param['param'], $body);
 
 		if ($str !== FALSE) {
 			return $str; // Succeed
 		} else {
 			// No such plugin, or Failed
 			$body = (($body == '') ? '' : '{' . $body . '}') . ';';
-			return make_line_rules(htmlspecialchars('&' . $this->plain) . $body);
+			return make_line_rules(htmlspecialchars('&' . $param['plain']) . $body);
 		}
 	}
 }
@@ -280,7 +256,7 @@
 {
 	function Link_note($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'LocalLink');
 	}
 
 	function get_pattern()
@@ -299,38 +275,34 @@
 
 	function set($arr, $page)
 	{
-		global $foot_explain, $vars;
 		static $note_id = 0;
 
-		list(, $body) = $this->splice($arr);
+		list(, $body) = $arr;
+		$id   = ++$note_id;
+		return parent::setParam(compact('body', 'id', 'page'));
+	}
 
-		if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
-			$script = '';
-		} else {
-			$script = get_script_uri() . '?' . rawurlencode($page);
-		}
+	function toString($param)
+	{
+		global $foot_explain;
 
-		$id   = ++$note_id;
-		$note = make_link($body);
-		$page = isset($vars['page']) ? rawurlencode($vars['page']) : '';
+		$note = make_link($param['body']);
+		$script = PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR ?
+			'' : get_script_uri() . '?' . rawurlencode($param['page']);
 
 		// Footnote
-		$foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
-			$script . '#notetext_' . $id . '" class="note_super">*' .
-			$id . '</a>' . "\n" .
+		$foot_explain[$param['id']] = '<a id="notefoot_' . $param['id'] .
+			'" href="' . $script . '#notetext_' . $param['id'] .
+			'" class="note_super">*' . $param['id'] . '</a>' . "\n" .
 			'<span class="small">' . $note . '</span><br />';
 
 		// A hyperlink, content-body to footnote
-		$name = '<a id="notetext_' . $id . '" href="' . $script .
-			'#notefoot_' . $id . '" class="note_super" title="' .
-			strip_tags($note) . '">*' . $id . '</a>';
-
-		return parent::setParam($page, $name, $body);
-	}
+		$name = '<a id="notetext_' . $param['id'] .
+			'" href="' . $script . '#notefoot_' . $param['id'] .
+			'" class="note_super" title="' . strip_tags($note) .
+			'">*' . $param['id'] . '</a>';
 
-	function toString()
-	{
-		return $this->name;
+		return $name;
 	}
 }
 
@@ -339,7 +311,7 @@
 {
 	function Link_url($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'Url');
 	}
 
 	function get_pattern()
@@ -364,19 +336,22 @@
 
 	function set($arr, $page)
 	{
-		list(, , $alias, $name) = $this->splice($arr);
-		return parent::setParam($page, htmlspecialchars($name),
-			'', 'url', $alias == '' ? $name : $alias);
+		list(, , $alias, $name) = $arr;
+		return parent::setParam(compact('name', 'alias', 'page'));
 	}
 
-	function toString()
+	function toString($param)
 	{
 		if (FALSE) {
 			$rel = '';
 		} else {
 			$rel = ' rel="nofollow"';
 		}
-		return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
+		//	aliasはparent::set()内部でhtmlspecialcharsを通る
+		$name = htmlspecialchars($param['name']);
+		if ($param['alias'] == '')
+			$param['alias'] = $name;
+		return '<a href="' . $name . '"' . $rel . '>' . $param['alias'] . '</a>';
 	}
 }
 
@@ -385,7 +360,7 @@
 {
 	function Link_url_interwiki($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'Url');
 	}
 
 	function get_pattern()
@@ -408,24 +383,22 @@
 
 	function set($arr, $page)
 	{
-		list(, $name, $alias) = $this->splice($arr);
-		return parent::setParam($page, htmlspecialchars($name), '', 'url', $alias);
+		list(, $name, $alias) = $arr;
+		return parent::setParam(compact('name', 'alias', 'page'));
 	}
 
-	function toString()
+	function toString($param)
 	{
-		return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
+		return '<a href="' . htmlspecialchars($param['name']) . '" rel="nofollow">' . $param['alias'] . '</a>';
 	}
 }
 
 // mailto: URL schemes
 class Link_mailto extends Link
 {
-	var $is_image, $image;
-
 	function Link_mailto($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'MailTo');
 	}
 
 	function get_pattern()
@@ -448,26 +421,24 @@
 
 	function set($arr, $page)
 	{
-		list(, $alias, $name) = $this->splice($arr);
-		return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
+		list(, $alias, $name) = $arr;
+		return parent::setParam(compact('name', 'alias', 'page'));
 	}
 	
-	function toString()
+	function toString($param)
 	{
-		return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
+		if ($param['alias'] == '')
+			$param['alias'] = $param['name'];
+		return '<a href="mailto:' . $param['name'] . '" rel="nofollow">' . $param['alias'] . '</a>';
 	}
 }
 
 // InterWikiName-rendered URLs
 class Link_interwikiname extends Link
 {
-	var $url    = '';
-	var $param  = '';
-	var $anchor = '';
-
 	function Link_interwikiname($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'InterWikiName');
 	}
 
 	function get_pattern()
@@ -500,43 +471,38 @@
 
 	function set($arr, $page)
 	{
-		global $script;
+		list(, $alias, , $name, $param) = $arr;
+		return parent::setParam(compact('name', 'alias', 'param', 'page'));
+	}
 
-		list(, $alias, , $name, $this->param) = $this->splice($arr);
+	function toString($param)
+	{
+		global $script;
 
+		$anchor = '';
 		$matches = array();
-		if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
-			list(, $this->param, $this->anchor) = $matches;
+		if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $param['param'], $matches))
+			list(, $param['param'], $anchor) = $matches;
 
-		$url = get_interwiki_url($name, $this->param);
-		$this->url = ($url === FALSE) ?
-			$script . '?' . rawurlencode('[[' . $name . ':' . $this->param . ']]') :
+		$url = get_interwiki_url($param['name'], $param['param']);
+		$url = ($url === FALSE) ?
+			$script . '?' . rawurlencode('[[' . $param['name'] . ':' . $param['param'] . ']]') :
 			htmlspecialchars($url);
 
-		return parent::setParam(
-			$page,
-			htmlspecialchars($name . ':' . $this->param),
-			'',
-			'InterWikiName',
-			$alias == '' ? $name . ':' . $this->param : $alias
-		);
-	}
+		$name = htmlspecialchars($param['name'] . ':' . $param['param']);
+		if ($param['alias'] == '') $param['alias'] = $name;
 
-	function toString()
-	{
-		return '<a href="' . $this->url . $this->anchor . '" title="' .
-			$this->name . '" rel="nofollow">' . $this->alias . '</a>';
+		return '<a href="' . $url . $anchor . '" title="' .
+			$name . '" rel="nofollow">' . $param['alias'] . '</a>';
 	}
 }
 
 // BracketNames
 class Link_bracketname extends Link
 {
-	var $anchor, $refer;
-
 	function Link_bracketname($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'PageLink');
 	}
 
 	function get_pattern()
@@ -568,28 +534,25 @@
 	{
 		global $WikiName;
 
-		list(, $alias, , $name, $this->anchor) = $this->splice($arr);
-		if ($name == '' && $this->anchor == '') return FALSE;
+		list($all, $alias, , $name, $anchor) = $arr;
+		if ($name == '' && $anchor == '') return $all;	//	旧FALSE
 
 		if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
-			if ($alias == '') $alias = $name . $this->anchor;
+			if ($alias == '') $alias = $name . $anchor;
 			if ($name != '') {
 				$name = get_fullname($name, $page);
-				if (! is_pagename($name)) return FALSE;
+				if (! is_pagename($name)) return $all;	//	旧FALSE;
 			}
 		}
+		if ($name == '' || $name == $page) $type = 'LocalLink';	//	type override
 
-		return parent::setParam($page, $name, '', 'pagename', $alias);
+		return parent::setParam(compact('name', 'alias', 'anchor', 'type', 'page'));
 	}
 
-	function toString()
+	function toString($param)
 	{
-		return make_pagelink(
-			$this->name,
-			$this->alias,
-			$this->anchor,
-			$this->page
-		);
+		return !is_array($param) ? make_line_rules(htmlspecialchars($param)) :
+			make_pagelink($param['name'], $param['alias'], $param['anchor'], $param['page']);
 	}
 }
 
@@ -598,7 +561,7 @@
 {
 	function Link_wikiname($start)
 	{
-		parent::Link($start);
+		parent::Link($start, 'PageLink');
 	}
 
 	function get_pattern()
@@ -615,18 +578,14 @@
 
 	function set($arr, $page)
 	{
-		list($name) = $this->splice($arr);
-		return parent::setParam($page, $name, '', 'pagename', $name);
+		list($name) = $arr;
+		if ($name == $page) $type = 'LocalLink';	// type override
+		return parent::setParam(compact('name', 'type', 'page'));
 	}
 
-	function toString()
+	function toString($param)
 	{
-		return make_pagelink(
-			$this->name,
-			$this->alias,
-			'',
-			$this->page
-		);
+		return make_pagelink($param['name'], '', '', $param['page']);
 	}
 }
 
@@ -641,7 +600,7 @@
 	{
 		global $autolink;
 
-		parent::Link($start);
+		parent::Link($start, 'AutoLink');
 
 		if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
 			return;
@@ -664,20 +623,19 @@
 
 	function set($arr, $page)
 	{
-		global $WikiName;
+		list($name) = $arr;
 
-		list($name) = $this->splice($arr);
-
 		// Ignore pages listed, or Expire ones not found
 		if (in_array($name, $this->forceignorepages) || ! is_page($name))
-			return FALSE;
-
-		return parent::setParam($page, $name, '', 'pagename', $name);
+			return $name;	//	旧FALSE
+		if ($name == $page) $type = 'LocalLink';	// type override
+		return parent::setParam(compact('name', 'type', 'page'));
 	}
 
-	function toString()
+	function toString($param)
 	{
-		return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
+		return !is_array($param) ? make_line_rules(htmlspecialchars($param)) :
+			make_pagelink($param['name'], '', '', $param['page'], TRUE);
 	}
 }
 
@@ -685,7 +643,7 @@
 {
 	function Link_autolink_a($start)
 	{
-		parent::Link_autolink($start);
+		parent::Link_autolink($start, 'AutoLink');
 	}
 
 	function get_pattern()

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2010-03-01 (月) 12:25:01
Site admin: PukiWiki Development Team

PukiWiki 1.5.4+ © 2001-2022 PukiWiki Development Team. Powered by PHP 8.2.12. HTML convert time: 0.278 sec.

SourceForge