Permalink
Browse files

Fixes and changes

1) Added site map generation for google..

2) Made it so that draft links automatically get tagged as links
  • Loading branch information...
1 parent 2a17ea9 commit 367579aa69123373add7d5c86ac0dd6287744a52 @freekrai committed Mar 17, 2012
Showing with 164 additions and 5 deletions.
  1. +3 −4 api-www/add-draft.php
  2. +105 −0 engine/Sitemap.php
  3. +56 −1 engine/Updater.php
View
@@ -15,7 +15,7 @@
}
$bookmarklet_code = <<<EOF
-var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),l=d.location,e=encodeURIComponent;w.location.href='TARGETadd-draft.php?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&EXTRA';
+var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),l=d.location,e=encodeURIComponent;w.location.href='TARGET/add-draft.php?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&EXTRA';
EOF;
$bookmarklet_code = str_replace('TARGET', (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/', trim($bookmarklet_code));
@@ -38,7 +38,6 @@
<?
exit;
}
-
$url = substring_before(normalize_space($_GET['u']), ' ');
$title = normalize_space($_GET['t']);
$selection = trim($_GET['s']);
@@ -56,7 +55,7 @@
$draft_contents =
$title . "\n" .
str_repeat('=', max(10, min(40, strlen($title)))) . "\n" .
- ($is_link ? "Link: " . $url . "\n" : '') .
+ ($is_link ? "Link: " . $url . "\nTags: links\n" : '') .
"publish-not-yet\n" .
"\n" .
$body
@@ -80,7 +79,7 @@
<meta name="viewport" content="width=320"/>
<title>Saved draft</title>
</head>
- <body style="font: Normal 26px 'Lucida Grande', Verdana, sans-serif; text-align:center; color:#888; margin-top:100px;">
+ <body style="background: #000; font: Normal 26px 'Lucida Grande', Verdana, sans-serif; text-align:center; color:#fff; margin-top:100px;">
Saved.
<br/>
<br/>
View
@@ -0,0 +1,105 @@
+<?php
+
+class Sitemap
+{
+ private static $cache_path = "";
+ private static $xml_file_path = "";
+ private static $robots_file_path = "";
+
+ // you can override these in config.php; e.g.:
+ // Sitemap::$sitemap_name = "different_sitemap_name.xml";
+ public static $sitemap_name = "sitemap.xml";
+ public static $robot_name = "robots.txt";
+ public static $index_changefreq = "daily";
+ public static $index_priority = "1.0";
+ public static $page_changefreq = "monthly";
+ public static $page_priority = "0.8";
+ public static $post_changefreq = "monthly";
+ public static $post_priority = "0.8";
+
+ public static function should_write_sitemap($dp)
+ {
+ return (!file_exists($dp . "/" . self::$sitemap_name));
+ }
+
+ public static function write_sitemap($dp, $cp)
+ {
+ self::$cache_path = $cp;
+
+ self::$xml_file_path = $dp . "/" . self::$sitemap_name;
+ self::$robots_file_path = $dp . "/" . self::$robot_name;
+
+ error_log("writing sitemap (" . self::$xml_file_path . ")");
+ self::write_header();
+ self::entry("", time(), self::$index_changefreq, self::$index_priority); // entry for the index page
+ self::process_cache();
+ self::write_closing();
+
+ self::generate_robot_file();
+ }
+
+ private static function write_header()
+ {
+ $t = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
+ file_put_contents(self::$xml_file_path, $t);
+
+ $t = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
+ file_put_contents(self::$xml_file_path, $t, FILE_APPEND);
+ }
+
+ private static function write_closing()
+ {
+ $t = '</urlset>' . "\n";
+ file_put_contents(self::$xml_file_path, $t, FILE_APPEND);
+ }
+
+ private static function process_cache()
+ {
+ if (is_dir(self::$cache_path)) {
+ if ($dh = opendir(self::$cache_path)) {
+ while ( ($file = readdir($dh) ) !== false) {
+ if (substr($file, 0, 4) != "dir-") continue;
+ $fullpath = self::$cache_path . '/' . $file;
+ $fileinfo = unserialize(file_get_contents($fullpath));
+ foreach ($fileinfo as $key => $value) {
+ $filename = pathinfo($key, PATHINFO_FILENAME);
+ $parent = array_pop(split("/", pathinfo($key, PATHINFO_DIRNAME)));
+ $ts = array_shift(split("-", $value));
+ if ($parent == "pages")
+ {
+ self::entry($filename, $ts, self::$page_changefreq, self::$page_priority);
+ }
+ elseif (pathinfo($key, PATHINFO_EXTENSION) == substr(UPDATER::$post_extension, 1))
+ {
+ $location = substr($filename, 0, 4) . "/" . substr($filename, 4, 2) . "/" . substr($filename, 6, 2) . "/";
+ $location .= substr($filename, 12);
+ self::entry($location, $ts, self::$post_changefreq, self::$post_priority);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private static function entry($loc, $lastmod_timestamp, $changefreq, $priority)
+ {
+ $entry = "";
+ $entry .= "<url>" . "\n";
+ $entry .= "<loc>" . POST::$blog_url . $loc . "</loc>" . "\n";
+ $entry .= "<lastmod>" . date("Y-m-d", $lastmod_timestamp) . "</lastmod>" . "\n";
+ $entry .= "<changefreq>" . $changefreq . "</changefreq>" . "\n";
+ $entry .= "<priority>" . $priority . "</priority>" . "\n";
+ $entry .= "</url>" . "\n\n";
+
+ file_put_contents(self::$xml_file_path, $entry, FILE_APPEND);
+ }
+
+ private static function generate_robot_file()
+ {
+ error_log("writing robots.txt");
+ file_put_contents(self::$robots_file_path, "User-agent: *" . "\n");
+ file_put_contents(self::$robots_file_path, "Disallow:" . "\n", FILE_APPEND);
+ file_put_contents(self::$robots_file_path, "" . "\n", FILE_APPEND);
+ file_put_contents(self::$robots_file_path, "SITEMAP: " . Post::$blog_url . self::$sitemap_name . "\n", FILE_APPEND);
+ }
+}
View
@@ -2,6 +2,7 @@
require_once(dirname(__FILE__) . '/Post.php');
require_once(dirname(__FILE__) . '/Hook.php');
+require_once(dirname(__FILE__) . '/Sitemap.php');
class Updater
{
@@ -93,7 +94,49 @@ public static function posts_in_year_month($year, $month, $require_tag = false,
}
return $files;
}
-
+ public static function posts_in_year($year, $require_tag = false, $require_type = false)
+ {
+ $month = str_pad($month, 2, '0', STR_PAD_LEFT);
+ $cache_fname = self::$cache_path . "/posts-$year-" . md5($require_tag . ' ' . $require_type);
+ if (file_exists($cache_fname)) {
+ $files = unserialize(file_get_contents($cache_fname));
+ } else {
+ $all_files = self::filelist(self::$source_path . "/posts/$year/", true);
+ ksort($all_files);
+ $in_month = false;
+ $files = array();
+ foreach ($all_files as $fname => $info) {
+ if (substr($fname, -(strlen(self::$post_extension))) != self::$post_extension) continue;
+
+ // Tag/type filtering
+ list($ignored_hash, $type, $tags) = explode('|', $info, 3);
+ $include = true;
+ if ($require_type) {
+ if ($require_type[0] == '!' && $type == $require_type) $include = false;
+ else if ($require_type[0] != '!' && $type != $require_type) $include = false;
+ }
+ if ($require_tag) {
+ if ($require_tag[0] == '!' && in_array($require_tag, Post::parse_tag_str($tags))) $include = false;
+ else if ($require_tag[0] != '!' && ! in_array($require_tag, Post::parse_tag_str($tags))) $include = false;
+ }
+ if (! $include) continue;
+
+ list($y, $m, $d) = array_map('intval', array_slice(explode('/', $fname), -3, 3));
+ $d = intval(substr($d, 6));
+ if ($year == $y && $month == $m) {
+ if (isset($files[$d])) $files[$d][] = $fname;
+ else $files[$d] = array($fname);
+ } else {
+ if ($in_month) break;
+ }
+ }
+
+ if (! file_exists(self::$cache_path)) mkdir_as_parent_owner(self::$cache_path, 0755, true);
+ file_put_contents_as_dir_owner($cache_fname, serialize($files));
+ }
+ return $files;
+ }
+
public static function post_filenames_in_year_month($year, $month, $require_tag = false, $require_type = false)
{
$out = array();
@@ -102,6 +145,15 @@ public static function post_filenames_in_year_month($year, $month, $require_tag
}
return $out;
}
+ public static function post_filenames_in_year($year, $require_tag = false, $require_type = false)
+ {
+ $out = array();
+ foreach (self::posts_in_year($year, $require_tag, $require_type) as $day) {
+ foreach ($day as $filename) $out[] = $filename;
+ }
+ return $out;
+ }
+
private static function resequence_post_offsets($year, $month, $day)
{
@@ -675,6 +727,9 @@ private static function _update($restart_if_resequenced = true)
);
}
}
+ if ((self::$changes_were_written) || (Sitemap::should_write_sitemap(self::$dest_path))) {
+ Sitemap::write_sitemap(self::$dest_path, self::$cache_path);
+ }
}
}

0 comments on commit 367579a

Please sign in to comment.