--- ./modules/mediawiki_api_filter/mediawiki_api_filter.module	2009-09-06 15:26:05.000000000 -0500
+++ ./modules/mediawiki_api_filter/mediawiki_api_filter.module.updated	2010-08-04 02:50:31.000000000 -0500
@@ -17,9 +17,10 @@ function mediawiki_api_filter_help($path
   switch ($path) {
     case 'admin/help#' . $module :
       $link = theme('advanced_help_topic', $module, $topic, 'title'); 
-      if($link){
+      if ($link) {
         $output = advanced_help_view_topic($module, $topic);    
-      }else{
+      }
+      else {
         $output .= t("<p>Full help documentation requires the Advanced Help module 
         is installed. See README.txt for more information.</p>");
       }
@@ -32,7 +33,7 @@ function mediawiki_api_filter_help($path
 /**
  * 
  * @return global admin settings form. 
- * This is not aviable at this stage. If it is goiing to be coded the logic for overiding per filter setting with global settings needs to be designed.
+ * This is not available at this stage. If it is going to be coded, the logic for overiding per filter setting with global settings needs to be designed.
  */
 /*
 function mediawiki_api_filter_admin_settings() {
@@ -77,7 +78,7 @@ function mediawiki_api_filter_menu() {
  * @param $format string specifying the format these settings are for. Blank implies global
  * @return a form array
  */
-function mediawiki_api_filter_make_settings_form($format = ''){
+function mediawiki_api_filter_make_settings_form($format = '') {
   $form = array();
   $form['mediawiki_api'] = array(
     '#type' => 'fieldset', 
@@ -88,91 +89,100 @@ function mediawiki_api_filter_make_setti
     '#type' => 'checkbox',
     '#title' => t('Prevent MediaWiki TOC'),
     '#description' => t('Prevents MediaWiki from adding a Table Of Contents for all nodes using this filter. (This can be overridden for individual nodes by including either the __NOTOC__ or __TOC__ markup anywhere in the input text)'),
-    '#default_value' => variable_get('mediawiki_api_filter_prevent_toc_' . $format, true),
+    '#default_value' => variable_get('mediawiki_api_filter_prevent_toc_' . $format, TRUE),
     '#required' => TRUE);
   
   $form['mediawiki_api']['mediawiki_api_filter_prevent_section_edit_' . $format] = array(
     '#type' => 'checkbox',
     '#title' => t('Prevent MediaWiki Section Edit'),
     '#description' => t('Prevents MediaWiki from adding a Section Edit links. Since these will link to the MediaWiki installation you will almost certainly want to prevent them. (This can be overridden for individual nodes by including either the __NOSECTIONEDIT__ or __SECTIONEDIT__ markup anywhere in the input text)'),
-    '#default_value' => variable_get('mediawiki_api_filter_prevent_section_edit_' . $format, true),
+    '#default_value' => variable_get('mediawiki_api_filter_prevent_section_edit_' . $format, TRUE),
     '#required' => TRUE);
 
-  $form['mediawiki_api']['mediawiki_api_filter_prevent_mediawiki_links_' . $format] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Prevent MediaWiki links'),
-    '#description' => t('Prevents MediaWiki from managing links marked up [[link]]. Recommended because MediaWiki will almost certainly add the wrong link. Use a filter like ') . l('freelinking','http://drupal.org/project/freelinking') . t(' to deal with them inside Drupal.'),
-    '#default_value' => variable_get('mediawiki_api_filter_prevent_mediawiki_links_' . $format, true),
+  $form['mediawiki_api']['link_option_fieldset'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('MediaWiki Link Options'),
+    '#collapsible' => FALSE,
+    '#description' => t('Select how MediaWiki should manage links marked up <em>[[link]]</em>. '
+                        . 'Preventing links is recommended, because MediaWiki will almost certainly add the wrong link. '
+                        . 'Use a filter like ') 
+                      . l('freelinking', 'http://drupal.org/project/freelinking') 
+                      . t(' or ')
+                      . l('customfilter', 'http://drupal.org/project/customfilter')                    
+                      . t(' to deal with them inside Drupal.')
+  );
+
+  $form['mediawiki_api']['link_option_fieldset']['mediawiki_api_filter_prevent_mediawiki_links_' . $format] = array(
+    '#type' => 'radios',
+    '#name' => 'Link Options',
+    '#default_value' => variable_get('mediawiki_api_filter_prevent_mediawiki_links_' . $format, 'All'),
+    '#options' => array('All' => 'Prevent all links', 
+                        'File' => 'Prevent file links only (links marked up <em>[[file:]]</em> or <em>[[media:]]</em> or <em>[[image:]]</em>)',
+                        'None' => 'Allow all links'),
     '#required' => TRUE);
   
-   $form['mediawiki_api']['mediawiki_api_filter_prevent_mediawiki_images_' . $format] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Prevent MediaWiki images'),
-    '#description' => t('Prevents MediaWiki from managing images marked up [image:]. Recommended because MediaWiki will almost certainly add the wrong img tag. Use a filter like ') . l('image_filter','http://drupal.org/project/image_filter') . t(' to deal with them inside Drupal.'),
-    '#default_value' => variable_get('mediawiki_api_filter_prevent_mediawiki_images_' . $format, true),
-    '#required' => TRUE);
-   
    $protect_count = variable_get('mediawiki_api_filter_protect_count_' . $format, 1);
-   if(variable_get('mediawiki_api_filter_increase_protect_count_' . $format, false)){
+   if (variable_get('mediawiki_api_filter_increase_protect_count_' . $format, FALSE)) {
     $protect_count += 1;
-    variable_set('mediawiki_api_filter_increase_protect_count_' . $format, false);
+    variable_set('mediawiki_api_filter_increase_protect_count_' . $format, FALSE);
     variable_set('mediawiki_api_filter_protect_count_' . $format, $protect_count);
    }
    $form['mediawiki_api']['protectors'] = array(
     '#type' => 'fieldset', 
     '#title' => t('Protectors'),
     '#description' => t('Any input text that matches a protector will not be passed to MediaWiki for markup.'),
-    '#collapsible' => TRUE,);
+    '#collapsible' => TRUE);
          
    $form['mediawiki_api']['protectors']['mediawiki_api_filter_protect_count_' . $format] = array(
     '#type' => 'hidden',
     '#value' => $protect_count);
     
     $protecti=1;
-    while($protecti <= $protect_count){
-    	 $form['mediawiki_api']['protectors']['protect' . $protecti] = array(
-		    '#type' => 'fieldset', 
-		    '#title' => t('Protector ' . $protecti), 
-		    '#collapsible' => TRUE,);
-    	 
-       $form['mediawiki_api']['protectors']['protect' . $protecti]['mediawiki_api_filter_protect_' . $protecti . '_expresion_' . $format] = array(
+    while ($protecti <= $protect_count) {
+      $form['mediawiki_api']['protectors']['protect' . $protecti] = array(
+        '#type' => 'fieldset', 
+        '#title' => t('Protector ' . $protecti), 
+        '#collapsible' => TRUE);
+
+      $form['mediawiki_api']['protectors']['protect' . $protecti]['mediawiki_api_filter_protect_' . $protecti . '_expresion_' . $format] = array(
         '#type' => 'textfield',
         '#title' => t('Protect expression'),
         '#size' => 50,
-		    '#default_value' => variable_get('mediawiki_api_filter_protect_' . $protecti . '_expresion_' . $format, ''),
-		    '#description' => t('An expression, describing what text to protect, either a space-separated list of tag names or a single regular expression.'));
+        '#default_value' => variable_get('mediawiki_api_filter_protect_' . $protecti . '_expresion_' . $format, ''),
+        '#description' => t('An expression, describing what text to protect, either a space-separated list of tag names or a single regular expression.'));
        
-       $form['mediawiki_api']['protectors']['protect' . $protecti]['mediawiki_api_filter_protect_' . $protecti . '_type_' . $format] = array(
+      $form['mediawiki_api']['protectors']['protect' . $protecti]['mediawiki_api_filter_protect_' . $protecti . '_type_' . $format] = array(
         '#type' => 'select',
         '#title' => t('Protect expression type'), 
         '#default_value' => variable_get('mediawiki_api_filter_protect_' . $protecti . '_type_' . $format, 'tags'),
         '#description' => t('The type of expression. If a tag names, the entirety of each tag from opening to closing will be ignored. if a regular expression the full match will be protected.'),
         '#options' => array(
           'tags' => t('Tags'),
-          'regex' => t('Regular Expression'),
+          'regex' => t('Regular Expression')
         ),
        );
        $protecti+=1;
     }
     $form['mediawiki_api']['protectors']['mediawiki_api_filter_increase_protect_count_' . $format] = array(
-     '#type' => 'checkbox',
-     '#title' => t('Add another protector'),
-     '#description' => t('Check this box and click "Save configuration" to add another protector'),
-     '#default_value' => false);
+      '#type' => 'checkbox',
+      '#title' => t('Add another protector'),
+      '#description' => t('Check this box and click "Save configuration" to add another protector'),
+      '#default_value' => FALSE);
    
   return $form;
 
 }
 
-function mediawiki_api_filter_get_help_popup_link($topic='index'){
+function mediawiki_api_filter_get_help_popup_link($topic='index') {
   //TODO return an alternative location if advanced help is not installed.
   $output = '';
   $module= 'mediawiki_api_filter';
   $link = theme('advanced_help_topic', $module, $topic); 
-  if($link){
+  if ($link) {
     $output = '' . $link . '';
-  }else{
-    $output .= l(t('(help page)'),'admin/help/' . $module);
+  }
+  else {
+    $output .= l(t('(help page)'), 'admin/help/' . $module);
   }
   return $output;
 }
@@ -190,48 +200,62 @@ function mediawiki_api_filter_filter($op
       return t("Filter which uses the MediaWiki parser via the MediaWiki API.") . mediawiki_api_filter_get_help_popup_link();
 
     case 'process':
-      if(variable_get('mediawiki_api_filter_prevent_toc_' . $format, true)){
-        if(!preg_match('/__(NO)?TOC__/', $text)){
+
+      if (variable_get('mediawiki_api_filter_prevent_toc_' . $format, TRUE)) {
+        if (!preg_match('/__(NO)?TOC__/', $text)) {
           $text = $text . " __NOTOC__ " ; //add __NOTOC__ unless the user has already included __(NO)?TOC__
         }
       }
-      if(variable_get('mediawiki_api_filter_prevent_section_edit_' . $format, true)){        
-        if(!preg_match('/__(NO)?EDITSECTION__/', $text)){
+      if (variable_get('mediawiki_api_filter_prevent_section_edit_' . $format, TRUE)) {        
+        if (!preg_match('/__(NO)?EDITSECTION__/', $text)) {
           $text = $text . " __NOEDITSECTION__ " ; // add the __NOEDITSECTION__, unless the user has already included __(NO)?EDITSECTION__
-        }else{
-        	$text = preg_replace('/__EDITSECTION__/',$text,''); //__EDITSECTION__ is not a mediawiki market, if it has been put in remove it. Mediawiki will provide edit section links by default (but they won't be of much use inside Drupal unless they are further filtered.)
+        }
+        else {
+          $text = preg_replace('/__EDITSECTION__/', $text, ''); //__EDITSECTION__ is not a mediawiki marker, if it has been put in remove it. Mediawiki will provide edit section links by default (but they won't be of much use inside Drupal unless they are further filtered.)
         }
       }
-      if(variable_get('mediawiki_api_filter_prevent_mediawiki_images_' . $format, true)){
-        $text = preg_replace("/(\[\[.*\]\])/", "<nowiki>$1</nowiki>", $text);
-      }
-      if(variable_get('mediawiki_api_filter_prevent_mediawiki_links_' . $format, true)){
-        $text = preg_replace("/(\[image\:.*\])/", "<nowiki>$1</nowiki>", $text);      
-      }
-    
+
+      $linkOption = variable_get('mediawiki_api_filter_prevent_mediawiki_links_' . $format, 'All');
+      if ($linkOption == 'All') {
+        $text = preg_replace("/(\[\[[^#].*?\]\])/", "<nowiki>$1</nowiki>", $text);
+      }
+      elseif ($linkOption == 'File') {
+        $text = preg_replace("/(\[\[(File|file|Media|media|Image|image):.*?\]\])/", "<nowiki>$1</nowiki>", $text);      
+      } 
+   
       $api_url = variable_get('mediawiki_api_url', NULL);
-      if(empty($api_url )){
-        return t("MediaWiki API URL has not been set, In order for this folter to process the text the MediaWiki API URL needs to be set in admin/settings/mediawikiapi");
+      if (empty($api_url )) {
+        return t("MediaWiki API URL has not been set, In order for this filter to process the text the MediaWiki API URL needs to be set in admin/settings/mediawikiapi");
       }
       require_once "HTTP/Request.php";
       $query="action=parse&format=php";
       $req = new HTTP_Request($api_url . "?" . $query );
 
-    $req->setMethod(HTTP_REQUEST_METHOD_POST);
-    $req->addPostData("text", $text);
-    //$req->addPostData("editsection", 'false');
-    $ret='';
-    if (!PEAR::isError($req->sendRequest())) {
-        $response1 = $req->getResponseBody();
-      $a = unserialize($response1);
-      $ret=$a['parse']['text']['*'];
-    } else {
+      $req->setMethod(HTTP_REQUEST_METHOD_POST);
+      $req->addPostData("text", $text);
+      //$req->addPostData("editsection", 'false');
       $ret='';
-    }                
-    if(preg_match('/__MEDIAWIKI_API_DEBUG__/', $text)){
-      $ret .= '<pre>' . $text . '</pre>';
-    }
-    return $ret;
+    
+      if (!PEAR::isError($req->sendRequest())) {
+        $cResponse = $req->getResponseBody();
+        $cResponse = trim( preg_replace( '/\s+/', ' ', $cResponse ) );  
+        $cResponse = preg_replace("/(<script.*?>)(.*)<\/script>/e", '"$1" . base64_encode("$2") . "</script>"', $cResponse);
+        $cResponse = preg_replace('!s:(\d+):"(.*?)";!se', '"s:".strlen("$2").":\"$2\";"', $cResponse );
+      
+        $a = unserialize($cResponse);
+        $ret=$a['parse']['text']['*'];
+        $ret = preg_replace("/(<script.*?>)(.*)<\/script>/e", '"$1" . base64_decode("$2") . "</script>"', $ret);
+        $ret = preg_replace("/\\\\'/", "'",  $ret);
+      } 
+      else {
+        $ret='';
+      }    
+
+      if (preg_match('/__MEDIAWIKI_API_DEBUG__/', $text)) {
+        $ret .= '<pre>' . $text . '</pre>';
+      }
+      return $ret;
+
     case 'settings':
       return mediawiki_api_filter_make_settings_form($format);
       
