From 15e6dda2512403bb7738c34bccd906842d0b6a14 Mon Sep 17 00:00:00 2001 From: umorigu Date: Sat, 30 Sep 2017 15:31:10 +0900 Subject: [PATCH 1/1] BugTrack/692 Show page contents in search result - search2 plugin * Show page contents by client-side JavaScript * Add new "search2" plugin with "skin/search2.js" JavaScript * Toggle switch to show details or not * Supoort both UTF-8 and EUC-JP encodings * OR Search with "OR"-combined keywords (ex: "A OR B") * Always show passage * Color search texts in details view * Color search texts in each text-found page * Web browser requirement: HTML5 Fetch API (You can use Polyfill library) * Server requirement: PHP5.4+ (can handle JSON) --- en.lng.php | 6 +- ja.lng.php | 10 +- lib/file.php | 68 +++++- lib/func.php | 4 +- lib/html.php | 32 ++- lib/link.php | 4 +- plugin/search2.inc.php | 222 +++++++++++++++++++ skin/main.js | 2 +- skin/pukiwiki.css | 15 ++ skin/pukiwiki.skin.php | 1 + skin/search2.js | 585 +++++++++++++++++++++++++++++++++++++++++++++++++ skin/tdiary.css | 15 ++ 12 files changed, 940 insertions(+), 24 deletions(-) create mode 100644 plugin/search2.inc.php create mode 100644 skin/search2.js diff --git a/en.lng.php b/en.lng.php index 4384192..470104b 100644 --- a/en.lng.php +++ b/en.lng.php @@ -2,7 +2,7 @@ // PukiWiki - Yet another WikiWikiWeb clone. // en.lng.php // Copyright -// 2002-2016 PukiWiki Development Team +// 2002-2017 PukiWiki Development Team // 2001-2002 Originally written by yu-ji // License: GPL v2 or (at your option) any later version // @@ -53,6 +53,8 @@ $_msg_help = 'View Text Formatting Rules'; $_msg_week = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); $_msg_content_back_to_top = '
'; $_msg_word = 'These search terms have been highlighted:'; +$_msg_unsupported_webbrowser = 'This function doesn\'t support your current Web browser.'; +$_msg_use_alternative_link = 'Please go to the following link destination: $1'; /////////////////////////////////////// // Symbols @@ -367,6 +369,8 @@ $_btn_and = 'AND'; $_btn_or = 'OR'; $_search_pages = 'Search for page starts from $1'; $_search_all = 'Search for all pages'; +$_search_searching = 'Searching...'; +$_search_detail = 'Show details'; /////////////////////////////////////// // source.inc.php diff --git a/ja.lng.php b/ja.lng.php index f2bb9fc..0704d6c 100644 --- a/ja.lng.php +++ b/ja.lng.php @@ -2,7 +2,7 @@ // PukiWiki - Yet another WikiWikiWeb clone. // ja.lng.php // Copyright -// 2002-2016 PukiWiki Development Team +// 2002-2017 PukiWiki Development Team // 2001-2002 Originally written by yu-ji // License: GPL v2 or (at your option) any later version // @@ -55,6 +55,8 @@ $_msg_help = 'テキスト整形のルールを表示する'; $_msg_week = array('日','月','火','水','木','金','土'); $_msg_content_back_to_top = '
'; $_msg_word = 'これらのキーワードがハイライトされています:'; +$_msg_unsupported_webbrowser = 'この機能はお使いのWebブラウザには対応していません。'; +$_msg_use_alternative_link = 'リンク先の機能をご利用ください: $1'; /////////////////////////////////////// // Symbols @@ -114,7 +116,7 @@ $_LANG['skin']['rename'] = '名前変更'; // Rename a page (and related) $_LANG['skin']['rss'] = '最終更新のRSS'; // RSS of RecentChanges $_LANG['skin']['rss10'] = & $_LANG['skin']['rss']; $_LANG['skin']['rss20'] = & $_LANG['skin']['rss']; -$_LANG['skin']['search'] = '単語検索'; +$_LANG['skin']['search'] = '検索'; $_LANG['skin']['top'] = 'トップ'; // Top page $_LANG['skin']['unfreeze'] = '凍結解除'; $_LANG['skin']['upload'] = '添付'; // Attach a file @@ -361,7 +363,7 @@ $_rename_messages = array( /////////////////////////////////////// // search.inc.php -$_title_search = '単語検索'; +$_title_search = '検索'; $_title_result = '$1 の検索結果'; $_msg_searching = '全てのページから単語を検索します。大文字小文字の区別はありません。'; $_btn_search = '検索'; @@ -369,6 +371,8 @@ $_btn_and = 'AND検索'; $_btn_or = 'OR検索'; $_search_pages = '$1 から始まるページを検索'; $_search_all = '全てのページを検索'; +$_search_searching = '検索中...'; +$_search_detail = '詳細表示'; /////////////////////////////////////// // source.inc.php diff --git a/lib/file.php b/lib/file.php index 597b5fc..39c3af8 100644 --- a/lib/file.php +++ b/lib/file.php @@ -15,9 +15,16 @@ define('PKWK_MAXSHOW_CACHE', 'recent.dat'); // AutoLink define('PKWK_AUTOLINK_REGEX_CACHE', 'autolink.dat'); -// Get source(wiki text) data of the page -// Returns FALSE if error occurerd -function get_source($page = NULL, $lock = TRUE, $join = FALSE) +/** + * Get source(wiki text) data of the page + * + * @param $page page name + * @param $lock lock + * @param $join true: return string, false: return array of string + * @param $raw true: return file content as-is + * @return FALSE if error occurerd + */ +function get_source($page = NULL, $lock = TRUE, $join = FALSE, $raw = FALSE) { //$result = NULL; // File is not found $result = $join ? '' : array(); @@ -44,6 +51,9 @@ function get_source($page = NULL, $lock = TRUE, $join = FALSE) } else { $result = fread($fp, $size); if ($result !== FALSE) { + if ($raw) { + return $result; + } // Removing Carriage-Return $result = str_replace("\r", '', $result); } @@ -204,16 +214,54 @@ function remove_author_info($wikitext) return preg_replace('/^\s*#author\([^\n]*(\n|$)/m', '', $wikitext); } -function remove_author_lines($lines) +/** + * Remove author line from wikitext + */ +function remove_author_header($wikitext) { - $author_head = '#author('; - $len = strlen($author_head); - for ($i = 0; $i < 5; $i++) { - if (substr($lines[$i], 0, $len) === $author_head) { - unset($lines[$i]); + $start = 0; + while (($pos = strpos($wikitext, "\n", $start)) != false) { + $line = substr($wikitext, $start, $pos); + $m = null; + if (preg_match('/^#author\(/', $line, $m)) { + // fond #author line, Remove this line only + if ($start === 0) { + return substr($wikitext, $pos + 1); + } else { + return substr($wikitext, 0, $start - 1) . + substr($wikitext, $pos + 1); + } + } else if (preg_match('/^#freeze(\W|$)/', $line, $m)) { + // Found #freeze still in header + } else { + // other line, #author not found + return $wikitext; + } + $start = $pos + 1; + } + return $wikitext; +} + +/** + * Get author info from wikitext + */ +function get_author_info($wikitext) +{ + $start = 0; + while (($pos = strpos($wikitext, "\n", $start)) != false) { + $line = substr($wikitext, $start, $pos); + $m = null; + if (preg_match('/^#author\(/', $line, $m)) { + return $line; + } else if (preg_match('/^#freeze(\W|$)/', $line, $m)) { + // Found #freeze still in header + } else { + // other line, #author not found + return false; } + $start = $pos + 1; } - return $lines; + return false; } function get_date_atom($timestamp) diff --git a/lib/func.php b/lib/func.php index 716243f..9536393 100644 --- a/lib/func.php +++ b/lib/func.php @@ -346,8 +346,8 @@ function do_search($word, $type = 'AND', $non_format = FALSE, $base = '') // Search for page contents foreach ($keys as $key) { - $lines = remove_author_lines(get_source($page, TRUE, FALSE)); - $b_match = preg_match($key, join('', $lines)); + $body = get_source($page, TRUE, TRUE, TRUE); + $b_match = preg_match($key, remove_author_header($body)); if ($b_type xor $b_match) break; // OR } if ($b_match) continue; diff --git a/lib/html.php b/lib/html.php index 46e1d97..92972b6 100644 --- a/lib/html.php +++ b/lib/html.php @@ -206,10 +206,28 @@ function get_html_scripting_data() if (!isset($ticket_link_sites) || !is_array($ticket_link_sites)) { return ''; } + $is_utf8 = (bool)defined('PKWK_UTF8_ENABLE'); // Require: PHP 5.4+ - if (!defined('JSON_UNESCAPED_UNICODE')) { - return ''; - }; + $json_enabled = defined('JSON_UNESCAPED_UNICODE'); + if (!$json_enabled) { + $empty_data = <<