<commit_before><?php
/*=========================================================================

  Program:   CDash - Cross-Platform Dashboard System
  Module:    $Id$
  Language:  PHP
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

/** */
function checkEmailPreferences($emailcategory,$errors)
{
  include_once("cdash/common.php");
  if($errors['update_errors']>0 && check_email_category("update",$emailcategory))
    {
    return true;
    }
  if($errors['configure_errors']>0 && check_email_category("configure",$emailcategory))
    {
    return true;
    }
  if($errors['build_warnings']>0 && check_email_category("warning",$emailcategory))
    {
    return true;
    }
  if($errors['build_errors']>0 && check_email_category("error",$emailcategory))
    {
    return true;
    }
  if($errors['test_errors']>0 && check_email_category("test",$emailcategory))
    {
    return true;
    }
  return false;  
}

/** Given a user check if we should send an email based on labels */
function checkEmailLabel($projectid, $userid, $buildid)
{
  include_once("models/labelemail.php");
  include_once("models/build.php");
  $LabelEmail = new LabelEmail();
  $LabelEmail->UserId = $userid;
  $LabelEmail->ProjectId = $projectid;
  
  $labels = $LabelEmail->GetLabels();
  if(count($labels)==0) // if the number of subscribed labels is zero we send the email
    {
    return true;
    }
  
  $Build = new Build();
  $Build->Id = $buildid;
  $buildlabels = $Build->GetLabels();
  if(count(array_intersect($labels, $buildlabels))>0)
    {
    return true;
    }
  return false;
} // end checkEmailLabel


/** Send a summary email */
function sendsummaryemail($projectid,$projectname,$dashboarddate,$groupid,$errors,$buildid)
{
  include("config.php");
 
  // Check if the email has been sent
  $date = ""; // now
  list ($previousdate, $currentstarttime, $nextdate, $today) = get_dates($date,$project_array["nightlytime"]);
  $dashboarddate = gmdate(FMT_DATE, $currentstarttime);

  // If we already have it we return
  if(pdo_num_rows(pdo_query("SELECT buildid FROM summaryemail WHERE date='$dashboarddate' AND groupid=".qnum($groupid)))==1)
    {
    return;
    }  

  // Update the summaryemail table to specify that we have send the email
  // We also delete any previous rows from that groupid
  pdo_query("DELETE FROM summaryemail WHERE groupid=$groupid");
  pdo_query("INSERT INTO summaryemail (buildid,date,groupid) VALUES ($buildid,'$dashboarddate',$groupid)");
  add_last_sql_error("sendmail");
    
  // If the trigger for SVN/CVS diff is not done yet we specify that the asynchronous trigger should
  // send an email
  $dailyupdatequery = pdo_query("SELECT status FROM dailyupdate WHERE projectid=".qnum($projectid)." AND date='$dashboarddate'");
  add_last_sql_error("sendmail");
    
  if(pdo_num_rows($dailyupdatequery) == 0)
    {
    return;
    }
      
  $dailyupdate_array = pdo_fetch_array($dailyupdatequery);
  $dailyupdate_status = $dailyupdate_array['status'];
  if($dailyupdate_status == 0)
    {
    pdo_query("UPDATE dailyupdate SET status='2' WHERE projectid='$projectid' AND date='$dashboarddate'");
    return;
    }
       
  // Find the current updaters from the night using the dailyupdatefile table
  $summaryEmail = "";
  $query = "SELECT ".qid("user").".email,user2project.emailcategory,".qid("user").".id FROM ".qid("user").",user2project,dailyupdate,dailyupdatefile WHERE 
                           user2project.projectid=$projectid
                           AND user2project.userid=".qid("user").".id 
                           AND user2project.cvslogin=dailyupdatefile.author
                           AND dailyupdatefile.dailyupdateid=dailyupdate.id
                           AND dailyupdate.date='$dashboarddate'
                           AND dailyupdate.projectid=$projectid
                           AND user2project.emailtype>0
                           ";
  $user = pdo_query($query);
  add_last_sql_error("sendmail");
      
  // Loop through the users and add them to the email array  
  while($user_array = pdo_fetch_array($user))
    {
    // If the user is already in the list we quit
    if(strpos($summaryEmail,$user_array["email"]) !== false)
      {
      continue;
      }
        
    // If the user doesn't want to receive email
    if(!checkEmailPreferences($user_array["emailcategory"],$errors))
      {
      continue;
      }
    
    // Check if the labels are defined for this user
    if(!checkEmailLabel($projectid, $user_array["id"], $buildid))
      {
      continue;
      }
      
    if($summaryEmail != "")
      {
      $summaryEmail .= ", ";
      }
    $summaryEmail .= $user_array["email"];
    }
    
  // Select the users who want to receive all emails
  $user = pdo_query("SELECT ".qid("user").".email,user2project.emailtype,".qid("user").".id  FROM ".qid("user").",user2project WHERE user2project.projectid='$projectid' 
                       AND user2project.userid=".qid("user").".id AND user2project.emailtype>1");
  add_last_sql_error("sendsummaryemail");
  while($user_array = pdo_fetch_array($user))
    {
    // If the user is already in the list we quit
    if(strpos($summaryEmail,$user_array["email"]) !== false)
       {
       continue;
       }
    
    // Check if the labels are defined for this user
    if(!checkEmailLabel($projectid, $user_array["id"], $buildid))
      {
      continue;
      }   
       
    if($summaryEmail != "")
      {
      $summaryEmail .= ", ";
      }
     $summaryEmail .= $user_array["email"];
    }
       
  // Send the email
  if($summaryEmail != "")
    {
    $summaryemail_array = pdo_fetch_array(pdo_query("SELECT name FROM buildgroup WHERE id=$groupid"));
    add_last_sql_error("sendsummaryemail");

    $title = "CDash [".$projectname."] - ".$summaryemail_array["name"]." Failures";
      <commit_msg>ENH: Summary email is not checking the user of the failing build<commit_after><?php
/*=========================================================================

  Program:   CDash - Cross-Platform Dashboard System
  Module:    $Id$
  Language:  PHP
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

/** */
function checkEmailPreferences($emailcategory,$errors)
{
  include_once("cdash/common.php");
  if($errors['update_errors']>0 && check_email_category("update",$emailcategory))
    {
    return true;
    }
  if($errors['configure_errors']>0 && check_email_category("configure",$emailcategory))
    {
    return true;
    }
  if($errors['build_warnings']>0 && check_email_category("warning",$emailcategory))
    {
    return true;
    }
  if($errors['build_errors']>0 && check_email_category("error",$emailcategory))
    {
    return true;
    }
  if($errors['test_errors']>0 && check_email_category("test",$emailcategory))
    {
    return true;
    }
  return false;  
}

/** Given a user check if we should send an email based on labels */
function checkEmailLabel($projectid, $userid, $buildid)
{
  include_once("models/labelemail.php");
  include_once("models/build.php");
  $LabelEmail = new LabelEmail();
  $LabelEmail->UserId = $userid;
  $LabelEmail->ProjectId = $projectid;
  
  $labels = $LabelEmail->GetLabels();
  if(count($labels)==0) // if the number of subscribed labels is zero we send the email
    {
    return true;
    }
  
  $Build = new Build();
  $Build->Id = $buildid;
  $buildlabels = $Build->GetLabels();
  if(count(array_intersect($labels, $buildlabels))>0)
    {
    return true;
    }
  return false;
} // end checkEmailLabel


/** Send a summary email */
function sendsummaryemail($projectid,$projectname,$dashboarddate,$groupid,$errors,$buildid)
{
  include("config.php");
  require_once("models/userproject.php");
  require_once("models/user.php");
     
  // Check if the email has been sent
  $date = ""; // now
  list ($previousdate, $currentstarttime, $nextdate, $today) = get_dates($date,$project_array["nightlytime"]);
  $dashboarddate = gmdate(FMT_DATE, $currentstarttime);

  // If we already have it we return
  if(pdo_num_rows(pdo_query("SELECT buildid FROM summaryemail WHERE date='$dashboarddate' AND groupid=".qnum($groupid)))==1)
    {
    return;
    }  

  // Update the summaryemail table to specify that we have send the email
  // We also delete any previous rows from that groupid
  pdo_query("DELETE FROM summaryemail WHERE groupid=$groupid");
  pdo_query("INSERT INTO summaryemail (buildid,date,groupid) VALUES ($buildid,'$dashboarddate',$groupid)");
  add_last_sql_error("sendmail");
    
  // If the trigger for SVN/CVS diff is not done yet we specify that the asynchronous trigger should
  // send an email
  $dailyupdatequery = pdo_query("SELECT status FROM dailyupdate WHERE projectid=".qnum($projectid)." AND date='$dashboarddate'");
  add_last_sql_error("sendmail");
    
  if(pdo_num_rows($dailyupdatequery) == 0)
    {
    return;
    }
      
  $dailyupdate_array = pdo_fetch_array($dailyupdatequery);
  $dailyupdate_status = $dailyupdate_array['status'];
  if($dailyupdate_status == 0)
    {
    pdo_query("UPDATE dailyupdate SET status='2' WHERE projectid='$projectid' AND date='$dashboarddate'");
    return;
    }
       
  // Find the current updaters from the night using the dailyupdatefile table
  $summaryEmail = "";
  $query = "SELECT ".qid("user").".email,user2project.emailcategory,".qid("user").".id FROM ".qid("user").",user2project,dailyupdate,dailyupdatefile WHERE 
                           user2project.projectid=$projectid
                           AND user2project.userid=".qid("user").".id 
                           AND user2project.cvslogin=dailyupdatefile.author
                           AND dailyupdatefile.dailyupdateid=dailyupdate.id
                           AND dailyupdate.date='$dashboarddate'
                           AND dailyupdate.projectid=$projectid
                           AND user2project.emailtype>0
                           ";
  $user = pdo_query($query);
  add_last_sql_error("sendmail");
      
  // Loop through the users and add them to the email array  
  while($user_array = pdo_fetch_array($user))
    {
    // If the user is already in the list we quit
    if(strpos($summaryEmail,$user_array["email"]) !== false)
      {
      continue;
      }
        
    // If the user doesn't want to receive email
    if(!checkEmailPreferences($user_array["emailcategory"],$errors))
      {
      continue;
      }
    
    // Check if the labels are defined for this user
    if(!checkEmailLabel($projectid, $user_array["id"], $buildid))
      {
      continue;
      }
      
    if($summaryEmail != "")
      {
      $summaryEmail .= ", ";
      }
    $summaryEmail .= $user_array["email"];
    }
  
  // Select the users that are part of this build
  $authors = pdo_query("SELECT author FROM updatefile WHERE buildid=".qnum($buildid));
  add_last_sql_error("sendmail");
  while($authors_array = pdo_fetch_array($authors))
    { 
    $author = $authors_array["author"];
    if($author=="Local User")
      {
      continue;
      }
    
    $UserProject = new UserProject();
    $UserProject->CvsLogin = $author;
    $UserProject->ProjectId = $projectid;
    
    if(!$UserProject->FillFromCVSLogin())
      {
      continue;
      }
       
    // If the user doesn't want to receive email
    if(!checkEmailPreferences($UserProject->EmailCategory,$errors))
      {
      continue;
      }
    
    // Check if the labels are defined for this user
    if(!checkEmailLabel($projectid,$UserProject->UserId, $buildid))
      {
      continue;
      }
    
    // Find the email
    $User = new User();
    $User->Id = $UserProject->UserId;
    $useremail = $User->GetEmail();
    
    // If the user is already in the list we quit
    if(strpos($summaryEmail,$useremail) !== false)
       {
       continue;
       }
 
    if($summaryEmail != "")
      {
      $summaryEmail .= ", ";
      }
    $summaryEmail .= $useremail;
    } 
  
    
  // Select the users who want to receive all emails
  $user = pdo_query("SELECT ".qid("user").".email,user2project.emailtype,".qid("user").".id  FROM ".qid("user").",user2project WHERE user2project.projectid='$projectid' 
                       AND user2project.userid=".qid("user").".id AND user2project.emailtype>1");
  add_last_sql_error("sendsummaryemail");
  while($user_array = pdo_fetch_array($user))
    {
    // If the user is already in the list we quit
    if(strpos($summaryEmail,$user_array["email"]) !== false)
       {
       continue;
       }
    
    // Check if the labels are defined for this user
    if(!checkEmailLabel($projectid, $user_array["id"], $buildid))
      {
      continue;
      }   
       
    if($summaryEmail != "")
      {
      $summaryEmail .= ", ";
      }
    $summaryEmail .= $user_array["email"];
    }
       
  // Send the email
  if($summaryEmail != "")
    {
    $summaryemail_array = pdo_fetch_array(pdo_query("SELECT name FROM buildgroup WHERE id=$groupid"));
    add_last_sql_error("sendsummaryemail");

    $title = "CDash [".$projectname."] - ".$summaryemail_array["name"]." Failures";
      
{'fraction_non_alphanumeric': 0.1506514071834737, 'fraction_numerical': 0.004030806881163176, 'mean_word_length': 3.0685212298682285, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7015026', 'n_tokens_mistral': 4170, 'n_tokens_neox': 4110, 'n_words': 931}