OSDN Git Service

BugTrack/2420 AutoTicketLink - Improve regex and JSON encode
[pukiwiki/pukiwiki.git] / plugin / rename.inc.php
index c396e1b..f85e1d7 100644 (file)
@@ -1,12 +1,12 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
 // rename.inc.php
-// Copyright 2002-2017 PukiWiki Development Team
+// Copyright 2002-2018 PukiWiki Development Team
 // License: GPL v2 or (at your option) any later version
 //
 // Rename plugin: Rename page-name and related data
 //
-// Usage: http://path/to/pukiwikiphp?plugin=rename[&refer=page_name]
+// Usage: http://path/to/index.php?plugin=rename[&refer=page_name]
 
 define('PLUGIN_RENAME_LOGPAGE', ':RenameLog');
 
@@ -25,7 +25,7 @@ function plugin_rename_action()
                if ($src == '') return plugin_rename_phase1();
 
                $src_pattern = '/' . preg_quote($src, '/') . '/';
-               $arr0 = preg_grep($src_pattern, get_existpages());
+               $arr0 = preg_grep($src_pattern, plugin_rename_get_existpages());
                if (! is_array($arr0) || empty($arr0))
                        return plugin_rename_phase1('nomatch');
 
@@ -45,7 +45,7 @@ function plugin_rename_action()
                if ($refer == '') {
                        return plugin_rename_phase1();
 
-               } else if (! is_page($refer)) {
+               } else if (! plugin_rename_is_page($refer)) {
                        return plugin_rename_phase1('notpage', $refer);
 
                } else if ($refer === $whatsnew) {
@@ -326,7 +326,6 @@ function plugin_rename_get_files($pages)
                        }
                }
        }
-
        return $files;
 }
 
@@ -344,11 +343,11 @@ function plugin_rename_proceed($pages, $files, $exists)
                        if (isset($exists[$page][$old]) && $exists[$page][$old])
                                unlink($new);
                        rename($old, $new);
-
-                       // linkデータベースを更新する BugTrack/327 arino
-                       links_update($old);
-                       links_update($new);
                }
+               // linkデータベースを更新する BugTrack/327 arino
+               $new_page = $pages[$page];
+               links_update(decode($page));
+               links_update(decode($new_page));
        }
        // Rename counter
        $pages_decoded = array();
@@ -405,7 +404,7 @@ function plugin_rename_proceed($pages, $files, $exists)
 function plugin_rename_getrelated($page)
 {
        $related = array();
-       $pages = get_existpages();
+       $pages = plugin_rename_get_existpages();
        $pattern = '/(?:^|\/)' . preg_quote(strip_bracket($page), '/') . '(?:\/|$)/';
        foreach ($pages as $name) {
                if ($name === $page) continue;
@@ -419,7 +418,7 @@ function plugin_rename_getselecttag($page)
        global $whatsnew;
 
        $pages = array();
-       foreach (get_existpages() as $_page) {
+       foreach (plugin_rename_get_existpages() as $_page) {
                if ($_page === $whatsnew) continue;
 
                $selected = ($_page === $page) ? ' selected' : '';
@@ -438,3 +437,51 @@ function plugin_rename_getselecttag($page)
 EOD;
 
 }
+
+/**
+ * List exist pages and deleted pages
+ */
+function plugin_rename_get_existpages() {
+       $list1 = array_values(get_existpages());
+       $list2 = array_values(get_existpages(DIFF_DIR, '.txt'));
+       $list3 = array_values(get_existpages(BACKUP_DIR, '.txt'));
+       $list4 = array_values(get_existpages(BACKUP_DIR, '.gz'));
+       $list5 = array_values(get_existpages(COUNTER_DIR, '.count'));
+       $wholelist = array_merge($list1, $list2, $list3, $list4, $list5);
+       $list = array_unique($wholelist);
+       return $list;
+}
+
+/**
+ * Return where the page exists or existed
+ */
+function plugin_rename_is_page($page) {
+       $enc = encode($page);
+       if (is_page($page)) {
+               return true;
+       }
+       if (file_exists(DIFF_DIR . $enc . '.txt')) {
+               return true;
+       }
+       if (file_exists(BACKUP_DIR . $enc . '.txt')) {
+               return true;
+       }
+       if (file_exists(BACKUP_DIR . $enc . '.gz')) {
+               return true;
+       }
+       if (file_exists(COUNTER_DIR . $enc . '.count')) {
+               return true;
+       }
+       return false;
+}
+
+/**
+ * Setup initial pages (:RenameLog)
+ */
+function plugin_rename_setup_initial_pages() {
+       if (!is_page(PLUGIN_RENAME_LOGPAGE)) {
+               // Create :RenameLog
+               $body = "#freeze\n// :RenameLog (rename plugin)\n";
+               page_write(PLUGIN_RENAME_LOGPAGE, $body);
+       }
+}