A Wordpress broken link helper

20 January, 2009 (15:43) | Default | By: feuvan

Just an example.
For wordpress ppl who changed their perm link.

  1. <?php
  2. require_once( dirname(__FILE__) . '/wp-load.php' );
  3.  
  4. global $wpdb;
  5.  
  6. $uri = $_SERVER["REQUEST_URI"];
  7. $pattern = "/^\/(\d{4})\/(\d{2})\/(\d{2})\/([^\d].*|\d+[.^-]*).html$/";
  8. if (preg_match($pattern, $uri, $match) == 1)
  9. {
  10.     $year = $match[1];
  11.     $month = $match[2];
  12.     $mday = $match[3];
  13.     $post_name = $match[4];
  14.  
  15.     if ((string)(int)$post_name == $post_name) {
  16.         $id = (int)$post_name;
  17.         $link = get_permalink($id);
  18.         if ($link)
  19.             wp_redirect($link, '301'); // Permanent redirect
  20.     }
  21.     $query = "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID=post_id ".
  22.         "AND YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
  23.         "AND post_name = \"$post_name\" LIMIT 1";
  24.  
  25.     $id = (int)$wpdb->get_var($query);
  26.     $link = get_permalink($id);
  27.     if ($link) wp_redirect($link, '301'); // Permanent redirect
  28.  
  29.     $query = "SELECT ID FROM $wpdb->posts WHERE ".
  30.         "YEAR(post_date) = $year AND MONTH(post_date) = $month AND DAYOFMONTH(post_date) = $mday ".
  31.         "AND post_name = \"$post_name\" LIMIT 1";
  32.     $id = (int)$wpdb->get_var($query);
  33.     $link = get_permalink($id);
  34.         if ($link) wp_redirect($link, '301'); // Permanent redirect
  35.  
  36.     $link = "/$year/$month/$mday/";
  37.  
  38.     if (strstr($_SERVER["HTTP_USER_AGENT"], "Mozilla") === FALSE) // if it's not a user
  39.     {
  40.         wp_redirect($link, '302');
  41.     }
  42. ?>
  43. <html><head>
  44. <meta http-equiv="refresh" content="5;url=<?php echo $link;?>" / > <title>Page has moved</title> </head>
  45. <body><p>The permant link changes, however <i>broken link helper</i> can't solve it automaticlly, it will redirect you to <a href="<?php echo $link;?>">the date archive page</a>. It would be nice if you can report broken links to feuvan#feuvan.net.</p>
  46. </body></html>
  47. <?php } ?>

Corresponding nginx configuration block:

server {
listen 80;
server_name blog.feuvan.net;

root /home/feuvan/wwwdata/blog;
index index.php;
if (-e $request_filename) {
break;
}

rewrite ^/index.php/(.+)$ /index.php?q=$1 last;
rewrite “^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\d].*|\d+[.^-]*).html” /brokenlinkhelper.php last;
#rewrite ^/(.+)$ /index.php?q=$1 last;
rewrite ^/ /index.php last;

location ~ \.php$ {
fastcgi_pass unix:/tmp/php.sock;
}
}

Hope it helps.

2009: A dynamic future of C#

31 October, 2008 (16:39) | Default | By: feuvan

70s coders learn C, Pascal, COBOL(one of the best IT jobs in economic crisis)
80s coders learn C
90s coders learn C++, Java, Delphi
21-century (long time no see this hot word during 1999-2001)  coders learn C#?

- A homemade rumor by anonymous craven

On PDC 2008, Anders, former architecture of Turbo Pascal, Delphi, Visual J++, currently the father of C#, gives us a presentation “The Future of C#” about what new features C# 4.0 will have. The demo “Compiler as a service” is really cool. Think about the prompt “C# >”.

Mads Torgersen, C# Language PM gives a public released document on C# Future site, describes four main new features:

Dynamic lookup

Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.

Named and optional parameters

Parameters in C# can now be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.

COM specific interop features

Dynamic lookup as well as named and optional parameters both help making programming against COM less painful than today. On top of that, however, we are adding a number of other small features that further improve the interop experience.

Variance

It used to be that an IEnumerable<string> wasn’t an IEnumerable<object>. Now it is – C# embraces type safe “co-and contravariance” and common BCL types are updated to take advantage of that.

My comments:

features benefit side effect
Dynamic lookup dynamic (runtime type detection) versus var (compile time type inference) introduced in C#3.0.
No handwritten reflection codes any more.
No IntelliSense when programming.
Difficult to defend unknown runtime exceptions, errors.
Named and optional parameters ease function call of COM interop like VSTO, etc. Named parameters looks Pythonic. But maybe Jim like this?
COM specific interop features based on previous two new features, it’s easier.
The biggest benefit: no runtime PIA more!
N/A? Time to migrate old workaround code.
Variance more friendly as above.

These new features certainly relies on new .NET framework. At least, dynamic is not a language sugar like var.

Overall, C# is becoming more and more free-style language, combined compile-time and run-time power. It’s worthy to take a look at what C# will be if you are a C# developer or even a Java, C++ lover. Write less, do more. That’s the best value of the C# evolution, IMO.

mod_extforward for lighttpd 1.5 r2994

11 October, 2008 (18:19) | Default | By: feuvan

Lighttpd team roll out a prerelease of 1.5 r2294, get it here(http://blog.lighttpd.net/articles/2008/08/20/new-prereleases-1-4-20-r2294-and-1-5-r2294). I modified mod_extforward to make it work with it (some constants changed since last prerelease).

orz to the author of mod_extforward, great kxn.

see att for patch.

lighttpd-150-r2294-mod_extforward

reloaded

11 October, 2008 (18:09) | Default | By: feuvan

FreeBSD 6.2 jailed host, CMCC network.

Hulu starts invitation

19 February, 2008 (06:21) | Default | By: feuvan

Hulu started invitation several days ago, every beta user has 10 invitations to send to their friends.
It reminds me the old days, the age of gmail.

Currently ppl living in mainland China may have some difficult to view contents. However, you are clever enough to skip check.

If you are interested in getting an invitation, just drop me a mail.:)