1<?php
2/*=========================================================================
3
4  Program:   CDash - Cross-Platform Dashboard System
5  Module:    $Id$
6  Language:  PHP
7  Date:      $Date$
8  Version:   $Revision$
9
10  Copyright (c) 2002 Kitware, Inc.  All rights reserved.
11  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12
13     This software is distributed WITHOUT ANY WARRANTY; without even
14     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15     PURPOSE.  See the above copyright notices for more information.
16
17=========================================================================*/
18/** This class shouldn't be used externally */
19class CoverageFile
20{
21  var $Id;
22  var $File;
23  var $FullPath;
24  var $Crc32;
25
26  private $LastPercentCoverage; // used when GetMetric
27
28  /** Update the content of the file */
29  function Update($buildid)
30    {
31    if(!is_numeric($buildid) || $buildid == 0)
32      {
33      return;
34      }
35
36    include("cdash/config.php");
37
38    // Compute the crc32 of the file (before compression for backward compatibility)
39    $this->Crc32 = crc32($this->FullPath.$this->File);
40
41    $this->FullPath = pdo_real_escape_string($this->FullPath);
42    if($CDASH_USE_COMPRESSION)
43      {
44      $file = gzcompress($this->File);
45      if($file === false)
46        {
47        $file = $this->File;
48        }
49      else
50        {
51        if($CDASH_DB_TYPE == "pgsql")
52          {
53          if(strlen($this->File)<2000) // compression doesn't help for small chunk
54            {
55            $file = $this->File;
56            }
57          $file = pg_escape_bytea(base64_encode($file)); // hopefully does the escaping correctly
58          }
59        }
60      }
61    else
62      {
63      $file = $this->File;
64      if($CDASH_DB_TYPE == "pgsql")
65        {
66        $file = pg_escape_bytea($file);
67        }
68      }
69    $file = pdo_real_escape_string($file);
70
71    $coveragefile = pdo_query("SELECT id FROM coveragefile WHERE crc32=".qnum($this->Crc32));
72    add_last_sql_error("CoverageFile:Update");
73
74    if(pdo_num_rows($coveragefile)>0) // we have the same crc32
75      {
76      $coveragefile_array = pdo_fetch_array($coveragefile);
77      $this->Id = $coveragefile_array["id"];
78
79      // Update the current coverage.fileid
80      $coverage = pdo_query("SELECT c.fileid FROM coverage AS c,coveragefile AS cf
81                            WHERE c.fileid=cf.id AND c.buildid=".qnum($buildid)."
82                              AND cf.fullpath='$this->FullPath'");
83      $coverage_array = pdo_fetch_array($coverage);
84      $prevfileid = $coverage_array["fileid"];
85
86      pdo_query("UPDATE coverage SET fileid=".qnum($this->Id)." WHERE buildid=".qnum($buildid)." AND fileid=".qnum($prevfileid));
87      add_last_sql_error("CoverageFile:Update");
88
89      $row = pdo_single_row_query("SELECT COUNT(*) AS c FROM label2coveragefile WHERE buildid=".qnum($buildid)." AND coveragefileid=".qnum($prevfileid));
90      if(isset($row['c']) && $row['c']>0)
91        {
92        pdo_query("UPDATE label2coveragefile SET coveragefileid=".qnum($this->Id)." WHERE buildid=".qnum($buildid)." AND coveragefileid=".qnum($prevfileid));
93        add_last_sql_error("CoverageFile:Update");
94        }
95
96      // Remove the file if the crc32 is NULL
97      pdo_query("DELETE FROM coveragefile WHERE id=".qnum($prevfileid)." AND file IS NULL and crc32 IS NULL");
98      add_last_sql_error("CoverageFile:Update");
99      }
100    else // The file doesn't exist in the database
101      {
102      // We find the current fileid based on the name and the file should be null
103      $coveragefile = pdo_query("SELECT cf.id,cf.file FROM coverage AS c,coveragefile AS cf
104                                   WHERE c.fileid=cf.id AND c.buildid=".qnum($buildid)."
105                                   AND cf.fullpath='$this->FullPath' ORDER BY cf.id ASC");
106      $coveragefile_array = pdo_fetch_array($coveragefile);
107      $this->Id = $coveragefile_array["id"];
108      pdo_query("UPDATE coveragefile SET file='$file',crc32='$this->Crc32' WHERE id=".qnum($this->Id));
109      add_last_sql_error("CoverageFile:Update");
110      }
111    return true;
112    }
113
114  /** Get the path */
115  function GetPath()
116    {
117    if(!$this->Id)
118      {
119      echo "CoverageFile GetPath(): Id not set";
120      return false;
121      }
122
123    $coverage = pdo_query("SELECT fullpath FROM coveragefile WHERE id=".qnum($this->Id));
124    if(!$coverage)
125      {
126      add_last_sql_error("Coverage GetPath");
127      return false;
128      }
129
130    $coverage_array = pdo_fetch_array($coverage);
131    return $coverage_array['fullpath'];
132
133    }  // GetPath
134
135  /** Return the metric */
136  function GetMetric()
137    {
138    if(!$this->Id)
139      {
140      echo "CoverageFile GetMetric(): Id not set";
141      return false;
142      }
143
144    $coveragefile = pdo_query("SELECT loctested,locuntested,branchstested,branchsuntested,
145                                     functionstested,functionsuntested FROM coverage WHERE fileid=".qnum($this->Id));
146    if(!$coveragefile)
147      {
148      add_last_sql_error("CoverageFile:GetMetric()");
149      return false;
150      }
151
152    if(pdo_num_rows($coveragefile)==0)
153      {
154      return false;
155      }
156
157    $coveragemetric = 1;
158    $coveragefile_array = pdo_fetch_array($coveragefile);
159    $loctested = $coveragefile_array["loctested"];
160    $locuntested = $coveragefile_array["locuntested"];
161    $branchstested = $coveragefile_array["branchstested"];
162    $branchsuntested = $coveragefile_array["branchsuntested"];
163    $functionstested = $coveragefile_array["functionstested"];
164    $functionsuntested = $coveragefile_array["functionsuntested"];
165
166    // Compute the coverage metric for bullseye
167    if($branchstested>0 || $branchsuntested>0 || $functionstested>0 || $functionsuntested>0)
168      {
169      // Metric coverage
170      $metric = 0;
171      if($functionstested+$functionsuntested>0)
172        {
173        $metric += $functionstested/($functionstested+$functionsuntested);
174        }
175      if($branchsuntested+$branchsuntested>0)
176        {
177        $metric += $branchsuntested/($branchstested+$branchsuntested);
178        $metric /= 2.0;
179        }
180      $coveragemetric = $metric;
181      $this->LastPercentCoverage = $metric*100;
182      }
183    else // coverage metric for gcov
184      {
185      $coveragemetric = ($loctested+10)/($loctested+$locuntested+10);
186      $this->LastPercentCoverage = ($loctested/($loctested+$locuntested))*100;
187      }
188
189    return $coveragemetric;
190    } // end function GetMetric
191
192  // Get the percent coverage
193  function GetLastPercentCoverage()
194    {
195    return $this->LastPercentCoverage;
196    }
197
198  // Get the fileid from the name
199  function GetIdFromName($file,$buildid)
200    {
201    $coveragefile = pdo_query("SELECT id FROM coveragefile,coverage WHERE fullpath LIKE '%".$file."%'
202                               AND coverage.buildid=".qnum($buildid)." AND coverage.fileid=coveragefile.id");
203    if(!$coveragefile)
204      {
205      add_last_sql_error("CoverageFile:GetIdFromName()");
206      return false;
207      }
208    if(pdo_num_rows($coveragefile)==0)
209      {
210      return false;
211      }
212    $coveragefile_array = pdo_fetch_array($coveragefile);
213    return $coveragefile_array['id'];
214    }
215}
216?>
217