OSDN Git Service

BugTrack/2420 AutoTicketLink - Improve regex and JSON encode
[pukiwiki/pukiwiki.git] / plugin / rename.inc.php
index e97c0e7..f85e1d7 100644 (file)
@@ -1,15 +1,18 @@
 <?php
 // PukiWiki - Yet another WikiWikiWeb clone
-// $Id: rename.inc.php,v 1.38 2011/01/25 15:01:01 henoheno Exp $
-// Copyright (C) 2002-2005, 2007 PukiWiki Developers Team
+// rename.inc.php
+// 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');
 
+if (file_exists(PLUGIN_DIR . 'counter.inc.php'))
+       require_once(PLUGIN_DIR . 'counter.inc.php');
+
 function plugin_rename_action()
 {
        global $whatsnew;
@@ -22,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');
 
@@ -39,16 +42,16 @@ function plugin_rename_action()
                $page  = plugin_rename_getvar('page');
                $refer = plugin_rename_getvar('refer');
 
-               if ($refer === '') {
+               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) {
                        return plugin_rename_phase1('norename', $refer);
 
-               } else if ($page === '' || $page === $refer) {
+               } else if ($page == '' || $page === $refer) {
                        return plugin_rename_phase2();
 
                } else if (! is_pagename($page)) {
@@ -60,14 +63,14 @@ function plugin_rename_action()
        }
 }
 
-// ÊÑ¿ô¤ò¼èÆÀ¤¹¤ë
+// 変数を取得する
 function plugin_rename_getvar($key)
 {
        global $vars;
        return isset($vars[$key]) ? $vars[$key] : '';
 }
 
-// ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òºî¤ë
+// エラーメッセージを作る
 function plugin_rename_err($err, $page = '')
 {
        global $_rename_messages;
@@ -86,11 +89,12 @@ function plugin_rename_err($err, $page = '')
        return $msg;
 }
 
-//Âè°ìÃʳ¬:¥Ú¡¼¥¸Ì¾¤Þ¤¿¤ÏÀµµ¬É½¸½¤ÎÆþÎÏ
+//第一段階:ページ名または正規表現の入力
 function plugin_rename_phase1($err = '', $page = '')
 {
-       global $script, $_rename_messages;
+       global $_rename_messages;
 
+       $script = get_base_uri();
        $msg    = plugin_rename_err($err, $page);
        $refer  = plugin_rename_getvar('refer');
        $method = plugin_rename_getvar('method');
@@ -128,11 +132,12 @@ EOD;
        return $ret;
 }
 
-//ÂèÆóÃʳ¬:¿·¤·¤¤Ì¾Á°¤ÎÆþÎÏ
+//第二段階:新しい名前の入力
 function plugin_rename_phase2($err = '')
 {
-       global $script, $_rename_messages;
+       global $_rename_messages;
 
+       $script = get_base_uri();
        $msg   = plugin_rename_err($err);
        $page  = plugin_rename_getvar('page');
        $refer = plugin_rename_getvar('refer');
@@ -174,7 +179,7 @@ EOD;
        return $ret;
 }
 
-//¥Ú¡¼¥¸Ì¾¤È´ØÏ¢¤¹¤ë¥Ú¡¼¥¸¤òÎóµó¤·¡¢phase3¤Ø
+//ページ名と関連するページを列挙し、phase3へ
 function plugin_rename_refer()
 {
        $page  = plugin_rename_getvar('page');
@@ -190,7 +195,7 @@ function plugin_rename_refer()
        return plugin_rename_phase3($pages);
 }
 
-//Àµµ¬É½¸½¤Ç¥Ú¡¼¥¸¤òÃÖ´¹
+//正規表現でページを置換
 function plugin_rename_regex($arr_from, $arr_to)
 {
        $exists = array();
@@ -210,8 +215,9 @@ function plugin_rename_regex($arr_from, $arr_to)
 
 function plugin_rename_phase3($pages)
 {
-       global $script, $_rename_messages;
+       global $_rename_messages;
 
+       $script = get_base_uri();
        $msg = $input = '';
        $files = plugin_rename_get_files($pages);
 
@@ -307,18 +313,16 @@ function plugin_rename_get_files($pages)
        $matches = array();
        foreach ($dirs as $path) {
                $dir = opendir($path);
-               if (! $dir) continue;
-
-               while ($file = readdir($dir)) {
+               if (! $dir) continue;   // TODO: !== FALSE or die()?
+               while (($file = readdir($dir)) !== FALSE) {
                        if ($file == '.' || $file == '..') continue;
-
-                       foreach ($pages as $from=>$to) {
+                       foreach ($pages as $from => $to) {
+                               // TODO: preg_quote()?
                                $pattern = '/^' . str_replace('/', '\/', $from) . '([._].+)$/';
-                               if (! preg_match($pattern, $file, $matches))
-                                       continue;
-
-                               $newfile = $to . $matches[1];
-                               $files[$from][$path . $file] = $path . $newfile;
+                               if (preg_match($pattern, $file, $matches)) {
+                                       $newfile = $to . $matches[1];
+                                       $files[$from][$path . $file] = $path . $newfile;
+                               }
                        }
                }
        }
@@ -339,12 +343,18 @@ 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();
+       foreach ($pages as $old=>$new) {
+               $pages_decoded[decode($old)] = decode($new);
        }
+       plugin_counter_page_rename($pages_decoded);
 
        $postdata = get_source(PLUGIN_RENAME_LOGPAGE);
        $postdata[] = '*' . $now . "\n";
@@ -374,24 +384,27 @@ function plugin_rename_proceed($pages, $files, $exists)
                $postdata[] = '-' . decode($old) .
                        $_rename_messages['msg_arrow'] . decode($new) . "\n";
 
-       // ¹¹¿·¤Î¾×ÆͤϥÁ¥§¥Ã¥¯¤·¤Ê¤¤¡£
+       // 更新の衝突はチェックしない。
 
-       // ¥Õ¥¡¥¤¥ë¤Î½ñ¤­¹þ¤ß
+       // ファイルの書き込み
        page_write(PLUGIN_RENAME_LOGPAGE, join('', $postdata));
 
-       //¥ê¥À¥¤¥ì¥¯¥È
+       // Refresh RecentChanges / Delete cache/recent.dat
+       delete_recent_changes_cache();
+
+       //リダイレクト
        $page = plugin_rename_getvar('page');
        if ($page == '') $page = PLUGIN_RENAME_LOGPAGE;
 
        pkwk_headers_sent();
-       header('Location: ' . get_script_uri() . '?' . rawurlencode($page));
+       header('Location: ' . get_page_uri($page, PKWK_URI_ROOT));
        exit;
 }
 
 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;
@@ -405,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' : '';
@@ -424,4 +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);
+       }
+}