[ewiki] Re: Custom link formats

On Wednesday 01 December 2004 8:42 pm, mario@xxxxxxxxxxxxx wrote:
> Mike Payson <mpayson@xxxxxxxxxxxx> wrote:
> > [...]ustify hacking up a shortcut. I'm still curious as to why my code
> > didn't work, though. I understand the http part, but I'm baffled by the
> > duplication. NOt a big deal since I won't be using it, but if anyone can
> > explain what's going on, I'd be grateful.
>
> You accidently added up your replacement patterns with "$rep .= ___", and
> you may have had problems with .*? - an .+? often works much safer.

Oops... Thanks. That explains it...

> > Certainly. I'm happy to contribute anything I can. I've also been working
> > on an improved mpi_include that allows collapsing of the sub pages. It's
> > [...]
>
> I'm curious, but patient ;)

What I did was pass a variable like SubPageName=0 (or 1) to the page to 
specify whether to include the page or not. If it's hidden, only the title 
will be displayed. My code isn't really suitable for public consumption, but 
I've attached it at the bottom of this email (after the other attached php 
script) if you want to see what I've done. Note that there are problems with 
this... As it's implemented now, only one subpage can be expanded at a time. 
For the same reason, it also doesn't work properly with multiple levels of 
embedded pages. These all should be fixable, but it seems like it will be 
much easier to just do it via dhtml.

> http://erfurtwiki.sf.net/downloads/spin_offs/favicon-0.tgz
> (untested within ewiki, but sample script seemed ok)

Here's what I have so far. I decided against using MySQL, at least to start. 
It seems like it adds unnecessary complication, with little benefit. I'm not 
sure whether this will lead to a substantial performance hit. This could 
easily be changed if needed. The other big difference is how the script is 
called. I made it so that it can even be used from a plain HTML file. Just 
use an img link such as <img 
src="http://localhost/favicon.php?site=amazon.com";>. Everything else is 
handled automatically. If the site doesn't have a favicon, it will return a 
default icon. Doesn't currently support refreshing the cache, but the basic 
functionality all works.

<?php
##
# favicon.php - downloads & caches locally the favicon for a site. 
# If your server supports php as a shell script, it can be used 
# from cron to automatically refresh the icons periodically.
#    
#   Usage: <img src="http://hostname/favicon.php?site=domain.com";>
#      will return the favicon for domain.com (or default.ico 
#      if it's not available). Image is returned directly. Can be 
#      called from a plain HTML file if desired.
#      
# v.0.01 - Downloads & stores locally, sends alternate if not.
#     Refreshing of the cache not currently supported.
#
## Where to cache the icons. 
#  Be sure that the server can write to the directory
#  and that the directory ends in a trailing slash!
#  Upcoming versions will handle this better.
$cache_folder= "./favicons/";
# Image to send if the site doesn't have a favicon.
# Put it in the cache folder above, but make sure it's 
# named so that it won't be overwritten (I recommend 
# default.ico) 
$default_icon= "default.png";

if (!function_exists('mime_content_type')) {
   function mime_content_type($f) {
       $f = escapeshellarg($f);
       return trim( `file -bi $f` );
   }
}

$filename = trim ($site) . ".ico";
if (!file_exists($filename)) {
 $url= "http://"; . $site . "/favicon.ico";
 $f = fopen ($url, "r");
 if ($f) {
  while (!feof($f)) {
   $contents .= fread($f, 8192);
  }
  if (!$local_file = fopen($cache_folder . $filename, 'wb')) {
   $filename = $default_icon;
  } else {
  // Write $somecontent to our opened file.
   if (fwrite($local_file, $contents) === FALSE) {
    $filename = $default_icon;
   }
  fclose($local_file);
  fclose($f);
  }
 } else {
  $filename = $default_icon;
 }
}

$filename= $cache_folder . $filename;
if (file_exists($filename)) {
 $f = fopen ( $filename, "r");
 $data = fread($f, filesize($filename));
 $t = mime_content_type($filename);
 $t = "Content-type: $t";
 header($t);
 echo $data;
 fclose($f);
}
?>


mpi_myinsert.php:
<?php
/*
   This mpi allows you to insert another wikipage into the current
   one using <?plugin Insert ThisWikiPage ?>. You can also temporarily
   change some rendering parameters, by supplying them as optional
   parameters:
     <?plugins insert PageName split_title=0 control_line=0 ?>

   The table=0 parameter would disable the optional table+border
   around the inserted page:
     <?plugins insert PageName table=0 ?>

   Please note, that the inserted page will be requested through
   an "sub-request" with ewiki_page(), thus usually incorporating
   all settings from the main page.
   
   As additional extension, you can have a split view (vertical) with
   multiple pages:
     <?plugins insert PageOne PageTwo  ?>
*/

  # you can disable the <table> generation, if you style pages via CSS
define("EWIKI_MPI_MYINSERT_TBL", 0);

$ewiki_plugins["mpi"]["myinsert"] = "ewiki_mpi_myinsert";
$ewiki_config["mpi_myinsert"] = array(
   "table" => EWIKI_MPI_MYINSERT_TBL,
);

function ewiki_mpi_myinsert($action="html", $args, &$iii, &$s) {
   global $ewiki_config;

   #-- save environment
   $save = array(
      "id", "config", "title", "ring", "author",
   );
   unset($prevG);
   $prevG = array();
   foreach ($save as $name) {
      $prevG["$name"] = $GLOBALS["ewiki_$name"];
   }

   #-- use any params as _config settings
   $args = $args + $ewiki_config["mpi_myinsert"];
   foreach ($args as $set=>$val) {
      if ($set != "_") { 
         $ewiki_config[$set] = $val;
      }
   }

   #-- render requested page, through sub-request
   $o = array();
   $o[] = ewiki_page($args["id"]);
   for ($n=1; $n<=10; $n++) {
      if ($id = $args[$n]) {
         $o[] = ewiki_page($id);
      }
   }

   #-- reset env
   foreach ($save as $name) {
      $GLOBALS["ewiki_$name"] = $prevG[$name];
   }

   if ($_GET[$args["id"]] == 1) { 
      #-- mk table around output
      $on = count($o);
      if ($args["table"] || ($on >= 2)) {
         $o = implode("</td>\n<td valign=\"top\">", $o);
         $o = '<table border="'.$args["table"].'" cellpadding="5" 
cellspacing="5">'
            . '<tr><td valign="top">' . $o . '</td></tr></table>';
      } else {
         $o = implode("\n<br /><!-- cut-here --><br />\n", $o);
  $t = "<div class=\"subhead\"><a href=\"index.php?id=FrontPage&" . 
$args["id"] . "=0\" target=\"_self\">" . $args["id"] . "</a></div>";
  $t2 .= "<a href=\"index.php?page=edit/" . $args["id"] . "><img 
src=\"/ewiki/icons/e.png\" border=0></a>";
  $o = $t . $t2 . $o;
      }
   } else {
      $o = "<div class=\"subhead\"><a href=\"index.php?th=th&id=FrontPage&" . 
$args["id"] . "=1\" target=\"_self\">" .$args["id"] . "</a></div>";
   }
   $o = '<div class="mpi-myinsert">' . $o . '</div>';
   return($o);
}

?>

Other related posts: