| |
|
| :: |
| Author |
Message |
Professional PR2


Joined: 07 Apr 2008 Posts: 252 Location: 1/2 of the World
|
Posted: Mon Aug 11, 2008 12:37 pm Post subject: helping to optimze thank post mod for phpbb3 |
|
|
hi guys.
i have a problem with thank post mod.
also this mod was written for phpbb3 RC4,so it's not match for big forums because of bad coding.
for example last week my forum suspended because of using a lot of ram just for this mod !!!!!!
i now C++ and a little java but my php is poor
i think this mod will check the number of thanks each time and this bad,so plz help to optimze it.
i think the major problem is thank.php:
| Code: | <?php
/**
*
* @package phpBB3
* @version $Id: thanks.php,v 0.2.0 2007/04/21 23:56:31 geoffreak Exp $
* @copyright (c) 2007 Geoffreak
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
define('THANKS_TABLE', $table_prefix . 'thanks');
// create an array of all users
$sql = 'SELECT *
FROM ' . USERS_TABLE;
$result = $db->sql_query($sql);
$users = array();
while ($row = $db->sql_fetchrow($result))
{
if ((!isset($row['user_thanked']) || !isset($row['user_thanked_others'])) && $user->data['user_type'] == USER_FOUNDER)
{
install_040();
}
$users[$row['user_id']] = array(
'username' => $row['username'],
'user_id' => $row['user_id'],
'user_colour' => $row['user_colour'],
'user_gender' => (isset($row['user_gender'])) ? $row['user_gender'] : false,
'thanks_give' => (isset($row['user_thanked_others'])) ? $row['user_thanked_others'] : 0,
'thanks_receive' => (isset($row['user_thanked'])) ? $row['user_thanked'] : 0,
);
}
$db->sql_freeresult($result);
// Do stuff based on header variables
// Two variables are needed to avoid accidental refresh errors
if (isset($_REQUEST['thanks']) && !isset($_REQUEST['rthanks']))
{
insert_thanks(request_var('thanks', 0), $user->data['user_id']);
}
if (isset($_REQUEST['rthanks']) && !isset($_REQUEST['thanks']))
{
delete_thanks(request_var('rthanks', 0), $user->data['user_id']);
}
// create an array of all thanks info
$sql = 'SELECT *
FROM ' . THANKS_TABLE;
$result = $db->sql_query($sql);
$thankers = array();
$i = 0;
while ($row = $db->sql_fetchrow($result))
{
$thankers[$i] = array(
'user_id' => $row['user_id'],
'post_id' => $row['post_id'],
);
$i++;
}
$db->sql_freeresult($result);
function install_040()
{
global $db, $user;
$sql = 'SELECT *
FROM ' . POSTS_TABLE;
$result = $db->sql_query($sql);
$posts_arr = array();
while ($row = $db->sql_fetchrow($result))
{
$posts_arr[$row['poster_id']][] = $row['post_id'];
}
$db->sql_freeresult($result);
$sql = 'SELECT *
FROM ' . USERS_TABLE;
$result = $db->sql_query($sql);
$users_thanked = array();
$users_thanked_others = array();
while ($row = $db->sql_fetchrow($result))
{
$users_thanked[$row['user_id']] = 0;
$users_thanked_others[$row['user_id']] = 0;
}
$db->sql_freeresult($result);
$sql = 'ALTER TABLE `' . USERS_TABLE . '` ADD `user_thanked` INT NOT NULL ;';
$db->sql_query($sql);
$sql = 'ALTER TABLE `' . USERS_TABLE . '` ADD `user_thanked_others` INT NOT NULL ;';
$db->sql_query($sql);
foreach ($users_thanked as $this_user_id => $thanks_count)
{
if (isset($posts_arr[$this_user_id]) && is_array($posts_arr[$this_user_id]))
{
foreach ($posts_arr[$this_user_id] as $key2 => $this_post_id)
{
foreach ($thankers as $key => $values)
{
if ($values['post_id'] == $this_post_id)
{
$users_thanked[$this_user_id]++;
}
}
}
}
foreach ($thankers as $key => $values)
{
if ($values['user_id'] == $this_user_id)
{
$users_thanked_others[$this_user_id]++;
}
}
$sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
'user_thanked' => $users_thanked[$this_user_id],
'user_thanked_others' => $users_thanked_others[$this_user_id],
)) . " WHERE user_id = $this_user_id";
$db->sql_query($sql);
}
$sql2 = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array(
'config_name' => 'thanks_version',
'config_value' => '0.4.0',
));
$db->sql_query($sql2);
trigger_error($user->lang['TP_040_UPGRADED']);
}
// Output thanks list
function get_thanks($post_id)
{
global $db, $users, $poster_id, $thankers;
$return = '';
$user_list = array();
foreach($thankers as $key => $value)
{
if ($thankers[$key]['post_id'] == $post_id && $thankers[$key]['user_id'] != $poster_id)
{
$user_list[ strtolower( $users[$thankers[$key]['user_id']]['username'] ) ] = array(
'username' => $users[$thankers[$key]['user_id']]['username'],
'user_id' => $users[$thankers[$key]['user_id']]['user_id'],
'user_colour' => $users[$thankers[$key]['user_id']]['user_colour'],
);
}
}
ksort($user_list);
$i = 0;
foreach($user_list as $key => $value)
{
if ($i > 0)
{
$return .= ', ';
}
$i++;
$return .= get_username_string('full', $value['user_id'], $value['username'], $value['user_colour'], $value['username']);
}
$return = ($return == '') ? false : $return;
return $return;
}
function get_thanks_number($post_id)
{
global $db, $thankers;
$i = 0;
foreach($thankers as $key => $value)
{
if ($thankers[$key]['post_id'] == $post_id)
{
$i++;
}
}
return $i;
}
// add a user to the thanks list
function insert_thanks($post_id, $user_id)
{
global $db, $users;
if ($user_id != ANONYMOUS)
{
$sql = 'SELECT *
FROM ' . THANKS_TABLE . "
WHERE post_id = $post_id
AND user_id = $user_id
LIMIT 1";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$to_id = request_var('to_id', 0);
if (empty($row) && !empty($to_id))
{
$sql2 = 'INSERT INTO ' . THANKS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
'user_id' => $user_id,
'post_id' => $post_id
));
$db->sql_query($sql2);
$users[$user_id]['thanks_give'] += 1;
$users[$to_id]['thanks_receive'] += 1;
$sql1 = 'UPDATE ' . USERS_TABLE . '
SET user_thanked_others = ' . $users[$user_id]['thanks_give'] . "
WHERE user_id = $user_id";
$sql3 = 'UPDATE ' . USERS_TABLE . '
SET user_thanked = ' . $users[$to_id]['thanks_receive'] . "
WHERE user_id = $to_id";
$db->sql_query($sql1);
$db->sql_query($sql3);
}
}
}
// remove a user's thanks
function delete_thanks($post_id, $user_id)
{
global $db, $user, $users;
if ($user_id != ANONYMOUS)
{
$sql = 'SELECT *
FROM ' . THANKS_TABLE . "
WHERE post_id = $post_id AND user_id = $user_id
LIMIT 1";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$to_id = request_var('to_id', 0);
if (!empty($row) && !empty($to_id))
{
$sql = "DELETE FROM " . THANKS_TABLE . "
WHERE post_id = $post_id AND user_id = " . $user->data['user_id'];
$db->sql_query($sql);
$users[$user_id]['thanks_give'] -= 1;
$users[$to_id]['thanks_receive'] -= 1;
$sql1 = 'UPDATE ' . USERS_TABLE . '
SET user_thanked_others = ' . $users[$user_id]['thanks_give'] . "
WHERE user_id = $user_id";
$sql3 = 'UPDATE ' . USERS_TABLE . '
SET user_thanked = ' . $users[$to_id]['thanks_receive'] . "
WHERE user_id = $to_id";
$db->sql_query($sql1);
$db->sql_query($sql3);
}
}
}
// display the text/image saying either to add or remove thanks
function get_thanks_text($post_id)
{
global $db, $user, $postrow;
if (already_thanked($post_id, $user->data['user_id']))
{
$postrow = array_merge($postrow, array(
'THANK_ALT' => $user->lang['REMOVE_THANKS'],
'THANK_ALT2' => $user->lang['THANK_POST2'],
'THANKS_IMG' => '/removethanks.gif',
));
return;
}
$postrow = array_merge($postrow, array(
'THANK_ALT' => $user->lang['THANK_POST1'],
'THANK_ALT2' => $user->lang['THANK_POST2'],
'THANKS_IMG' => '/thankposts.gif',
));
return;
}
// change the variable sent via the link to avoid odd errors
function get_thanks_link($post_id)
{
global $db, $user;
if (already_thanked($post_id, $user->data['user_id']))
{
return 'rthanks';
}
return 'thanks';
}
// check if the user has already thanked that post
function already_thanked($post_id, $user_id)
{
global $db, $thankers;
$thanked = false;
foreach($thankers as $key => $value)
{
if ($thankers[$key]['post_id'] == $post_id && $thankers[$key]['user_id'] == $user_id)
{
$thanked = true;
}
}
return $thanked;
}
// check gender in applicable
function get_gender($user_id)
{
global $users, $user;
if ($user_id == ANONYMOUS || $users[$user_id]['user_gender'] == false)
{
return $user->lang['THANK_GENDER_U'];
}
else if ($users[$user_id]['user_gender'] == 1)
{
return $user->lang['THANK_GENDER_M'];
}
else if ($users[$user_id]['user_gender'] == 2)
{
return $user->lang['THANK_GENDER_F'];
}
return $user->lang['THANK_GENDER_U'];
}
// gets the number of users that have thanked the poster
function get_user_count($user_id, $receive)
{
global $users;
if ($receive)
{
return $users[$user_id]['thanks_receive'];
}
else
{
return $users[$user_id]['thanks_give'];
}
}
// stuff goes here to avoid over-editing viewtopic.php
function output_thanks($user_id)
{
global $db, $user, $poster_id, $postrow, $row, $phpEx, $topic_data, $phpbb_root_path;
if (!empty($postrow))
{
$forum_id = (isset($forum_id)) ? $forum_id : 0;
$number = get_thanks_number($row['post_id']) . ' ';
$pl_text = $user->lang['THANK_TEXT_2pl'];
if ($number == 1)
{
$pl_text = $user->lang['THANK_TEXT_2'];
$number = '';
}
get_thanks_text($row['post_id']);
$postrow = array_merge($postrow, array(
'THANKS_GENDER' => ' ' . get_gender($user_id),
'THANKS' => get_thanks($row['post_id']),
'THANKS_LINK' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id'] . (($topic_data['topic_type'] == POST_GLOBAL) ? '&f=' . $forum_id : '') . '&' . get_thanks_link($row['post_id']) . '=' . $row['post_id'] . '&to_id=' . $poster_id) . '#p' . $row['post_id'],
'THANK_TEXT' => $user->lang['THANK_TEXT_1'] . ' ' . $number . $pl_text . ' ',
'POSTER_RECEIVE_COUNT' => get_user_count($poster_id, true),
'POSTER_GIVE_COUNT' => get_user_count($poster_id, false),
'S_IS_OWN_POST' => ($user->data['user_id'] == $poster_id) ? true : false,
));
}
}
?> |
another thing for they who use rewrite mod shoud find this line:
| Code: | | 'THANKS_LINK' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id']) . (($topic_data['topic_type'] == POST_GLOBAL) ? '&f=' . $forum_id : '') . '&' . get_thanks_link($row['post_id']) . '=' . $row['post_id'] . '&to_id=' . $poster_id . '#p' . $row['post_id'], |
and replace with:
| Code: | | 'THANKS_LINK' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'p=' . $row['post_id'] . (($topic_data['topic_type'] == POST_GLOBAL) ? '&f=' . $forum_id : '') . '&' . get_thanks_link($row['post_id']) . '=' . $row['post_id'] . '&to_id=' . $poster_id) . '#p' . $row['post_id'], |
to work.
than u.  |
_________________ انجمن تخصصی موبایل برای ایرانیان
http://www.mobile4persian.com
برنامه موبایل - بازی موبایل - تم موبایل - اخبار موبایل - ترفند موبایل - s60 -s60 3rd- s80-s90-java-pocket pc-palm-ppc-UIQ3-UIQ |
|
| Back to top |
|
 |
|
 |
Professional PR2


Joined: 07 Apr 2008 Posts: 252 Location: 1/2 of the World
|
|
| Back to top |
|
 |
dcz Administrateur - Site Admin

Joined: 28 Apr 2006 Posts: 15242
|
|
| Back to top |
|
 |
Professional PR2


Joined: 07 Apr 2008 Posts: 252 Location: 1/2 of the World
|
Posted: Wed Aug 13, 2008 4:09 pm Post subject: Re: helping to optimze thank post mod for phpbb3 |
|
|
yes but after this,when i wanted to insr this for new mod:
| Code: | CREATE TABLE `phpbb_thanks` (
`thanks_id` mediumint(8) unsigned NOT NULL auto_increment,
`thanks_from` mediumint(8) unsigned NOT NULL,
`thanks_to` mediumint(8) unsigned NOT NULL,
`post_id` mediumint(8) unsigned NOT NULL,
PRIMARY KEY (`thanks_id`)
);
ALTER TABLE `phpbb_posts` ADD `post_thanked` INT(11) UNSIGNED NOT NULL DEFAULT '0';
ALTER TABLE `phpbb_users` ADD `user_thanked` INT(11) UNSIGNED NOT NULL DEFAULT '0',
ADD `user_thanks` INT(11) UNSIGNED NOT NULL DEFAULT '0',
ADD `user_thanks_post` INT(11) UNSIGNED NOT NULL DEFAULT '0'; |
it says:
| Code: | #1060 - Duplicate column name 'user_thanked'
#1060 - Duplicate column name 'post_thanked' |
i think i shoud remove this two  |
_________________ انجمن تخصصی موبایل برای ایرانیان
http://www.mobile4persian.com
برنامه موبایل - بازی موبایل - تم موبایل - اخبار موبایل - ترفند موبایل - s60 -s60 3rd- s80-s90-java-pocket pc-palm-ppc-UIQ3-UIQ |
|
| Back to top |
|
 |
SeO Administrateur - Site Admin

Joined: 15 Mar 2006 Posts: 3987
|
Posted: Sat Aug 16, 2008 8:06 am Post subject: Re: helping to optimze thank post mod for phpbb3 |
|
|
Yes, for a clean install, it would be better to first drop these two columns, it's rather easy using phpmyadmin.
Chances are great though that both mods use the same column definitions since they use the same column names, so it could work right away. |
_________________ phpBB SEO || SEO Forum || Forum Référencement
GYM Sitemap & RSS for phpBB3 has been released ! || GYM Sitemap & RSS for phpBB3 est disponible ! |
|
| Back to top |
|
 |
Professional PR2


Joined: 07 Apr 2008 Posts: 252 Location: 1/2 of the World
|
|
| Back to top |
|
 |
|
|
| Navigation |
Similar Topics |
|
|
|
|
|
|
|