Revision | 9fd99786b6f537176eecf10b2dae4f0e38ed04f1 (tree) |
---|---|
Time | 2020-02-14 02:57:42 |
Author | teanan <teanan> |
Commiter | umorigu |
BugTrack/2002 AutoAlias
Merged with autoalias branches (r1_4_7_autoalias).
* Link defined keywords in AutoAliasName page
@@ -0,0 +1,3 @@ | ||
1 | +pukiwiki\.(?:dev|official) | |
2 | +(?!) | |
3 | + |
@@ -2,7 +2,7 @@ | ||
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone. |
3 | 3 | // file.php |
4 | 4 | // Copyright |
5 | -// 2002-2017 PukiWiki Development Team | |
5 | +// 2002-2020 PukiWiki Development Team | |
6 | 6 | // 2001-2002 Originally written by yu-ji |
7 | 7 | // License: GPL v2 or (at your option) any later version |
8 | 8 | // |
@@ -15,6 +15,9 @@ define('PKWK_MAXSHOW_CACHE', 'recent.dat'); | ||
15 | 15 | // AutoLink |
16 | 16 | define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat'); |
17 | 17 | |
18 | +// AutoAlias | |
19 | +define('PKWK_AUTOALIAS_REGEX_CACHE', 'autoalias.dat'); | |
20 | + | |
18 | 21 | /** |
19 | 22 | * Get source(wiki text) data of the page |
20 | 23 | * |
@@ -104,6 +107,8 @@ function get_filename($page) | ||
104 | 107 | // Put a data(wiki text) into a physical file(diff, backup, text) |
105 | 108 | function page_write($page, $postdata, $notimestamp = FALSE) |
106 | 109 | { |
110 | + global $trackback, $autoalias, $aliaspage; | |
111 | + | |
107 | 112 | if (PKWK_READONLY) return; // Do nothing |
108 | 113 | |
109 | 114 | $postdata = make_str_rules($postdata); |
@@ -133,6 +138,19 @@ function page_write($page, $postdata, $notimestamp = FALSE) | ||
133 | 138 | file_write(DATA_DIR, $page, $postdata, $notimestamp, $is_delete); |
134 | 139 | |
135 | 140 | links_update($page); |
141 | + | |
142 | + // Update autoalias.dat (AutoAliasName) | |
143 | + if ($autoalias && $page == $aliaspage) { | |
144 | + $aliases = get_autoaliases(); | |
145 | + if (empty($aliases)) { | |
146 | + // Remove | |
147 | + @unlink(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE); | |
148 | + } else { | |
149 | + // Create or Update | |
150 | + autolink_pattern_write(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE, | |
151 | + get_autolink_pattern(array_keys($aliases), $autoalias)); | |
152 | + } | |
153 | + } | |
136 | 154 | } |
137 | 155 | |
138 | 156 | // Modify original text with user-defined / system-defined rules |
@@ -602,23 +620,9 @@ function put_lastmodified() | ||
602 | 620 | fclose($fp); |
603 | 621 | |
604 | 622 | // For AutoLink |
605 | - if ($autolink) { | |
606 | - list($pattern, $pattern_a, $forceignorelist) = | |
607 | - get_autolink_pattern($pages); | |
608 | - | |
609 | - $file = CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE; | |
610 | - pkwk_touch_file($file); | |
611 | - $fp = fopen($file, 'r+') or | |
612 | - die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_AUTOLINK_REGEX_CACHE); | |
613 | - set_file_buffer($fp, 0); | |
614 | - flock($fp, LOCK_EX); | |
615 | - ftruncate($fp, 0); | |
616 | - rewind($fp); | |
617 | - fputs($fp, $pattern . "\n"); | |
618 | - fputs($fp, $pattern_a . "\n"); | |
619 | - fputs($fp, join("\t", $forceignorelist) . "\n"); | |
620 | - flock($fp, LOCK_UN); | |
621 | - fclose($fp); | |
623 | + if ($autolink){ | |
624 | + autolink_pattern_write(CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE, | |
625 | + get_autolink_pattern($pages, $autolink)); | |
622 | 626 | } |
623 | 627 | } |
624 | 628 |
@@ -648,6 +652,23 @@ function delete_recent_changes_cache() { | ||
648 | 652 | unlink($file); |
649 | 653 | } |
650 | 654 | |
655 | +// update autolink data | |
656 | +function autolink_pattern_write($filename, $autolink_pattern) | |
657 | +{ | |
658 | + list($pattern, $pattern_a, $forceignorelist) = $autolink_pattern; | |
659 | + | |
660 | + $fp = fopen($filename, 'w') or | |
661 | + die_message('Cannot open ' . $filename); | |
662 | + set_file_buffer($fp, 0); | |
663 | + flock($fp, LOCK_EX); | |
664 | + rewind($fp); | |
665 | + fputs($fp, $pattern . "\n"); | |
666 | + fputs($fp, $pattern_a . "\n"); | |
667 | + fputs($fp, join("\t", $forceignorelist) . "\n"); | |
668 | + flock($fp, LOCK_UN); | |
669 | + fclose($fp); | |
670 | +} | |
671 | + | |
651 | 672 | // Get elapsed date of the page |
652 | 673 | function get_pg_passage($page, $sw = TRUE) |
653 | 674 | { |
@@ -2,7 +2,7 @@ | ||
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone. |
3 | 3 | // func.php |
4 | 4 | // Copyright |
5 | -// 2002-2019 PukiWiki Development Team | |
5 | +// 2002-2020 PukiWiki Development Team | |
6 | 6 | // 2001-2002 Originally written by yu-ji |
7 | 7 | // License: GPL v2 or (at your option) any later version |
8 | 8 | // |
@@ -723,7 +723,7 @@ function drop_submit($str) | ||
723 | 723 | } |
724 | 724 | |
725 | 725 | // Generate AutoLink patterns (thx to hirofummy) |
726 | -function get_autolink_pattern(& $pages) | |
726 | +function get_autolink_pattern(& $pages, $min_len = -1) | |
727 | 727 | { |
728 | 728 | global $WikiName, $autolink, $nowikiname; |
729 | 729 |
@@ -734,9 +734,13 @@ function get_autolink_pattern(& $pages) | ||
734 | 734 | unset($config); |
735 | 735 | $auto_pages = array_merge($ignorepages, $forceignorepages); |
736 | 736 | |
737 | + if ($min_len == -1) { | |
738 | + $min_len = $autolink; // set $autolink, when omitted. | |
739 | + } | |
740 | + | |
737 | 741 | foreach ($pages as $page) |
738 | 742 | if (preg_match('/^' . $WikiName . '$/', $page) ? |
739 | - $nowikiname : strlen($page) >= $autolink) | |
743 | + $nowikiname : strlen($page) >= $min_len) | |
740 | 744 | $auto_pages[] = $page; |
741 | 745 | |
742 | 746 | if (empty($auto_pages)) { |
@@ -783,6 +787,50 @@ function get_autolink_pattern_sub(& $pages, $start, $end, $pos) | ||
783 | 787 | return $result; |
784 | 788 | } |
785 | 789 | |
790 | +// Load/get setting pairs from AutoAliasName | |
791 | +function get_autoaliases($word = '') | |
792 | +{ | |
793 | + global $aliaspage, $autoalias_max_words; | |
794 | + static $pairs; | |
795 | + | |
796 | + if (! isset($pairs)) { | |
797 | + $pairs = array(); | |
798 | + $pattern = <<<EOD | |
799 | +\[\[ # open bracket | |
800 | +((?:(?!\]\]).)+)> # (1) alias name | |
801 | +((?:(?!\]\]).)+) # (2) alias link | |
802 | +\]\] # close bracket | |
803 | +EOD; | |
804 | + $postdata = join('', get_source($aliaspage)); | |
805 | + $matches = array(); | |
806 | + $count = 0; | |
807 | + $max = max($autoalias_max_words, 0); | |
808 | + if (preg_match_all('/' . $pattern . '/x', $postdata, $matches, PREG_SET_ORDER)) { | |
809 | + foreach($matches as $key => $value) { | |
810 | + if ($count == $max) break; | |
811 | + $name = trim($value[1]); | |
812 | + if (! isset($pairs[$name])) { | |
813 | + ++$count; | |
814 | + $pairs[$name] = trim($value[2]); | |
815 | + } | |
816 | + unset($matches[$key]); | |
817 | + } | |
818 | + } | |
819 | + } | |
820 | + | |
821 | + if ($word === '') { | |
822 | + // An array(): All pairs | |
823 | + return $pairs; | |
824 | + } else { | |
825 | + // A string: Seek the pair | |
826 | + if (isset($pairs[$word])) { | |
827 | + return $pairs[$word]; | |
828 | + } else { | |
829 | + return ''; | |
830 | + } | |
831 | + } | |
832 | +} | |
833 | + | |
786 | 834 | /** |
787 | 835 | * Get propery URI of this script |
788 | 836 | * |
@@ -1,7 +1,7 @@ | ||
1 | 1 | <?php |
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone |
3 | -// $Id: link.php,v 1.20 2011/01/25 15:01:01 henoheno Exp $ | |
4 | -// Copyright (C) 2003-2007 PukiWiki Developers Team | |
3 | +// link.php | |
4 | +// Copyright 2003-2020 PukiWiki Development Team | |
5 | 5 | // License: GPL v2 or (at your option) any later version |
6 | 6 | // |
7 | 7 | // Backlinks / AutoLinks related functions |
@@ -72,6 +72,11 @@ function links_update($page) | ||
72 | 72 | |
73 | 73 | if (is_a($_obj, 'Link_autolink')) { // 行儀が悪い |
74 | 74 | $rel_auto[] = $_obj->name; |
75 | + } else if (is_a($_obj, 'Link_autoalias')) { | |
76 | + $_alias = get_autoaliases($_obj->name); | |
77 | + if (is_pagename($_alias)) { | |
78 | + $rel_auto[] = $_alias; | |
79 | + } | |
75 | 80 | } else { |
76 | 81 | $rel_new[] = $_obj->name; |
77 | 82 | } |
@@ -152,11 +157,18 @@ function links_init() | ||
152 | 157 | $_obj->name == $page || $_obj->name == '') |
153 | 158 | continue; |
154 | 159 | |
155 | - $rel[] = $_obj->name; | |
156 | - if (! isset($ref[$_obj->name][$page])) | |
157 | - $ref[$_obj->name][$page] = 1; | |
160 | + $_name = $_obj->name; | |
161 | + if (is_a($_obj, 'Link_autoalias')) { | |
162 | + $_alias = get_autoaliases($_name); | |
163 | + if (! is_pagename($_alias)) | |
164 | + continue; // not PageName | |
165 | + $_name = $_alias; | |
166 | + } | |
167 | + $rel[] = $_name; | |
168 | + if (! isset($ref[$_name][$page])) | |
169 | + $ref[$_name][$page] = 1; | |
158 | 170 | if (! is_a($_obj, 'Link_autolink')) |
159 | - $ref[$_obj->name][$page] = 0; | |
171 | + $ref[$_name][$page] = 0; | |
160 | 172 | } |
161 | 173 | $rel = array_unique($rel); |
162 | 174 | if (! empty($rel)) { |
@@ -83,9 +83,11 @@ class InlineConverter | ||
83 | 83 | 'url_interwiki', // URLs (interwiki definition) |
84 | 84 | 'mailto', // mailto: URL schemes |
85 | 85 | 'interwikiname', // InterWikiNames |
86 | + 'autoalias', // AutoAlias | |
86 | 87 | 'autolink', // AutoLinks |
87 | 88 | 'bracketname', // BracketNames |
88 | 89 | 'wikiname', // WikiNames |
90 | + 'autoalias_a', // AutoAlias(alphabet) | |
89 | 91 | 'autolink_a', // AutoLinks(alphabet) |
90 | 92 | ); |
91 | 93 | } |
@@ -766,6 +768,72 @@ class Link_autolink_a extends Link_autolink | ||
766 | 768 | } |
767 | 769 | } |
768 | 770 | |
771 | +// AutoAlias | |
772 | +class Link_autoalias extends Link | |
773 | +{ | |
774 | + var $forceignorepages = array(); | |
775 | + var $auto; | |
776 | + var $auto_a; // alphabet only | |
777 | + var $alias; | |
778 | + | |
779 | + function Link_autoalias($start) | |
780 | + { | |
781 | + global $autoalias, $aliaspage; | |
782 | + | |
783 | + parent::Link($start); | |
784 | + | |
785 | + if (! $autoalias || ! file_exists(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE) || $this->page == $aliaspage) | |
786 | + { | |
787 | + return; | |
788 | + } | |
789 | + | |
790 | + @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE); | |
791 | + $this->auto = $auto; | |
792 | + $this->auto_a = $auto_a; | |
793 | + $this->forceignorepages = explode("\t", trim($forceignorepages)); | |
794 | + $this->alias = ''; | |
795 | + } | |
796 | + function get_pattern() | |
797 | + { | |
798 | + return isset($this->auto) ? '(' . $this->auto . ')' : FALSE; | |
799 | + } | |
800 | + function get_count() | |
801 | + { | |
802 | + return 1; | |
803 | + } | |
804 | + function set($arr,$page) | |
805 | + { | |
806 | + list($name) = $this->splice($arr); | |
807 | + // Ignore pages listed | |
808 | + if (in_array($name, $this->forceignorepages)) { | |
809 | + return FALSE; | |
810 | + } | |
811 | + return parent::setParam($page,$name,'','pagename',$name); | |
812 | + } | |
813 | + | |
814 | + function toString() | |
815 | + { | |
816 | + $this->alias = get_autoaliases($this->name); | |
817 | + if ($this->alias != '') { | |
818 | + $link = '[[' . $this->name . '>' . $this->alias . ']]'; | |
819 | + return make_link($link); | |
820 | + } | |
821 | + return ''; | |
822 | + } | |
823 | +} | |
824 | + | |
825 | +class Link_autoalias_a extends Link_autoalias | |
826 | +{ | |
827 | + function Link_autoalias_a($start) | |
828 | + { | |
829 | + parent::Link_autoalias($start); | |
830 | + } | |
831 | + function get_pattern() | |
832 | + { | |
833 | + return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE; | |
834 | + } | |
835 | +} | |
836 | + | |
769 | 837 | // Make hyperlink for the page |
770 | 838 | function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE) |
771 | 839 | { |
@@ -2,7 +2,7 @@ | ||
2 | 2 | // PukiWiki - Yet another WikiWikiWeb clone |
3 | 3 | // pukiwiki.ini.php |
4 | 4 | // Copyright |
5 | -// 2002-2019 PukiWiki Development Team | |
5 | +// 2002-2020 PukiWiki Development Team | |
6 | 6 | // 2001-2002 Originally written by yu-ji |
7 | 7 | // License: GPL v2 or (at your option) any later version |
8 | 8 | // |
@@ -129,6 +129,7 @@ $defaultpage = 'FrontPage'; // Top / Default page | ||
129 | 129 | $whatsnew = 'RecentChanges'; // Modified page list |
130 | 130 | $whatsdeleted = 'RecentDeleted'; // Removeed page list |
131 | 131 | $interwiki = 'InterWikiName'; // Set InterWiki definition here |
132 | +$aliaspage = 'AutoAliasName'; // Set AutoAlias definition here | |
132 | 133 | $menubar = 'MenuBar'; // Menu |
133 | 134 | |
134 | 135 | ///////////////////////////////////////////////// |
@@ -159,11 +160,22 @@ $nowikiname = 0; | ||
159 | 160 | |
160 | 161 | ///////////////////////////////////////////////// |
161 | 162 | // AutoLink feature |
163 | +// Automatic link to existing pages (especially helpful for non-wikiword pages, but heavy) | |
162 | 164 | |
163 | -// AutoLink minimum length of page name | |
165 | +// Minimum length of page name | |
164 | 166 | $autolink = 0; // Bytes, 0 = OFF (try 8) |
165 | 167 | |
166 | 168 | ///////////////////////////////////////////////// |
169 | +// AutoAlias feature | |
170 | +// Automatic link from specified word, to specifiled URI, page or InterWiki | |
171 | + | |
172 | +// Minimum length of alias "from" word | |
173 | +$autoalias = 0; // Bytes, 0 = OFF (try 8) | |
174 | + | |
175 | +// Limit loading valid alias pairs | |
176 | +$autoalias_max_words = 50; // pairs | |
177 | + | |
178 | +///////////////////////////////////////////////// | |
167 | 179 | // Enable Freeze / Unfreeze feature |
168 | 180 | $function_freeze = 1; |
169 | 181 |
@@ -0,0 +1,6 @@ | ||
1 | +*AutoAliasName [#qf9311bb] | |
2 | +AutoAlias用の定義リストです。 | |
3 | + | |
4 | +* PukiWiki [#ee87d39e] | |
5 | +-[[pukiwiki.official>https://pukiwiki.osdn.jp/]] | |
6 | +-[[pukiwiki.dev>https://pukiwiki.osdn.jp/dev/]] |