|  | <?php | 
|  | /* | 
|  | * Summary Paragraph Rule | 
|  | * | 
|  | * This rule validates if a new executive summary has been written in the last quarter. | 
|  | * | 
|  | * @author: mparra@openmex.com | 
|  | */ | 
|  | //dl("xdiff.so"); | 
|  |  | 
|  | class summary_paragraph_rule { | 
|  |  | 
|  | function summary_paragraph_rule() { | 
|  |  | 
|  | } | 
|  |  | 
|  | /* | 
|  | * Process Function | 
|  | * | 
|  | * Standar function that runs the rule. | 
|  | * | 
|  | * @old_object: Old project-info-object to compare against | 
|  | * @new_object: New project-info-object to compare against | 
|  | */ | 
|  | function process($old_object, $new_object){ | 
|  |  | 
|  | //Fetch all the required data from the objects | 
|  | $projectname = $old_object->getName(); | 
|  | $old_summary = $old_object->getSummary(); | 
|  | $new_summary = $new_object->getSummary(); | 
|  | $old_timestamp = $old_object->getSummaryTimestamp(); | 
|  | $result = ""; | 
|  |  | 
|  | //Initialize the files variables | 
|  | $old_summary_file = "cache/$projectname/summary_old.txt"; | 
|  | $new_summary_file = "cache/$projectname/summary_new.txt"; | 
|  |  | 
|  | //Put the summary data into the respective files | 
|  | if (!$old_file = fopen($old_summary_file,"w")) { echo "Cannot open file $old_summary_file"; } | 
|  | if (!fwrite($old_file, $old_summary)) { echo "Cannot write to file $old_summary_file"; } | 
|  |  | 
|  | if (!$new_file = fopen($new_summary_file,"w")) { echo "Cannot open file $new_summary_file"; } | 
|  | if (!fwrite($new_file, $new_summary)) { echo "Cannot write to file $new_summary_file"; } | 
|  |  | 
|  | //Close the files | 
|  | fclose($old_file); | 
|  | fclose($new_file); | 
|  |  | 
|  | //Check differences between the 2 files (old and new) | 
|  | //$diff_files = xdiff_file_diff($old_summary_file, $new_summary_file, "cache/$projectname/summary.diff"); | 
|  |  | 
|  | $diff_result = $this->diff_compute($old_summary,$new_summary,$projectname); | 
|  |  | 
|  | //Fetch the date of the last validation of the rule | 
|  | $last_validation_date = date("j:n:Y",$old_timestamp); | 
|  |  | 
|  | //Get the size of the diff file | 
|  | //$diff_size = filesize("cache/$projectname/summary.diff"); | 
|  |  | 
|  | if(($diff_result == "") || ($diff_result == NULL)){ //The file is empty, so no differences between the files, lets check the date | 
|  | if($this->validate_quarter($last_validation_date)){ //If true, a quarter had past form the last check, so we add a message to the email | 
|  | //Attach summary reminder to email | 
|  | $result = "\n  - Please write a new executive summary."; | 
|  | } | 
|  | } | 
|  | return $result; | 
|  |  | 
|  | } | 
|  |  | 
|  | /* | 
|  | * Function Validate Quarter | 
|  | * | 
|  | * Takes the $last_validation argument and compares it against the current date to check if a quarter has past from the last validation | 
|  | * | 
|  | * @$last_validation: The date to compare against to. In this case will be the date of the last validation. | 
|  | * | 
|  | * @return: A boolean | 
|  | * 			True: if at least a quarter has past since the last validation | 
|  | * 			False: if a quarter hasn't past since the last validation | 
|  | * | 
|  | */ | 
|  | function validate_quarter ($last_validation) { | 
|  |  | 
|  | $result = ""; | 
|  |  | 
|  | //Explode the current date | 
|  | $current_date = date("j:n:Y"); //D:M:YYYY | 
|  | //$current_date = "20:4:2006"; | 
|  | $current_date_array = explode(":",$current_date); | 
|  |  | 
|  | $c_day = $current_date_array[0]; | 
|  | $c_month = $current_date_array[1]; | 
|  | $c_year = $current_date_array[2]; | 
|  |  | 
|  | //Explode the release date | 
|  | //$last_validation = "22:10:2005"; | 
|  | $release_date_array = explode(":",$last_validation); | 
|  |  | 
|  | $l_day = $release_date_array[0]; | 
|  | $l_month = $release_date_array[1]; | 
|  | $l_year = $release_date_array[2]; | 
|  |  | 
|  | if($c_year == $l_year){ //Same year, lets check the months | 
|  | //Difference between months | 
|  | $months_diff = $c_month - $l_month; | 
|  | if($months_diff < 4){ //Less than 3 months between the validations, lets check the days to be sure | 
|  | if(($months_diff == 3) && ($c_day >= $l_day)){ | 
|  | $result = true; | 
|  | } else { | 
|  | $result = false; | 
|  | } | 
|  | } else {//There are 3 or more months between the validations | 
|  | $result = true; | 
|  | } | 
|  | } else { //Different years, lets check the months | 
|  | if($l_year < $c_year){ //Just to make sure, =) | 
|  | //Difference between months | 
|  | $months_diff = 0; | 
|  | for($m = $l_month;$m < 12; $m++){ | 
|  | $months_diff = $months_diff + 1; | 
|  | } | 
|  | $months_diff = $months_diff + $c_month; | 
|  | if($months_diff < 4){ //Less than 3 months between the validations, lets check the days to be sure | 
|  | if(($months_diff == 3) && ($c_day >= $l_day)){ | 
|  | $result = true; | 
|  | } else { | 
|  | $result = false; | 
|  | } | 
|  | } else {//There are 3 or more months between the validations | 
|  | $result = true; | 
|  | } | 
|  | } | 
|  | } | 
|  |  | 
|  | return $result; | 
|  |  | 
|  | } | 
|  |  | 
|  | function diff_compute($text1, $text2, $projectname) { | 
|  | global $TempDir, $DiffCmd, $ErrorCreatingTemp, $ErrorWritingTemp; | 
|  |  | 
|  | $TempDir = "cache/$projectname"; | 
|  |  | 
|  | # <!-- $DiffCmd = "/location/of/the/diff/command"; | 
|  | $DiffCmd= "/usr/bin/diff"; | 
|  | # --> | 
|  |  | 
|  | $num = posix_getpid();                // Comment if running on Windows. | 
|  | // $num = rand();                     // Uncomment if running on Windows. | 
|  |  | 
|  | $temp1 = $TempDir . '/wiki_' . $num . '_1.txt'; | 
|  | $temp2 = $TempDir . '/wiki_' . $num . '_2.txt'; | 
|  |  | 
|  | if(!($h1 = fopen($temp1, 'w')) || !($h2 = fopen($temp2, 'w'))) { | 
|  | die($ErrorCreatingTemp); | 
|  | } | 
|  |  | 
|  | if(fwrite($h1, $text1) < 0 || fwrite($h2, $text2) < 0) { | 
|  | die($ErrorWritingTemp); | 
|  | } | 
|  |  | 
|  | fclose($h1); | 
|  | fclose($h2); | 
|  |  | 
|  | $diff = `$DiffCmd $temp1 $temp2`; | 
|  |  | 
|  | unlink($temp1); | 
|  | unlink($temp2); | 
|  |  | 
|  | return $diff; | 
|  | } | 
|  | } | 
|  | ?> |