--- C:/Apache2/social/sites/default/modules/old_freelinking/freelinking.install	Thu Oct 04 10:19:53 2007
+++ C:/Apache2/social/sites/default/modules/freelinking/freelinking.install	Fri Dec 28 15:39:53 2007
@@ -1,75 +1,55 @@
 <?php
 
-function freelinking_install() {
-
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      db_query('CREATE TABLE {freelinking} (
-                   hash   CHAR(32)     NOT NULL,
-                   phrase VARCHAR(200) NOT NULL,
-                   path   VARCHAR(200) NOT NULL,
-                   args   VARCHAR(200) NOT NULL, 
-                   PRIMARY KEY(hash))
-                TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;');
-      break;
-    case 'pgsql':
-      db_query('CREATE TABLE {freelinking} (
-                  hash   CHAR(32)     NOT NULL,
-                  phrase VARCHAR(200) NOT NULL,
-                  path   VARCHAR(200) NOT NULL,
-                  args   VARCHAR(200) NOT NULL,
-                  PRIMARY KEY(hash));');
-      break;
-    }
-} // endfunction freelinking_install
-
-function freelinking_update_1() {
-  $ret = array();
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql('ALTER TABLE {freelinking} DROP PRIMARY KEY');
-      break;
-    case 'pgsql':
-      $ret[] = update_sql('ALTER TABLE {freelinking} DROP CONSTRAINT {freelinking}_pkey');
-      break;
-  } // endswitch on db type
+function freelinking_schema() {
+  $schema = array();
 
-  $ret[] = _system_update_utf8(array('freelinking'));
+  $schema['freelinking'] = array(
+    'fields' => array(
+      'phrase' => array( 'type' => 'varchar', 'length' => 200, 'not null' => TRUE),
+      'path' => array( 'type' => 'varchar', 'length' => 200, 'not null' => TRUE),
+      'in_nodes' => array( 'type' => 'varchar', 'length' => 100, 'not null' => TRUE)
+    ),
+    'primary key' => array('phrase'),
+    'indexes' => array(
+      'phrase' => array('phrase')
+    )
+  );
 
-  return $ret;
-};
-
-function freelinking_update_2() { // this is the update to the 4.7 version
-  $ret = array();
+  return $schema;
+}
 
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      $ret[] = update_sql('ALTER TABLE {freelinking} ADD COLUMN hash CHAR(32)');
-      $ret[] = update_sql('ALTER TABLE {freelinking} ADD COLUMN path VARCHAR(200)');
-      $ret[] = update_sql('ALTER TABLE {freelinking} ADD COLUMN args VARCHAR(200)');
-      $ret[] = update_sql('UPDATE      {freelinking} SET path = target');
-      $ret[] = update_sql('UPDATE      {freelinking} SET hash = MD5(CONCAT(phrase, path))');
-      $ret[] = update_sql('ALTER TABLE {freelinking} DROP COLUMN target');
-      $ret[] = update_sql('ALTER TABLE {freelinking} ADD PRIMARY KEY hash (hash)');
-      break;
-
-    case 'pgsql':
-      $ret[] = update_sql('ALTER TABLE {freelinking} DROP COLUMN target');
-      db_add_column($ret, 'freelinking', 'hash', 'char(32)');
-      db_add_column($ret, 'freelinking', 'path', 'varchar(200)');
-      db_add_column($ret, 'freelinking', 'args', 'varchar(200)');
-      $ret[] = update_sql('ALTER TABLE {freelinking} ADD PRIMARY KEY hash');
-      break;
-  } // endswitch db_table
-  return $ret;
+function freelinking_install() {
+  drupal_install_schema('freelinking');
 }
 
 function freelinking_uninstall() {
   drupal_uninstall_schema('freelinking');
 }
 
-// vim: tw=300 nowrap syn=php
-?>
+/**
+ * Setup 1st and 2nd updates as doing nothing
+ * to maintain compatibility with 5.x branch
+ */
+function freelinking_update_1() {
+  return array();
+}
+
+function freelinking_update_2() {
+  return array();
+}
+
+/**
+ * First update to prepare for 6.x compatibility
+ * and this version
+ */
+function freelinking_update_6000() {
+  $ret = array();
+  
+  db_drop_field($ret, 'freelinking', 'args');
+  db_drop_primary_key($ret, 'freelinking');
+  db_drop_field($ret, 'freelinking', 'hash');
+  db_add_field($ret, 'freelinking', 'in_nodes', array('type' => 'varchar', 'length' => 100, 'not null' => TRUE));
+  db_add_index($ret, 'freelinking', 'phrase', array('phrase'));
+  
+  return $ret;
+}
\ No newline at end of file
--- C:/Apache2/social/sites/default/modules/old_freelinking/freelinking.module	Thu Oct 04 10:19:53 2007
+++ C:/Apache2/social/sites/default/modules/freelinking/freelinking.module	Fri Dec 28 20:24:55 2007
@@ -6,7 +6,7 @@
    Drupal freelinking project: http://www.drupal.org/project/freelinking
 
    $Id: freelinking.module,v 1.33 2007/10/04 15:19:53 eafarris Exp $
-*/
+ */
 
 function freelinking_menu() {
   $items['freelinking'] = array(
@@ -32,26 +32,25 @@
 function freelinking_page($thetitle = NULL) {
   if ($thetitle) { // find the matching title
     $freelink = _freelinking_make_link($thetitle);
-    drupal_goto($freelink['path'], isset($freelink['args']) ? $freelink['args'] : '');
+    drupal_goto($freelink['path'], $freelink['args']);
   }
   else { // no title was passed -- show a list of wikiwords and status
     $header = array('phrase', 'target');
-    $query = "SELECT phrase, path FROM {freelinking} ORDER BY phrase";
+    $query = "SELECT phrase, nid FROM {freelinking} ORDER BY phrase";
     $result = db_query($query);
     $i = 0;
-    while ($freelink = db_fetch_object($result)) { // looping through phrase, target pairs
+    while ($freelink = db_fetch_object($result)) { // looping through phrase/path pairs
       $rows[$i][] = urldecode($freelink->phrase);
       if (preg_match('/^(http|mailto|https|ftp):/', $freelink->path)) { // an absolute link
-        $rows[$i][] = '<a class="freelink" href="' . $freelink->path . '">' . $flpair->path . '</a>';
+        $rows[$i][] = '<a class="freelink" href="'. $freelink->path .'">'. $flpair->path .'</a>';
       }
       else { // a freelink
-        $fltargetnid = _freelinking_exists($freelink->phrase);
-        $freelink = _freelinking_make_link($freelink->phrase);
-        if ($fltargetnid) {
-          $link = l(t('see this content'), drupal_get_path_alias('node/' . $fltargetnid));
+        if ($freelink->path) {
+          $link = l(t('see this content'), drupal_get_path_alias($freelink->path));
         }
         else { // content not found, show link to create
-          $link = '<a href=' . url($freelink['path'], $freelink['args']) . '>' . t('create this content') . '</a>';
+          $freelink = _freelinking_make_link($freelink->phrase);
+          $link = '<a href='. url($freelink['path'], array('query' => $freelink['args'])) .'>'. t('create this content') .'</a>';
         }
         $rows[$i][] = $link;
       }
@@ -102,15 +101,16 @@
     case 'view':
       switch ($delta) {
         case 0:
-          $query = 'SELECT * FROM {freelinking} WHERE path LIKE "%node/add%" ORDER BY RAND()';
+          $query = 'SELECT phrase FROM {freelinking} WHERE nid = 0 ORDER BY RAND()';
           $result = db_query($query);
           $i = 0;
           $content = '';
+          $items = array();
           while ($freelink = db_fetch_object($result)) {
             if ($i == variable_get('freelinking_blocknum', 10)) { // we're done
               break;
             }
-            $items[] = l(urldecode($freelink->phrase), $freelink->path, array(), $freelink->args);
+            $items[] = l(urldecode($freelink->phrase), 'freelinking/'. urldecode($freelink->phrase));
             $i++;
           } // endwhile looping through flpairs
           $block['subject'] = variable_get('freelinking_blocktitle', 'Create This Content');
@@ -125,66 +125,61 @@
 } // endfunction freelinking_block
 
 function freelinking_settings() {
-  $notfoundoptions = array(
-    'create only' => t('Only try to create content'),
-    'no access search' => t('Search for content if user can\'t create'),
-    'always search' => t('Always search for content'),
-  );
-  $form["freelinking_nodetype"] = array(
+  $form['freelinking_nodetype'] = array(
     '#title' => t('Default for new content'),
     '#type' => 'select',
     '#options' => node_get_types('names'),
-    '#default_value' => variable_get("freelinking_nodetype", 'story'),
+    '#default_value' => variable_get('freelinking_nodetype', 'story'),
     '#description' => t('Type of content that the freelinking filter will create when clicking on a freelink without a target.')
   );
   $form['freelinking_notfound'] = array(
     '#title' => t('What to do if content not found'),
     '#type' => 'select',
-    '#options' => $notfoundoptions,
+    '#options' => array(
+      'create only' => t('Only try to create content'),
+      'no access search' => t('Search for content if user can\'t create'),
+      'always search' => t('Always search for content'),
+    ),
     '#default_value' => variable_get('freelinking_notfound', 'no access search'),
     '#description' => t('What to do when clicking on a freelink without a target. Choose to always attempt to create the content, search if the user doesn\'t have permission to create (the default), or to always search. NOTE: search functions require search.module to be activated.'),
   );
 
-  $form["freelinking_restriction"] = array(
+  $form['freelinking_restriction'] = array(
     '#title' => t('Restrict free links to this content type'),
     '#type' => 'select',
     '#options' => array_merge(array('none' => t('No restrictions')), node_get_types('names')),
-    '#default_value' => variable_get("freelinking_restriction", 'none'),
+    '#default_value' => variable_get('freelinking_restriction', 'none'),
     '#description' => t('If desired, you can restrict the freelinking title search to just content of this type. Note that if it is not the same as the "Default for new content," above, new freelinked content cannot be found.')
   );
-  $form["freelinking_camelcase"] = array(
+  $form['freelinking_camelcase'] = array(
     '#title' => t('Allow CamelCase linking'),
     '#type' => 'checkbox',
-    '#default_value' => variable_get("freelinking_camelcase", 0) == 0 ? TRUE : FALSE,
+    '#default_value' => variable_get('freelinking_camelcase', 0),
     '#description' => t('If desired, you can enable CamelCase linking')
   );
-  $form["freelinking_onceonly"] = array(
+  $form['freelinking_onceonly'] = array(
     '#title' => t('Only link first occurance'),
     '#type'  => 'checkbox',
-    '#default_value' => variable_get("freelinking_onceonly", 0) == 1 ? TRUE : FALSE,
+    '#default_value' => variable_get('freelinking_onceonly', 0),
     '#description' => t('If desired you can only turn the first occurance of a freelink into a link. This can improve the appearance of content that includes a lot of the same CamelCase words.')
   );
 
   return system_settings_form($form);
 }
 
-
 function freelinking_filter($op, $delta = 0, $format = -1, $text = '') {
   switch ($op) {
     case 'list':
       return (array(0 => t('freelinking filter')));
       break;
-
-    case 'name':
-      return t('freelinking filter');
-      break;
-    
+      
     case 'description':
       return t('Enables freelinking between nodes with CamelCase or delimiters like [[ and ]].');
       break;
 
     case 'process':
-      return _freelinking_do_filtering($text, FALSE);
+      $wikiwords = _freelinking_find_freelinks($text);
+      return _freelinking_do_filtering($text, $wikiwords, arg(1));
       break;
 
     case 'prepare':
@@ -196,11 +191,20 @@
 
 function freelinking_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   switch ($op) {
-    case 'update':
-      _freelinking_do_filtering($node->body, TRUE);
+    case 'delete': // Make sure we clear the freelinking DB when a node is deleted
+      _freelinking_store($node->title, 0);
+      
+      // Remove this node from the in_nodes for all freelinks in it
+      $wikiwords = _freelinking_find_freelinks($node->body);
+      _freelinking_update_in_nodes($wikiwords['internal'], $node->nid);
       break;
     case 'insert':
-      _freelinking_do_filtering($node->body, TRUE);
+      // Update the DB so that this page, if represented by a freelink, is stored
+      _freelinking_store($node->title, $node->nid);
+    case 'update':
+      // Update the DB for all contained freelinks for being attached to this node
+      $wikiwords = _freelinking_find_freelinks($node->body);
+      _freelinking_update_freelinks($wikiwords['internal'], $node->nid);
       break;
   } // endswitch $op
 } // endfunction freelinking_nodeapi
@@ -231,8 +235,12 @@
   return t($output);
 }
 
+/**
+ * Check that this is a new node we're adding and if an edit variable
+ * from freelinking is found, set the title to that value
+ */
 function freelinking_form_alter(&$form, $form_state, $form_id) {
-  if (isset($form['type'])) {
+  if (isset($form['type']) && arg(1) == 'add' && isset($_GET['edit'])) {
     $type = $form['type']['#value'];
     if (variable_get('freelinking_nodetype', 'story') == $type && $_GET['edit']) { // on the right node type, with GET data
       $form['title']['#default_value'] = urldecode($_GET['edit']['title']); // prepopulate the title field
@@ -247,73 +255,156 @@
  * Please do not use these functions outside of freelinking.module, as they are
  * subject to change without notice.
  *
-*/
+ */
+
+function _freelinking_do_filtering($text, $wikiwords, $nid) {
+  
+  // Find all freelinks attached to this node that don't have
+  // have a node
+  $qry = db_query('SELECT phrase FROM {freelinking} WHERE in_nodes LIKE "%%|%d|%%" AND nid = 0', $nid);
+  $lack_nodes = array();
+  while ($o = db_fetch_object($qry)) {
+    $lack_nodes[$o->phrase] = true;
+  }  
+  
+  // Handle external links
+  foreach ($wikiwords['external'] as $wikiword => $data) {
+    $replacement = l($wikiword, $data['text']?$data['text']:$wikiword, array('attributes' => array('class' => 'freelinking external')));
+    $text = str_replace($data['orig_text'], $replacement, $text);
+  }
+  
+  // Handle internal links
+  foreach ($wikiwords['internal'] as $wikiword => $data) {
+    $class = 'freelinking';
+    
+    // Add another class for freelinks that don't have nodes
+    if (array_key_exists($wikiword, $lack_nodes)) {
+      $class .= ' nonexistent';
+    }
+    
+    $replacement = l($data['text']?$data['text']:urldecode($wikiword), 'freelinking/'. $wikiword, array('attributes' => array('class' => $class)));
+    $text = str_replace($data['orig_text'], $replacement, $text);
+  }
+  
+  return $text;
+}
 
-function _freelinking_do_filtering($text, $store = FALSE) {
-  $allowcamelcase = variable_get("freelinking_camelcase", TRUE);
-  $freelinkingregexp = '/\[\[.+]]/U'; // this finds [[links like this]], un-greedily
+/**
+ * Returns a pair of arrays where the first contains
+ * the actual text matched and the second contains the
+ * corresponding fulltext match. Values guaranteed to
+ * be unique.
+ */
+function _freelinking_find_freelinks($text) {
+  $freelinkingregexp = '/\[\[(.+)]]/U'; // this finds [[links like this]], un-greedily
   preg_match_all($freelinkingregexp, $text, $flmatches);
-  if ($allowcamelcase) {
-     $camelcaseregexp = '/\b([[:upper:]][[:lower:]]+){2,}\b/'; // this gets us close, but is not perfect. Example: ThisIsACamelCaseWord won't match (two caps in a row)
-     preg_match_all($camelcaseregexp, $text, $ccmatches);
-     $wikiwords = array_merge($ccmatches[0], $flmatches[0]);
+  if (variable_get('freelinking_camelcase', TRUE)) {
+    $camelcaseregexp = '/\b([[:upper:]][[:lower:]]+){2,}\b/'; // this gets us close, but is not perfect. Example: ThisIsACamelCaseWord won't match (two caps in a row)
+    preg_match_all($camelcaseregexp, $text, $ccmatches);
+    $wikiwords = array($flmatches[0] + $ccmatches[0], $flmatches[1] + $ccmatches[1]);
   }
   else {
-     $wikiwords = $flmatches[0];
+    $wikiwords = $flmatches;
   }
-  foreach (array_unique($wikiwords) as $wikiword) {
-    if (substr($wikiword, 0, 2) == '[[') { // if it's a freelink, the expressions are different
-      $phrase = substr($wikiword, 2, -2);
-      $freelink = $phrase;
-      $barpos = strpos($phrase, '|');
-      $pattern = '/\[\[' . preg_quote($phrase,'/') . ']]/';
-      if ($barpos) {
-         $freelink = substr($freelink, 0, $barpos);
-         $phrase = substr($phrase, $barpos + 1);
-      }
-      if (preg_match('/^(http|mailto|https|ftp):/', $freelink)) {
-         $replacement = '<a class="freelinking external" href="' . $freelink . '">' . $phrase . '</a>';
-      }
-      else {
-        $replacement = l($phrase, 'freelinking/' . rawurlencode($freelink), array('class' => 'freelinking'));
-      }
-    }
+  
+  // Sort freelinks into internal and external lists of links
+  // Links are represented by key, text, and orig_text
+  $nice_array = array(
+    'internal' => array(),
+    'external' => array()
+  );
 
-    else if ($allowcamelcase) { // it's a CamelCase, expressions are a bit simpler
-      $pattern = '/\b' . $wikiword . '\b(?![^<]*>)/';
-      $phrase = $wikiword; // consistency for the db
-      $freelink = $wikiword; // also for the db
-      $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword));
+  for ($i=0;$i<count($wikiwords[1]);$i++) {
+    $wikiword = $wikiwords[1][$i];
+    // If this is a dual-freelink, split it up into correct pieces
+    if (strpos($wikiword, '|')) {
+      list($freelink, $phrase) = explode('|', $wikiword);
+    }
+    else {
+      $phrase = $wikiword;
+      $freelink = '';
     }
-    $text = preg_replace($pattern, $replacement, $text, variable_get("freelinking_onceonly", 0) ? 1 : -1);
     
-    if ($store) {
-      _freelinking_store($freelink, $replacement);
+    // Sort into external and internal arrays
+    if (preg_match('/^(http|mailto|https|ftp):/', $freelink)) {
+      $nice_array['external'][$phrase] = array('text' => $freelink, 'orig_text' => $wikiwords[0][$i]);
     }
-  } // foreach wikiword
-  return $text;
-} // endfunction _freelinking_do_filtering
+    else {
+      $nice_array['internal'][urlencode($phrase)] = array('text' => $freelink, 'orig_text' => $wikiwords[0][$i]);
+    }
+  }
+
+  return $nice_array;
+}
+
+/**
+ * Checks all freelinks found in a node and updates them in the DB,
+ * whether that means adding them in to the DB or just adding this
+ * node to their in_nodes column.
+ */
+function _freelinking_update_freelinks($wikiwords, $nid) {
+  
+  // Find all the links that think they're attached to this node
+  $qry = db_query('SELECT phrase FROM {freelinking} WHERE in_nodes LIKE "%%|%d|%%"', $nid);
+  $old_freelinks = array();
+  while ($o = db_fetch_object($qry)) {
+    $old_freelinks[] = $o->phrase;
+  }
+  
+  // Update all freelinks currently in this node
+  $phrases = array();
+  foreach ($wikiwords as $phrase => $data) {
+    $phrases[] = $phrase;
+    
+    $qry = db_query('SELECT in_nodes FROM {freelinking} WHERE phrase = "%s"', $phrase);
+    if ($in_nodes = db_result($qry)) { // If the entry already exists...
+      if (strpos($in_nodes, "|$nid") === false) { // And doesn't have this node in it already
+        // Add this node to the list of nodes this phrase is in
+        db_query('UPDATE {freelinking} SET in_nodes = CONCAT(in_nodes, "|%d|")', $nid);
+      }
+    }
+    else { // Otherwise if this freelink isn't in the DB...
+      // Add it!
+      db_query('INSERT INTO {freelinking} (phrase, nid, in_nodes) VALUES ("%s", %d, "%s")', $phrase, 0, "|$nid|");
+    }
+  }
+  
+  // Update all freelinks that had been attached to this node, but are no longer
+  $links_to_remove = array_diff($old_freelinks, $phrases);
+  db_query('UPDATE {freelinking} SET in_nodes = REPLACE(in_nodes, "|%d|", "") WHERE phrase IN ("%s")', $nid, implode('","', $links_to_remove), $nid);
+  
+  // Remove any freelinks that've been orphaned by this procedure
+  db_query('DELETE FROM {freelinking} WHERE in_nodes = ""');
+}
 
-function _freelinking_store($phrase, $path, $args=NULL) { // store freelinking pair in the db
-  $hash = md5($phrase . $path . $args);
-  $query = "SELECT hash FROM {freelinking} WHERE phrase = '%s'";
+/**
+ * 
+ * @param $insert Used for when updating of a row is required and not generation
+ * of new rows
+ */
+function _freelinking_store($phrase, $nid, $insert = TRUE) { // store freelinking pair in the db
+  $phrase = urlencode($phrase);
+  $query = 'SELECT nid, in_nodes FROM {freelinking} WHERE phrase = "%s"';
   $result = db_query($query, $phrase); 
-  $num_rows = FALSE;
-  while ($dbobj = db_fetch_object($result)) {
-    $num_rows = TRUE;
-    $dbhash = $dbobj;
-  }
-  if ( !$num_rows) { // not in the db
-    $query = "INSERT INTO {freelinking} (hash, phrase, path, args) VALUES ('%s', '%s', '%s', '%s')";
-    $result = db_query($query, $hash, $phrase, $path, $args);
-  } // endif row not found in table
+  $freelink = db_fetch_object($result);
+  
+  if ( $freelink === FALSE) { // not in the db
+    if ($insert) {
+      $query = 'INSERT INTO {freelinking} (phrase, nid) VALUES ("%s", 0)';
+      db_query($query, $phrase);
+    }
+  }
   else { // in the db, but does it match?
-    if ($dbhash->hash != $hash) { // hashes don't match, replace db entry with new values
-      $query = "UPDATE {freelinking} SET hash = '%s', path = '%s', args = '%s' WHERE phrase = '%s'";
-      $result = db_query($query, $hash, $path, $args, $phrase);
-    } // endif hashes don't match
-  } // endifelse row found
-} // endfunction _freelinking_store
+    if ($nid && $freelink->nid != $nid) { // If we found the content, update DB
+      $query = 'UPDATE {freelinking} SET nid = %d WHERE phrase = "%s"';
+      db_query($query, $nid, $phrase);
+    }
+    else if (!$nid) { // If we didn't find a node for this freelink, clear DB
+      $query = 'UPDATE {freelinking} SET nid = 0, in_nodes = "" WHERE phrase = "%s"';
+      db_query($query, $phrase);
+    }
+  }
+}
 
 function _freelinking_exists($thetitle) { // helper function for freelinking_page
   // looks through the db for nodes matching $title. Returns the nid if such a node exists, otherwise, returns 0
@@ -322,48 +413,50 @@
   $noderestrict = variable_get('freelinking_restriction', 'none');
   if ($noderestrict != 'none') { // need to add the where clause
     $query .= " AND type = '%s'";
-    $result = db_query($query, $title, $noderestrict);
+    $result = db_query($query .' LIMIT 1', $title, $noderestrict);
   }
   else { // no restriction. query is fine but db_query doesn't need the extra argument
-    $result = db_query($query, $title);
+    $result = db_query($query .' LIMIT 1', $title);
   }
-// FIXME ***
-  while ($node = db_fetch_object($result)) { // only one, I hope... what if there's more than one?
-    $nid = $node->nid;
+  
+  $nid = 0;
+  if ($node = db_fetch_object($result)){
+    $nid = $nod->nid; 
   }
-  return (empty($nid) ? 0 : $nid);
+  return $nid;
 }
 
 
-function _freelinking_make_link($thetitle) { // helper function for freelinking_page
+function _freelinking_make_link($thetitle) { // helper function or freelinking_page
   global $user;
+  
+  // Create a default array to prevent key lookup misses
+  $freelink = array('path' => '', 'args' => '');
+  
   // Returns a link to a node named $thetitle if found, or a link to new content otherwise.
   $nid = _freelinking_exists($thetitle); 
   if ($nid) { // the node exists, set the path to go there
-    $freelink['path'] = 'node/' . $nid;
+    $freelink['path'] = 'node/'. $nid;
   }
   else { // node doesn't exist, set path to create it
     switch (variable_get('freelinking_notfound', 'no access search')) {
       case 'create only':
-        $freelink['path'] = 'node/add/' . variable_get('freelinking_nodetype', 'story');
-        $freelink['args'] = 'edit[title]=' . $thetitle;
+        $freelink['path'] = 'node/add/'. variable_get('freelinking_nodetype', 'story');
+        $freelink['args'] = 'edit[title]='. urlencode($thetitle);
         break;
       case 'no access search':
         if (node_access('create', variable_get('freelinking_nodetype', 'story'))) {
-          $freelink['path'] = 'node/add/' . variable_get('freelinking_nodetype', 'story');
-          $freelink['args'] = 'edit[title]=' . $thetitle;
+          $freelink['path'] = 'node/add/'. variable_get('freelinking_nodetype', 'story');
+          $freelink['args'] = 'edit[title]='. urlencode($thetitle);
         }
         else {
-          $freelink['path'] = 'search/node/' . $thetitle;
+          $freelink['path'] = 'search/node/'. $thetitle;
         }
         break;
       case 'always search':
-        $freelink['path'] = 'search/node/' . $thetitle;
+        $freelink['path'] = 'search/node/'. $thetitle;
         break;
-    } // endswitch notfound options
+    }
   }
-  _freelinking_store($thetitle, $freelink['path'], isset($freelink['args']) ? $freelink['args'] : '');
   return $freelink;
-} // endfunction _freelinking_make_link
-
-// vim: tw=300 nowrap syn=php
+}
\ No newline at end of file
