<?php
-/////////////////////////////////////////////////
-// PukiWiki - Yet another WikiWikiWeb clone.
-//
-// $Id: links.inc.php,v 1.5 2003/02/17 08:38:03 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;
-
- set_time_limit(0);
-
- if (!defined('LINK_DB')) {
- if ($vars['page'] != '') {
- links_update_file($vars['page']);
- return;
- }
- return links_init_file();
- }
- else {
- if ($vars['page'] != '') {
- links_update_db($vars['page']);
- return;
- }
- return links_init_db();
- }
-}
-function links_init_file()
-{
- global $get,$adminpass,$whatsnew,$non_list;
-
- if (md5($get['adminpass']) != $adminpass) {
- return array('msg'=>'update database',
- 'body'=>'<p>administrator password require.</p>');
- }
-
- // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î½é´ü²½
- 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(); // »²¾È¸µ
- $obj = new InlineConverter(array('page','auto'));
- foreach ($pages as $page) {
- if ($page == $whatsnew) {
- continue;
- }
- $time = get_filetime($page);
- $rel = array(); // »²¾ÈÀè
- $links = $obj->get_objects(join('',get_source($page)),$page);
- foreach ($links as $_obj) {
- if (!isset($_obj->type) or $_obj->type != 'pagename') {
- continue;
- }
- $_page = $_obj->name;
- if ($_page != $page and !preg_match("/$non_list/",$_page)) {
- $rel[$_page] = 1;
- $ref[$_page][$page] = $time;
- }
- }
- $fp = fopen(CACHE_DIR.encode($page).'.rel','w')
- or die_message('cannot write cache.');
- fputs($fp,join("\t",array_keys($rel)));
- fclose($fp);
- }
-
- foreach ($ref as $page=>$arr) {
- $fp = fopen(CACHE_DIR.encode($page).'.ref','w')
- or die_message('cannot write refer cache.');
- foreach ($arr as $_page=>$time) {
- fputs($fp,"$_page\t$time\n");
- }
- fclose($fp);
- }
- return array('msg'=>'update database','body'=>'<p>done.</p>');
-}
-function links_update_file($page)
+// Message setting
+function plugin_links_init()
{
- global $whatsnew,$non_list;
-
- $obj = new InlineConverter(array('page','auto'));
- $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]));
- }
- }
- $rel_new = array(); // »²¾ÈÀè
- $links = $obj->get_objects(join('',get_source($page)),$page);
- foreach ($links as $_obj) {
- if (!isset($_obj->type) or $_obj->type != 'pagename') {
- continue;
- }
- $_page = $_obj->name;
- if ($_page != $page and !preg_match("/$non_list/",$_page)) {
- $rel_new[$_page] = 1;
- }
- }
- $rel_new = array_keys($rel_new);
-
- $fp = fopen($rel_file,'w')
- or die_message('cannot write cache.');
- fputs($fp,join("\t",$rel_new));
- fclose($fp);
-
- $add = array_diff($rel_new,$rel_old);
- $ref = "$page\t$time\n";
- foreach ($add as $_page) {
- $ref_file = CACHE_DIR.encode($_page).'.ref';
- 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;
- 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;
- }
- }
- $fp = fopen($ref_file,'w') or die;
- fputs($fp,$ref);
- fclose($fp);
- }
- }
-}
-function links_init_db()
-{
- global $get,$adminpass,$whatsnew;
-
- if (md5($get['adminpass']) != $adminpass) {
- return array('msg'=>'update database',
- 'body'=>'<p>administrator password require.</p>');
- }
- // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î½é´ü²½
- $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'];
- }
-
- $obj = new InlineConverter();
- foreach ($pages as $page=>$id) {
- $links = $obj->get_objects(join('',get_source($page)),$page);
- foreach ($links as $_obj) {
- if ($_obj->type == 'pagename') {
- $_page = $_obj->name;
- if (array_key_exists($_page,$pages)) {
- $ref_id = $pages[$_page];
- if ($ref_id and $ref_id != $id) {
- db_exec("INSERT INTO link (page_id,ref_id) VALUES ($id,$ref_id);");
- }
- }
- }
- }
- }
- 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 links_update_db($page)
+
+function plugin_links_action()
{
- global $whatsnew;
+ global $post, $vars, $foot_explain;
+ global $_links_messages;
- if ($vars['page'] != $whatsnew) {
- return;
- }
-
- $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) { // not exist
- db_exec("INSERT INTO page (name,lastmod) VALUES ('$a_page',$time);");
- }
- else {
- $id = $rows[0]['id'];
- db_exec("DELETE FROM link WHERE page_id=$id;");
- if ($is_page) {
- db_exec("UPDATE page SET lastmod=$time WHERE id=$id;");
- }
- else {
- db_exec("DELETE FROM page WHERE id=$id;");
- db_exec("DELETE FROM link WHERE ref_id=$id;");
- return;
- }
- }
-
- $rows = db_query("SELECT id,name FROM page;");
- $pages = array();
- foreach ($rows as $row) {
- $pages[$row['name']] = $row['id'];
- }
-
- $id = $pages[$page];
-
- $obj = new InlineConverter();
- $links = $obj->get_objects(join('',get_source($page)),$page);
- foreach ($links as $_obj) {
- if ($_obj->type == 'pagename') {
- $_page = $_obj->name;
- if (array_key_exists($_page,$pages)) {
- $ref_id = $pages[$_page];
- if ($ref_id and $ref_id != $id) {
- db_exec("INSERT INTO link (page_id,ref_id) VALUES ($id,$ref_id);");
- }
- }
- }
- }
+ $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);
}
-?>