Index: php.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/php/php.install,v
retrieving revision 1.1
diff -u -p -r1.1 php.install
--- php.install	24 Apr 2007 10:54:34 -0000	1.1
+++ php.install	17 Jan 2008 08:12:44 -0000
@@ -17,6 +17,9 @@ function php_install() {
     // Enable the PHP evaluator filter.
     db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, 'php', 0, 0)", $format);
 
+   // Write the filter id to variable table, to facilitate removal later.
+    variable_set('php_code_filter_id',$format);
+
     drupal_set_message(t('A !php-code input format has been created.', array('!php-code' => l('PHP code', 'admin/settings/filters/'. $format))));
   }
 }
@@ -27,3 +30,43 @@ function php_install() {
 function php_disable() {
   drupal_set_message(t('The PHP module has been disabled. Please note that any existing content that was using the PHP filter will now be visible in plain text. This might pose a security risk by exposing sensitive information, if any, used in the PHP code.'));
 }
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function php_uninstall() {
+  // Get default input format.
+  $default = variable_get('filter_default_format',1);
+
+  // Get list of all formats.
+  $formats = filter_formats();
+
+  $phpfilter = variable_get('php_code_filter_id',1);
+
+  foreach($formats as $format) {
+    if(($default == $format->format) && ($format->format  == $phpfilter)) //Default input format is the one that we are uninstalling!
+    {
+      $conflict = TRUE;
+    }
+  }
+
+  if($conflict)
+  {
+    variable_set('filter_default_format',1); //Set default input format to filtered html if php code is set as the input.
+    drupal_set_message(t('The default input format has been reset to Filtered HTML.'));
+  }
+
+  //Remove php filter format.
+  db_query("DELETE FROM {filter_formats} WHERE format = %d", $phpfilter);
+  db_query("DELETE FROM {filters} WHERE format = %d", $phpfilter);
+
+  $default = variable_get('filter_default_format',1);
+  //Replace existing instances of the deleted fromat with the default format.
+  db_query("UPDATE {node_revisions} SET format = %d WHERE format = %d", $default, $phpfilter);
+  db_query("UPDATE {comments} SET format = %d WHERE format = %d", $default, $phpfilter);
+  db_query("UPDATE {boxes} SET format = %d WHERE format = %d", $default, $phpfilter);
+
+  cache_clear_all($phpfilter .':', 'cache_filter', TRUE);
+  variable_del('php_code_filter_id');
+  drupal_set_message(t('The PHP module has been uninstalled successfully.'));
+}
