OSDN Git Service

BugTrack/2002 AutoAlias: Link defined keywords in AutoAliasName
authorteanan <teanan>
Tue, 8 Aug 2006 18:11:00 +0000 (03:11 +0900)
committerumorigu <umorigu@gmail.com>
Sat, 15 Feb 2020 13:16:13 +0000 (22:16 +0900)
Link defined keywords in AutoAliasName page

lib/file.php
lib/func.php
lib/link.php
lib/make_link.php
plugin/edit.inc.php
pukiwiki.ini.php

index fd3d309..9d9a29e 100644 (file)
@@ -2,7 +2,7 @@
 // PukiWiki - Yet another WikiWikiWeb clone.
 // file.php
 // Copyright
-//   2002-2017 PukiWiki Development Team
+//   2002-2020 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -15,6 +15,9 @@ define('PKWK_MAXSHOW_CACHE', 'recent.dat');
 // AutoLink
 define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat');
 
+// AutoAlias
+define('PKWK_AUTOALIAS_REGEX_CACHE', 'autoalias.dat');
+
 /**
  * Get source(wiki text) data of the page
  *
@@ -104,6 +107,8 @@ function get_filename($page)
 // Put a data(wiki text) into a physical file(diff, backup, text)
 function page_write($page, $postdata, $notimestamp = FALSE)
 {
+       global $autoalias, $aliaspage;
+
        if (PKWK_READONLY) return; // Do nothing
 
        $postdata = make_str_rules($postdata);
@@ -133,6 +138,11 @@ function page_write($page, $postdata, $notimestamp = FALSE)
        file_write(DATA_DIR, $page, $postdata, $notimestamp, $is_delete);
 
        links_update($page);
+
+       // Update autoalias.dat (AutoAliasName)
+       if ($autoalias && $page === $aliaspage) {
+               update_autoalias_cache_file();
+       }
 }
 
 // Modify original text with user-defined / system-defined rules
@@ -603,22 +613,8 @@ function put_lastmodified()
 
        // For AutoLink
        if ($autolink) {
-               list($pattern, $pattern_a, $forceignorelist) =
-                       get_autolink_pattern($pages);
-
-               $file = CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE;
-               pkwk_touch_file($file);
-               $fp = fopen($file, 'r+') or
-                       die_message('Cannot open ' . 'CACHE_DIR/' . PKWK_AUTOLINK_REGEX_CACHE);
-               set_file_buffer($fp, 0);
-               flock($fp, LOCK_EX);
-               ftruncate($fp, 0);
-               rewind($fp);
-               fputs($fp, $pattern   . "\n");
-               fputs($fp, $pattern_a . "\n");
-               fputs($fp, join("\t", $forceignorelist) . "\n");
-               flock($fp, LOCK_UN);
-               fclose($fp);
+               autolink_pattern_write(CACHE_DIR . PKWK_AUTOLINK_REGEX_CACHE,
+                       get_autolink_pattern($pages, $autolink));
        }
 }
 
@@ -648,6 +644,38 @@ function delete_recent_changes_cache() {
        unlink($file);
 }
 
+// update autolink data
+function autolink_pattern_write($filename, $autolink_pattern)
+{
+       list($pattern, $pattern_a, $forceignorelist) = $autolink_pattern;
+
+       $fp = fopen($filename, 'w') or
+               die_message('Cannot open ' . $filename);
+       set_file_buffer($fp, 0);
+       flock($fp, LOCK_EX);
+       rewind($fp);
+       fputs($fp, $pattern   . "\n");
+       fputs($fp, $pattern_a . "\n");
+       fputs($fp, join("\t", $forceignorelist) . "\n");
+       flock($fp, LOCK_UN);
+       fclose($fp);
+}
+
+// Update AutoAlias regex cache
+function update_autoalias_cache_file()
+{
+       global $autoalias; // Disable (0), Enable (min-length)
+       $aliases = get_autoaliases();
+       if (empty($aliases)) {
+               // Remove
+               @unlink(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE);
+       } else {
+               // Create or Update
+               autolink_pattern_write(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE,
+                       get_autolink_pattern(array_keys($aliases), $autoalias));
+       }
+}
+
 // Get elapsed date of the page
 function get_pg_passage($page, $sw = TRUE)
 {
index 4650529..1d50316 100644 (file)
@@ -2,7 +2,7 @@
 // PukiWiki - Yet another WikiWikiWeb clone.
 // func.php
 // Copyright
-//   2002-2019 PukiWiki Development Team
+//   2002-2020 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -723,9 +723,9 @@ function drop_submit($str)
 }
 
 // Generate AutoLink patterns (thx to hirofummy)
-function get_autolink_pattern(& $pages)
+function get_autolink_pattern($pages, $min_length)
 {
-       global $WikiName, $autolink, $nowikiname;
+       global $WikiName, $nowikiname;
 
        $config = new Config('AutoLink');
        $config->read();
@@ -734,11 +734,12 @@ function get_autolink_pattern(& $pages)
        unset($config);
        $auto_pages = array_merge($ignorepages, $forceignorepages);
 
-       foreach ($pages as $page)
+       foreach ($pages as $page) {
                if (preg_match('/^' . $WikiName . '$/', $page) ?
-                   $nowikiname : strlen($page) >= $autolink)
+                   $nowikiname : strlen($page) >= $min_length) {
                        $auto_pages[] = $page;
-
+               }
+       }
        if (empty($auto_pages)) {
                $result = $result_a = '(?!)';
        } else {
@@ -754,7 +755,7 @@ function get_autolink_pattern(& $pages)
        return array($result, $result_a, $forceignorepages);
 }
 
-function get_autolink_pattern_sub($pages, $start, $end, $pos)
+function get_autolink_pattern_sub($pages, $start, $end, $pos)
 {
        if ($end == 0) return '(?!)';
 
@@ -783,6 +784,50 @@ function get_autolink_pattern_sub(& $pages, $start, $end, $pos)
        return $result;
 }
 
+// Get AutoAlias value
+function get_autoalias_right_link($alias_name)
+{
+       $pairs = get_autoaliases();
+       // A string: Seek the pair
+       if (isset($pairs[$alias_name])) {
+               return $pairs[$alias_name];
+       }
+       return '';
+}
+
+// Load setting pairs from AutoAliasName
+function get_autoaliases()
+{
+       global $aliaspage, $autoalias_max_words;
+       static $pairs;
+
+       if (! isset($pairs)) {
+               $pairs = array();
+               $pattern = <<<EOD
+\[\[                # open bracket
+((?:(?!\]\]).)+)>   # (1) alias name
+((?:(?!\]\]).)+)    # (2) alias link
+\]\]                # close bracket
+EOD;
+               $postdata = join('', get_source($aliaspage));
+               $matches  = array();
+               $count = 0;
+               $max   = max($autoalias_max_words, 0);
+               if (preg_match_all('/' . $pattern . '/x', $postdata, $matches, PREG_SET_ORDER)) {
+                       foreach($matches as $key => $value) {
+                               if ($count ==  $max) break;
+                               $name = trim($value[1]);
+                               if (! isset($pairs[$name])) {
+                                       ++$count;
+                                        $pairs[$name] = trim($value[2]);
+                               }
+                               unset($matches[$key]);
+                       }
+               }
+       }
+       return $pairs;
+}
+
 /**
  * Get propery URI of this script
  *
index 67ccd9f..6a5d568 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
-// $Id: link.php,v 1.20 2011/01/25 15:01:01 henoheno Exp $
-// Copyright (C) 2003-2007 PukiWiki Developers Team
+// link.php
+// Copyright 2003-2020 PukiWiki Development Team
 // License: GPL v2 or (at your option) any later version
 //
 // Backlinks / AutoLinks related functions
@@ -72,6 +72,11 @@ function links_update($page)
 
                if (is_a($_obj, 'Link_autolink')) { // 行儀が悪い
                        $rel_auto[] = $_obj->name;
+               } else if (is_a($_obj, 'Link_autoalias')) {
+                       $_alias = get_autoalias_right_link($_obj->name);
+                       if (is_pagename($_alias)) {
+                               $rel_auto[] = $_alias;
+                       }
                } else {
                        $rel_new[]  = $_obj->name;
                }
@@ -149,14 +154,24 @@ function links_init()
                $links = links_get_objects($page);
                foreach ($links as $_obj) {
                        if (! isset($_obj->type) || $_obj->type != 'pagename' ||
-                           $_obj->name == $page || $_obj->name == '')
+                           $_obj->name == $page || $_obj->name == '') {
                                continue;
-
-                       $rel[] = $_obj->name;
-                       if (! isset($ref[$_obj->name][$page]))
-                               $ref[$_obj->name][$page] = 1;
-                       if (! is_a($_obj, 'Link_autolink'))
-                               $ref[$_obj->name][$page] = 0;
+                       }
+                       $_name = $_obj->name;
+                       if (is_a($_obj, 'Link_autoalias')) {
+                               $_alias = get_autoalias_right_link($_name);
+                               if (! is_pagename($_alias)) {
+                                       continue;       // not PageName
+                               }
+                               $_name = $_alias;
+                       }
+                       $rel[] = $_name;
+                       if (! isset($ref[$_name][$page])) {
+                               $ref[$_name][$page] = 1;
+                       }
+                       if (! is_a($_obj, 'Link_autolink')) {
+                               $ref[$_name][$page] = 0;
+                       }
                }
                $rel = array_unique($rel);
                if (! empty($rel)) {
index be1b0c7..6ac9a96 100644 (file)
@@ -83,9 +83,11 @@ class InlineConverter
                                'url_interwiki', // URLs (interwiki definition)
                                'mailto',        // mailto: URL schemes
                                'interwikiname', // InterWikiNames
+                               'autoalias',     // AutoAlias
                                'autolink',      // AutoLinks
                                'bracketname',   // BracketNames
                                'wikiname',      // WikiNames
+                               'autoalias_a',   // AutoAlias(alphabet)
                                'autolink_a',    // AutoLinks(alphabet)
                        );
                }
@@ -766,6 +768,72 @@ class Link_autolink_a extends Link_autolink
        }
 }
 
+// AutoAlias
+class Link_autoalias extends Link
+{
+       var $forceignorepages = array();
+       var $auto;
+       var $auto_a; // alphabet only
+       var $alias;
+
+       function Link_autoalias($start)
+       {
+               global $autoalias, $aliaspage;
+
+               parent::Link($start);
+
+               if (! $autoalias || ! file_exists(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE) || $this->page == $aliaspage)
+               {
+                       return;
+               }
+
+               @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . PKWK_AUTOALIAS_REGEX_CACHE);
+               $this->auto = $auto;
+               $this->auto_a = $auto_a;
+               $this->forceignorepages = explode("\t", trim($forceignorepages));
+               $this->alias = '';
+       }
+       function get_pattern()
+       {
+               return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
+       }
+       function get_count()
+       {
+               return 1;
+       }
+       function set($arr,$page)
+       {
+               list($name) = $this->splice($arr);
+               // Ignore pages listed
+               if (in_array($name, $this->forceignorepages)) {
+                       return FALSE;
+               }
+               return parent::setParam($page,$name,'','pagename',$name);
+       }
+
+       function toString()
+       {
+               $this->alias = get_autoalias_right_link($this->name);
+               if ($this->alias != '') {
+                       $link = '[[' . $this->name . '>' . $this->alias . ']]';
+                       return make_link($link);
+               }
+               return '';
+       }
+}
+
+class Link_autoalias_a extends Link_autoalias
+{
+       function Link_autoalias_a($start)
+       {
+               parent::Link_autoalias($start);
+       }
+       function get_pattern()
+       {
+               return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
+       }
+}
+
 // Make hyperlink for the page
 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
 {
@@ -995,3 +1063,25 @@ Reference: https://pukiwiki.osdn.jp/?AutoTicketLink
 EOS;
        page_write($autoticketlink_def_page, $body);
 }
+
+function init_autoalias_def_page()
+{
+       global $aliaspage; // 'AutoAliasName'
+       $autoticketlink_def_page = get_autoticketlink_def_page();
+       if (is_page($aliaspage)) {
+               return;
+       }
+       $body = <<<EOS
+#freeze
+*AutoAliasName [#qf9311bb]
+AutoAlias definition
+
+Reference: https://pukiwiki.osdn.jp/?AutoAlias
+
+* PukiWiki [#ee87d39e]
+-[[pukiwiki.official>https://pukiwiki.osdn.jp/]]
+-[[pukiwiki.dev>https://pukiwiki.osdn.jp/dev/]]
+EOS;
+       page_write($aliaspage, $body);
+       update_autoalias_cache_file();
+}
index 1ebfb79..732db5b 100644 (file)
@@ -275,10 +275,16 @@ function plugin_edit_cancel()
  */
 function plugin_edit_setup_initial_pages()
 {
+       global $autoalias;
+
        // Related: Rename plugin
        if (exist_plugin('rename') && function_exists('plugin_rename_setup_initial_pages')) {
                plugin_rename_setup_initial_pages();
        }
        // AutoTicketLinkName page
        init_autoticketlink_def_page();
+       // AutoAliasName page
+       if ($autoalias) {
+               init_autoalias_def_page();
+       }
 }
index 9dda985..6714b03 100644 (file)
@@ -2,7 +2,7 @@
 // PukiWiki - Yet another WikiWikiWeb clone
 // pukiwiki.ini.php
 // Copyright
-//   2002-2019 PukiWiki Development Team
+//   2002-2020 PukiWiki Development Team
 //   2001-2002 Originally written by yu-ji
 // License: GPL v2 or (at your option) any later version
 //
@@ -129,6 +129,7 @@ $defaultpage  = 'FrontPage';     // Top / Default page
 $whatsnew     = 'RecentChanges'; // Modified page list
 $whatsdeleted = 'RecentDeleted'; // Removeed page list
 $interwiki    = 'InterWikiName'; // Set InterWiki definition here
+$aliaspage    = 'AutoAliasName'; // Set AutoAlias definition here
 $menubar      = 'MenuBar';       // Menu
 
 /////////////////////////////////////////////////
@@ -159,11 +160,22 @@ $nowikiname = 0;
 
 /////////////////////////////////////////////////
 // AutoLink feature
+// Automatic link to existing pages
 
 // AutoLink minimum length of page name
 $autolink = 0; // Bytes, 0 = OFF (try 8)
 
 /////////////////////////////////////////////////
+// AutoAlias feature
+// Automatic link from specified word, to specifiled URI, page or InterWiki
+
+// AutoAlias minimum length of alias "from" word
+$autoalias = 0; // Bytes, 0 = OFF (try 8)
+
+// Limit loading valid alias pairs
+$autoalias_max_words = 50; // pairs
+
+/////////////////////////////////////////////////
 // Enable Freeze / Unfreeze feature
 $function_freeze = 1;