Jump to content

Comments using separate database?


JoshoB
 Share

Recommended Posts

Hello everyone,

I manage a large site and would like to add the ability for visitors to add comments to articles. However, I also want to keep the core website database relatively "clean". So my idea was to install a fresh copy of ProcessWire to a subdomain (say, account.ancientworldmagazine.com), where users can register, sign in, and edit their profile. I could also sign in and manage comments.

My idea is to load the new database into the website, so that a page can check if there's any pages with comments in the new database. So, for example, article X on my original website gets loaded, there's an if-statement that checks if there's a page in the new database with the field awm_page_id = $page->id (where $page is the article on the original website). If yes, it proceeds to load the comments from that database; if no, it creates a new page with the field awm_page_id = $page->id and people who are signed in can then start posting comments.

So, is this feasible? Does it pose any security risks? Will it create unnecessary overhead and slow everything down? Am I better off just sacrificing the cleanliness of my website's database and enabling comments on there directly from the backend? I'd love to hear your thoughts.

Link to comment
Share on other sites

Have you looked at the structure of the PW database? If not, it's quite easy to understand, and you may find that storing comments would be a lot "cleaner" than you expect.

Essentially, the data for each field is held in a separate table, so the comments would be quite distinct from other data.

If you'd be happy with the database structure, it'd at the very least save you what I imagine would be a huge amount of work!

  • Like 1
Link to comment
Share on other sites

Oh yeah, I am familiar with the database structure.

I should have added that I want people to be signed in before they're allowed to post comments, so the website would see an influx of users. Currently, all "users" on the website are authors who write articles, and I would like to keep this list as small as possible. (I know I can use roles to manage the lists, but I would prefer a simpler solution.)

There's also the issue of security: I don't want there to be any risk that a user is able to mess anything up, and I'd prefer to keep them out of the main database.

Link to comment
Share on other sites

I think it's a good idea and plan – plus if your main site is cached with something like procache, you could load user comments via ajax from the subdomain "user" site.

I'd probably do it the same way as you; It's sort of like making your own version of a commenting platform which is integrated into the site, like Disqus or something. And if eventually at some point you had a huge user base, you could run the entire subdomain on a different server.

  • Like 1
Link to comment
Share on other sites

You can use multiple instances of processwire and include one in another.

$pwComments = new ProcessWire($config->paths->root . 'site-comments');
$home = $pages->get(1);
$homeOfComments = $pwComments->pages->get(1);

https://processwire.com/docs/more/multi-site-support/ use Option #1
https://processwire.com/blog/posts/multi-instance-pw3/#using-multi-instance-in-pw3

This is also possible under a single domain if you do not need a frontend at one of the two installations and use different names for your /admin/ pages and session cookies. Under special circumstances you can also access the current user of one instance within the other. Look here:

https://github.com/processwire/processwire-issues/issues/1294#issuecomment-759351593

 

  • Like 3
Link to comment
Share on other sites

On 6/3/2021 at 11:07 AM, JoshoB said:

What would be the best way to connect to the second database/website?

The usual way that I would do it is to create a service URL* on the 2nd website, which returns JSON to the ajax request, if you're going to load the comment markup and form via ajax.

Alternately you could use an iframe, so the 2nd website would have an output template to render the comments thread and form; this could be a single template/page with get params;

I don't see any benefit or reason to use multi-site / multi-instance (but maybe i'm missing something), unless it makes managing comments for an article easier; but you could also create a field in the comments site that pulls in the title or other info from the main install, using a service page on the main install, returning JSON response;

In terms of the service "page" or URL - there are some new api features that will allow you to have a service URL without having to actually create a page as an endpoint.

https://processwire.com/blog/posts/pw-3.0.173/

  • Like 3
Link to comment
Share on other sites

1 hour ago, Macrura said:

Alternately you could use an iframe, so the 2nd website would have an output template to render the comments thread and form; this could be a single template/page with get params;

For this to work, one needs to modify PW's root .htaccess at "# 4. Protect from XSS with Apache headers" of the "loaded site", something like:

<IfModule mod_headers.c>
# prevent site from being loaded in an iframe on another site
# you will need to remove this one if you want to allow external iframes
# Header always append X-Frame-Options SAMEORIGIN

# Lets the listed domains load content into an iFrame
Header always append Content-Security-Policy "frame-ancestors 'self' example.com example.test www.anotherexample.com;"

also, it is probably a good idea to turn the iframe into a responsive container: https://github.com/davidjbradshaw/iframe-resizer

  • Like 2
Link to comment
Share on other sites

Edit (complete wipe of original message):

I couldn't get this to function, so I've changed how I handled my authors to clean up the users for comments etc. This means I'm using the original database/install instead of a separate one for comments.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...