Commit daeb8c4a by Scott

Coding style (feed)

parent ac597698
...@@ -20,52 +20,51 @@ ...@@ -20,52 +20,51 @@
More about this license: http://www.question2answer.org/license.php More about this license: http://www.question2answer.org/license.php
*/ */
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../'); header('Location: ../');
exit; exit;
} }
@ini_set('display_errors', 0); // we don't want to show PHP errors to RSS readers @ini_set('display_errors', 0); // we don't want to show PHP errors to RSS readers
qa_report_process_stage('init_feed'); qa_report_process_stage('init_feed');
require_once QA_INCLUDE_DIR.'app/options.php'; require_once QA_INCLUDE_DIR . 'app/options.php';
// Functions used within this file // Functions used within this file
function qa_feed_db_fail_handler($type, $errno=null, $error=null, $query=null) /**
/* * Database failure handler function for RSS feeds - outputs HTTP and text errors
Database failure handler function for RSS feeds - outputs HTTP and text errors */
*/ function qa_feed_db_fail_handler($type, $errno = null, $error = null, $query = null)
{
{
header('HTTP/1.1 500 Internal Server Error'); header('HTTP/1.1 500 Internal Server Error');
echo qa_lang_html('main/general_error'); echo qa_lang_html('main/general_error');
qa_exit('error'); qa_exit('error');
} }
function qa_feed_not_found() /**
/* * Common function called when a non-existent feed is requested - outputs HTTP and text errors
Common function called when a non-existent feed is requested - outputs HTTP and text errors */
*/ function qa_feed_not_found()
{ {
header('HTTP/1.0 404 Not Found'); header('HTTP/1.0 404 Not Found');
echo qa_lang_html('misc/feed_not_found'); echo qa_lang_html('misc/feed_not_found');
qa_exit(); qa_exit();
} }
function qa_feed_load_ifcategory($categoryslugs, $allkey, $catkey, &$title, /**
$questionselectspec1=null, $questionselectspec2=null, $questionselectspec3=null, $questionselectspec4=null) * Common function to load appropriate set of questions for requested feed, check category exists, and set up page title
/* */
Common function to load appropriate set of questions for requested feed, check category exists, and set up page title function qa_feed_load_ifcategory($categoryslugs, $allkey, $catkey, &$title,
*/ $questionselectspec1 = null, $questionselectspec2 = null, $questionselectspec3 = null, $questionselectspec4 = null)
{ {
$countslugs=@count($categoryslugs); $countslugs = @count($categoryslugs);
list($questions1, $questions2, $questions3, $questions4, $categories, $categoryid)=qa_db_select_with_pending( list($questions1, $questions2, $questions3, $questions4, $categories, $categoryid) = qa_db_select_with_pending(
$questionselectspec1, $questionselectspec1,
$questionselectspec2, $questionselectspec2,
$questionselectspec3, $questionselectspec3,
...@@ -78,7 +77,7 @@ ...@@ -78,7 +77,7 @@
qa_feed_not_found(); qa_feed_not_found();
if (isset($allkey)) if (isset($allkey))
$title=(isset($categoryid) && isset($catkey)) ? qa_lang_sub($catkey, $categories[$categoryid]['title']) : qa_lang($allkey); $title = (isset($categoryid) && isset($catkey)) ? qa_lang_sub($catkey, $categories[$categoryid]['title']) : qa_lang($allkey);
return array_merge( return array_merge(
is_array($questions1) ? $questions1 : array(), is_array($questions1) ? $questions1 : array(),
...@@ -86,141 +85,139 @@ ...@@ -86,141 +85,139 @@
is_array($questions3) ? $questions3 : array(), is_array($questions3) ? $questions3 : array(),
is_array($questions4) ? $questions4 : array() is_array($questions4) ? $questions4 : array()
); );
} }
// Connect to database and get the type of feed and category requested (in some cases these are overridden later) // Connect to database and get the type of feed and category requested (in some cases these are overridden later)
qa_db_connect('qa_feed_db_fail_handler'); qa_db_connect('qa_feed_db_fail_handler');
qa_preload_options(); qa_preload_options();
$requestlower=strtolower(qa_request()); $requestlower = strtolower(qa_request());
$foursuffix=substr($requestlower, -4); $foursuffix = substr($requestlower, -4);
if ( ($foursuffix=='.rss') || ($foursuffix=='.xml') ) if ($foursuffix == '.rss' || $foursuffix == '.xml') {
$requestlower=substr($requestlower, 0, -4); $requestlower = substr($requestlower, 0, -4);
}
$requestlowerparts=explode('/', $requestlower); $requestlowerparts = explode('/', $requestlower);
$feedtype=@$requestlowerparts[1]; $feedtype = @$requestlowerparts[1];
$feedparams=array_slice($requestlowerparts, 2); $feedparams = array_slice($requestlowerparts, 2);
// Choose which option needs to be checked to determine if this feed can be requested, and stop if no matches // Choose which option needs to be checked to determine if this feed can be requested, and stop if no matches
$feedoption=null; $feedoption = null;
$categoryslugs=$feedparams; $categoryslugs = $feedparams;
switch ($feedtype) { switch ($feedtype) {
case 'questions': case 'questions':
$feedoption='feed_for_questions'; $feedoption = 'feed_for_questions';
break; break;
case 'hot': case 'hot':
$feedoption='feed_for_hot'; $feedoption = 'feed_for_hot';
if (!QA_ALLOW_UNINDEXED_QUERIES) if (!QA_ALLOW_UNINDEXED_QUERIES)
$categoryslugs=null; $categoryslugs = null;
break; break;
case 'unanswered': case 'unanswered':
$feedoption='feed_for_unanswered'; $feedoption = 'feed_for_unanswered';
if (!QA_ALLOW_UNINDEXED_QUERIES) if (!QA_ALLOW_UNINDEXED_QUERIES)
$categoryslugs=null; $categoryslugs = null;
break; break;
case 'answers': case 'answers':
case 'comments': case 'comments':
case 'activity': case 'activity':
$feedoption='feed_for_activity'; $feedoption = 'feed_for_activity';
break; break;
case 'qa': case 'qa':
$feedoption='feed_for_qa'; $feedoption = 'feed_for_qa';
break; break;
case 'tag': case 'tag':
if (strlen(@$feedparams[0])) { if (strlen(@$feedparams[0])) {
$feedoption='feed_for_tag_qs'; $feedoption = 'feed_for_tag_qs';
$categoryslugs=null; $categoryslugs = null;
} }
break; break;
case 'search': case 'search':
if (strlen(@$feedparams[0])) { if (strlen(@$feedparams[0])) {
$feedoption='feed_for_search'; $feedoption = 'feed_for_search';
$categoryslugs=null; $categoryslugs = null;
} }
break; break;
} }
$countslugs=@count($categoryslugs); $countslugs = @count($categoryslugs);
if (!isset($feedoption)) if (!isset($feedoption))
qa_feed_not_found(); qa_feed_not_found();
// Check that all the appropriate options are in place to allow this feed to be retrieved // Check that all the appropriate options are in place to allow this feed to be retrieved
if (!( if (!(qa_opt($feedoption) && ($countslugs ? (qa_using_categories() && qa_opt('feed_per_category')) : true)))
(qa_opt($feedoption)) &&
($countslugs ? (qa_using_categories() && qa_opt('feed_per_category')) : true)
))
qa_feed_not_found(); qa_feed_not_found();
// Retrieve the appropriate questions and other information for this feed // Retrieve the appropriate questions and other information for this feed
require_once QA_INCLUDE_DIR.'db/selects.php'; require_once QA_INCLUDE_DIR . 'db/selects.php';
$sitetitle=qa_opt('site_title'); $sitetitle = qa_opt('site_title');
$siteurl=qa_opt('site_url'); $siteurl = qa_opt('site_url');
$full=qa_opt('feed_full_text'); $full = qa_opt('feed_full_text');
$count=qa_opt('feed_number_items'); $count = qa_opt('feed_number_items');
$showurllinks=qa_opt('show_url_links'); $showurllinks = qa_opt('show_url_links');
$linkrequest=$feedtype.($countslugs ? ('/'.implode('/', $categoryslugs)) : ''); $linkrequest = $feedtype . ($countslugs ? ('/' . implode('/', $categoryslugs)) : '');
$linkparams=null; $linkparams = null;
switch ($feedtype) { switch ($feedtype) {
case 'questions': case 'questions':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/recent_qs_title', 'main/recent_qs_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/recent_qs_title', 'main/recent_qs_in_x', $title,
qa_db_qs_selectspec(null, 'created', 0, $categoryslugs, null, false, $full, $count) qa_db_qs_selectspec(null, 'created', 0, $categoryslugs, null, false, $full, $count)
); );
break; break;
case 'hot': case 'hot':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/hot_qs_title', 'main/hot_qs_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/hot_qs_title', 'main/hot_qs_in_x', $title,
qa_db_qs_selectspec(null, 'hotness', 0, $categoryslugs, null, false, $full, $count) qa_db_qs_selectspec(null, 'hotness', 0, $categoryslugs, null, false, $full, $count)
); );
break; break;
case 'unanswered': case 'unanswered':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/unanswered_qs_title', 'main/unanswered_qs_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/unanswered_qs_title', 'main/unanswered_qs_in_x', $title,
qa_db_unanswered_qs_selectspec(null, null, 0, $categoryslugs, false, $full, $count) qa_db_unanswered_qs_selectspec(null, null, 0, $categoryslugs, false, $full, $count)
); );
break; break;
case 'answers': case 'answers':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/recent_as_title', 'main/recent_as_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/recent_as_title', 'main/recent_as_in_x', $title,
qa_db_recent_a_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count) qa_db_recent_a_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count)
); );
break; break;
case 'comments': case 'comments':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/recent_cs_title', 'main/recent_cs_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/recent_cs_title', 'main/recent_cs_in_x', $title,
qa_db_recent_c_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count) qa_db_recent_c_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count)
); );
break; break;
case 'qa': case 'qa':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/recent_qs_as_title', 'main/recent_qs_as_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/recent_qs_as_title', 'main/recent_qs_as_in_x', $title,
qa_db_qs_selectspec(null, 'created', 0, $categoryslugs, null, false, $full, $count), qa_db_qs_selectspec(null, 'created', 0, $categoryslugs, null, false, $full, $count),
qa_db_recent_a_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count) qa_db_recent_a_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count)
); );
break; break;
case 'activity': case 'activity':
$questions=qa_feed_load_ifcategory($categoryslugs, 'main/recent_activity_title', 'main/recent_activity_in_x', $title, $questions = qa_feed_load_ifcategory($categoryslugs, 'main/recent_activity_title', 'main/recent_activity_in_x', $title,
qa_db_qs_selectspec(null, 'created', 0, $categoryslugs, null, false, $full, $count), qa_db_qs_selectspec(null, 'created', 0, $categoryslugs, null, false, $full, $count),
qa_db_recent_a_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count), qa_db_recent_a_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count),
qa_db_recent_c_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count), qa_db_recent_c_qs_selectspec(null, 0, $categoryslugs, null, false, $full, $count),
...@@ -229,200 +226,195 @@ ...@@ -229,200 +226,195 @@
break; break;
case 'tag': case 'tag':
$tag=$feedparams[0]; $tag = $feedparams[0];
$questions=qa_feed_load_ifcategory(null, null, null, $title, $questions = qa_feed_load_ifcategory(null, null, null, $title,
qa_db_tag_recent_qs_selectspec(null, $tag, 0, $full, $count) qa_db_tag_recent_qs_selectspec(null, $tag, 0, $full, $count)
); );
$title=qa_lang_sub('main/questions_tagged_x', $tag); $title = qa_lang_sub('main/questions_tagged_x', $tag);
$linkrequest='tag/'.$tag; $linkrequest = 'tag/' . $tag;
break; break;
case 'search': case 'search':
require_once QA_INCLUDE_DIR.'app/search.php'; require_once QA_INCLUDE_DIR . 'app/search.php';
$query=$feedparams[0]; $query = $feedparams[0];
$results=qa_get_search_results($query, 0, $count, null, true, $full); $results = qa_get_search_results($query, 0, $count, null, true, $full);
$title=qa_lang_sub('main/results_for_x', $query); $title = qa_lang_sub('main/results_for_x', $query);
$linkrequest='search'; $linkrequest = 'search';
$linkparams=array('q' => $query); $linkparams = array('q' => $query);
$questions=array(); $questions = array();
foreach ($results as $result) { foreach ($results as $result) {
$setarray=array( $setarray = array(
'title' => $result['title'], 'title' => $result['title'],
'url' => $result['url'], 'url' => $result['url'],
); );
if (isset($result['question'])) if (isset($result['question']))
$questions[]=array_merge($result['question'], $setarray); $questions[] = array_merge($result['question'], $setarray);
elseif (isset($result['url'])) elseif (isset($result['url']))
$questions[]=$setarray; $questions[] = $setarray;
} }
break; break;
} }
// Remove duplicate questions (perhaps referenced in an answer and a comment) and cut down to size // Remove duplicate questions (perhaps referenced in an answer and a comment) and cut down to size
require_once QA_INCLUDE_DIR.'app/format.php'; require_once QA_INCLUDE_DIR . 'app/format.php';
require_once QA_INCLUDE_DIR.'app/updates.php'; require_once QA_INCLUDE_DIR . 'app/updates.php';
require_once QA_INCLUDE_DIR.'util/string.php'; require_once QA_INCLUDE_DIR . 'util/string.php';
if ( ($feedtype!='search') && ($feedtype!='hot') ) // leave search results and hot questions sorted by relevance if ($feedtype != 'search' && $feedtype != 'hot') // leave search results and hot questions sorted by relevance
$questions=qa_any_sort_and_dedupe($questions); $questions = qa_any_sort_and_dedupe($questions);
$questions=array_slice($questions, 0, $count); $questions = array_slice($questions, 0, $count);
$blockwordspreg=qa_get_block_words_preg(); $blockwordspreg = qa_get_block_words_preg();
// Prepare the XML output // Prepare the XML output
$lines=array(); $lines = array();
$lines[]='<?xml version="1.0" encoding="utf-8"?>'; $lines[] = '<?xml version="1.0" encoding="utf-8"?>';
$lines[]='<rss version="2.0">'; $lines[] = '<rss version="2.0">';
$lines[]='<channel>'; $lines[] = '<channel>';
$lines[]='<title>'.qa_xml($sitetitle.' - '.$title).'</title>'; $lines[] = '<title>' . qa_xml($sitetitle . ' - ' . $title) . '</title>';
$lines[]='<link>'.qa_xml(qa_path($linkrequest, $linkparams, $siteurl)).'</link>'; $lines[] = '<link>' . qa_xml(qa_path($linkrequest, $linkparams, $siteurl)) . '</link>';
$lines[]='<description>Powered by Question2Answer</description>'; $lines[] = '<description>Powered by Question2Answer</description>';
foreach ($questions as $question) { foreach ($questions as $question) {
// Determine whether this is a question, answer or comment, and act accordingly // Determine whether this is a question, answer or comment, and act accordingly
$options=array('blockwordspreg' => @$blockwordspreg, 'showurllinks' => $showurllinks); $options = array('blockwordspreg' => @$blockwordspreg, 'showurllinks' => $showurllinks);
$time=null; $time = null;
$htmlcontent=null; $htmlcontent = null;
if (isset($question['opostid'])) { if (isset($question['opostid'])) {
$time=$question['otime']; $time = $question['otime'];
if ($full) if ($full)
$htmlcontent=qa_viewer_html($question['ocontent'], $question['oformat'], $options); $htmlcontent = qa_viewer_html($question['ocontent'], $question['oformat'], $options);
} elseif (isset($question['postid'])) { } elseif (isset($question['postid'])) {
$time=$question['created']; $time = $question['created'];
if ($full) if ($full)
$htmlcontent=qa_viewer_html($question['content'], $question['format'], $options); $htmlcontent = qa_viewer_html($question['content'], $question['format'], $options);
} }
if ($feedtype=='search') { if ($feedtype == 'search') {
$titleprefix=''; $titleprefix = '';
$urlxml=qa_xml($question['url']); $urlxml = qa_xml($question['url']);
} else { } else {
switch (@$question['obasetype'].'-'.@$question['oupdatetype']) { switch (@$question['obasetype'] . '-' . @$question['oupdatetype']) {
case 'Q-': case 'Q-':
case '-': case '-':
$langstring=null; $langstring = null;
break; break;
case 'Q-'.QA_UPDATE_VISIBLE: case 'Q-' . QA_UPDATE_VISIBLE:
$langstring=$question['hidden'] ? 'misc/feed_hidden_prefix' : 'misc/feed_reshown_prefix'; $langstring = $question['hidden'] ? 'misc/feed_hidden_prefix' : 'misc/feed_reshown_prefix';
break; break;
case 'Q-'.QA_UPDATE_CLOSED: case 'Q-' . QA_UPDATE_CLOSED:
$langstring=isset($question['closedbyid']) ? 'misc/feed_closed_prefix' : 'misc/feed_reopened_prefix'; $langstring = isset($question['closedbyid']) ? 'misc/feed_closed_prefix' : 'misc/feed_reopened_prefix';
break; break;
case 'Q-'.QA_UPDATE_TAGS: case 'Q-' . QA_UPDATE_TAGS:
$langstring='misc/feed_retagged_prefix'; $langstring = 'misc/feed_retagged_prefix';
break; break;
case 'Q-'.QA_UPDATE_CATEGORY: case 'Q-' . QA_UPDATE_CATEGORY:
$langstring='misc/feed_recategorized_prefix'; $langstring = 'misc/feed_recategorized_prefix';
break; break;
case 'A-': case 'A-':
$langstring='misc/feed_a_prefix'; $langstring = 'misc/feed_a_prefix';
break; break;
case 'A-'.QA_UPDATE_SELECTED: case 'A-' . QA_UPDATE_SELECTED:
$langstring='misc/feed_a_selected_prefix'; $langstring = 'misc/feed_a_selected_prefix';
break; break;
case 'A-'.QA_UPDATE_VISIBLE: case 'A-' . QA_UPDATE_VISIBLE:
$langstring=$question['ohidden'] ? 'misc/feed_hidden_prefix' : 'misc/feed_a_reshown_prefix'; $langstring = $question['ohidden'] ? 'misc/feed_hidden_prefix' : 'misc/feed_a_reshown_prefix';
break; break;
case 'A-'.QA_UPDATE_CONTENT: case 'A-' . QA_UPDATE_CONTENT:
$langstring='misc/feed_a_edited_prefix'; $langstring = 'misc/feed_a_edited_prefix';
break; break;
case 'C-': case 'C-':
$langstring='misc/feed_c_prefix'; $langstring = 'misc/feed_c_prefix';
break; break;
case 'C-'.QA_UPDATE_TYPE: case 'C-' . QA_UPDATE_TYPE:
$langstring='misc/feed_c_moved_prefix'; $langstring = 'misc/feed_c_moved_prefix';
break; break;
case 'C-'.QA_UPDATE_VISIBLE: case 'C-' . QA_UPDATE_VISIBLE:
$langstring=$question['ohidden'] ? 'misc/feed_hidden_prefix' : 'misc/feed_c_reshown_prefix'; $langstring = $question['ohidden'] ? 'misc/feed_hidden_prefix' : 'misc/feed_c_reshown_prefix';
break; break;
case 'C-'.QA_UPDATE_CONTENT: case 'C-' . QA_UPDATE_CONTENT:
$langstring='misc/feed_c_edited_prefix'; $langstring = 'misc/feed_c_edited_prefix';
break; break;
case 'Q-'.QA_UPDATE_CONTENT: case 'Q-' . QA_UPDATE_CONTENT:
default: default:
$langstring='misc/feed_edited_prefix'; $langstring = 'misc/feed_edited_prefix';
break; break;
} }
$titleprefix=isset($langstring) ? qa_lang($langstring) : ''; $titleprefix = isset($langstring) ? qa_lang($langstring) : '';
$urlxml=qa_xml(qa_q_path($question['postid'], $question['title'], true, @$question['obasetype'], @$question['opostid'])); $urlxml = qa_xml(qa_q_path($question['postid'], $question['title'], true, @$question['obasetype'], @$question['opostid']));
} }
if (isset($blockwordspreg)) if (isset($blockwordspreg))
$question['title']=qa_block_words_replace($question['title'], $blockwordspreg); $question['title'] = qa_block_words_replace($question['title'], $blockwordspreg);
// Build the inner XML structure for each item // Build the inner XML structure for each item
$lines[]='<item>'; $lines[] = '<item>';
$lines[]='<title>'.qa_xml($titleprefix.$question['title']).'</title>'; $lines[] = '<title>' . qa_xml($titleprefix . $question['title']) . '</title>';
$lines[]='<link>'.$urlxml.'</link>'; $lines[] = '<link>' . $urlxml . '</link>';
if (isset($htmlcontent)) if (isset($htmlcontent))
$lines[]='<description>'.qa_xml($htmlcontent).'</description>'; $lines[] = '<description>' . qa_xml($htmlcontent) . '</description>';
if (isset($question['categoryname'])) if (isset($question['categoryname']))
$lines[]='<category>'.qa_xml($question['categoryname']).'</category>'; $lines[] = '<category>' . qa_xml($question['categoryname']) . '</category>';
$lines[]='<guid isPermaLink="true">'.$urlxml.'</guid>'; $lines[] = '<guid isPermaLink="true">' . $urlxml . '</guid>';
if (isset($time)) if (isset($time))
$lines[]='<pubDate>'.qa_xml(gmdate('r', $time)).'</pubDate>'; $lines[] = '<pubDate>' . qa_xml(gmdate('r', $time)) . '</pubDate>';
$lines[]='</item>'; $lines[] = '</item>';
} }
$lines[]='</channel>'; $lines[] = '</channel>';
$lines[]='</rss>'; $lines[] = '</rss>';
// Disconnect here, once all output is ready to go // Disconnect here, once all output is ready to go
qa_db_disconnect(); qa_db_disconnect();
// Output the XML - and we're done! // Output the XML - and we're done!
header('Content-type: text/xml; charset=utf-8'); header('Content-type: text/xml; charset=utf-8');
echo implode("\n", $lines); echo implode("\n", $lines);
/*
Omit PHP closing tag to help avoid accidental output
*/
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment