plugins/nuggie/planet.php
changeset 8 160f1170aa32
parent 7 cd46e29ae699
child 10 219a0133622e
equal deleted inserted replaced
7:cd46e29ae699 8:160f1170aa32
    19 
    19 
    20 function nuggie_planet_uri_handler($page)
    20 function nuggie_planet_uri_handler($page)
    21 {
    21 {
    22   global $db, $session, $paths, $template, $plugins; // Common objects
    22   global $db, $session, $paths, $template, $plugins; // Common objects
    23   
    23   
    24   $planet_id = $page->page_id;
    24   $planet_id = dirtify_page_id($page->page_id);
       
    25   $offset = 0;
       
    26   if ( preg_match('#/start=([0-9]+)$#', $planet_id, $match) )
       
    27   {
       
    28     $planet_id = substr($planet_id, 0, (strlen($planet_id) - strlen($match[0])));
       
    29     $offset = intval($match[1]);
       
    30   }
    25   
    31   
    26   //
    32   //
    27   // VALIDATION
    33   // VALIDATION
    28   //
    34   //
    29   
    35   
    40     $db->_die();
    46     $db->_die();
    41   
    47   
    42   if ( $db->numrows() < 1 )
    48   if ( $db->numrows() < 1 )
    43   {
    49   {
    44     // planet not found, fall out
    50     // planet not found, fall out
    45     return false;
    51     return $page->err_page_not_existent();
    46   }
    52   }
    47   
    53   
    48   // fetch first row, then seek back to the first result to allow mapping fetching later
    54   // fetch first row, then seek back to the first result to allow mapping fetching later
    49   $planet_data = $db->fetchrow();
    55   $planet_data = $db->fetchrow();
    50   $db->sql_data_seek(0);
    56   $db->sql_data_seek(0);
    59   if ( !$perms->get_permissions('read') )
    65   if ( !$perms->get_permissions('read') )
    60   {
    66   {
    61     return $page->err_access_denied();
    67     return $page->err_access_denied();
    62   }
    68   }
    63   
    69   
    64   // fetch mappings to prepare to select the actual blog data
    70   // Set page title
    65   echo 'WiP';
    71   $page_title = dirtify_page_id($planet_data['planet_name']);
       
    72   $template->assign_vars(array(
       
    73       'PAGE_NAME' => htmlspecialchars($page_title)
       
    74     ));
       
    75   
       
    76   // Try to grab the posts. The SQL tricks here are rather interesting, you'll have to look at it from a distance to grasp it.
       
    77   // Basically just using MySQL to apply all the filters in one go. Nuggie doesn't do PostgreSQL yet.
       
    78   $sql_base = "SELECT <columns>\n"
       
    79        . "  FROM " . table_prefix . "blog_posts AS p\n"
       
    80        . "  LEFT JOIN " . table_prefix . "blogs AS b\n"
       
    81        . "    ON ( b.user_id = p.post_author )\n"
       
    82        . "  LEFT JOIN " . table_prefix . "users AS u\n"
       
    83        . "    ON ( u.user_id = p.post_author )\n"
       
    84        . "  LEFT JOIN " . table_prefix . "comments AS c\n"
       
    85        . "    ON ( c.page_id = CAST(p.post_id AS char) AND c.namespace = 'BlogPost' )\n"
       
    86        . "  LEFT JOIN " . table_prefix . "tags AS t\n"
       
    87        . "    ON ( t.page_id = CAST(p.post_id AS char) AND t.namespace = 'BlogPost' )\n"
       
    88        . "  LEFT JOIN " . table_prefix . "planets_mapping AS m\n"
       
    89        . "    ON (\n"
       
    90        . "         ( m.mapping_type = " . NUGGIE_PLANET_FILTER_TAG . " AND m.mapping_value = t.tag_name  ) OR\n"
       
    91        . "         ( m.mapping_type = " . NUGGIE_PLANET_FILTER_AUTHOR . " AND CAST(m.mapping_value AS unsigned integer) = p.post_author ) OR\n"
       
    92        . "         ( m.mapping_type = " . NUGGIE_PLANET_FILTER_KEYWORD . " AND ( p.post_text LIKE CONCAT('%', m.mapping_value, '%') OR p.post_title LIKE CONCAT('%', m.mapping_value, '%') ) )\n"
       
    93        . "       )\n"
       
    94        . "  WHERE m.planet_id = {$planet_data['planet_id']}\n"
       
    95        . "    AND p.post_published = 1\n"
       
    96        . "  GROUP BY p.post_id\n"
       
    97        . "  ORDER BY p.post_timestamp DESC\n"
       
    98        . "  <limit>;";
       
    99        
       
   100   // pass 1: a test run to count the number of results
       
   101   $sql = str_replace('<columns>', 'p.post_id', $sql_base);
       
   102   $sql = str_replace('<limit>', "", $sql);
       
   103   $q = $db->sql_query($sql);
       
   104   if ( !$q )
       
   105     $db->_die();
       
   106   
       
   107   $count = $db->numrows();
       
   108   $db->free_result($sql);
       
   109   
       
   110   // pass 2: production run
       
   111   $columns = 'p.post_id, p.post_title, p.post_title_clean, p.post_author, p.post_timestamp, p.post_text, b.blog_name, b.blog_subtitle, b.blog_type, b.allowed_users, u.username, u.user_level, COUNT(c.comment_id) AS num_comments, \'' . $db->escape($planet_id) . '\' AS referring_planet';
       
   112   $sql = str_replace('<columns>', $columns, $sql_base);
       
   113   $sql = str_replace('<limit>', "LIMIT $offset, 10", $sql);
       
   114   
       
   115   // yea. that was one query.
       
   116   $q = $db->sql_unbuffered_query($sql);
       
   117   if ( !$q )
       
   118     $db->_die();
       
   119   
       
   120   // just let the paginator do the rest
       
   121   $postbit = new NuggiePostbit();
       
   122   // $q, $tpl_text, $num_results, $result_url, $start = 0, $perpage = 10, $callers = Array(), $header = '', $footer = ''
       
   123   $html = paginate(
       
   124       $q,
       
   125       '{post_id}',
       
   126       $count,
       
   127       makeUrlNS('Planet', "$planet_id/start=%s", true),
       
   128       0,
       
   129       10,
       
   130       array( 'post_id' => array($postbit, 'paginate_handler') ),
       
   131       '<span class="menuclear"></span>'
       
   132     );
       
   133   $db->free_result($q);
       
   134   
       
   135   $template->add_header('<link rel="stylesheet" type="text/css" href="' . scriptPath . '/plugins/nuggie/style.css" />');
       
   136   $template->header();
       
   137   echo $planet_data['planet_subtitle'];
       
   138   echo $html;
       
   139   $template->footer();
    66 }
   140 }
    67  
   141  
    68 ?>
   142 ?>