- Fixed issues reported by  Miri4ever
- Removed all MySQL specific SQL
- Better language management
- Minor code cleanup
This commit is contained in:
privet.fun 2023-05-29 13:32:30 -07:00
parent 1e062a4aa0
commit f21720b139
9 changed files with 84 additions and 43 deletions

View file

@ -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)

View file

@ -199,8 +199,8 @@
<span>{{ lang('LBL_AILABS_REPLY_POST_FORUMS_EXPLAIN') }}</span>
<select id="ailabs_forums_post_select" class="chosen-select" multiple data-placeholder="{{ lang('LBL_AILABS_SELECT_FORUMS') }}"
style="width: 100%;">
{% for FORUM in AILABS_FORUMS_LIST %}
<option value="{{ FORUM.VALUE }}">{{ FORUM.NAME }}</option>
{% for key,value in AILABS_FORUMS_LIST %}
<option value="{{ key }}">{{ value }}</option>
{% endfor %}
</select>
</fieldset>
@ -210,8 +210,8 @@
<span>{{ lang('LBL_AILABS_REPLY_QUOTE_FORUMS_EXPLAIN') }}</span>
<select id="ailabs_forums_mention_select" class="chosen-select" multiple data-placeholder="{{ lang('LBL_AILABS_SELECT_FORUMS') }}"
style="width: 100%;">
{% for FORUM in AILABS_FORUMS_LIST %}
<option value="{{ FORUM.VALUE }}">{{ FORUM.NAME }}</option>
{% for key,value in AILABS_FORUMS_LIST %}
<option value="{{ key }}">{{ value }}</option>
{% endfor %}
</select>
</fieldset>

View file

@ -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 . '&amp;action=add' : $this->u_action . '&amp;action=edit&amp;user_id=' . $this->user_id,
'U_BACK' => $this->u_action,
'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=ailabs_configuration&amp;field=ailabs_username&amp;select_single=true'),
'S_ERROR' => isset($error) ? $error : '',
'U_AILABS_ADD_EDIT' => true,
'U_ACTION' => $this->action == 'add' ? $this->u_action . '&amp;action=add' : $this->u_action . '&amp;action=edit&amp;user_id=' . $this->user_id,
'U_BACK' => $this->u_action,
'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=ailabs_configuration&amp;field=ailabs_username&amp;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 . '&amp;action=edit&amp;user_id=' . $row['user_id'] . '&amp;hash=' . generate_link_hash('acp_ailabs');
$row['U_DELETE'] = $this->u_action . '&amp;action=delete&amp;user_id=' . $row['user_id'] . '&amp;username=' . $row['username'] . '&amp;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);
}
}

View file

@ -46,8 +46,6 @@ class chatgpt extends AIController
protected function process()
{
$this->language->add_lang('common', 'privet/ailabs');
$this->job['status'] = 'exec';
$set = [

View file

@ -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]),
]);
}
}

View file

@ -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);
}

View file

@ -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'
]);

View file

@ -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'
]);

View file

@ -1,6 +1,6 @@
{% if postrow.U_AILABS %}
<div id="ailabs-post-{{ postrow.POST_ID }}" class="ailabs">
<span>AI&nbsp;</span>
<span>{{ lang('L_AILABS_AI') }}&nbsp;</span>
{% if postrow.U_AILABS_VIEW_LOG %}
<a href="{{ postrow.U_AILABS_VIEW_LOG }}" onclick="ailabs_view_log(this.href);return false;"><i
class="icon tpr-font fa-external-link fa-fw"></i></a>