diff --git a/privet/ailabs/README.md b/privet/ailabs/README.md index 973237b..2d02f4f 100644 --- a/privet/ailabs/README.md +++ b/privet/ailabs/README.md @@ -1,4 +1,4 @@ -# AI Labs v 1.0.0 RC +# AI Labs v 1.0.1 RC Incorporate AI into your phpBB board and get ready for an exciting experience. Currently supported ChatGPT, DALL-E (OpenAI) and Stable Diffusion (Stability AI). @@ -16,7 +16,6 @@ Examples: ## Requirements * php >=7.1 * phpbb >= 3.2 -* only tested with MySQL/mariadb ## Important notes @@ -109,6 +108,14 @@ Refer to https://platform.openai.com/docs/api-reference/images/create to learn m This extension is currently being actively developed. For communication, please use https://github.com/privet-fun/phpbb_ailabs/issues. +## Changelog + +* 1.0.1 +- Fixed issues reported by [ Miri4ever](https://www.phpbb.com/community/viewtopic.php?p=15958523#p15958523) +- Removed all MySQL specific SQL, going forward extension should be SQL server agnostic +- Better language management +- Minor code cleanup + ## License [GPLv2](../privet/ailabs/license.txt) diff --git a/privet/ailabs/adm/style/acp_ailabs_body.html b/privet/ailabs/adm/style/acp_ailabs_body.html index 1f1d0d0..c480f72 100644 --- a/privet/ailabs/adm/style/acp_ailabs_body.html +++ b/privet/ailabs/adm/style/acp_ailabs_body.html @@ -199,8 +199,8 @@ {{ lang('LBL_AILABS_REPLY_POST_FORUMS_EXPLAIN') }} @@ -210,8 +210,8 @@ {{ lang('LBL_AILABS_REPLY_QUOTE_FORUMS_EXPLAIN') }} diff --git a/privet/ailabs/controller/acp_controller.php b/privet/ailabs/controller/acp_controller.php index d02a5e7..981bd4a 100644 --- a/privet/ailabs/controller/acp_controller.php +++ b/privet/ailabs/controller/acp_controller.php @@ -31,7 +31,6 @@ class acp_controller //implements acp_interface protected $submit; protected $u_action; protected $user_id; - protected $ailabs_enabled; protected $tpr_ailabs; protected $desc_contollers = [ @@ -75,12 +74,11 @@ class acp_controller //implements acp_interface $this->submit = $submit; $this->u_action = $u_action; $this->user_id = $this->request->variable('user_id', 0); - $this->ailabs_enabled = !empty($this->config['ailabs_enabled']) ? true : false; } public function edit_add() { - $username = utf8_normalize_nfc($this->request->variable('ailabs_username', '', true)); + $username = $this->request->variable('ailabs_username', '', true); $new_user_id = $this->find_user_id($username); if ($this->action == 'edit' && empty($this->user_id)) { @@ -92,8 +90,8 @@ class acp_controller //implements acp_interface $data = [ 'user_id' => $new_user_id, 'controller' => $this->request->variable('ailabs_controller', ''), - 'config' => utf8_normalize_nfc($this->request->variable('ailabs_config', '', true)), - 'template' => utf8_normalize_nfc($this->request->variable('ailabs_template', '', true)), + 'config' => $this->request->variable('ailabs_config', '', true), + 'template' => $this->request->variable('ailabs_template', '', true), 'forums_post' => $this->request->variable('ailabs_forums_post', ''), 'forums_mention' => $this->request->variable('ailabs_forums_mention', ''), 'enabled' => $this->request->variable('ailabs_enabled', true), @@ -169,16 +167,6 @@ class acp_controller //implements acp_interface } } - $sql = 'SELECT forum_id, forum_name FROM ' . FORUMS_TABLE . ' ORDER BY left_id'; - $result = $this->db->sql_query($sql); - while ($row = $this->db->sql_fetchrow($result)) { - $this->template->assign_block_vars('AILABS_FORUMS_LIST', [ - 'VALUE' => $row['forum_id'], - 'NAME' => $row['forum_name'] - ]); - } - $this->db->sql_freeresult($result); - foreach ($this->desc_contollers as $key => $value) { $controller = explode("/", $value); $name = end($controller); @@ -195,11 +183,12 @@ class acp_controller //implements acp_interface array_merge( $edit, [ - 'S_ERROR' => isset($error) ? $error : '', - 'U_AILABS_ADD_EDIT' => true, - 'U_ACTION' => $this->action == 'add' ? $this->u_action . '&action=add' : $this->u_action . '&action=edit&user_id=' . $this->user_id, - 'U_BACK' => $this->u_action, - 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=ailabs_configuration&field=ailabs_username&select_single=true'), + 'S_ERROR' => isset($error) ? $error : '', + 'U_AILABS_ADD_EDIT' => true, + 'U_ACTION' => $this->action == 'add' ? $this->u_action . '&action=add' : $this->u_action . '&action=edit&user_id=' . $this->user_id, + 'U_BACK' => $this->u_action, + 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=ailabs_configuration&field=ailabs_username&select_single=true'), + 'AILABS_FORUMS_LIST' => $this->build_forums_list(), ] ) ); @@ -222,15 +211,15 @@ class acp_controller //implements acp_interface public function acp_ailabs_main() { - $sql = 'SELECT a.*, u.username, ' . - '(SELECT GROUP_CONCAT(f.forum_name SEPARATOR ", ") FROM phpbblt_forums f WHERE INSTR(a.forums_post, CONCAT(\'"\',f.forum_id,\'"\')) > 0) as forums_post_names, ' . - '(SELECT GROUP_CONCAT(f.forum_name SEPARATOR ", ") FROM phpbblt_forums f WHERE INSTR(a.forums_mention, CONCAT(\'"\',f.forum_id,\'"\')) > 0) as forums_mention_names ' . + $sql = 'SELECT a.*, u.username ' . 'FROM ' . $this->ailabs_users_table . ' a ' . 'LEFT JOIN ' . USERS_TABLE . ' u ON u.user_id = a.user_id ' . 'ORDER BY u.username'; $result = $this->db->sql_query($sql); + $forums = $this->build_forums_list(); + $ailabs_users = []; while ($row = $this->db->sql_fetchrow($result)) { @@ -240,6 +229,8 @@ class acp_controller //implements acp_interface $controller = explode("/", $row['controller']); $row['controller'] = end($controller); + $row['forums_post_names'] = $this->get_forums_names($row['forums_post'], $forums); + $row['forums_mention_names'] = $this->get_forums_names($row['forums_mention'], $forums); $row['U_EDIT'] = $this->u_action . '&action=edit&user_id=' . $row['user_id'] . '&hash=' . generate_link_hash('acp_ailabs'); $row['U_DELETE'] = $this->u_action . '&action=delete&user_id=' . $row['user_id'] . '&username=' . $row['username'] . '&hash=' . generate_link_hash('acp_ailabs'); @@ -302,4 +293,32 @@ class acp_controller //implements acp_interface } return $count; } + + protected function build_forums_list() + { + $return = []; + $sql = 'SELECT forum_id, forum_name FROM ' . FORUMS_TABLE . ' ORDER BY left_id'; + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) { + $return[$row['forum_id']] = $row['forum_name']; + } + $this->db->sql_freeresult($result); + return $return; + } + + protected function get_forums_names($str, $forums) { + $result = []; + if(!empty($str)) { + $arr = json_decode($str); + if(!empty($arr) && is_array($arr)) { + foreach($arr as $id) + { + $name = empty($forums[$id]) ? $id : $forums[$id]; + array_push($result, $name); + } + } + } + return join(', ', $result); + } + } diff --git a/privet/ailabs/controller/chatgpt.php b/privet/ailabs/controller/chatgpt.php index 75b81f5..8f76646 100644 --- a/privet/ailabs/controller/chatgpt.php +++ b/privet/ailabs/controller/chatgpt.php @@ -46,8 +46,6 @@ class chatgpt extends AIController protected function process() { - $this->language->add_lang('common', 'privet/ailabs'); - $this->job['status'] = 'exec'; $set = [ diff --git a/privet/ailabs/event/listener.php b/privet/ailabs/event/listener.php index 284fa44..334548e 100644 --- a/privet/ailabs/event/listener.php +++ b/privet/ailabs/event/listener.php @@ -62,9 +62,27 @@ class listener implements EventSubscriberInterface 'core.posting_modify_submit_post_after' => 'post_ailabs_message', 'core.viewtopic_post_rowset_data' => 'viewtopic_post_rowset_data', 'core.viewtopic_modify_post_row' => 'viewtopic_modify_post_row', + 'core.user_setup' => 'load_language_on_setup', ); } + /** + * https://area51.phpbb.com/docs/dev/3.2.x/extensions/tutorial_events.html + * Load the Acme Demo language file + * acme/demo/language/en/demo.php + * + * @param \phpbb\event\data $event The event object + */ + public function load_language_on_setup($event) + { + $lang_set_ext = $event['lang_set_ext']; + $lang_set_ext[] = array( + 'ext_name' => 'privet/ailabs', + 'lang_set' => 'common', + ); + $event['lang_set_ext'] = $lang_set_ext; + } + /** * Post a message * @@ -228,8 +246,6 @@ class listener implements EventSubscriberInterface private function get_status($status) { - $this->language->add_lang('common', 'privet/ailabs'); - switch ($status) { case null: return $this->language->lang('AILABS_THINKING'); @@ -240,7 +256,7 @@ class listener implements EventSubscriberInterface case 'fail': return $this->language->lang('AILABS_UNABLE_TO_REPLY'); } - + return $status; } @@ -299,12 +315,12 @@ class listener implements EventSubscriberInterface } if (!empty($ailabs)) { - $event['post_row'] = array_merge($event['post_row'], [ - 'U_AILABS' => $ailabs, + $event['post_row'] = array_merge($event['post_row'], [ + 'U_AILABS' => $ailabs, ]); if ($this->auth->acl_get('a_', 'm_')) { $event['post_row'] = array_merge($event['post_row'], [ - 'U_AILABS_VIEW_LOG' => $this->helper->route('privet_ailabs_view_log_controller_page', ['post_id' => $post_id]), + 'U_AILABS_VIEW_LOG' => $this->helper->route('privet_ailabs_view_log_controller_page', ['post_id' => $post_id]), ]); } } diff --git a/privet/ailabs/includes/AIController.php b/privet/ailabs/includes/AIController.php index 9c95884..c5f4d21 100644 --- a/privet/ailabs/includes/AIController.php +++ b/privet/ailabs/includes/AIController.php @@ -230,9 +230,8 @@ class AIController 'post_id' => $job['post_id'] ]; $set = '\'' . json_encode($data) . ',\''; - $sql = 'UPDATE ' . POSTS_TABLE . - ' SET post_ailabs_data = CONCAT(post_ailabs_data, ' . $set . ')' . - ' WHERE ' . $this->db->sql_build_array('SELECT', $where); + $concat = $this->db->sql_concatenate('post_ailabs_data', $set); + $sql = 'UPDATE ' . POSTS_TABLE . ' SET post_ailabs_data = ' . $concat . ' WHERE ' . $this->db->sql_build_array('SELECT', $where); $result = $this->db->sql_query($sql); $this->db->sql_freeresult($result); } diff --git a/privet/ailabs/language/en/common.php b/privet/ailabs/language/en/common.php index 3a23370..7a475b4 100644 --- a/privet/ailabs/language/en/common.php +++ b/privet/ailabs/language/en/common.php @@ -24,5 +24,6 @@ $lang = array_merge($lang, [ 'AILABS_THINKING' => 'thinking', 'AILABS_REPLYING' => 'replying…', 'AILABS_REPLIED' => 'replied ↓', - 'AILABS_UNABLE_TO_REPLY' => 'unable to reply' + 'AILABS_UNABLE_TO_REPLY' => 'unable to reply', + 'L_AILABS_AI' => 'AI' ]); diff --git a/privet/ailabs/language/ru/common.php b/privet/ailabs/language/ru/common.php index 4cb49a5..b28c6c9 100644 --- a/privet/ailabs/language/ru/common.php +++ b/privet/ailabs/language/ru/common.php @@ -24,5 +24,6 @@ $lang = array_merge($lang, [ 'AILABS_THINKING' => 'думает', 'AILABS_REPLYING' => 'отвечает…', 'AILABS_REPLIED' => 'ответил ↓', - 'AILABS_UNABLE_TO_REPLY' => 'ответить не смог' + 'AILABS_UNABLE_TO_REPLY' => 'ответить не смог', + 'L_AILABS_AI' => 'AI' ]); diff --git a/privet/ailabs/styles/prosilver/template/post_ailabs.html b/privet/ailabs/styles/prosilver/template/post_ailabs.html index 4beb28b..bc40692 100644 --- a/privet/ailabs/styles/prosilver/template/post_ailabs.html +++ b/privet/ailabs/styles/prosilver/template/post_ailabs.html @@ -1,6 +1,6 @@ {% if postrow.U_AILABS %}
- AI  + {{ lang('L_AILABS_AI') }}  {% if postrow.U_AILABS_VIEW_LOG %}