Skip to content

Commit

Permalink
Merge pull request #105 from VSEphpbb/ideas-bot
Browse files Browse the repository at this point in the history
Remove the Ideas Bot
  • Loading branch information
DavidIQ authored Mar 25, 2019
2 parents d329140 + 363501c commit 2a7c1af
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 123 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Opt into fully virtualized infrastructure
sudo: required
dist: precise

language: php
sudo: required

matrix:
include:
Expand Down
5 changes: 0 additions & 5 deletions adm/style/acp_phpbb_ideas.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ <h3>{{ lang('WARNING') }}</h3>
<dt><label for="ideas_forum_id">{{ lang('ACP_IDEAS_FORUM_ID') ~ lang('COLON') }}</label><br /><span>{{ lang('ACP_IDEAS_FORUM_ID_EXPLAIN') }}</span></dt>
<dd>{{ S_FORUM_SELECT_BOX }}</dd>
</dl>
<dl>
<dt><label for="ideas_poster_id">{{ lang('ACP_IDEAS_POSTER_ID') ~ lang('COLON') }}</label><br /><span>{{ lang('ACP_IDEAS_POSTER_ID_EXPLAIN') }}</span></dt>
<dd><input id="ideas_poster_id" type="text" size="45" maxlength="255" name="config[ideas_poster_id]" value="{{ IDEAS_POSTER }}" /></dd>
<dd>[ <a href="{{ U_FIND_USERNAME }}" onclick="find_username(this.href); return false;">{{ lang('FIND_USERNAME') }}</a> ]</dd>
</dl>
<dl>
<dt><label for="ideas_base_url">{{ lang('ACP_IDEAS_BASE_URL') ~ lang('COLON') }}</label><br /><span>{{ lang('ACP_IDEAS_BASE_URL_EXPLAIN') }}</span></dt>
<dd><input id="ideas_base_url" type="text" size="45" maxlength="255" name="config[ideas_base_url]" value="{{ IDEAS_BASE_URL }}" /></dd>
Expand Down
49 changes: 1 addition & 48 deletions controller/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ public function __construct(\phpbb\config\config $config, \phpbb\db\driver\drive
public function display_options()
{
$this->template->assign_vars(array(
'IDEAS_POSTER' => $this->get_ideas_topics_poster_username(),
'IDEAS_BASE_URL' => $this->config['ideas_base_url'] ?: '',

'S_FORUM_SELECT_BOX' => $this->select_ideas_forum(),
'S_IDEAS_FORUM_ID' => !empty($this->config['ideas_forum_id']),

'U_ACTION' => $this->u_action,
'U_FIND_USERNAME' => append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_phpbb_ideas_settings&amp;field=ideas_poster_id&amp;select_single=true'),
));
}

Expand All @@ -116,15 +114,8 @@ public function set_config_options()
$errors[] = $this->language->lang('FORM_INVALID');
}

// Check if selected user exists
$user_id = $this->get_ideas_topics_poster_id();
if (!$user_id)
{
$errors[] = $this->language->lang('NO_USER');
}

// Don't save settings if errors have occurred
if (sizeof($errors))
if (count($errors))
{
$submit = false;

Expand All @@ -136,13 +127,9 @@ public function set_config_options()

if ($submit)
{
// If selected user does exist, reassign the config value to its ID
$this->cfg_array['ideas_poster_id'] = $user_id;

// Configuration options to list through
$display_vars = array(
'ideas_forum_id',
'ideas_poster_id',
'ideas_base_url',
'ideas_forum_setup',
);
Expand Down Expand Up @@ -225,40 +212,6 @@ public function set_ideas_forum_options()
}
}

/**
* Get Ideas poster bot user ID
*
* @return int user_id Ideas bot user ID
* @access protected
*/
protected function get_ideas_topics_poster_id()
{
$sql = 'SELECT user_id
FROM ' . USERS_TABLE . "
WHERE username_clean = '" . $this->db->sql_escape(utf8_clean_string($this->cfg_array['ideas_poster_id'])) . "'";
$result = $this->db->sql_query($sql);
$user_id = (int) $this->db->sql_fetchfield('user_id');
$this->db->sql_freeresult($result);

return $user_id;
}

/**
* Get Ideas poster bot username
*
* @return string Ideas bot username
* @access protected
*/
protected function get_ideas_topics_poster_username()
{
$sql = 'SELECT username FROM ' . USERS_TABLE . '
WHERE user_id = ' . (int) $this->config['ideas_poster_id'];
$this->db->sql_query($sql);
$username = $this->db->sql_fetchfield('username');

return ($username !== false) ? $username : '';
}

/**
* Generate ideas forum select options
*
Expand Down
4 changes: 2 additions & 2 deletions controller/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public function __construct(auth $auth, config $config, helper $helper, ideas $i

/**
* Check if Ideas is properly configured after installation
* Ideas is available only after forum and poster settings have been set in ACP
* Ideas is available only after forum settings have been set in ACP
*
* @return bool Depending on whether or not the extension is properly configured
*/
public function is_available()
{
return (bool) $this->config['ideas_forum_id'] && (bool) $this->config['ideas_poster_id'];
return (bool) $this->config['ideas_forum_id'];
}

/**
Expand Down
9 changes: 2 additions & 7 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,8 @@ public function show_post_buttons($event)

if ($event['topic_data']['topic_first_post_id'] == $event['row']['post_id'])
{
$post_row = $event['post_row'];

$post_row['U_DELETE'] = false;
$post_row['U_QUOTE'] = false;
$post_row['U_WARN'] = false;

$event['post_row'] = $post_row;
$event->update_subarray('post_row', 'U_DELETE', false);
$event->update_subarray('post_row', 'U_WARN', false);
}
}

Expand Down
17 changes: 1 addition & 16 deletions factory/ideas.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public function submit($title, $message, $user_id)
'forum_id' => (int) $this->config['ideas_forum_id'],
'topic_id' => 0,
'icon_id' => false,
'poster_id' => (int) $this->config['ideas_poster_id'],
'poster_id' => (int) $this->user->data['user_id'],

'enable_bbcode' => true,
'enable_smilies' => true,
Expand All @@ -657,24 +657,9 @@ public function submit($title, $message, $user_id)
'force_approved_state' => (!$this->auth->acl_get('f_noapprove', $this->config['ideas_forum_id'])) ? ITEM_UNAPPROVED : true,
);

// Get Ideas Bot info
$sql = 'SELECT *
FROM ' . USERS_TABLE . '
WHERE user_id = ' . (int) $this->config['ideas_poster_id'];
$result = $this->db->sql_query_limit($sql, 1);
$poster_bot = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

$poster_bot['is_registered'] = true;

$tmpdata = $this->user->data;
$this->user->data = $poster_bot;

$poll = array();
submit_post('post', $title, $this->user->data['username'], POST_NORMAL, $poll, $data);

$this->user->data = $tmpdata;

// Edit topic ID into idea; both should link to each other
$sql_ary = array(
'topic_id' => $data['topic_id'],
Expand Down
2 changes: 0 additions & 2 deletions language/en/phpbb_ideas_acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
'ACP_IDEAS_FORUM_SETUP_EXPLAIN' => 'Sets up the Ideas forum. Users will not be able to start new topics. Additionally, auto-pruning will be disabled. Note: you have to set the Ideas forum first.',
'ACP_IDEAS_FORUM_SETUP_UPDATED' => 'phpBB Ideas forum settings successfully updated.',
'ACP_IDEAS_NO_FORUM' => 'No forum selected',
'ACP_IDEAS_POSTER_ID' => 'Ideas user bot',
'ACP_IDEAS_POSTER_ID_EXPLAIN' => 'Enter a username for the Idea-bot that will post Ideas topics into the forum.',
'ACP_IDEAS_SETTINGS_UPDATED' => 'phpBB Ideas settings updated.',
'ACP_IDEAS_UTILITIES' => 'Ideas utilities',
'ACP_IDEAS_BASE_URL' => 'Ideas base URL (optional)',
Expand Down
118 changes: 118 additions & 0 deletions migrations/m9_remove_idea_bot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
*
* Ideas extension for the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/

namespace phpbb\ideas\migrations;

class m9_remove_idea_bot extends \phpbb\db\migration\migration
{
public function effectively_installed()
{
return !$this->config->offsetExists('ideas_poster_id');
}

static public function depends_on()
{
return [
'\phpbb\ideas\migrations\m1_initial_schema',
'\phpbb\ideas\migrations\m3_acp_data',
'\phpbb\ideas\migrations\m4_update_statuses',
'\phpbb\ideas\migrations\m6_migrate_old_tables',
'\phpbb\ideas\migrations\m7_drop_old_tables',
];
}

public function update_data()
{
return [
['custom', [[$this, 'update_topic_authors']]],
['config.remove', ['ideas_poster_id']],
];
}

/**
* Replace the Ideas Bot stored in the posts and topics tables with the
* original author's information. Bot gone, order restored to universe.
*/
public function update_topic_authors()
{
// Return if the Ideas Bot does not exist at this point for some reason.
if (!$this->config->offsetExists('ideas_poster_id'))
{
return;
}

// Get real author info for ideas that were posted by the Ideas Bot
$topics = [];
$sql_array = [
'SELECT' => 'i.topic_id, i.idea_author, u.username, u.user_colour, t.topic_first_post_id',
'FROM' => [
$this->table_prefix . 'ideas_ideas' => 'i',
],
'LEFT_JOIN' => [
[
'FROM' => [$this->table_prefix . 'topics' => 't'],
'ON' => 't.topic_id = i.topic_id',
],
[
'FROM' => [$this->table_prefix . 'users' => 'u'],
'ON' => 'u.user_id = i.idea_author',
],
],
'WHERE' => 't.topic_poster = ' . (int) $this->config->offsetGet('ideas_poster_id'),
];

$sql = $this->db->sql_build_query('SELECT', $sql_array);
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$topics[$row['topic_id']] = [
'topic_poster_id' => $row['idea_author'] ?: ANONYMOUS,
'topic_poster_name' => $row['username'] ?: '',
'topic_poster_colour' => $row['user_colour'] ?: '',
'topic_first_post_id' => $row['topic_first_post_id'] ?: 0,
];
}
$this->db->sql_freeresult($result);

// Begin updating topics and posts
$this->db->sql_transaction('begin');
foreach ($topics as $topic_id => $data)
{
// Update topic author (first poster)
$sql = 'UPDATE ' . $this->table_prefix . 'topics
SET ' . $this->db->sql_build_array('UPDATE', [
'topic_poster' => $data['topic_poster_id'],
'topic_first_poster_name' => $data['topic_poster_name'],
'topic_first_poster_colour' => $data['topic_poster_colour'],
]) . '
WHERE topic_id = ' . (int) $topic_id;
$this->db->sql_query($sql);

// Update last poster if it's also the Ideas Bot (i.e: no replies)
$sql = 'UPDATE ' . $this->table_prefix . 'topics
SET ' . $this->db->sql_build_array('UPDATE', [
'topic_last_poster_id' => $data['topic_poster_id'],
'topic_last_poster_name' => $data['topic_poster_name'],
'topic_last_poster_colour' => $data['topic_poster_colour'],
]) . '
WHERE topic_id = ' . (int) $topic_id . '
AND topic_last_poster_id = ' . (int) $this->config->offsetGet('ideas_poster_id');
$this->db->sql_query($sql);

// Update first post's poster id if it's the Ideas Bot
$sql = 'UPDATE ' . $this->table_prefix . 'posts' . '
SET poster_id = ' . (int) $data['topic_poster_id'] . '
WHERE post_id = ' . (int) $data['topic_first_post_id'] . '
AND poster_id = ' . (int) $this->config->offsetGet('ideas_poster_id');
$this->db->sql_query($sql);
}
$this->db->sql_transaction('commit');
}
}
2 changes: 1 addition & 1 deletion styles/prosilver/template/idea_body.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
{% if IDEA_DUPLICATE or S_IS_MOD %}
<dt class="duplicatetoggle idealabel"{% if IDEA_STATUS_ID != STATUS_ARY.DUPLICATE %} style="display:none"{% endif %}>{{ lang('DUPLICATE') ~ lang('COLON') }}</dt>
<dd class="duplicatetoggle" {% if IDEA_STATUS_ID != STATUS_ARY.DUPLICATE %}style="display:none"{% endif %}>
<a id="duplicatelink" class="ideamodbtn" data-link="{{ U_IDEA_DUPLICATE }}" data-l-msg="{{ lang('IDEA_NUM') }}" {% if IDEA_DUPLICATE %}href="{{ U_IDEA_DUPLICATE }}">{{ lang('IDEA_NUM') }}{{ IDEA_DUPLICATE }}{% else %}style="display:none">{% endif %}</a>
<a id="duplicatelink" class="ideamodbtn" data-link="{{ U_IDEA_DUPLICATE }}" data-l-msg="{{ lang('IDEA_NUM') }}" {% if IDEA_DUPLICATE %}href="{{ U_IDEA_DUPLICATE }}">{{ lang('IDEA_NUM') ~ IDEA_DUPLICATE }}{% else %}style="display:none">{% endif %}</a>
{% if S_IS_MOD %}
<a href="{{ U_EDIT_DUPLICATE }}" id="duplicateedit" data-l-add="{{ lang('ADD') }}" data-l-edit="{{ lang('EDIT') }}">{% if IDEA_DUPLICATE %}<i class="fa fa-fw fa-pencil"></i>{{ lang('EDIT') }}{% else %}<i class="fa fa-fw fa-plus-circle"></i>{{ lang('ADD') }}{% endif %}</a>
<input type="text" id="duplicateeditinput" class="ideainput"{% if IDEA_DUPLICATE %} value="{{ IDEA_DUPLICATE }}"{% endif %} placeholder="###" data-l-err="{{ lang('ERROR') }}" data-l-msg="{{ lang('TICKET_ERROR_DUP') }}" />
Expand Down
2 changes: 1 addition & 1 deletion tests/controller/controller_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function setUp()
$this->auth = $this->getMockBuilder('\phpbb\auth\auth')
->disableOriginalConstructor()
->getMock();
$this->config = new \phpbb\config\config(array('ideas_forum_id' => 2, 'ideas_poster_id' => 2));
$this->config = new \phpbb\config\config(array('ideas_forum_id' => 2));
$this->controller_helper = $this->getMockBuilder('\phpbb\controller\helper')
->disableOriginalConstructor()
->getMock();
Expand Down
10 changes: 4 additions & 6 deletions tests/controller/idea_controller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ public function test_controller($idea_id, $mode, $callback, $is_ajax, $authorise
public function display_fails_data()
{
return array(
array(1, 2, 0, 404, 'IDEAS_NOT_AVAILABLE'), // no ideas_poster_id
array(1, 0, 2, 404, 'IDEAS_NOT_AVAILABLE'), // no ideas_forum_id
array(1, '', '', 404, 'IDEAS_NOT_AVAILABLE'), // no ideas_poster_id or ideas_forum_id
array(0, 2, 2, 404, 'IDEA_NOT_FOUND'), // no idea data
array(0, 2, 404, 'IDEA_NOT_FOUND'), // no idea data
array(1, 0, 404, 'IDEAS_NOT_AVAILABLE'), // no ideas_forum_id
array(1, '', 404, 'IDEAS_NOT_AVAILABLE'), // no ideas_forum_id
);
}

Expand All @@ -151,10 +150,9 @@ public function display_fails_data()
*
* @dataProvider display_fails_data
*/
public function test_controller_exception($idea_id, $idea_bot, $forum, $status_code, $page_content)
public function test_controller_exception($idea_id, $forum, $status_code, $page_content)
{
$this->config['ideas_forum_id'] = $forum;
$this->config['ideas_poster_id'] = $idea_bot;

/** @var \phpbb\ideas\controller\idea_controller $controller */
$controller = $this->get_controller('idea_controller');
Expand Down
8 changes: 3 additions & 5 deletions tests/controller/index_controller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ public function test_controller($status_code, $page_content)
public function controller_exception_test_data()
{
return array(
array(2, 0),
array(0, 2),
array('', ''),
array(0),
array(''),
);
}

Expand All @@ -61,10 +60,9 @@ public function controller_exception_test_data()
* @dataProvider controller_exception_test_data
* @expectedException \phpbb\exception\http_exception
*/
public function test_controller_exception($idea_bot, $forum)
public function test_controller_exception($forum)
{
$this->config['ideas_forum_id'] = $forum;
$this->config['ideas_poster_id'] = $idea_bot;

/** @var \phpbb\ideas\controller\index_controller $controller */
$controller = $this->get_controller('index_controller');
Expand Down
8 changes: 3 additions & 5 deletions tests/controller/list_controller_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ public function test_controller($status_code, $page_content, $params, $expected)
public function controller_exception_test_data()
{
return array(
array(2, 0),
array(0, 2),
array('', ''),
array(0),
array(''),
);
}

Expand All @@ -192,10 +191,9 @@ public function controller_exception_test_data()
* @dataProvider controller_exception_test_data
* @expectedException \phpbb\exception\http_exception
*/
public function test_controller_exception($idea_bot, $forum)
public function test_controller_exception($forum)
{
$this->config['ideas_forum_id'] = $forum;
$this->config['ideas_poster_id'] = $idea_bot;

/** @var \phpbb\ideas\controller\list_controller $controller */
$controller = $this->get_controller('list_controller');
Expand Down
Loading

0 comments on commit 2a7c1af

Please sign in to comment.