public function process_request($request) { $requestparts = explode('/', qa_request()); $slugs = array_slice($requestparts, 1); $countslugs = count($slugs); $userid = qa_get_logged_in_userid(); $start = qa_get_start(); $count = qa_opt_if_loaded('page_size_activity'); $totalcount = qa_opt('cache_qcount'); $qspec = qa_db_posts_basic_selectspec($userid, false); qa_db_add_selectspec_opost($qspec, 'ra', false, false); qa_db_add_selectspec_ousers($qspec, 'rau', 'raup'); $qspec['source'] .= " JOIN (SELECT questionid, childid FROM ^homepage ORDER BY ^homepage.updated DESC) AS rcaq ON ^posts.postid=rcaq.questionid" . " LEFT JOIN ^posts AS ra ON childid=ra.postid" . (QA_FINAL_EXTERNAL_USERS ? "" : " LEFT JOIN ^users AS rau ON ra.userid=rau.userid") . " LEFT JOIN ^userpoints AS raup ON ra.userid=raup.userid LIMIT #,#"; array_push($qspec['columns'], 'childid'); array_push($qspec['arguments'], $start, $count); $qspec['sortdesc'] = 'otime'; $query = 'SELECT '; foreach ($qspec['columns'] as $columnas => $columnfrom) { $query .= $columnfrom . (is_int($columnas) ? '' : ' AS ' . $columnas) . ', '; } $query = qa_db_apply_sub(substr($query, 0, -2) . (strlen(@$qspec['source']) ? ' FROM ' . $qspec['source'] : ''), @$qspec['arguments']); $results = qa_db_read_all_assoc(qa_db_query_raw($query)); qa_db_post_select($results, $qspec); list($categories, $categoryid) = qa_db_select_with_pending(qa_db_category_nav_selectspec($slugs, false, false, true), $countslugs ? qa_db_slugs_to_category_id_selectspec($slugs) : null); $questions = qa_any_sort_and_dedupe($results); // $questions=qa_any_sort_and_dedupe(array_merge($recentquestions,$recentanswers)); $pagesize = qa_opt('page_size_home'); if ($countslugs) { if (!isset($categoryid)) { return include QA_INCLUDE_DIR . 'qa-page-not-found.php'; } $categorytitlehtml = qa_html($categories[$categoryid]['title']); $sometitle = qa_lang_html_sub('main/recent_qs_as_in_x', $categorytitlehtml); $nonetitle = qa_lang_html_sub('main/no_questions_in_x', $categorytitlehtml); } else { $sometitle = qa_lang_html('main/recent_qs_as_title'); $nonetitle = qa_lang_html('main/no_questions_found'); } require_once QA_INCLUDE_DIR . 'qa-app-q-list.php'; $qa_content = qa_q_list_page_content($questions, $pagesize, $start, $totalcount, $sometitle, $nonetitle, $categories, $categoryid, true, qa_opt('eql_homepage_url'), qa_opt('feed_for_qa') ? qa_opt('eql_homepage_url') : null, count($questions) < $pagesize ? qa_html_suggest_ask($categoryid) : qa_html_suggest_qs_tags(qa_using_tags(), qa_category_path_request($categories, $categoryid)), null, null); return $qa_content; }
/** * Return the data specified by each element of $selectspecs, where the keys of the * returned array match the keys of the supplied $selectspecs array. See long comment above. */ function qa_db_multi_select($selectspecs) { if (!count($selectspecs)) { return array(); } // Perform simple queries if the database is local or there are only 0 or 1 selectspecs if (QA_OPTIMIZE_LOCAL_DB || count($selectspecs) <= 1) { $outresults = array(); foreach ($selectspecs as $selectkey => $selectspec) { $outresults[$selectkey] = qa_db_single_select($selectspec); } return $outresults; } // Otherwise, parse columns for each spec to deal with columns without an 'AS' specification foreach ($selectspecs as $selectkey => $selectspec) { $selectspecs[$selectkey]['outcolumns'] = array(); $selectspecs[$selectkey]['autocolumn'] = array(); foreach ($selectspec['columns'] as $columnas => $columnfrom) { if (is_int($columnas)) { $periodpos = strpos($columnfrom, '.'); $columnas = is_numeric($periodpos) ? substr($columnfrom, $periodpos + 1) : $columnfrom; $selectspecs[$selectkey]['autocolumn'][$columnas] = true; } if (isset($selectspecs[$selectkey]['outcolumns'][$columnas])) { qa_fatal_error('Duplicate column name in qa_db_multi_select()'); } $selectspecs[$selectkey]['outcolumns'][$columnas] = $columnfrom; } if (isset($selectspec['arraykey'])) { if (!isset($selectspecs[$selectkey]['outcolumns'][$selectspec['arraykey']])) { qa_fatal_error('Used arraykey not in columns in qa_db_multi_select()'); } } if (isset($selectspec['arrayvalue'])) { if (!isset($selectspecs[$selectkey]['outcolumns'][$selectspec['arrayvalue']])) { qa_fatal_error('Used arrayvalue not in columns in qa_db_multi_select()'); } } } // Work out the full list of columns used $outcolumns = array(); foreach ($selectspecs as $selectspec) { $outcolumns = array_unique(array_merge($outcolumns, array_keys($selectspec['outcolumns']))); } // Build the query based on this full list $query = ''; foreach ($selectspecs as $selectkey => $selectspec) { $subquery = "(SELECT '" . qa_db_escape_string($selectkey) . "'" . (empty($query) ? ' AS selectkey' : ''); foreach ($outcolumns as $columnas) { $subquery .= ', ' . (isset($selectspec['outcolumns'][$columnas]) ? $selectspec['outcolumns'][$columnas] : 'NULL'); if (empty($query) && !isset($selectspec['autocolumn'][$columnas])) { $subquery .= ' AS ' . $columnas; } } if (strlen(@$selectspec['source'])) { $subquery .= ' FROM ' . $selectspec['source']; } $subquery .= ')'; if (strlen($query)) { $query .= ' UNION ALL '; } $query .= qa_db_apply_sub($subquery, @$selectspec['arguments']); } // Perform query and extract results $rawresults = qa_db_read_all_assoc(qa_db_query_raw($query)); $outresults = array(); foreach ($selectspecs as $selectkey => $selectspec) { $outresults[$selectkey] = array(); } foreach ($rawresults as $rawresult) { $selectkey = $rawresult['selectkey']; $selectspec = $selectspecs[$selectkey]; $keepresult = array(); foreach ($selectspec['outcolumns'] as $columnas => $columnfrom) { $keepresult[$columnas] = $rawresult[$columnas]; } if (isset($selectspec['arraykey'])) { $outresults[$selectkey][$keepresult[$selectspec['arraykey']]] = $keepresult; } else { $outresults[$selectkey][] = $keepresult; } } // Post-processing to apply various stuff include sorting request, since we can't rely on ORDER BY due to UNION foreach ($selectspecs as $selectkey => $selectspec) { qa_db_post_select($outresults[$selectkey], $selectspec); } // Return results return $outresults; }