OSDN Git Service

BugTrack/2557 Fix touchgraph plugin Moji-bake
[pukiwiki/pukiwiki.git] / plugin / links.inc.php
index 72d4286..b2c824c 100644 (file)
 <?php
-/////////////////////////////////////////////////
-// PukiWiki - Yet another WikiWikiWeb clone.
-//
-// $Id: links.inc.php,v 1.13 2003/03/10 12:35:19 panda Exp $
+// PukiWiki - Yet another WikiWikiWeb clone
+// links.inc.php
+// Copyright 2003-2017 PukiWiki Development Team
+// License: GPL v2 or (at your option) any later version
 //
+// Update link cache plugin
 
-function plugin_links_action()
-{
-       global $vars,$adminpass,$whatsnew;
-       
-       set_time_limit(0);
-       
-       if ($vars['page'] != '' and $vars['page'] != $whatsnew)
-       {
-               plugin_links_updatedata($vars['page']);
-               return;
-       }
-       if (md5($vars['adminpass']) != $adminpass)
-       {
-               return array('msg'=>'update database',
-                       'body'=>'<p>administrator password require.</p>');
-       }
-       return plugin_links_initdata();
-}
-function &plugin_links_get_objects($page)
+// Message setting
+function plugin_links_init()
 {
-       static $obj;
-       
-       if (!isset($obj))
-       {
-               $obj = new InlineConverter(array('url','mailto','interwiki','page','auto'));
-       }
-       
-       return $obj->get_objects(join('',preg_grep('/^(?!\/\/|\s)./',get_source($page))),$page);
-}
-function plugin_links_initdata()
-{
-       global $whatsnew;
-       
-       if (defined('LINK_DB'))
-       {
-               // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î½é´ü²½
-               $pages = get_existpages();
-               db_exec('DELETE FROM page;');
-               db_exec('DELETE FROM link;');
-               foreach ($pages as $page)
-               {
-                       if ($page == $whatsnew)
-                       {
-                               continue;
-                       }
-                       $time = get_filetime($page);
-                       $a_page = addslashes($page);
-                       db_exec("INSERT INTO page (name,lastmod) VALUES ('$a_page',$time);");
-               }
-               $rows = db_query('SELECT id,name FROM page;');
-               $pages = array();
-               foreach ($rows as $row)
-               {
-                       $pages[$row['name']] = $row['id'];
-               }
-               
-               foreach ($pages as $page=>$id)
-               {
-                       $links = plugin_links_get_objects($page);
-                       foreach ($links as $_obj)
-                       {
-                               if ($_obj->type != 'pagename')
-                               {
-                                       continue;
-                               }
-                               $_page = $_obj->name;
-                               if (!array_key_exists($_page,$pages))
-                               {
-                                       $a_page = addslashes($_page);
-                                       db_exec("INSERT INTO page (name) VALUES ('$a_page');");
-                                       $rows = db_query("SELECT id,name FROM page WHERE name='$a_page';");
-                                       $row = $rows[0];
-                                       $pages[$row['name']] = $row['id'];
-                               }       
-                                       
-                               $ref_id = $pages[$_page];
-                               if ($ref_id and $ref_id != $id)
-                               {
-                                       db_exec("INSERT INTO link (page_id,ref_id) VALUES ($id,$ref_id);");
-                               }
-                       }
-               }
-       }
-       else // if (!defined('LINK_DB'))
-       {
-               // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î½é´ü²½
-               foreach (get_existfiles(CACHE_DIR,'.ref') as $cache)
-               {
-                       unlink($cache);
-               }
-               foreach (get_existfiles(CACHE_DIR,'.rel') as $cache)
-               {
-                       unlink($cache);
-               }
-               $pages = get_existpages();
-               $ref = array(); // »²¾È¸µ
-               foreach ($pages as $page)
-               {
-                       if ($page == $whatsnew)
-                       {
-                               continue;
-                       }
-                       $time = get_filetime($page);
-                       $rel = array(); // »²¾ÈÀè
-                       $links = plugin_links_get_objects($page);
-                       foreach ($links as $_obj)
-                       {
-                               if (!isset($_obj->type) or $_obj->type != 'pagename')
-                               {
-                                       continue;
-                               }                       
-                               $_page = $_obj->name;
-                               if ($_page != $page)
-                               {
-                                       $rel[$_page] = 1;
-                                       $ref[$_page][$page] = $time;
-                               }
-                       }
-                       if (count($rel))
-                       {
-                               $fp = fopen(CACHE_DIR.encode($page).'.rel','w')
-                                       or die_message('cannot write '.htmlspecialchars(CACHE_DIR.encode($page).'.rel'));
-                               fputs($fp,join("\t",array_keys($rel)));
-                               fclose($fp);
-                       }
-               }
-               
-               foreach ($ref as $page=>$arr)
-               {
-                       if (count($arr) == 0)
-                       {
-                               continue;
-                       }
-                       $fp = fopen(CACHE_DIR.encode($page).'.ref','w')
-                               or die_message('cannot write '.htmlspecialchars(CACHE_DIR.encode($page).'.ref'));
-                       foreach ($arr as $_page=>$time)
-                       {
-                               fputs($fp,"$_page\t$time\n");
-                       }
-                       fclose($fp);
-               }
-       }
-       return array('msg'=>'update database','body'=>'<p>done.</p>');
+       $messages = array(
+               '_links_messages'=>array(
+                       'title_update'  => 'キャッシュ更新',
+                       'msg_adminpass' => '管理者パスワード',
+                       'btn_submit'    => '実行',
+                       'msg_done'      => 'キャッシュの更新が完了しました。',
+                       'msg_usage'     => "
+* 処理内容
+
+:キャッシュを更新|
+全てのページをスキャンし、あるページがどのページからリンクされているかを調査して、キャッシュに記録します。
+
+* 注意
+実行には数分かかる場合もあります。実行ボタンを押したあと、しばらくお待ちください。
+
+* 実行
+管理者パスワードを入力して、[実行]ボタンをクリックしてください。
+"
+               )
+       );
+       set_plugin_messages($messages);
 }
-function plugin_links_updatedata($page)
+
+function plugin_links_action()
 {
-       global $whatsnew;
-       
-       if (defined('LINK_DB'))
-       {
-               $is_page = is_page($page);
-               $time = ($is_page) ? get_filetime($page) : 0;
-               $a_page = addslashes($page);
-               
-               $rows = db_query("SELECT id FROM page WHERE name='$a_page';");
-               if (count($rows) == 0)
-               {
-                       if (!$is_page)
-                       {
-                               return;
-                       }
-                       db_exec("INSERT INTO page (name,lastmod) VALUES ('$a_page',$time);");
-                       $rows = db_query("SELECT id FROM page WHERE name='$a_page';");
-                       $id = $rows[0]['id'];
-               }
-               else // if (count($rows) > 0)
-               {
-                       $id = $rows[0]['id'];
-                       // $page¤¬»²¾È¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤ò½é´ü²½
-                       db_exec("DELETE FROM link WHERE page_id=$id;");
-                       if (!$is_page)
-                       {
-                               $_rows = db_query("SELECT * FROM link WHERE ref_id=$id;");
-                               if (count($_rows) == 0)
-                               {
-                                       // $page¤ò»²¾È¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤¬¤Ê¤¤¤Î¤Ç¡¢¤³¤Î¥ì¥³¡¼¥É¤òºï½ü
-                                       db_exec("DELETE FROM page WHERE id=$id;");
-                                       return;
-                               }
-                       }
-                       // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤ò¥»¥Ã¥È
-                       db_exec("UPDATE page SET lastmod=$time WHERE id=$id;");
-               }
-               
-               // cache
-               $pages = array();
-               
-               $links = plugin_links_get_objects($page);
-               foreach ($links as $_obj)
-               {
-                       if (!isset($_obj->type) or $_obj->type != 'pagename' or $_obj->name == $page)
-                       {
-                               continue;
-                       }                       
-                       $_page = $_obj->name;
-                       if (!array_key_exists($_page,$pages))
-                       {
-                               $a_page = addslashes($_page);
-                               $rows = db_query("SELECT id,name FROM page WHERE name='$a_page';");
-                               if (count($rows) == 0)
-                               {
-                                       db_exec("INSERT INTO page (name,lastmod) VALUES ('$a_page',0);");
-                                       $rows = db_query("SELECT id,name FROM page WHERE name='$a_page';");
-                               }
-                               $pages[$rows[0]['name']] = TRUE;
-                               $ref_id =$rows[0]['id'];
-                               db_exec("INSERT INTO link (page_id,ref_id) VALUES ($id,$ref_id);");
-                       }
-               }
-               // Ã¯¤«¤é¤â»²¾È¤µ¤ì¤Ê¤¯¤Ê¤Ã¤¿¡Ö¸ºß¤·¤Ê¤¤¥Ú¡¼¥¸¡×¤ò¾Ãµî
-               // MySQL3¤Ï¡ÖÉûÌ䤤¹ç¤ï¤»¡×¤Ë̤Âбþ¡Ä?
-//             db_exec("DELETE FROM page WHERE id in (SELECT id FROM page LEFT JOIN link ON id=ref_id WHERE lastmod=0 AND page_id IS NULL);");
-               $rows = db_query("SELECT id FROM page LEFT JOIN link ON id=ref_id WHERE lastmod=0 AND page_id IS NULL;");
-               $_arr = array();
-               foreach ($rows as $row)
-               {
-                       $_arr[] = $row['id'];
-               }
-               if (count($_arr))
-               {
-                       db_exec("DELETE FROM page WHERE id in (".join(',',$_arr).");");
-               }
-       }
-       else // if (!defined('LINK_DB'))
-       {
-               $time = is_page($page) ? get_filetime($page) : 0;
-               
-               $rel_old = array();
-               $rel_file = CACHE_DIR.encode($page).'.rel';
-               if (file_exists($rel_file))
-               {
-                       $lines = file($rel_file);
-                       if (array_key_exists(0,$lines))
-                       {
-                               $rel_old = explode("\t",rtrim($lines[0]));
-                       }
-                       unlink($rel_file);
-               }
-               $rel_new = array(); // »²¾ÈÀè
-               $links = plugin_links_get_objects($page);
-               foreach ($links as $_obj)
-               {
-                       if (!isset($_obj->type) or $_obj->type != 'pagename')
-                       {
-                               continue;
-                       }                       
-                       $_page = $_obj->name;
-                       if ($_page != $page)
-                       {
-                               $rel_new[$_page] = 1;
-                       }
-               }
-               
-               // .rel:$page¤¬»²¾È¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤Î°ìÍ÷
-               if (count($rel_new))
-               {
-                       $rel_new = array_keys($rel_new);
-                       $fp = fopen($rel_file,'w')
-                               or die_message('cannot write '.htmlspecialchars($rel_file));
-                       fputs($fp,join("\t",$rel_new));
-                       fclose($fp);
-               }
-               
-               // .ref:$_page¤ò»²¾È¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤Î°ìÍ÷
-               $add = array_diff($rel_new,$rel_old);
-               foreach ($add as $_page)
-               {
-                       $ref_file = CACHE_DIR.encode($_page).'.ref';
-                       $ref = "$page\t$time\n";
-                       if (file_exists($ref_file))
-                       {
-                               foreach (file($ref_file) as $line)
-                               {
-                                       list($ref_page,$time) = explode("\t",rtrim($line));
-                                       if ($ref_page != $page)
-                                       {
-                                               $ref .= $line;
-                                       }
-                               }
-                       }
-                       $fp = fopen($ref_file,'w')
-                                or die_message('cannot write '.htmlspecialchars($ref_file));
-                       fputs($fp,$ref);
-                       fclose($fp);
-               }
-               
-               $del = array_diff($rel_old,$rel_new);
-               foreach ($del as $_page)
-               {
-                       $ref_file = CACHE_DIR.encode($_page).'.ref';
-                       if (file_exists($ref_file))
-                       {
-                               $ref = '';
-                               foreach (file($ref_file) as $line)
-                               {
-                                       list($ref_page,$time) = explode("\t",rtrim($line));
-                                       if ($ref_page != $page)
-                                       {
-                                               $ref .= $line;
-                                       }
-                               }
-                               unlink($ref_file);
-                               if ($ref != '')
-                               {
-                                       $fp = fopen($ref_file,'w')
-                                               or die_message('cannot write '.htmlspecialchars($ref_file));
-                                       fputs($fp,$ref);
-                                       fclose($fp);
-                               }
-                       }
-               }
+       global $post, $vars, $foot_explain;
+       global $_links_messages;
+
+       $script = get_base_uri();
+       if (PKWK_READONLY) die_message('PKWK_READONLY prohibits this');
+
+       $msg = $body = '';
+       if (empty($vars['action']) || empty($post['adminpass']) || ! pkwk_login($post['adminpass'])) {
+               $msg   = & $_links_messages['title_update'];
+               $body  = convert_html($_links_messages['msg_usage']);
+               $body .= <<<EOD
+<form method="post" action="$script">
+ <div>
+  <input type="hidden" name="plugin" value="links" />
+  <input type="hidden" name="action" value="update" />
+  <label for="_p_links_adminpass">{$_links_messages['msg_adminpass']}</label>
+  <input type="password" name="adminpass" id="_p_links_adminpass" size="20" value="" />
+  <input type="submit" value="{$_links_messages['btn_submit']}" />
+ </div>
+</form>
+EOD;
+
+       } else if ($vars['action'] == 'update') {
+               links_init();
+               $foot_explain = array(); // Exhaust footnotes
+               $msg  = & $_links_messages['title_update'];
+               $body = & $_links_messages['msg_done'    ];
+       } else {
+               $msg  = & $_links_messages['title_update'];
+               $body = & $_links_messages['err_invalid' ];
        }
+       return array('msg'=>$msg, 'body'=>$body);
 }
-?>