Commit MetaInfo

Revisão77932ee3ad27a150d07b20c2771ce888856fb1ce (tree)
Hora2020-02-15 22:16:13
Autorteanan <teanan>
Commiterumorigu

Mensagem de Log

BugTrack/2002 AutoAlias: Link defined keywords in AutoAliasName

Link defined keywords in AutoAliasName page

Mudança Sumário

Diff

--- a/lib/file.php
+++ b/lib/file.php
@@ -2,7 +2,7 @@
22 // PukiWiki - Yet another WikiWikiWeb clone.
33 // file.php
44 // Copyright
5-// 2002-2017 PukiWiki Development Team
5+// 2002-2020 PukiWiki Development Team
66 // 2001-2002 Originally written by yu-ji
77 // License: GPL v2 or (at your option) any later version
88 //
@@ -15,6 +15,9 @@ define('PKWK_MAXSHOW_CACHE', 'recent.dat');
1515 // AutoLink
1616 define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat');
1717
18+// AutoAlias
19+define('PKWK_AUTOALIAS_REGEX_CACHE', 'autoalias.dat');
20+
1821 /**
1922 * Get source(wiki text) data of the page
2023 *
@@ -104,6 +107,8 @@ function get_filename($page)
104107 // Put a data(wiki text) into a physical file(diff, backup, text)
105108 function page_write($page, $postdata, $notimestamp = FALSE)
106109 {
110+ global $autoalias, $aliaspage;
111+
107112 if (PKWK_READONLY) return; // Do nothing
108113
109114 $postdata = make_str_rules($postdata);
@@ -133,6 +138,11 @@ function page_write($page, $postdata, $notimestamp = FALSE)
133138 file_write(DATA_DIR, $page, $postdata, $notimestamp, $is_delete);
134139
135140 links_update($page);
141+
142+ // Update autoalias.dat (AutoAliasName)
143+ if ($autoalias && $page === $aliaspage) {
144+ update_autoalias_cache_file();
145+ }
136146 }
137147
138148 // Modify original text with user-defined / system-defined rules
@@ -603,22 +613,8 @@ function put_lastmodified()
603613
604614 // For AutoLink
605615 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);
616+ autolink_pattern_write(CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE,
617+ get_autolink_pattern($pages, $autolink));
622618 }
623619 }
624620
@@ -648,6 +644,38 @@ function delete_recent_changes_cache() {
648644 unlink($file);
649645 }
650646
647+// update autolink data
648+function autolink_pattern_write($filename, $autolink_pattern)
649+{
650+ list($pattern, $pattern_a, $forceignorelist) = $autolink_pattern;
651+
652+ $fp = fopen($filename, 'w') or
653+ die_message('Cannot open ' . $filename);
654+ set_file_buffer($fp, 0);
655+ flock($fp, LOCK_EX);
656+ rewind($fp);
657+ fputs($fp, $pattern . "\n");
658+ fputs($fp, $pattern_a . "\n");
659+ fputs($fp, join("\t", $forceignorelist) . "\n");
660+ flock($fp, LOCK_UN);
661+ fclose($fp);
662+}
663+
664+// Update AutoAlias regex cache
665+function update_autoalias_cache_file()
666+{
667+ global $autoalias; // Disable (0), Enable (min-length)
668+ $aliases = get_autoaliases();
669+ if (empty($aliases)) {
670+ // Remove
671+ @unlink(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE);
672+ } else {
673+ // Create or Update
674+ autolink_pattern_write(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE,
675+ get_autolink_pattern(array_keys($aliases), $autoalias));
676+ }
677+}
678+
651679 // Get elapsed date of the page
652680 function get_pg_passage($page, $sw = TRUE)
653681 {
--- a/lib/func.php
+++ b/lib/func.php
@@ -2,7 +2,7 @@
22 // PukiWiki - Yet another WikiWikiWeb clone.
33 // func.php
44 // Copyright
5-// 2002-2019 PukiWiki Development Team
5+// 2002-2020 PukiWiki Development Team
66 // 2001-2002 Originally written by yu-ji
77 // License: GPL v2 or (at your option) any later version
88 //
@@ -723,9 +723,9 @@ function drop_submit($str)
723723 }
724724
725725 // Generate AutoLink patterns (thx to hirofummy)
726-function get_autolink_pattern(& $pages)
726+function get_autolink_pattern($pages, $min_length)
727727 {
728- global $WikiName, $autolink, $nowikiname;
728+ global $WikiName, $nowikiname;
729729
730730 $config = new Config('AutoLink');
731731 $config->read();
@@ -734,11 +734,12 @@ function get_autolink_pattern(& $pages)
734734 unset($config);
735735 $auto_pages = array_merge($ignorepages, $forceignorepages);
736736
737- foreach ($pages as $page)
737+ foreach ($pages as $page) {
738738 if (preg_match('/^' . $WikiName . '$/', $page) ?
739- $nowikiname : strlen($page) >= $autolink)
739+ $nowikiname : strlen($page) >= $min_length) {
740740 $auto_pages[] = $page;
741-
741+ }
742+ }
742743 if (empty($auto_pages)) {
743744 $result = $result_a = '(?!)';
744745 } else {
@@ -754,7 +755,7 @@ function get_autolink_pattern(& $pages)
754755 return array($result, $result_a, $forceignorepages);
755756 }
756757
757-function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
758+function get_autolink_pattern_sub($pages, $start, $end, $pos)
758759 {
759760 if ($end == 0) return '(?!)';
760761
@@ -783,6 +784,50 @@ function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
783784 return $result;
784785 }
785786
787+// Get AutoAlias value
788+function get_autoalias_right_link($alias_name)
789+{
790+ $pairs = get_autoaliases();
791+ // A string: Seek the pair
792+ if (isset($pairs[$alias_name])) {
793+ return $pairs[$alias_name];
794+ }
795+ return '';
796+}
797+
798+// Load setting pairs from AutoAliasName
799+function get_autoaliases()
800+{
801+ global $aliaspage, $autoalias_max_words;
802+ static $pairs;
803+
804+ if (! isset($pairs)) {
805+ $pairs = array();
806+ $pattern = <<<EOD
807+\[\[ # open bracket
808+((?:(?!\]\]).)+)> # (1) alias name
809+((?:(?!\]\]).)+) # (2) alias link
810+\]\] # close bracket
811+EOD;
812+ $postdata = join('', get_source($aliaspage));
813+ $matches = array();
814+ $count = 0;
815+ $max = max($autoalias_max_words, 0);
816+ if (preg_match_all('/' . $pattern . '/x', $postdata, $matches, PREG_SET_ORDER)) {
817+ foreach($matches as $key => $value) {
818+ if ($count == $max) break;
819+ $name = trim($value[1]);
820+ if (! isset($pairs[$name])) {
821+ ++$count;
822+ $pairs[$name] = trim($value[2]);
823+ }
824+ unset($matches[$key]);
825+ }
826+ }
827+ }
828+ return $pairs;
829+}
830+
786831 /**
787832 * Get propery URI of this script
788833 *
--- a/lib/link.php
+++ b/lib/link.php
@@ -1,7 +1,7 @@
11 <?php
22 // 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
55 // License: GPL v2 or (at your option) any later version
66 //
77 // Backlinks / AutoLinks related functions
@@ -72,6 +72,11 @@ function links_update($page)
7272
7373 if (is_a($_obj, 'Link_autolink')) { // 行儀が悪い
7474 $rel_auto[] = $_obj->name;
75+ } else if (is_a($_obj, 'Link_autoalias')) {
76+ $_alias = get_autoalias_right_link($_obj->name);
77+ if (is_pagename($_alias)) {
78+ $rel_auto[] = $_alias;
79+ }
7580 } else {
7681 $rel_new[] = $_obj->name;
7782 }
@@ -149,14 +154,24 @@ function links_init()
149154 $links = links_get_objects($page);
150155 foreach ($links as $_obj) {
151156 if (! isset($_obj->type) || $_obj->type != 'pagename' ||
152- $_obj->name == $page || $_obj->name == '')
157+ $_obj->name == $page || $_obj->name == '') {
153158 continue;
154-
155- $rel[] = $_obj->name;
156- if (! isset($ref[$_obj->name][$page]))
157- $ref[$_obj->name][$page] = 1;
158- if (! is_a($_obj, 'Link_autolink'))
159- $ref[$_obj->name][$page] = 0;
159+ }
160+ $_name = $_obj->name;
161+ if (is_a($_obj, 'Link_autoalias')) {
162+ $_alias = get_autoalias_right_link($_name);
163+ if (! is_pagename($_alias)) {
164+ continue; // not PageName
165+ }
166+ $_name = $_alias;
167+ }
168+ $rel[] = $_name;
169+ if (! isset($ref[$_name][$page])) {
170+ $ref[$_name][$page] = 1;
171+ }
172+ if (! is_a($_obj, 'Link_autolink')) {
173+ $ref[$_name][$page] = 0;
174+ }
160175 }
161176 $rel = array_unique($rel);
162177 if (! empty($rel)) {
--- a/lib/make_link.php
+++ b/lib/make_link.php
@@ -83,9 +83,11 @@ class InlineConverter
8383 'url_interwiki', // URLs (interwiki definition)
8484 'mailto', // mailto: URL schemes
8585 'interwikiname', // InterWikiNames
86+ 'autoalias', // AutoAlias
8687 'autolink', // AutoLinks
8788 'bracketname', // BracketNames
8889 'wikiname', // WikiNames
90+ 'autoalias_a', // AutoAlias(alphabet)
8991 'autolink_a', // AutoLinks(alphabet)
9092 );
9193 }
@@ -766,6 +768,72 @@ class Link_autolink_a extends Link_autolink
766768 }
767769 }
768770
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_autoalias_right_link($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+
769837 // Make hyperlink for the page
770838 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
771839 {
@@ -995,3 +1063,25 @@ Reference: https://pukiwiki.osdn.jp/?AutoTicketLink
9951063 EOS;
9961064 page_write($autoticketlink_def_page, $body);
9971065 }
1066+
1067+function init_autoalias_def_page()
1068+{
1069+ global $aliaspage; // 'AutoAliasName'
1070+ $autoticketlink_def_page = get_autoticketlink_def_page();
1071+ if (is_page($aliaspage)) {
1072+ return;
1073+ }
1074+ $body = <<<EOS
1075+#freeze
1076+*AutoAliasName [#qf9311bb]
1077+AutoAlias definition
1078+
1079+Reference: https://pukiwiki.osdn.jp/?AutoAlias
1080+
1081+* PukiWiki [#ee87d39e]
1082+-[[pukiwiki.official>https://pukiwiki.osdn.jp/]]
1083+-[[pukiwiki.dev>https://pukiwiki.osdn.jp/dev/]]
1084+EOS;
1085+ page_write($aliaspage, $body);
1086+ update_autoalias_cache_file();
1087+}
--- a/plugin/edit.inc.php
+++ b/plugin/edit.inc.php
@@ -275,10 +275,16 @@ function plugin_edit_cancel()
275275 */
276276 function plugin_edit_setup_initial_pages()
277277 {
278+ global $autoalias;
279+
278280 // Related: Rename plugin
279281 if (exist_plugin('rename') && function_exists('plugin_rename_setup_initial_pages')) {
280282 plugin_rename_setup_initial_pages();
281283 }
282284 // AutoTicketLinkName page
283285 init_autoticketlink_def_page();
286+ // AutoAliasName page
287+ if ($autoalias) {
288+ init_autoalias_def_page();
289+ }
284290 }
--- a/pukiwiki.ini.php
+++ b/pukiwiki.ini.php
@@ -2,7 +2,7 @@
22 // PukiWiki - Yet another WikiWikiWeb clone
33 // pukiwiki.ini.php
44 // Copyright
5-// 2002-2019 PukiWiki Development Team
5+// 2002-2020 PukiWiki Development Team
66 // 2001-2002 Originally written by yu-ji
77 // License: GPL v2 or (at your option) any later version
88 //
@@ -129,6 +129,7 @@ $defaultpage = 'FrontPage'; // Top / Default page
129129 $whatsnew = 'RecentChanges'; // Modified page list
130130 $whatsdeleted = 'RecentDeleted'; // Removeed page list
131131 $interwiki = 'InterWikiName'; // Set InterWiki definition here
132+$aliaspage = 'AutoAliasName'; // Set AutoAlias definition here
132133 $menubar = 'MenuBar'; // Menu
133134
134135 /////////////////////////////////////////////////
@@ -159,11 +160,22 @@ $nowikiname = 0;
159160
160161 /////////////////////////////////////////////////
161162 // AutoLink feature
163+// Automatic link to existing pages
162164
163165 // AutoLink minimum length of page name
164166 $autolink = 0; // Bytes, 0 = OFF (try 8)
165167
166168 /////////////////////////////////////////////////
169+// AutoAlias feature
170+// Automatic link from specified word, to specifiled URI, page or InterWiki
171+
172+// AutoAlias 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+/////////////////////////////////////////////////
167179 // Enable Freeze / Unfreeze feature
168180 $function_freeze = 1;
169181
Show on old repository browser