Skip to content

Commit

Permalink
ページの再読み込みが必要のバグ #95 jQueryへの依存を少なく #96
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Jun 29, 2024
1 parent ef363d2 commit d033d4b
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 164 deletions.
62 changes: 44 additions & 18 deletions kona3engine/action/edit.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function kona3edit_checkEditToken($page, $i_mode) {
// check edit_token
if (!kona3_checkEditToken($page)) {
$label = lang('Edit');
$edit_token = kona3_getEditToken($page, TRUE);
$edit_token = kona3_getEditToken($page, FALSE);
$url = kona3getPageURL($page, 'edit', '', "edit_token=" . $edit_token);
$page_html = htmlspecialchars($page, ENT_QUOTES);
if ($i_mode == 'form') {
Expand All @@ -150,7 +150,14 @@ function kona3edit_checkEditToken($page, $i_mode) {
"$label - $page_html</a>"
);
} else {
kona3_edit_err(lang('Invalid edit token.'), $i_mode);
$postId = intval(kona3param('postId', 0));
$edit_token = '';
foreach ($_SESSION as $key => $val) {
if (is_string($val)) {
$edit_token .= "[$key=$val]";
}
}
kona3_edit_err(lang('Invalid edit token.') . "et=$edit_token", $i_mode, $postId);
}
exit;
}
Expand All @@ -176,7 +183,7 @@ function edit_command($cmd) {
'WHERE history_id=? AND hash=?',
[$history_id, $hash]);
if ($r) {
$edit_token = kona3_getEditToken();
$edit_token = kona3_getEditToken($page, FALSE);
$url = kona3getPageURL($page, "edit", "", "edit_token=$edit_token");
return kona3showMessage(
'DELETE History',
Expand Down Expand Up @@ -278,6 +285,7 @@ function kona3_trywrite(&$txt, &$a_hash, $i_mode, &$result) {
$a_hash_frm = kona3param('a_hash', '');
$tags = kona3param('tags', '');
$edit_ext = kona3param('edit_ext', '');
$postId = intval(kona3param('postId', 0)); // option

$fname = kona3getEditFile("{$page}.{$edit_ext}", $ext);
$user_id = kona3getUserId();
Expand All @@ -291,7 +299,7 @@ function kona3_trywrite(&$txt, &$a_hash, $i_mode, &$result) {
// === for FILE ===
if (file_exists($fname)) {
if (!is_writable($fname)) {
kona3_edit_err(lang('Could not write file.'), $i_mode);
kona3_edit_err(lang('Could not write file.'), $i_mode, $postId);
return "";
}
} else {
Expand All @@ -313,25 +321,25 @@ function kona3_trywrite(&$txt, &$a_hash, $i_mode, &$result) {
// check directories level
if ($cnt > $max_level) {
if ($max_level == 0) {
kona3_edit_err(lang("Invalid Wiki Name: not allow use '/'"), $i_mode);
kona3_edit_err(lang("Invalid Wiki Name: not allow use '/'"), $i_mode, $postId);
exit;
}
kona3_edit_err(
sprintf(lang("Invalid Wiki Name: not allow use '/' over %s times"),
$max_level),
$i_mode);
$i_mode, $postId);
exit;
}
// get dir mode
$dir_mode = @octdec($kona3conf['chmod_mkdir']);
if ($dir_mode == 0) {
kona3_edit_err('Invalid value: chmod_mkdir in config', $i_mode);
kona3_edit_err('Invalid value: chmod_mkdir in config', $i_mode, $postId);
exit;
}
// mkdir
$b = @mkdir($dirname, $dir_mode, TRUE);
if (!$b) {
kona3_edit_err('mkdir failed.', $i_mode);
kona3_edit_err('mkdir failed.', $i_mode, $postId);
exit;
}
}
Expand All @@ -341,7 +349,7 @@ function kona3_trywrite(&$txt, &$a_hash, $i_mode, &$result) {
$bytes = @file_put_contents($fname, $edit_txt);
if ($bytes === FALSE) {
$msg = lang('Could not write file.');
kona3_edit_err($msg, $i_mode);
kona3_edit_err($msg, $i_mode, $postId);
$result = FALSE;
return $msg;
}
Expand All @@ -357,6 +365,7 @@ function kona3_trywrite(&$txt, &$a_hash, $i_mode, &$result) {
echo json_encode(array(
'result' => 'ok',
'a_hash' => kona3getPageHash($edit_txt),
'postId' => $postId,
));
return TRUE;
}
Expand Down Expand Up @@ -450,14 +459,19 @@ function kona3edit_ai() {
function kona3edit_ai_load_template()
{
// read wiki data (ai_prompt)
// read user defined
$prompt_file = KONA3_DIR_DATA."/ai_prompt.md";
$prompt = file_exists($prompt_file) ? file_get_contents($prompt_file) : '';
if ($prompt == '') {
// read default template
$lang = kona3getLangCode();
$prompt_file = KONA3_DIR_ENGINE."/lang/{$lang}-ai_prompt.md";
$prompt = file_get_contents($prompt_file);
$prompt = file_exists($prompt_file) ? @file_get_contents($prompt_file) : '';
// read system defined
$lang = kona3getLangCode();
$prompt_file = KONA3_DIR_ENGINE."/lang/{$lang}-ai_prompt.md";
if (file_exists($prompt_file)) {
$promptSys = file_get_contents($prompt_file);
if ($promptSys != '') {
$prompt .= $promptSys;
}
}
//
echo json_encode([
'result' => 'ok',
'message' => $prompt,
Expand All @@ -470,9 +484,21 @@ function kona3ai_edit_template()
$prompt_file = KONA3_DIR_DATA."/ai_prompt.md";
if (!file_exists($prompt_file)) {
// copy template
$lang = kona3getLangCode();
$prompt_file_template = KONA3_DIR_ENGINE."/lang/$lang-ai_prompt.md";
file_put_contents($prompt_file, file_get_contents($prompt_file_template));
$template = <<<EOS
# prompt name1
### Instruction:
(prompt here)
### Input:
__TEXT__
-----
# prompt name2
### Instruction:
(prompt here)
### Input:
__TEXT__
-----
EOS;
file_put_contents($prompt_file, $template);
}
// jump to edit
$url = kona3getPageURL('ai_prompt', 'edit', '', "");
Expand Down
1 change: 1 addition & 0 deletions kona3engine/kona3conf.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function kona3conf_gen() {
check_conf($kona3conf, "header.tags", []); // additional header
check_conf($kona3conf, 'js', [
kona3getResourceURL('jquery-3.7.0.min.js'),
kona3getResourceURL('ajax_qq.js'),
kona3getSkinURL('drawer.js', TRUE),
]);
// css files
Expand Down
2 changes: 1 addition & 1 deletion kona3engine/kona3lib.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ function kona3_getEditToken($key = 'default', $update = TRUE) {
return kona3_getEditTokenForceUpdate($key);
}
// check time
$ONE_DAY = 3;// 60 * 60 * 24; // 1day
$ONE_DAY = 60 * 60 * 24; // 1day
$time = isset($_SESSION[$sname_time]) ? $_SESSION[$sname_time] : time();
$expire_time = $time + $ONE_DAY;
if (time() > $expire_time) {
Expand Down
2 changes: 1 addition & 1 deletion kona3engine/konawiki_version.inc.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
define("KONAWIKI_VERSION", "3.3.6");
define("KONAWIKI_VERSION", "3.3.7");
12 changes: 11 additions & 1 deletion kona3engine/lang/ja-ai_prompt.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# 要約

### 指示:
以下に入力した文章を要約してください。
要点を強調して、3文くらいにまとめてください。
### 入力:
__TEXT__
-----

# 続きを作文
### 指示:
以下に入力した文章の続きを1文だけ作成してください。
入力文章の内容を踏まえた上で、文体などを合わせて作成してください。
### 入力:
__TEXT__
-----

# 誤字脱字をチェック
### 指示:
以下に入力した文章の誤字脱字を指摘してください。
Expand All @@ -32,9 +35,17 @@ __TEXT__
### 入力:
__TEXT__
-----

# 分かりやすく言い換え
### 指示:
以下に入力した文章を、より自然で分かりやすくなるように言い換えてください。
### 入力:
__TEXT__
-----

# 言い換え(置換候補)
### 指示:
以下に入力した文章を、より自然で分かりやすくなるように言い換えてください。
なお次のJSON形式で指摘してください。
### 出力例:
[
Expand All @@ -50,4 +61,3 @@ __TEXT__
},
]
### 入力:
__TEXT__
158 changes: 158 additions & 0 deletions kona3engine/resource/ajax_qq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// 簡単なDOM操作の関数群
function qs(id) { return document.querySelector(id); }
function qsa(id) { return document.querySelectorAll(id); }
function prop(id, key) {
const e = qs(id);
if (!e) { return ''; }
return e.getAttribute(key);
}
function setEnabled(id, enabled) {
let e = id;
if (typeof (id) === 'string') { e = qs(id); }
if (!e) { return false; }
e.disabled = !enabled;
}
function getEnabled(id) {
let e = id;
if (typeof (id) === 'string') { e = qs(id); }
if (!e) { return false; }
return !e.disabled;
}
function qq(id) {
let e = id;
if (id === undefined) { e = window } // dummy
if (typeof (id) === 'string') {
e = document.querySelector(id);
}
if (!e) {
console.warn('qq: not found', id);
return null;
}
const obj = {}
const events = {}
obj.click = (f) => {
e.addEventListener('click', f);
};
obj.keydown = (f) => {
e.addEventListener('keydown', f);
};
obj.change = (f) => {
e.addEventListener('change', f);
};
obj.attr = (key, val) => {
if (val !== undefined) {
return e.setAttribute(key, val);
}
return e.getAttribute(key, val);
};
obj.prop = (key, val) => {
if (val !== undefined) {
e[key] = val;
}
return e[key];
};
obj.html = (val) => {
if (val !== undefined) {
e.innerHTML = val;
}
return e.innerHTML;
};
obj.val = (val) => {
if (val !== undefined) {
e.value = val;
}
return e.value;
};
obj.text = (val) => {
if (val !== undefined) {
e.innerText = val;
}
return e.innerText;
};
obj.css = (styleName, val) => {
if (val !== undefined) {
e.style[styleName] = val;
}
return e.style[styleName];
};
obj.on = (event, f) => {
e.addEventListener(event, f);
if (events[event] === undefined) {
events[event] = [];
}
events[event].push(f);
};
obj.off = (event, f) => {
if (f !== undefined) {
e.removeEventListener(event, f);
} else {
if (events[event] !== undefined) {
events[event].forEach((f) => {
e.removeEventListener(event, f);
});
}
}
};
obj.enabled = (val) => {
if (val !== undefined) {
e.disabled = !val;
}
return !e.disabled;
};
obj.show = () => {
e.style.display = 'block';
}
obj.hide = () => {
e.style.display = 'none';
}
obj.append = (child) => {
e.appendChild(child);
}
// ajax method
obj.post = (url, dataObj) => {
// make FormData
let formData = new FormData();
if (dataObj instanceof FormData) {
formData = dataObj;
} else {
for (const key in dataObj) {
if (dataObj.hasOwnProperty(key)) {
formData.append(key, dataObj[key]);
}
}
}
// make request
setTimeout(() => {
// fetch
fetch(url, {
method: 'POST',
body: formData,
})
.then((response) => {
if (!response.ok) {
throw new Error('Network response was not ok.');
}
return response.text();
})
.then((text) => {
if (typeof (text) == 'string') {
try {
jsonObj = JSON.parse(text);
} catch (e) {
jsonObj = { "result": false, "reason": text };
}
}
obj._done(jsonObj);
})
.catch((error) => {
obj._fail(error);
})
}, 1)
return obj;
};
obj._done = (_f) => { }
obj._fail = (_f) => { }
obj.done = (f) => { obj._done = f; return obj; }
obj.fail = (f) => { obj._fail = f; return obj; }
return obj;
}
Loading

0 comments on commit d033d4b

Please sign in to comment.