? coverage.patch
? unstable_10.patch
? unstable_9.patch
Index: code_coverage.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/code_coverage/code_coverage.admin.inc,v
retrieving revision 1.11
diff -u -p -r1.11 code_coverage.admin.inc
--- code_coverage.admin.inc	29 Jun 2008 23:46:23 -0000	1.11
+++ code_coverage.admin.inc	22 Sep 2009 10:59:41 -0000
@@ -28,8 +28,8 @@ function code_coverage_settings_form() {
   // Generate a list of all files we /could/ generate reports for.
   $result = db_query('SELECT DISTINCT(filename) FROM {code_coverage}');
   $files = array();
-  while ($filename = db_fetch_array($result)) {
-    $files[$filename['filename']] = str_replace('\\', '/', substr(str_replace(getcwd() , '', $filename['filename']), 1));
+  foreach($result as $filename) {
+    $files[$filename->filename] = str_replace('\\', '/', substr(str_replace(getcwd() , '', $filename->filename), 1));
   }
 
   $form['code_coverage_all'] = array(
@@ -124,8 +124,8 @@ function code_coverage_generate_report($
   if (is_null($cid)) {
     $result = db_query('SELECT DISTINCT(cid) FROM {code_coverage}');
     $items = array();
-    while ($cid = db_fetch_array($result)) {
-      $items[] = l(t('Code coverage report #@num', array('@num' => $cid['cid'])), 'coverage/'. $reporter . '/' . $cid['cid']);
+    foreach ($result as $cid) {
+      $items[] = l(t('Code coverage report #@num', array('@num' => $cid->cid)), 'coverage/'. $reporter . '/' . $cid->cid);
     }
     return theme('item_list', $items, t('Choose from any of the following reports.'), 'ol');
   }
@@ -160,19 +160,15 @@ function code_coverage_generate_report($
  *   An array of lines loaded from the database.
  */
 function code_coverage_load_lines($files, $cid) {
-  if (is_array($files)) {
-    $placeholders = db_placeholders($files, 'varchar');
-    $result = db_query('SELECT * FROM {code_coverage} WHERE filename IN (' . $placeholders . ') AND cid = %d', array_merge($files, array($cid)));
-  }
-  elseif (!is_null($files)) {
-    $result = db_query("SELECT * FROM {code_coverage} WHERE filename = '%s' AND cid = %d", $files, $cid);
+  if (is_array($files) || !is_null($files)) {
+    $result = db_query('SELECT * FROM {code_coverage} WHERE filename IN (:files) AND cid = :cid', array(':files' => $files, ':cid' => $cid));
   }
   else {
-    $result = db_query('SELECT * FROM {code_coverage} WHERE cid = %d', $cid);
+    $result = db_query('SELECT * FROM {code_coverage} WHERE cid = :cid', array(':cid' => $cid));
   }
   $lines = array();
-  while ($line = db_fetch_array($result)) {
-    $lines[$line['filename']][$line['line']] = $line['times'];
+  foreach ($result as $line) {
+    $lines[$line->filename][$line->line] = $line->times;
   }
   ksort($lines);
   return $lines;
@@ -237,7 +233,12 @@ function code_coverage_generate_html_rep
       $row[] = $covered_lines[$file];
       $row[] = $uncovered_lines[$file];
       $row[] = sprintf('%04.2f%%', ($covered_lines[$file] * 100) / ($covered_lines[$file] + $uncovered_lines[$file]));
-      $rows[] = array('data' => $row, 'class' => ((substr(end($row), 0, -1) >= $good) ? 'code-coverage-covered' : ((substr(end($row), 0, -1) >= $ok) ? 'code-coverage-moderate' : 'code-coverage-uncovered')));
+      $rows[] = array(
+        'data' => $row,
+        'class' => array(
+          ((substr(end($row), 0, -1) >= $good) ? 'code-coverage-covered' : ((substr(end($row), 0, -1) >= $ok) ? 'code-coverage-moderate' : 'code-coverage-uncovered'))
+        )
+      );
     }
     $total_covered = array_sum($covered_lines);
     $total_uncovered = array_sum($uncovered_lines);
@@ -246,9 +247,24 @@ function code_coverage_generate_html_rep
     $row[] = $total_covered;
     $row[] = $total_uncovered;
     $row[] = sprintf('%04.2f%%', ($total_covered * 100) / ($total_covered + $total_uncovered));
-    $row = array('data' => $row, 'class' => 'code-coverage-overview ' . ((substr(end($row), 0, -1) > $good) ? 'code-coverage-covered' : ((substr(end($row), 0, -1) > $ok) ? 'code-coverage-moderate' : 'code-coverage-uncovered')));
+    $row = array(
+      'data' => $row,
+      'class' => array(
+        'code-coverage-overview',
+        ((substr(end($row), 0, -1) > $good) ? 'code-coverage-covered' : ((substr(end($row), 0, -1) > $ok) ? 'code-coverage-moderate' : 'code-coverage-uncovered'))
+      )
+    );
     array_unshift($rows, $row);
-    return theme('table', array(t('File !arrow', array('!arrow' => theme('image', 'misc/arrow-asc.png'))), t('Covered lines'), t('Uncovered lines'), t('Code coverage %')), $rows, array('class' => 'code-coverage-overview-report'));
+
+    $header = array(
+      t('File !arrow',
+      array('!arrow' => theme('image', 'misc/arrow-asc.png'))),
+      t('Covered lines'),
+      t('Uncovered lines'),
+      t('Code coverage %')
+    );
+
+    return theme('table', $header, $rows, array('class' => array('code-coverage-overview-report')));
   }
   else {
     drupal_set_title(t('Code coverage for @file', array('@file' => str_replace('\\', '/', substr(str_replace(getcwd() , '', $file), 1)))));
@@ -261,9 +277,15 @@ function code_coverage_generate_html_rep
       $row[] = $key + 1;
       $row[] = isset($lines[$key + 1]) ? $lines[$key + 1] : '';
       $row[] = '<pre>' . check_plain(wordwrap($line)) . '</pre>';
-      $rows[] = array('data' => $row, 'id' => 'code-coverage-' . ($key + 1), 'class' => (isset($lines[$key + 1]) ? ($lines[$key + 1] >= $covered ? 'code-coverage-covered' : 'code-coverage-uncovered') : 'code-coverage-none'));
+      $rows[] = array(
+        'data' => $row,
+        'id' => 'code-coverage-' . ($key + 1),
+        'class' => array(
+          (isset($lines[$key + 1]) ? ($lines[$key + 1] >= $covered ? 'code-coverage-covered' : 'code-coverage-uncovered') : 'code-coverage-none')
+        )
+      );
     }
-    return theme('table', array(t('Line #'), t('Times called'), t('Code')), $rows, array('class' => 'code-coverage-file-report'));
+    return theme('table', array(t('Line #'), t('Times called'), t('Code')), $rows, array('class' => array('code-coverage-file-report')));
   }
 }
 
@@ -291,4 +313,4 @@ function code_coverage_generate_xml_repo
   print $xml;
   drupal_page_footer();
   exit;
-}
\ No newline at end of file
+}
Index: code_coverage.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/code_coverage/code_coverage.module,v
retrieving revision 1.4
diff -u -p -r1.4 code_coverage.module
--- code_coverage.module	29 Jun 2008 00:43:48 -0000	1.4
+++ code_coverage.module	22 Sep 2009 10:59:41 -0000
@@ -1,9 +1,21 @@
 <?php
 // $Id: code_coverage.module,v 1.4 2008/06/29 00:43:48 cwgordon7 Exp $
 
-if (preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT']) && function_exists('xdebug_start_code_coverage')) {
-  xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
-  register_shutdown_function('code_coverage_log');
+define('CODE_COVERAGE_PHASE_START', 0);
+define('CODE_COVERAGE_PHASE_LOADED', 1);
+
+set_time_limit(600);
+if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], "simpletest") !== FALSE) && function_exists('xdebug_start_code_coverage')) {
+  code_coverage_start(CODE_COVERAGE_PHASE_START);
+}
+
+function code_coverage_start() {
+  static $started = FALSE;
+
+  if (!$started) {
+    xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
+    register_shutdown_function('code_coverage_log');
+  }
 }
 
 /**
@@ -16,7 +28,7 @@ function code_coverage_check_hacks() {
     if (!variable_get('code_coverage_hacks', FALSE)) {
       $passes = TRUE;
       foreach (array('index.php', 'xmlrpc.php') as $file) {
-        $code = 'require_once \'./' . str_replace('\\', '/', substr(str_replace(getcwd(), '', __FILE__), 1)) . '\';' . "\n";
+        $code = 'require_once DRUPAL_ROOT . \'/' . str_replace('\\', '/', substr(str_replace(getcwd(), '', __FILE__), 1)) . '\';' . "\n";
         $contents = file_get_contents("./$file");
         if (strpos($contents, $code) === FALSE) {
           $passes = FALSE;
@@ -47,12 +59,13 @@ function code_coverage_perm() {
 function code_coverage_menu() {
   $items = array();
 
-  $items['admin/settings/coverage'] = array(
+  $items['admin/config/development/coverage'] = array(
     'title' => 'Code coverage',
     'description' => 'Configure the generation of Drupal\'s code coverage reports.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('code_coverage_settings_form'),
     'access arguments' => array('administer code coverage'),
+    'file' => 'code_coverage.admin.inc',
   );
 
   $items['coverage'] = array(
@@ -60,6 +73,7 @@ function code_coverage_menu() {
     'description' => 'Generate code coverage reports based on the collected coverage data.',
     'page callback' => 'code_coverage_generate_report',
     'access arguments' => array('view code coverage reports'),
+    'file' => 'code_coverage.admin.inc',
   );
 
   return $items;
@@ -91,7 +105,7 @@ function code_coverage_log() {
   global $db_prefix, $db_prefix_original;
   $preserved_prefix = $db_prefix;
   $db_prefix = $db_prefix_original;
-  $GLOBALS['conf'] = variable_init();
+  $GLOBALS['conf'] = variable_initialize();
   $info = xdebug_get_code_coverage();
   $text = '';
   foreach ($info as $file => $lines) {
@@ -111,34 +125,45 @@ function code_coverage_log() {
   }
   file_put_contents(variable_get('code_coverage_tmp_storage', '/tmp/') . variable_get('code_coverage_id', 1) . '.txt', $text, FILE_APPEND);
   $db_prefix = $preserved_prefix;
-  $GLOBALS['conf'] = variable_init();
+  $GLOBALS['conf'] = variable_initialize();
 }
 
 /**
  * Fetch all the code coverage data from a file and store it to the database.
  */
 function code_coverage_fetch_data() {
-  $file = variable_get('code_coverage_tmp_storage', '/tmp/') . variable_get('code_coverage_id', 1) . '.txt';
-  if (file_exists($file) && is_file($file) && $_GET['q'] == 'admin/build/testing' && user_access('administer code coverage')) {
-    $contents = file_get_contents($file);
-    file_delete($file);
-    $entries = explode("\n", $contents);
+  $cid = variable_get('code_coverage_id', 1);
+  $file = variable_get('code_coverage_tmp_storage', '/tmp/') . $cid . '.txt';
+
+  if (file_exists($file) && is_file($file) && $_GET['q'] == 'admin/config/development/testing' && user_access('administer code coverage')) {
     $data = array();
-    foreach ($entries as $entry) {
+    $fp = fopen($file, 'r');
+    while ($entry = fgets($fp)) {
       if (!empty($entry)) {
         list($filename, $lines) = explode('$', $entry);
         $packets = explode(',', $lines);
-        while ($line = array_shift($packets)) {
+        foreach ($packets as $line) {
           list($line_num, $count) = explode('-', $line);
           $data[$filename][$line_num] = (isset($data[$filename][$line_num]) ? ($data[$filename][$line_num] + $count) : $count);
         }
       }
     }
+    fclose($fp);
+
+    $query = db_insert('code_coverage')->fields(array('cid', 'filename', 'line', 'times'));
+
     foreach ($data as $filename => $info) {
       foreach ($info as $line => $times) {
-        db_query("INSERT INTO {code_coverage} VALUES (%d, '%s', %d, %d)", variable_get('code_coverage_id', 1), $filename, $line, $times);
+        $query->values(array(
+          'cid' => $cid,
+          'filename' => $filename,
+          'line' => $line,
+          'times' => (int)$times,
+        ));
       }
+      $query->execute();
     }
-    variable_set('code_coverage_id', variable_get('code_coverage_id', 1) + 1);
+    variable_set('code_coverage_id', $cid + 1);
+    unlink($file);
   }
-}
\ No newline at end of file
+}
