Skip to content
interwiki.module 27 KiB
Newer Older
Sheldon Rampton's avatar
Sheldon Rampton committed
<?php
 * An implementation of an input formatting module for a special non-html link  
 * syntax to wikis and other websites
Sheldon Rampton's avatar
Sheldon Rampton committed
 * Upgraded to Drupal 4.7, 5/13/2006
Sheldon Rampton's avatar
Sheldon Rampton committed
 * Updated to Drupal 5.0, 2/03/2007
 * Updaated to Drupal 6.0, 1/24/2010
Sheldon Rampton's avatar
Sheldon Rampton committed
 *
 * This module, when configured, is responsible for translating user-inputted links of the form
 * '[prefix:some term]' into the form '<a href="http://www.domain.com/some_term">some term</a>'
 * where prefix represents a key and http://www.domain.com/ is 
 * the path to the an article about the term
 */

/**
 * Implementation of hook_help().
 */
function interwiki_help($section) {
    case 'admin/help#interwiki':
      return t("<p>This module makes it easy to link to wikis and other websites. Users avoid entering entire URLs, as they would for regular web pages, " .
          "and instead use a shorthand similar to the syntax used by !Wikipedia and other !MediaWiki sites, in which \"[prefix:some term]\" creates a hyperlink to the ".
Sheldon Rampton's avatar
Sheldon Rampton committed
          "\"some term\" article on the website specified by \"prefix.\" It is also possible to use the \"|\" character to create a \"piped link,\" with display text that is ".
          "different from the search term. For example, \"[w:public transport|public transportation]\" could be translated as a reference to the Wikipedia article on \"public ".
          "transport\" that displays as \"<a href=\"http://en.wikipedia.org/wiki/public_transport\">public transportation</a>.\" In addition to the single bracket syntax, ".
          "a double bracket syntax like the one used by !MediaWiki can be chosen from the interwiki configuration in !settings. The double-bracket syntax lets you specify a ".
Sheldon Rampton's avatar
Sheldon Rampton committed
          "\"default prefix\" that is used if no prefix is explicitly specified. For example, a default prefix of \"w\" means that [[some term]] is synonymous with [[w:some term]]. ".
          "This makes it easier to cut-and-paste text from MediaWiki sites directly into your Drupal site.</p>\n".
        "<h3>Configuration</h3><p>To use this module, you have to take a few steps:</p>".
          "<ul><li>Install and enable the module in !modules, and create the \"interwiki\" table using file interwiki.sql.</li>".
          "<li>Set the appropriate access right in !access<br />The access right is <b>administer interwiki</b>.</li>".
          "<li>To add or edit new interwiki links, use !interwikis.</li></ul>\n",
Sheldon Rampton's avatar
Sheldon Rampton committed
            array(
              '!Wikipedia' => '<a href="http://www.wikipedia.org">Wikipedia</a>',
              '!MediaWiki' => '<a href="http://wikipedia.sourceforge.net/">MediaWiki</a>', 
              '!settings' => l(t("administer &raquo; filters"), "admin/settings/filters", array('html' => TRUE)), 
              '!modules' => l(t("administer &raquo; modules"), "admin/build/modules", array('html' => TRUE)), 
              '!access' => l(t("administer &raquo; access control"), "admin/user/permissions", array('html' => TRUE, 'fragment' => 'module-interwiki')),          
              '!interwikis' => l(t("administer &raquo; interwiki"), "admin/settings/interwiki", array('html' => TRUE))          
Sheldon Rampton's avatar
Sheldon Rampton committed
            )) . 
          t("<h3>Included search prefixes</h3>".
          "<p>The \"interwiki\" table created via file interwiki.sql comes with a number of records already included that facilitate linking to articles or search results on the following websites:</p>\n".
          "<table><tr><th>Prefix</th><th>Site</th></tr>\n".
          "<tr><td>w</td><td>!w, the online, open source encyclopedia</td></tr>".
          "<tr><td>sw</td><td>!sw, a wiki-based encyclopedia of lobbyists, PR firms, think tanks and other political advocacy groups</td></tr>".
          "<tr><td>kos</td><td>!kos, a wiki affiliated with the Daily Kos website</td></tr>".
          "<tr><td>ebay</td><td>!ebay</tr>".
          "<tr><td>google</td><td>!google, the online search engine</tr>".
          "<tr><td>th</td><td>an online !th</td></tr>".
          "<tr><td>archive</td><td>the !archive, also known as the Internet Archive, which stores and displays old versions of websites</td></tr>".
          "<tr><td>whois</td><td>!whois, Internic's search tool for information about who owns a domain name</td></tr>".
          "<tr><td>opendir</td><td>the !opendir, a human-edited web search engine</td></tr></table>\n".
Sheldon Rampton's avatar
Sheldon Rampton committed
          "<p>In addition, it has entries that facilitate linking to URLs in general and to content on your own local site. For example, [http://www.somesite.org|Some Website] ".
          "produces <a href=\"http://www.somesite.org\">Some Website</a>, and [:node/5|my fifth posting] produces <a href=\"node/5\">my fifth posting</a>. If \"http:\" is used as ".
          "the prefix, you can use !settings to specify the space character instead of the vertical bar character as the \"URL terminator\" which separates the URL from its display text. ".
Sheldon Rampton's avatar
Sheldon Rampton committed
          "(This option emulates the syntax used to specify external URLs in Wikipedia articles. If emulating Wikipedia is not important on your site, you'll probably want to use the default vertical bar character.)</p>",
          array(
            '!w' => l('Wikipedia', 'http://www.wikipedia.org'),
            '!sw' => l('SourceWatch', 'http://www.sourcewatch.org'),
            '!kos' => l('dKosopedia', 'http://www.dkosopedia.com'),
            '!ebay' => l('eBay', 'http://www.ebay.com'),
            '!google' => l('Google', 'http://www.google.com'),
            '!th' => l('thesaurus', 'http://thesaurus.reference.com'),
            '!archive' => l('Wayback Machine', 'http://web.archive.org'),
            '!whois' => l('whois', 'http://www.internic.net/whois.html'),
            '!opendir' => l('Open Directory project', 'http://search.dmoz.org'),
            '!settings' => l(t("administer &raquo; filters"), "admin/settings/filters", array('html' => TRUE)), 
Sheldon Rampton's avatar
Sheldon Rampton committed
          )) .
                  t("<h3>For more information</h3>".
                  "<ul><li><a href = \"http://en.wikipedia.org/wiki/Interwiki Wikipedia\" title = \"wikipedia interwiki definition\">Wikipedia's interwiki definition</a>.</li>".
                  "<li>Configuration and customization handbook: <a href = \"http://drupal.org/handbook/modules/interwiki\" title = \"interwikipage\">interwiki page</a>.</li></ul>"
          );
 * hook_perm: Define user permissions for module interwiki
 * 
 *   - access content: User can view the list of available filters
 *   - administer interwiki: User can edit or add to the list of filters
 *
 * @note See hook_perm() for a description of parameters and return values.
 */
function interwiki_perm() {
  return array('administer interwiki');
}

/**
 * Implementation of hook_menu().
 */
Sheldon Rampton's avatar
Sheldon Rampton committed

function interwiki_menu() {
  $items['interwiki'] = array(
    'title' => 'Wiki filter prefixes',
    'access arguments' => array(TRUE),
    'page callback' => 'interwiki_list',
    'type' => MENU_CALLBACK);
  $items['admin/settings/interwiki'] = array(
    'title' => 'Interwiki',
    'description' => 'Manage interwiki filters.',
    'access arguments' => array('administer interwiki'),
    'page callback' => 'interwiki_admin');
  $items['admin/settings/interwiki/list'] = array(
    'title' => 'list',
    'access arguments' => array('administer interwiki'),
    'page callback' => 'interwiki_admin',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10 );
  $items['admin/settings/interwiki/add'] = array(
    'title' => 'add',
    'access arguments' => array('administer interwiki'),
    'page callback' => 'interwiki_admin',
    'type' => MENU_LOCAL_TASK);
/**
 * Return a prefix from the interwiki table. The prefix chosen
 * should be something other than "http" or the empty string "".
 * Preference is first given to the default prefix for the specified
 * format. If no format is specified, preference is given to 
 * prefixes that have a value of 1 for iw_local, and then to
 * the prefix "w" if it exists. (The "w" prefix for Wikipedia is included
 * in the standard interwiki table that ships with this module.) 
 */
function _interwiki_sample_prefix($format) {
  if ($format) {
    $default_prefix = variable_get("interwiki_default_$format", '');
    $prefix = db_result(db_query("SELECT iw_prefix FROM {interwiki} WHERE iw_prefix = '%s'", $default_prefix));
  }
  if (!$prefix) {
    $prefix = db_result(db_query("SELECT iw_prefix FROM {interwiki} WHERE iw_prefix != 'http' AND iw_prefix != '' AND iw_local = 1"));
  }
  if (!$prefix) {
    $prefix = db_result(db_query("SELECT iw_prefix FROM {interwiki} WHERE iw_prefix = 'w'"));
  }
  if (!$prefix) {
    $prefix = db_result(db_query("SELECT iw_prefix FROM {interwiki} WHERE iw_prefix != '' ORDER BY iw_local DESC"));
  }
  return $prefix;
}

 * List interwiki filter prefixes.
function interwiki_list() {
  $format = arg(1);
  $sample_prefix = _interwiki_sample_prefix($format);
  $syntax = variable_get("interwiki_syntax_$format", array('single'));
  if ((array_search('double', $syntax) === FALSE) || !(array_search('single', $syntax) === FALSE)) {
    $unpiped = t('[prefix:some term]');
    $piped   = t("[$sample_prefix:public transport|public transportation]");
  } else {
    $unpiped = t('[[prefix:some term]]');
    $piped   = t("[[$sample_prefix:public transport|public transportation]]");
  $trans_result = interwiki_filter('process', 0, $format, $piped);
  $output = t("<p>You can easily link to terms in wikis and various other websites using a simplified markup syntax. For example, \"%unpiped\" creates a hyperlink to the ".
Sheldon Rampton's avatar
Sheldon Rampton committed
                "\"some term\" article on the website specified by \"prefix.\" It is also possible to use the \"|\" character to create a \"piped link,\" with display text that is ".
          "different from the search term. For example, \"%piped\" would be translated as a reference to an article about \"public ".
          "transport\" that displays as \"!trans.\"</p>",
Sheldon Rampton's avatar
Sheldon Rampton committed
             '%unpiped' => $unpiped, 
             '%piped' => $piped,
Sheldon Rampton's avatar
Sheldon Rampton committed
          ));
Sheldon Rampton's avatar
Sheldon Rampton committed
  $result = db_query("SELECT iw_prefix, iw_url, iw_rel from {interwiki}");
  $header = array(t('Prefix'), t('rel'), t('Translates to'));
  while ($record = db_fetch_object($result)) {
    // Strip out $1, $2, $3 and $4 from interwiki tables ...
    $url = preg_replace(array('/\$1/', '/\$2/', '/\$3/', '/\$4/'), '', $record->iw_url);
Sheldon Rampton's avatar
Sheldon Rampton committed
    if ($url == '') {
      $url = t('<i>a local path on this website</i>');
    }
Sheldon Rampton's avatar
Sheldon Rampton committed
    $rows[] = array ($record->iw_prefix . ":", $record->iw_rel, $url);
  }
  $output .= t('Available prefixes are:') . '<p>';
  $output .= theme('table',$header,$rows);
  print theme("page", $output);
}

/**
 * Implementation of hook_filter().
 */
function interwiki_filter($op, $delta = 0, $format = -1, $text = '') {
      return array(0 => t('Interwiki filter'));
      return t('Easily link to wikis and other reference websites'); 
Sheldon Rampton's avatar
Sheldon Rampton committed
      $result = db_query("SELECT iw_prefix, iw_url, iw_local, iw_rel from {interwiki}");
Sheldon Rampton's avatar
 
Sheldon Rampton committed
      $targets = array();
      while ($record = db_fetch_object($result)) {
         $matcha[$record->iw_prefix] = $record->iw_url;
Sheldon Rampton's avatar
 
Sheldon Rampton committed
         $targets[$record->iw_prefix] = $record->iw_local ? '_self' : '_blank';
Sheldon Rampton's avatar
Sheldon Rampton committed
         $rels[$record->iw_prefix] = $record->iw_rel;
      $syntax = variable_get("interwiki_syntax_$format", array('single'));
      if (!(array_search('double', $syntax) === FALSE)) {
        $pregs[] = '/\[\[([^]:]+)?:([^]]+)?\]\]/i';
      }
      if (!(array_search('single', $syntax) === FALSE) || (array_search('double', $syntax) === FALSE)) {
        $pregs[] = '/\[([^]:]+)?:([^]]+)?\]/i';
      }
Sheldon Rampton's avatar
 
Sheldon Rampton committed
      foreach ($pregs as $preg) {
        if (preg_match_all($preg, $text, $match)) {
          $index = 0;
          foreach($match[0] as $pattern_matched) {
            if (! ($matcha[$match[1][$index]] == "")) {
Sheldon Rampton's avatar
 
Sheldon Rampton committed
              // a prefix of 'http' is a special case where the prefix ought to display if no separate display term is specified
              if ($match[1][$index] == 'http') {
Sheldon Rampton's avatar
Sheldon Rampton committed
          $display_prefix = $match[1][$index] . ':';
                $url_terminator = (variable_get("interwiki_terminator_$format", 'vert') == 'space') ? ' ' : '|';
Sheldon Rampton's avatar
Sheldon Rampton committed
        } else {
          $display_prefix = '';
        $url_terminator = '|';
Sheldon Rampton's avatar
 
Sheldon Rampton committed
              }
              $target = $targets[$match[1][$index]];
Sheldon Rampton's avatar
Sheldon Rampton committed
              $rel = $rels[$match[1][$index]];
              $term = trim($match[2][$index]);
              $term_array = explode($url_terminator, $term, 2);
              $display_term = $term_array[1] ? $term_array[1] : $display_prefix . $term;
              $url_term = $term_array[0];
              // if the prefix is 'http', don't fix ampersands
Sheldon Rampton's avatar
Sheldon Rampton committed
        if (!$display_prefix) {
Sheldon Rampton's avatar
Sheldon Rampton committed
                $display_term = preg_replace('/\&#038;/', '&', $display_term); 
                $url_term = preg_replace('/\&#038;/', '%26', $url_term); 
                $url_term = preg_replace('/\&amp;/', '%26', $url_term); 
                $url_term = preg_replace('/\&/', '%26', $url_term); 
              }
              $display_term = preg_replace("/<[^>]*>/", "", $display_term); // strip out any HTML tags
              $url_term1 = preg_replace('/\ /', '_', $url_term);
              $url_term2 = preg_replace('/\ /', '+', $url_term);
              $url_term3 = preg_replace('/\ /', '%20', $url_term);
              $url_term4 = preg_replace('/\ /', '-', $url_term);
              $url = preg_replace(array('/\$1/','/\$2/', '/\$3/', '/\$4/'), array($url_term1, $url_term2, $url_term3, $url_term4), $matcha[$match[1][$index]]);
              // If it's an external path, don't use the l() function
              if (preg_match("/^(http|https|mailto|ftp):/i", $url)) {
Sheldon Rampton's avatar
Sheldon Rampton committed
                $html = '<a href="' . $url .'" title="reference on '. $display_term . ($rel != "" ? ('" rel="' . $rel) : '') .'" target="'. $target .'">'. $display_term .'</a>';
              } else {
                $html = l($display_term, $url, array('title' => "reference on $display_term", 'target' => $target));
              }
              $text = str_replace($pattern_matched, $html, $text);
            }
            $index++;

      // Handle the default prefix
Sheldon Rampton's avatar
Sheldon Rampton committed
    if (!(array_search('double', $syntax) === FALSE)) {
        $preg = '/\[\[([^]]+)?\]\]/i';
        $match_default = variable_get("interwiki_default_$format", _interwiki_sample_prefix($format));
        if (preg_match_all($preg, $text, $match)) {
          $index = 0;
          foreach($match[0] as $pattern_matched) {
            if (! ($matcha[$match_default] == "")) {
              // a prefix of 'http' is a special case where the prefix ought to display if no separate display term is specified
Sheldon Rampton's avatar
Sheldon Rampton committed
        if ($match_default == 'http') {
          $display_prefix = $match_default . ':';
                $url_terminator = (variable_get("interwiki_terminator_$format", 'vert') == 'space') ? ' ' : '|';
Sheldon Rampton's avatar
Sheldon Rampton committed
        } else {
          $display_prefix = '';
        $url_terminator = '|';
        }
              $target = $targets[$match_default];
Sheldon Rampton's avatar
Sheldon Rampton committed
              $rel = $rels[$match_default];
              $term = trim($match[1][$index]);
              $term_array = explode($url_terminator, $term, 2);
              $display_term = $term_array[1] ? $term_array[1] : $display_prefix . $term;
              $url_term = $term_array[0];
              // if the prefix is 'http', don't fix ampersands
Sheldon Rampton's avatar
Sheldon Rampton committed
        if (!$display_prefix) {
Sheldon Rampton's avatar
Sheldon Rampton committed
                $display_term = preg_replace('/\&#038;/', '&', $display_term); 
                $url_term = preg_replace('/\&#038;/', '%26', $url_term); 
                $url_term = preg_replace('/\&amp;/', '%26', $url_term); 
                $url_term = preg_replace('/\&/', '%26', $url_term); 
              }
              $url_term1 = preg_replace('/\ /', '_', $url_term);
              $url_term2 = preg_replace('/\ /', '+', $url_term);
              $url_term3 = preg_replace('/\ /', '%20', $url_term);
              $url_term4 = preg_replace('/\ /', '-', $url_term);
              $url = preg_replace(array('/\$1/','/\$2/', '/\$3/', '/\$4/'), array($url_term1, $url_term2, $url_term3, $url_term4), $matcha[$match_default]);
              // If it's an external path, don't use the l() function
              if (preg_match("/^(http|https|mailto|ftp):/i", $url)) {
Sheldon Rampton's avatar
Sheldon Rampton committed
                $html = '<a href="' . $url .'" title="reference on '. $display_term . ($rel != "" ? ('" rel="' . $rel) : '') .'" target="'. $target .'">'. $display_term .'</a>';
              } else {
                $html = l($display_term, $url, array('title' => "reference on $display_term", 'target' => $target));
              }
              $text = str_replace($pattern_matched, $html, $text);
            }
            $index++;
          }
        }
      }

      return $text; 

    case 'settings':
      $form['interwiki'] = array(
        '#type' => 'fieldset',
        '#title' => t('Interwiki settings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $sample_prefix = _interwiki_sample_prefix($format);
      $syntax = variable_get("interwiki_syntax_$format", array('single'));
      if ((array_search('double', $syntax) === FALSE) || !(array_search('single', $syntax) === FALSE)) {
        $unpiped = t('[prefix:term]');
        $piped   = t("[$sample_prefix:public transport|public transportation]");
      } else {
        $unpiped = t('[[prefix:term]]');
        $piped   = t("[[$sample_prefix:public transport|public transportation]]");
      $trans_result = interwiki_filter('process', 0, $format, $piped);
      $output = t('The interwiki filter is enabled. You can easily link to terms in various wikis or other websites by typing %unpiped. ' . 
        'Use the "|" character to create a "piped link," e.g., "%piped" ' .
        'displays as "!trans." ' .
        'For a full list of available prefixes and the websites to which they point, see !prefixes.',
        array(
          '!trans' => $trans_result,
          '!prefixes' => l('interwiki', "interwiki/$format")
        )
      );
      $form['interwiki']['intro'] = array(
        '#type' => 'markup',
        '#value' => "<p>$output</p>"
      );
      $syntax = array('single' => 'Single brackets, e.g., [prefix:some term]', 'double' => 'Double brackets, e.g., [[prefix:some term]]');
      $form['interwiki']["interwiki_syntax_$format"] = array(
        '#type' => 'checkboxes',
        '#title' => t('Syntax'),
        '#default_value' => variable_get("interwiki_syntax_$format", array('single')),
        '#options' => $syntax,
        '#description' => t('Check the boxes above to select the syntax(es) used to specify interwiki links. Single brackets are simpler, but double brackets are closer to the syntax used by <a href="http://wikipedia.sourceforge.net/">MediaWiki</a>, one of the most most popular wiki software packages. (If neither is checked, the filter defaults to single bracket syntax.)'),
      );
      $form['interwiki']["interwiki_default_$format"] = array(
        '#type' => 'textfield',
        '#title' => t('Default prefix'),
        '#default_value' => variable_get("interwiki_default_$format", _interwiki_sample_prefix($format)),
        '#size' => 10,
        '#maxlength' => 30,
        '#description' => t("The prefix used by default with the double bracket syntax. For example, a default prefix of \"$sample_prefix\" means that [[some term]] is synonymous with [[$sample_prefix:some term]]. (This may be useful if you want to be able to cut-and-paste text from an existing wiki into your site.)"),
      );
Sheldon Rampton's avatar
Sheldon Rampton committed
      $terminators = array('space' => t('Space character'), 'vert' => t('Vertical bar (|)'));
      $form['interwiki']["interwiki_terminator_$format"] = array(
        '#type' => 'radios',
        '#title' => t('URL terminator'),
        '#default_value' => variable_get("interwiki_terminator_$format", 'vert'),
        '#options' => $terminators,
        '#description' => t('The terminator character used to mark the end of a URL. For maximum compatibility with Wikipedia\'s syntax, choose "space." Otherwise, choose the vertical bar character (|).'),
      );
Sheldon Rampton's avatar
Sheldon Rampton committed
      return $form;
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function interwiki_filter_tips($delta, $format, $long = false) {
  $syntax = variable_get("interwiki_syntax_$format", array('single'));
  $sample_prefix = _interwiki_sample_prefix($format);
  if ((array_search('double', $syntax) === FALSE) || !(array_search('single', $syntax) === FALSE)) {
    $unpiped = t('[prefix:term]');
    $piped   = t("[$sample_prefix:public transport|public transportation]");
  } else {
    $unpiped = t('[[prefix:term]]');
    $piped   = t("[[$sample_prefix:public transport|public transportation]]");
 if ($long) {
    return t('Easily link to terms in various wikis or other websites by typing %unpiped. ' . 
      'Use the "|" character to create a "piped link," e.g., "%piped" '.
      'displays as "<a href="http://en.wikipedia.org/wiki/public_transport">public transportation</a>." '.
      'For a full list of available prefixes and the websites to which they point, see !prefixes.',
      array(
        '%unpiped' => $unpiped,
        '%piped' => $piped,
        '!prefixes' => l('interwiki', "interwiki/$format")
      )
    );
    return t('Easily link to terms in various wikis. For help, see !prefixes.',
      array(
        '!prefixes' => l('interwiki', "interwiki/$format"),
      )
    );
}

/**
 * Return a single filter from the database.
 */
function interwiki_get($iw_prefix) {
  return db_fetch_array(db_query("SELECT * FROM {interwiki} WHERE iw_prefix = '%s'", $iw_prefix));
}

/**
 * Display an editing form for adding or changing an individual filter.
 *
 */
function interwiki_form($edit = array()) {
Sheldon Rampton's avatar
Sheldon Rampton committed
  return drupal_get_form('interwiki_edit_form', $edit);
}

Sheldon Rampton's avatar
Sheldon Rampton committed
function interwiki_edit_form($form_state, $edit) {
Sheldon Rampton's avatar
Sheldon Rampton committed
  $form["iw_prefix"] = array(
    '#type' => 'textfield',
    '#title' => t("Prefix"),
    '#default_value' => $edit["iw_prefix"],
    '#size' => 32,
    '#maxlength' => 32,
    '#description' => t("The prefix goes before a colon when users edit content, e.g., \"[prefix:some text].\""),
  );
Sheldon Rampton's avatar
Sheldon Rampton committed
  $form["iw_rel"] = array(
    '#type' => 'textfield',
    '#title' => t("rel"),
    '#default_value' => $edit["iw_rel"],
    '#size' => 32,
    '#maxlength' => 32,
    '#description' => t('The rel= parameter, if any, required in this link. This may be blank.'),
  );
Sheldon Rampton's avatar
Sheldon Rampton committed
  $form["iw_url"] = array(
    '#type' => 'textfield',
    '#title' => t("URL"),
    '#default_value' => $edit["iw_url"],
    '#size' => 50,
Sheldon Rampton's avatar
Sheldon Rampton committed
    '#maxlength' => 255,
Sheldon Rampton's avatar
Sheldon Rampton committed
    '#description' => t('The URL to be created. The phrase "some_text" will replace the string "$1" in the translated hyperlink, "some+text" will replace "$2", "some%20text" will replace "$3", and "some-text" will replace "$4".'),
  );
  $form["iw_local"] = array(
    '#type' => 'checkbox',
    '#title' => t("Local?"),
    '#return_value' => 1,
    '#default_value' => $edit["iw_local"],
    '#description' => t("Is this link local to your website? Non-local links will open in a new window."),
  );
  $form[] = array(
    '#type' => 'submit',
    '#value' => t("Submit"),
  );
Sheldon Rampton's avatar
Sheldon Rampton committed
    $form[] = array(
      '#type' => 'submit',
      '#value' => t("Delete"),
    );
Sheldon Rampton's avatar
Sheldon Rampton committed
	return $form;
Sheldon Rampton's avatar
Sheldon Rampton committed

/**
 * Update, insert or delete a filter from the database.
 *
 */
function interwiki_save($edit) {
  db_query("DELETE FROM {interwiki} WHERE iw_prefix = '%s'", $edit["iw_prefix"]);
  if ($edit["action"] != 'delete') {
Sheldon Rampton's avatar
Sheldon Rampton committed
    db_query("INSERT INTO {interwiki} (iw_prefix, iw_url, iw_rel, iw_local) VALUES  ('%s', '%s', '%s', '%d')", $edit["iw_prefix"], $edit["iw_url"], $edit["iw_rel"], $edit["iw_local"]);
/**Confirm deletion of filter*/
Sheldon Rampton's avatar
Sheldon Rampton committed
function _interwiki_confirm_del($edit) {
	return drupal_get_form('interwiki_delete_confirm_form', $edit);
Sheldon Rampton's avatar
Sheldon Rampton committed
}

Sheldon Rampton's avatar
Sheldon Rampton committed
function interwiki_delete_confirm_form($form_state, $edit) {
Sheldon Rampton's avatar
Sheldon Rampton committed
  $form['confirm'] = array(
    '#type' => 'hidden',
    '#value' => 1,
  );
  $form['iw_prefix'] = array(
    '#type' => 'hidden',
Sheldon Rampton's avatar
Sheldon Rampton committed
    '#value' => $edit[iw_prefix],
Sheldon Rampton's avatar
Sheldon Rampton committed
  );
  return confirm_form(
    $form,
    t('Are you sure you want to delete the interwiki prefix %name?', array('%name' => $iw_prefix)),
    'admin/settings/interwiki',
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
/**
 * Display the list of filters for editing purposes.
 *
 */
function interwiki_display() {
Sheldon Rampton's avatar
Sheldon Rampton committed
  $output = '<p>' . t("The interwiki table lets website users easily link to terms in wikis and various other websites using a simplified markup syntax. For example, \"[prefix:some term]\" creates a hyperlink to the ".
    "\"some term\" article on the website specified by \"prefix.\" Available prefixes and the paths to which they point are:") . '</p>';
Sheldon Rampton's avatar
Sheldon Rampton committed
  $result = db_query("SELECT iw_prefix, iw_url, iw_rel, iw_local from {interwiki}");
  $header = array(t('Prefix'), t('rel'), t('Translates to'), t('Local?'), array("data" => t("operations"), "colspan" => 2));
  while ($record = db_fetch_object($result)) {
Sheldon Rampton's avatar
Sheldon Rampton committed
    $rows[] = array ($record->iw_prefix . ":", $record->iw_rel, $record->iw_url, ($record->iw_local ? 'Yes' : 'No'), l(t("edit"), "admin/settings/interwiki/edit/$record->iw_prefix"), l(t("delete"), "admin/settings/interwiki/delete/$record->iw_prefix"));
  }
  $output .= theme('table',$header,$rows);
Sheldon Rampton's avatar
Sheldon Rampton committed
  $output .= '<p>' . t('The paths in the "translates to" row above use placeholder strings to specify how the space character should be ' .
    'escaped in the URL generated by an interwiki translation. The placeholder strings are $1, $2, $3 and $4. They have the following effect:') . '</p>';
Sheldon Rampton's avatar
Sheldon Rampton committed
  $output .= '<ul><li>$1 ' . t('replaces spaces with underscore characters (_)') .'</li>
    <li>$2 ' . t('replaces spaces with plus signs (+)') .'</li>
    <li>$3 ' . t('replaces spaces with "%20"') .'</li>
    <li>$4 ' . t('replaces spaces with dashes (-)') .'</li></ul>';
Sheldon Rampton's avatar
Sheldon Rampton committed
  $output .= '<p>' . t('Therefore, [w:ad hominem] uses the string "ad_hominem" in building the URL to Wikipedia\'s article on <a href="http://en.wikipedia.org/wiki/ad_hominem">ad hominem</a> arguments, '.
    'while [th:ad hominem] uses the string "ad%20hominem" in building the URL to a thesaurus reference for the phrase <a href="http://thesaurus.reference.com/search?q=ad%20hominem">ad hominem</a>.') . '</p>';
  return $output;
}

/**
 * Route all requests to administer the filters.
 *
 */
function interwiki_admin() {
Sheldon Rampton's avatar
Sheldon Rampton committed
  $edit = $_POST;
Sheldon Rampton's avatar
Sheldon Rampton committed
    $op = arg(3);
      $output = interwiki_form();
Sheldon Rampton's avatar
Sheldon Rampton committed
      $output = interwiki_form(interwiki_get(arg(4)));
Sheldon Rampton's avatar
Sheldon Rampton committed
    case "delete": // the lower-case "d" in "delete" indicates that this came from a URL, so there's no $_POST variable and therefore no $edit
Sheldon Rampton's avatar
Sheldon Rampton committed
    $edit = interwiki_get(arg(4));
      // fall through:
    case t("Delete"): // the upper-case "D" indicates that this came from the editing form, so there is a $_POST variable but no arg(3)
Sheldon Rampton's avatar
Sheldon Rampton committed
      $output = _interwiki_confirm_del($edit);
        break;
       }
      else {
        $edit['action'] = 'delete';
        // fall through:
      }
      $output = interwiki_save($edit);
      $output .= interwiki_display();
  }
  print theme('page', $output);
}