I didn't read EVERY page of this thread, but here are a couple fixes (hopefully not already posted):
1) Get rid of excess queries for every post
Okay, part one here deals with this piece of code:
//fetch the rules of the forum, and check so that the user is allowed to download.
$result_attach_two = $db->query('SELECT ar.rules FROM '.$db->prefix.'attach_2_rules AS ar WHERE ar.group_id=\''.$pun_user['group_id'].'\' AND ar.forum_id='.$cur_topic['forum_id'].' LIMIT 1')or error('Unable to fetch rules for the attachments', __FILE__, __LINE__, $db->error());
if($db->num_rows($result_attach_two)==1){
list($attach_rules)=$db->fetch_row($result_attach_two);
$attach_allow_download = attach_rules($attach_rules,ATTACH_DOWNLOAD);
}
There is no conceivable reason why the attachment rules would need to be checked for every post because the user's group_id and forum_id don't change for each post. The fix? Move this above or below the SQL block beginning with "// Retrieve the posts (and their respective poster/online status)", whichever tickles your fancy. Oh, and if I've somehow gotten this wrong and this query isn't run every post (since it's just completely silly to be doing it) I apologize, but it seems to me that it is
Now for part two, dealing with this:
// Check if this post has any attachments
$result_attach = $db->query('SELECT af.id, af.filename, af.size, af.downloads FROM '.$db->prefix.'attach_2_files AS af WHERE af.post_id='.$cur_post['id']) or error('Unable to fetch if there were any attachments to the post', __FILE__, __LINE__, $db->error());
$attach_num = $db->num_rows($result_attach);
if($attach_num > 0){ ...
You can cut this part out pretty easily by adding the following to your post retrieval query
LEFT JOIN '.$db->prefix.'attach_2_files AS af ON af.post_id=p.id
This simply says "additionally, grab all posts that also have a corresponding attachment". Of course, since you can have more than one attachment per post, we need to clean dupes. Now, because of how PunBB is setup, we need to add a loop before the fact and get all the attachments for a specific post and clean dupes that way. If you want an arguably faster solution, you could build all the info for posts prior to outputting the template for a post instead of having the html spit out every loop, but I won't go into that.
FIND
while($cur_posts = $db->fetch_assoc($result)
{
REPLACE WITH
// i added this method to dblayer/pgsql.php, you could use <db>_fetch_all($result) also
$posts = $db->fetch_all($result);
$x = 0;
$post_ids = array();
foreach($posts as $post)
{
// if it has an attachment
if($post['attach_id'] != '')
{
// MAY NEED TO BE CHANGED! Or made dynamic, but I'm lazy. This basically depends on how many mods you have that affect the posts query.
// in reality the slice isn't even necessary, it just cuts down on array size
$attachments[$post['id']][] = array_slice($post,22,4);
}
// if this post is a dupe
if(in_array($post['id'],$post_ids))
{
// remove it
array_splice($posts,$x,1);
}
$post_ids[] = $post['id'];
$x++;
}
foreach ($posts as $cur_post)
{
From here it's just simple changes to the loop that generates $attach_output, and you're done! I would paste those changes, but I haven't actually bothered to go through and make them yet (or even install the mod ) I read that you didn't want to alter PunBB's posts query, but the fact is that almost all mods change the fundamental queries on pages, this is to limit the number of queries. Near as I can tell, you're adding n*2 queries per viewtopic page, where n = # replies. That number can add up pretty fast, and although JOINs add overhead to a query, it's not as bad as asking the DB for stuff 30 times per page or more. However, I still respect your decisions for the mod and am posting this more for those who want to replace your queries with a quick LEFT JOIN and PHP loop.
2) PostgreSQL Install Support
Since no mod developer anywhere seems to understand that MySQL syntax != standard, I've taken the liberty of posting the raw SQL for creating your tables in PostgreSQL below. Obvious changes need to be made (pun_ replaced by $db->prefix, etc).
CREATE SEQUENCE "public"."pun_attach_2_files_id_seq";
CREATE TABLE "public"."pun_attach_2_files" (
"id" SERIAL,
"owner" INTEGER DEFAULT 0 NOT NULL,
"post_id" INTEGER DEFAULT 0 NOT NULL,
"filename" VARCHAR(20) DEFAULT 'error.file'::character varying NOT NULL,
"extension" VARCHAR(64) DEFAULT 'error.file'::character varying NOT NULL,
"mime" VARCHAR(64),
"location" TEXT,
"size" INTEGER DEFAULT 0 NOT NULL,
"downloads" INTEGER DEFAULT 0 NOT NULL,
CONSTRAINT "pun_attach_2_files_pkey" PRIMARY KEY("id")
);
CREATE SEQUENCE "public"."pun_attach_2_rules_id_seq";
CREATE TABLE "public"."pun_attach_2_rules" (
"id" SERIAL,
"forum_id" INTEGER DEFAULT 0 NOT NULL,
"group_id" INTEGER DEFAULT 0 NOT NULL,
"rules" INTEGER DEFAULT 0 NOT NULL,
"size" INTEGER DEFAULT 0 NOT NULL,
"per_post" SMALLINT DEFAULT 1 NOT NULL,
"file_ext" VARCHAR(200),
CONSTRAINT "pun_attach_2_rules_pkey" PRIMARY KEY("id")
);
I went with a VARCHAR for the file_ext field because it's impossible that someone would need a TEXT block to enter some file extensions...
And of course, thanks for creating the mod