예제 #1
0
function subst($new, $old, $lat)
{
    return empty($lat) ? array() : (eq(car($lat), $old) ? cons($new, cdr($lat)) : cons(car($lat), subst($new, $old, cdr($lat))));
}
 /**
  * Generates and returns a string of the of ``sphinx.conf`` file.
  * ``$sphinxDir`` should be a string representing the directory in which to
  * store the indexes.
  * 
  * @param   string $sphinxDir   Path to where indexes should be stored.
  * @return  string
  */
 public function generateSphinxConf($sphinxDir = "", $sphinxConf = "")
 {
     /**
      * Simple "python-like" key->value string substitution:
      *
      *  // Create a string: "hi there"
      *  subst("%(a)s %(b)s", array("a" => "hi", "b" => "there"));
      *
      * @param   string    $str
      * @param   array     $dict
      * @return  string
      */
     function subst($str, $dict)
     {
         $fn = create_function('$a', 'return "/%\\($a\\)s/";');
         $map = array_map($fn, array_keys($dict));
         return preg_replace($map, array_values($dict), $str);
     }
     $nnRoot = realpath(FS_ROOT . "/../../");
     if (!$sphinxDir) {
         $sphinxDir = $nnRoot . "/db/sphinxdata";
     }
     if (!$sphinxConf) {
         $sphinxConf = $sphinxDir . "/sphinx.conf";
     }
     $sphinxConfFile = fopen($sphinxConf, 'w');
     $filename = $nnRoot . "/misc/sphinx/sphinx.tpl";
     $handle = fopen($filename, "r");
     $conf = fread($handle, filesize($filename));
     $vars = array("db_host" => DB_HOST, "db_user" => DB_USER, "db_pass" => DB_PASSWORD, "db_name" => DB_NAME, "sphinx_dir" => $sphinxDir);
     fwrite($sphinxConfFile, subst($conf, $vars));
     fclose($sphinxConfFile);
     return $sphinxConf;
 }