First commit with working planet presentation code. No UI written for planet management yet.
authorDan
Mon, 16 Jun 2008 19:01:51 -0400
changeset 8 160f1170aa32
parent 7 cd46e29ae699
child 9 8dc58f08a066
First commit with working planet presentation code. No UI written for planet management yet.
plugins/Nuggie.php
plugins/nuggie/constants.php
plugins/nuggie/planet.php
plugins/nuggie/postbit.php
--- a/plugins/Nuggie.php	Mon Jun 16 12:54:24 2008 -0400
+++ b/plugins/Nuggie.php	Mon Jun 16 19:01:51 2008 -0400
@@ -84,10 +84,14 @@
   $session->acl_extend_scope('edit_comments', 'BlogPost', $paths);
   $session->acl_extend_scope('post_comments', 'BlogPost', $paths);
   $session->acl_extend_scope('mod_comments', 'BlogPost', $paths);
+  $session->acl_extend_scope('tag_create', 'BlogPost', $paths);
+  $session->acl_extend_scope('tag_delete_own', 'BlogPost', $paths);
+  $session->acl_extend_scope('tag_delete_other', 'BlogPost', $paths);
 }
 
 $plugins->attachHook('page_type_string_set', 'nuggie_set_page_string();');
 
+require( ENANO_ROOT . '/plugins/nuggie/constants.php' );
 require( ENANO_ROOT . '/plugins/nuggie/planet.php' );
 require( ENANO_ROOT . '/plugins/nuggie/postbit.php' );
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/nuggie/constants.php	Mon Jun 16 19:01:51 2008 -0400
@@ -0,0 +1,18 @@
+<?php
+
+/*
+ * Nuggie
+ * Version 0.1
+ * Copyright (C) 2007 Dan Fuhry
+ *
+ * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ */
+
+define('NUGGIE_PLANET_FILTER_TAG', 1);
+define('NUGGIE_PLANET_FILTER_AUTHOR', 2);
+define('NUGGIE_PLANET_FILTER_KEYWORD', 3);
+
--- a/plugins/nuggie/planet.php	Mon Jun 16 12:54:24 2008 -0400
+++ b/plugins/nuggie/planet.php	Mon Jun 16 19:01:51 2008 -0400
@@ -21,7 +21,13 @@
 {
   global $db, $session, $paths, $template, $plugins; // Common objects
   
-  $planet_id = $page->page_id;
+  $planet_id = dirtify_page_id($page->page_id);
+  $offset = 0;
+  if ( preg_match('#/start=([0-9]+)$#', $planet_id, $match) )
+  {
+    $planet_id = substr($planet_id, 0, (strlen($planet_id) - strlen($match[0])));
+    $offset = intval($match[1]);
+  }
   
   //
   // VALIDATION
@@ -42,7 +48,7 @@
   if ( $db->numrows() < 1 )
   {
     // planet not found, fall out
-    return false;
+    return $page->err_page_not_existent();
   }
   
   // fetch first row, then seek back to the first result to allow mapping fetching later
@@ -61,8 +67,76 @@
     return $page->err_access_denied();
   }
   
-  // fetch mappings to prepare to select the actual blog data
-  echo 'WiP';
+  // Set page title
+  $page_title = dirtify_page_id($planet_data['planet_name']);
+  $template->assign_vars(array(
+      'PAGE_NAME' => htmlspecialchars($page_title)
+    ));
+  
+  // 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.
+  // Basically just using MySQL to apply all the filters in one go. Nuggie doesn't do PostgreSQL yet.
+  $sql_base = "SELECT <columns>\n"
+       . "  FROM " . table_prefix . "blog_posts AS p\n"
+       . "  LEFT JOIN " . table_prefix . "blogs AS b\n"
+       . "    ON ( b.user_id = p.post_author )\n"
+       . "  LEFT JOIN " . table_prefix . "users AS u\n"
+       . "    ON ( u.user_id = p.post_author )\n"
+       . "  LEFT JOIN " . table_prefix . "comments AS c\n"
+       . "    ON ( c.page_id = CAST(p.post_id AS char) AND c.namespace = 'BlogPost' )\n"
+       . "  LEFT JOIN " . table_prefix . "tags AS t\n"
+       . "    ON ( t.page_id = CAST(p.post_id AS char) AND t.namespace = 'BlogPost' )\n"
+       . "  LEFT JOIN " . table_prefix . "planets_mapping AS m\n"
+       . "    ON (\n"
+       . "         ( m.mapping_type = " . NUGGIE_PLANET_FILTER_TAG . " AND m.mapping_value = t.tag_name  ) OR\n"
+       . "         ( m.mapping_type = " . NUGGIE_PLANET_FILTER_AUTHOR . " AND CAST(m.mapping_value AS unsigned integer) = p.post_author ) OR\n"
+       . "         ( 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"
+       . "       )\n"
+       . "  WHERE m.planet_id = {$planet_data['planet_id']}\n"
+       . "    AND p.post_published = 1\n"
+       . "  GROUP BY p.post_id\n"
+       . "  ORDER BY p.post_timestamp DESC\n"
+       . "  <limit>;";
+       
+  // pass 1: a test run to count the number of results
+  $sql = str_replace('<columns>', 'p.post_id', $sql_base);
+  $sql = str_replace('<limit>', "", $sql);
+  $q = $db->sql_query($sql);
+  if ( !$q )
+    $db->_die();
+  
+  $count = $db->numrows();
+  $db->free_result($sql);
+  
+  // pass 2: production run
+  $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';
+  $sql = str_replace('<columns>', $columns, $sql_base);
+  $sql = str_replace('<limit>', "LIMIT $offset, 10", $sql);
+  
+  // yea. that was one query.
+  $q = $db->sql_unbuffered_query($sql);
+  if ( !$q )
+    $db->_die();
+  
+  // just let the paginator do the rest
+  $postbit = new NuggiePostbit();
+  // $q, $tpl_text, $num_results, $result_url, $start = 0, $perpage = 10, $callers = Array(), $header = '', $footer = ''
+  $html = paginate(
+      $q,
+      '{post_id}',
+      $count,
+      makeUrlNS('Planet', "$planet_id/start=%s", true),
+      0,
+      10,
+      array( 'post_id' => array($postbit, 'paginate_handler') ),
+      '<span class="menuclear"></span>'
+    );
+  $db->free_result($q);
+  
+  $template->add_header('<link rel="stylesheet" type="text/css" href="' . scriptPath . '/plugins/nuggie/style.css" />');
+  $template->header();
+  echo $planet_data['planet_subtitle'];
+  echo $html;
+  $template->footer();
 }
  
 ?>
--- a/plugins/nuggie/postbit.php	Mon Jun 16 12:54:24 2008 -0400
+++ b/plugins/nuggie/postbit.php	Mon Jun 16 19:01:51 2008 -0400
@@ -87,6 +87,13 @@
   var $blog_perms;
   
   /**
+   * The name of the planet referred, if any. Defaults to false.
+   * @var string
+   */
+  
+  var $referring_planet = false;
+  
+  /**
    * Renders the post.
    */
   
@@ -148,9 +155,15 @@
       $strings["DATE_$char"] = date($char, $this->post_timestamp);
     }
     
+    $permalink_params = '';
+    if ( $this->referring_planet )
+    {
+      $permalink_params = "planet=" . sanitize_page_id($this->referring_planet);
+    }
+    
     $strings['POST_TITLE'] = htmlspecialchars($this->post_title);
     $strings['POST_TEXT'] = RenderMan::render($this->post_text);
-    $strings['PERMALINK'] = makeUrlNS('Blog', $this->post_author . date('/Y/n/j/', $this->post_timestamp) . $this->post_title_clean, false, true);
+    $strings['PERMALINK'] = makeUrlNS('Blog', $this->post_author . date('/Y/n/j/', $this->post_timestamp) . $this->post_title_clean, $permalink_params, true);
     $strings['EDIT_LINK'] = makeUrlNS('Special', "Preferences/Blog/Write/{$this->post_id}", false, true);
     
     // if we're on an enano with user rank support, cool. if not, just don't link
@@ -198,12 +211,24 @@
     $perms = $session->fetch_page_acl("{$row['post_timestamp']}_{$row['post_id']}", 'Blog');
     $perms->perms = $session->acl_merge($this->blog_perms->perms, $perms->perms);
     
-    /*
+    // if the row has information about the blog's access configuration, process it here.
+    // this is only done from within planets...
+    if ( isset($row['blog_type']) && isset($row['allowed_users']) )
+    {
+      if ( $row['blog_type'] == 'private' )
+      {
+        $users = unserialize($row['allowed_users']);
+        if ( !in_array($session->user_id, $users) && !$perms->get_permissions('nuggie_see_non_public') && $row['post_author'] !== $session->user_id )
+        {
+          return ' ';
+        }
+      }
+    }
+    
     if ( !$perms->get_permissions('read') )
     {
-      return "POST {$this->post_id} DENIED";
+      return ' ';
     }
-    */
     
     $this->post_id = intval($row['post_id']);
     $this->post_title = $row['post_title'];
@@ -211,6 +236,7 @@
     $this->post_author = $row['username'];
     $this->post_timestamp = intval($row['post_timestamp']);
     $this->num_comments = intval($row['num_comments']);
+    $this->referring_planet = ( isset($row['referring_planet']) ) ? $row['referring_planet'] : false;
     
     return $this->render_post();
   }
@@ -274,13 +300,14 @@
     
     if ( $db->numrows() > 1 )
     {
-      die_friendly('Ambiguous blog posts', '<p>FIXME: You have two posts with the same title posted on the same day by the same user. I was
+      die_friendly('Ambiguous blog posts', '<p>[fixme] You have two posts with the same title posted on the same day by the same user. I was
                                                not able to distinguish which post you wish to view.</p>');
     }
     
     $row = $db->fetchrow();
     
     $realpost = new PageProcessor($row['post_id'], 'BlogPost');
+    $realpost->send_headers = true;
     
     // huge hack
     // the goal here is to fool the page metadata system into thinking that comments are enabled.
@@ -365,7 +392,9 @@
   $acl_type = ( $row['post_author'] == $session->user_id ) ? 'nuggie_edit_own' : 'nuggie_edit_other';
   
   if ( !$perms->get_permissions('read') )
+  {
     return $page->err_access_denied();
+  }
   
   // enable comments
   $paths->cpage['comments_on'] = 1;
@@ -397,7 +426,14 @@
   }
   
   $template->header();
-  echo '&lt; <a href="' . makeUrlNS('Blog', $row['username']) . '">' . htmlspecialchars($row['blog_name']) . '</a>';
+  if ( isset($_GET['planet']) )
+  {
+    echo '&lt; <a href="' . makeUrlNS('Planet', $_GET['planet']) . '">' . htmlspecialchars($_GET['planet']) . '</a>';
+  }
+  else
+  {
+    echo '&lt; <a href="' . makeUrlNS('Blog', $row['username']) . '">' . htmlspecialchars($row['blog_name']) . '</a>';
+  }
   echo $postbit->render_post();
   display_page_footers();
   $template->footer();
@@ -427,7 +463,7 @@
     $allowed_users = unserialize($allowed_users);
     if ( !in_array($session->username, $allowed_users) && !$perms->get_permissions('nuggie_see_non_public') && $username != $session->username )
     {
-      return '_err_access_denied';
+      return $page->err_access_denied();
     }
   }