Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+8 votes
1.5k views
in Q2A Core by
edited by

By default I can create feed for questions or for question's answers (without full question content).

I've found that I can change feed output in /qa-include/qa-feed.php

There is a function called qa_feed_load_ifcategory(), but despite of my poor php skills I figured out that it can either output question itself or the answer. All my tries of merging them together finished without any luck.

This is basic illustration of what I want to get in my feed:

1) Question 1 title
Question 1 content
Question 1 answer 1
Question 1 answer 2
Question 1 answer 3
-----
-----
2) Question 2 title
Question 2 content
Question 2 answer 1
Question 2 answer 2
Question 2 answer 3

-----
-----
etc...

When last parameter is passed to qa_feed_load_ifcategory(), which is $questionselectspec, the full question content is pushed away. Instead answer is added. I want to have them both.

Ofcourse, I could make some kind of workaround with my own code, but I don't want to do any crutches whith horrible piece of shit-like code.

Is there a way to achieve this through Q2A standard methods?

Q2A version: 1.8.3
by
If you do a MySQL query and display the results in XML format (instead of regular HTML format) , then you have a feed.

A normal RSS feed format contains such information like <title>, <link>, <pubDate>, <description>.

I'm not sure what your real purpose of feed modification is. But I guess you may want to display it in your Wordpress installtion.

So you have to put all the answers and comments and stuffs in to <description> tags.

Because your ideas will include extra variables like the answers content, answer postid,... or maybe you want to display author names and avatars too, you may not achieve this merely editing a few lines in core files. You have to write layout to display all these things.
by
edited by
What I'm trying to do is to add my website to Yandex Turbo Pages (the same as Google AMP, but for Yandex).

And they need special xml feed for this.

But all I can do now is feed them with trimmed content in xml, but rss feed in this situation has to represent the same content, as the normal website page.

1 Answer

0 votes
by

I didn't want to do it, but I had to radically change /qa-include/qa-feed.php code.

There are two functions in /qa-include/app/posts.php, which are:

  • qa_post_get_full() - gets the full question content by its id;
  • qa_post_get_question_answers() - gets all answers by the question id.
These functions are already available in qa-feed.php.
The only thing left unsolved is how to load more than 50 questions without performance issues.
by
Sitemap or Feed? Does Yandex require a 3000-question RSS feed? Anyway, you don't need to modify the core file. You can make a plugin. Q2A feed is actually a plugin.
by
edited by
Thank you for reply.

I need xml feed, not sitemap. And yes, Yandex needs everything. If you have 10.000 articles - load them all. However, there is a limit of 1000 items per one feed, so in that case I will need 10 separate feeds.

I can also use their API to load pages, bypassing xml, but didn't figure out yet how to do it. There will be more to do, because feed already contain all questions and answers, in case of API I will need to hang up an event for question creation, answer, comment and edit of any of them. This is tricky for me now, don't have enough knowledge, but maybe this will be the best approach.

Anyway, all of what I'm doing here is just my first test attempt, still in search of the best way.

I understand that it's not a good idea to modify core files, when I will figure out how to achieve this through my own plugin - will do so.

Currently I have 240 questions. In qa-feed.php I've changed $count variable from:

$count = qa_opt('feed_number_items');

To:

$count = 500;

But it loads only 66 items in my feed. Why?
by
+1
The $count variable is part of the function qa_db_qs_selectspec

https://github.com/q2a/question2answer/blob/0924669a1119bc441f3fb50c3c3438446619adb3/qa-include/db/selects.php

It will be recalculated as

$count = isset($count) ? min((int)$count, QA_DB_RETRIEVE_QS_AS) : QA_DB_RETRIEVE_QS_AS;

It'll be the minimum of $count and  QA_DB_RETRIEVE_QS_AS, which can be found in

 https://github.com/q2a/question2answer/blob/78b969da37468e8668559e3ffe23687ae5587129/qa-include/db/maxima.php

By the way, I won't understand why 66? It is expected be 50 in my thought.
by
I don't know why 66. I've printed count($questions) in my xml output from qa-feed.php and it gave me 66.

Ok, it's getting deeper into more core files, so I'll better dig into separate plugin.

Thank you for your help!
by
By the way, is there a function to get all questions ids? I already know qa_post_get_full() function to get specific question content, but it needs an id.

So what would be the the best practice to retrieve all questions? Or I can query DB directly?
by
edited by
$questions is actually an array of postids through a sophisticated function named function qa_db_qs_selectspec.

https://github.com/q2a/question2answer/blob/7763be86aa74f839f8fad89d30f2130f72716189/qa-include/qa-feed.php#L193

$questions = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid FROM ^posts WHERE `type`="Q"  '));
will get an array of 240 postids which are questions (not answers or comments).

I believe this is not a method for submitting Yandex Rss feed.

You may submit this only ONCE, so Yandex will pick up all the information and upload into its CDN server.

In the future you may only limit the number of feeds to 3 - 10 depending on how many new questions a day your site produce, and how regular Yandex bot picks up Rss feeds.

$questions = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid FROM ^posts WHERE `type`="Q" ORDER  BY created DESC  LIMIT  10  '));

Or you now chop your ^posts table into percentiles and create separate feeds for each.

Example:

$questions = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid FROM ^posts WHERE `type`="Q" AND `postid` BETWEEN 30 AND 60  '));

Correct all my syntax errors, as I'm not a coder.
by
Why don't you try to create a simple page module plugin that outputs XML format?

It is much easier to do than trying to modify core files.

Sitemap plugin is a good example.

https://github.com/q2a/question2answer/tree/dev/qa-plugin/xml-sitemap
by
Thank you one more time! ))

I'll look onto this plugin and take it as starting point.

>>> "I believe this is not a method for submitting Yandex Rss feed."

But it is. Turbo pages are served through Yandex servers, so I need to "upload" my content there. So there are two ways: xml feed or API. In case of xml, Yandex will load my feed every hour to find any changes.
by
bigboy, I also struggle with this problem that I just did not try it, nothing happens. If you can find how to solve the problem, please share the solution. If I find it, I'll write it too.
by
I'll write a plugin later, if I'll have time.

I can show you how I did it for now, but If you're not a programmer you will struggle a lot. But, you can try.

(!!!)
This example below IS NOT THE RIGHT SOLUTION and a really bad practice. It does not completely cover the issue and changes core files, which must to be avoided, so make backups in case something will go wrong. It's not something I would ever recommend doing. It is done only in test purposes, I'm still learning PHP and Q2A as well.
(!!!)

Go to /qa-include/qa-feed.php file.

Inside foreach ($questions as $question) section, approximately on 405 line add:

    $qcontent = qa_post_get_full($question['postid']);
    $acontent =  qa_post_get_question_answers($question['postid']);

Now, $qcontent variable contains all question data, while $acontent - holds all answers on particular question.

Then you have to comment every $lines variable and also comment this line:

//echo implode("\n", $lines);

This has to be done, because by default Q2A is printing feeds in a way Yandex Turbo will not understand. So you have to rewrite the output of qa-feed.php.

In order to debug you can print our arrays like this:
print_r($qcontent);
print_r($acontent);

You will see everything inside those arrays and will be able to print them as Yandex expects - https://yandex.ru/dev/turbo/doc/rss/example-docpage/
by
Thanks, I'll try
by
And be aware of the problem, that standard feed will provide no more than 50 questions, this number is hardcoded in Q2A core files.

So wee need to write a plugin in order to do it right way.
...