Skip to content
interwiki.module 24 KiB
Newer Older
// $Id$
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

/**
 * @file
 * An implementation of an input formatting module for a special non-html link  
 * syntax to wikis and other websites
 *
 * 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) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Easily link to wikis and other websites.');
    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 ".
	              "\"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 ".
				  "\"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",
                  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/filters", array(), NULL, NULL, FALSE, TRUE), 
				     '%modules' => l(t("administer &raquo; modules"), "admin/modules", array(), NULL, NULL, FALSE, TRUE), 
                     '%access' => l(t("administer &raquo; users &raquo; configure &raquo; permissions"), "admin/user/configure/permission", array(), NULL, NULL, FALSE, TRUE),				  
                     '%interwikis' => l(t("administer &raquo; interwiki"), "admin/interwiki", array(), NULL, NULL, FALSE, TRUE)				  
				  )) . 
				  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".
				  "<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. ".
				  "(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/filters", array(), NULL, NULL, FALSE, TRUE), 
					)) .
                  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().
 */
function interwiki_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array('path' => 'interwiki',
	  'title' => t('wiki filter prefixes'),
      'access' => TRUE,
      'callback' => 'interwiki_list',
      'type' => MENU_CALLBACK);
    $items[] = array('path' => 'admin/interwiki',
	  'title' => t('interwiki'),
      'access' => user_access('administer interwiki'),
      'callback' => 'interwiki_admin');
    $items[] = array('path' => 'admin/interwiki/list',
	  'title' => t('list'),
      'access' => user_access('administer interwiki'),
      'callback' => 'interwiki_admin',
      'type' => MENU_DEFAULT_LOCAL_TASK,
	  'weight' => -10 );
    $items[] = array('path' => 'admin/interwiki/add',
	  'title' => t('add'),
      'access' => user_access('administer interwiki'),
      'callback' => 'interwiki_admin',
      'type' => MENU_LOCAL_TASK);
  }
  return $items;
}

/**
 * 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 ".
	              "\"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>",
                  array(
				     '%unpiped' => $unpiped, 
				     '%piped' => $piped,
					 '%trans' => $trans_result				  
				  ));
  $result = db_query("SELECT iw_prefix, iw_url from {interwiki}");
  $header = array(t('Prefix'), 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);
	if ($url == '') {
	  $url = t('<i>a local path on this website</i>');
	}
    $rows[] = array ($record->iw_prefix . ":", $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 = '') {
  switch ($op) {
    case 'list':
      return array(0 => t('Interwiki filter'));

    case 'description':
      return t('Easily link to wikis and other reference websites'); 

    case 'process':
      $result = db_query("SELECT iw_prefix, iw_url, iw_local from {interwiki}");
      $targets = array();
      while ($record = db_fetch_object($result)) {
         $matcha[$record->iw_prefix] = $record->iw_url;
         $targets[$record->iw_prefix] = $record->iw_local ? '_self' : '_blank';
      }
      $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';
      }
      foreach ($pregs as $preg) {
        if (preg_match_all($preg, $text, $match)) {
          $index = 0;
          foreach($match[0] as $pattern_matched) {
            if (! ($matcha[$match[1][$index]] == "")) {
              // 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') {
			    $display_prefix = $match[1][$index] . ':';
                $url_terminator = (variable_get("interwiki_terminator_$format", 'vert') == 'space') ? ' ' : '|';
			  } else {
			    $display_prefix = '';
				$url_terminator = '|';
              }
              $target = $targets[$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
			  if (!$display_prefix) {
                $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)) {
                $html = '<a href="' . $url .'" title="reference on '. $display_term .'" 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
	  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
			  if ($match_default == 'http') {
			    $display_prefix = $match_default . ':';
                $url_terminator = (variable_get("interwiki_terminator_$format", 'vert') == 'space') ? ' ' : '|';
			  } else {
			    $display_prefix = '';
				$url_terminator = '|';
			  }
              $target = $targets[$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
			  if (!$display_prefix) {
                $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)) {
                $html = '<a href="' . $url .'" title="reference on '. $display_term .'" 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':
      $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(
          '%unpiped' => $unpiped,
          '%piped' => $piped,
		  '%trans' => $trans_result,
          '%prefixes' => l('interwiki', "interwiki/$format")
        ));

      $syntax = array('single' => 'Single brackets, e.g., [prefix:some term]', 'double' => 'Double brackets, e.g., [[prefix:some term]]');
      $output .= form_checkboxes(t('Syntax'), "interwiki_syntax_$format", variable_get("interwiki_syntax_$format", array('single')), $syntax, 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.)'));
      $output .= form_textfield(t('Default prefix'), "interwiki_default_$format", variable_get("interwiki_default_$format", _interwiki_sample_prefix($format)), 10, 30, 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.)"));

      $terminators = array('space' => t('Space character'), 'vert' => t('Vertical bar (|)'));
      $output .= form_radios(t('URL terminator'), "interwiki_terminator_$format", variable_get("interwiki_terminator_$format", 'vert'), $terminators, 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 (|).'));

      return form_group(t('interwiki'), $output);

    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")
        ));
  } else {
    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()) {
  $form .= form_textfield(t("Prefix"), "iw_prefix", $edit["iw_prefix"], 32, 32, t("The prefix goes before a colon when users edit content, e.g., \"[prefix:some text].\""));
  $form .= form_textfield(t("URL"), "iw_url", $edit["iw_url"], 50, 128, 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 .= form_checkbox(t("Local?"), "iw_local", 1, $edit["iw_local"], t("Is this link local to your website? Non-local links will open in a new window."));
  $form .= form_submit(t("Submit"));
  
  if ($edit["iw_url"]) {
    $form .= form_submit(t("Delete"));
  }
  $form = form($form);
  return $form;
}

/**
 * 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') {
    db_query("INSERT INTO {interwiki} (iw_prefix, iw_url, iw_local) VALUES  ('%s', '%s', '%d')", $edit["iw_prefix"], $edit["iw_url"], $edit["iw_local"]);
  }
}

/**Confirm deletion of filter*/
function _interwiki_confirm_del($edit=array()) {

  $extra  = form_hidden('confirm', 1);
  $extra .= form_hidden('iw_prefix', $edit['iw_prefix']);

  $output = theme('confirm',
                  t('Are you sure you want to delete the interwiki prefix %name?', array('%name' => theme('placeholder', $edit['iw_prefix']))),
                  'admin/interwiki',
                  t('This action cannot be undone.'),
                  t('Delete'),
                  t('Cancel'),
                  $extra);
  return $output;
}

/**
 * Display the list of filters for editing purposes.
 *
 */
function interwiki_display() {
  $output = t("<p>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>");
  $result = db_query("SELECT iw_prefix, iw_url, iw_local from {interwiki}");
  $header = array(t('Prefix'), t('Translates to'), t('Local?'), array("data" => t("operations"), "colspan" => 2));
  while ($record = db_fetch_object($result)) {
    $rows[] = array ($record->iw_prefix . ":", $record->iw_url, ($record->iw_local ? 'Yes' : 'No'), l(t("edit"), "admin/interwiki/edit/$record->iw_prefix"), l(t("delete"), "admin/interwiki/delete/$record->iw_prefix"));
  }
  $output .= theme('table',$header,$rows);
  return $output;
}

/**
 * Route all requests to administer the filters.
 *
 */
function interwiki_admin() {
  $op = $_POST["op"];
  $edit = $_POST["edit"];

  if (empty($op)) {
    $op = arg(2);
  }

  switch ($op) {
    case "add":
      $output = interwiki_form();
      break;
    case "edit":
      $output = interwiki_form(interwiki_get(arg(3)));
      break;
	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
	  $edit = interwiki_get(arg(3));
      // 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)
      if (!$edit['confirm']) {
	    $output = _interwiki_confirm_del($edit);
        break;
       }
      else {
        $edit['action'] = 'delete';
        // fall through:
      }
    case t("Submit"):
      $output = interwiki_save($edit);
      // fall through:
    default:
      $output .= interwiki_display();
  }

  print theme('page', $output);
}

?>