original_string: protected function rewrite_step_backup_file_for_legacy_freeze($filepath) {
          $foundnode = false;
          $newfile = make_request_directory(true) . DIRECTORY_SEPARATOR . 'file.xml';
          $fr = fopen($filepath, 'r');
          $fw = fopen($newfile, 'w');
          if ($fr && $fw) {
              while (($line = fgets($fr, 4096)) !== false) {
                  if (!$foundnode && strpos($line, '<gradebook ') === 0) {
                      $foundnode = true;
                      $matches = array();
                      $pattern = '@calculations_freeze=.([0-9]+).@';
                      if (preg_match($pattern, $line, $matches)) {
                          $freeze = $matches[1];
                          $line = preg_replace($pattern, '', $line);
                          $line .= "  <attributes>\n    <calculations_freeze>$freeze</calculations_freeze>\n  </attributes>\n";
                      }
                  }
                  fputs($fw, $line);
              }
              if (!feof($fr)) {
                  throw new restore_step_exception('Error while attempting to rewrite the gradebook step file.');
              }
              fclose($fr);
              fclose($fw);
              if (!rename($newfile, $filepath)) {
                  throw new restore_step_exception('Error while attempting to rename the gradebook step file.');
              }
          } else {
              if ($fr) {
                  fclose($fr);
              }
              if ($fw) {
                  fclose($fw);
              }
          }
      }
code: protected function rewrite_step_backup_file_for_legacy_freeze($filepath) {
          $foundnode = false;
          $newfile = make_request_directory(true) . DIRECTORY_SEPARATOR . 'file.xml';
          $fr = fopen($filepath, 'r');
          $fw = fopen($newfile, 'w');
          if ($fr && $fw) {
              while (($line = fgets($fr, 4096)) !== false) {
                  if (!$foundnode && strpos($line, '<gradebook ') === 0) {
                      $foundnode = true;
                      $matches = array();
                      $pattern = '@calculations_freeze=.([0-9]+).@';
                      if (preg_match($pattern, $line, $matches)) {
                          $freeze = $matches[1];
                          $line = preg_replace($pattern, '', $line);
                          $line .= "  <attributes>\n    <calculations_freeze>$freeze</calculations_freeze>\n  </attributes>\n";
                      }
                  }
                  fputs($fw, $line);
              }
              if (!feof($fr)) {
                  throw new restore_step_exception('Error while attempting to rewrite the gradebook step file.');
              }
              fclose($fr);
              fclose($fw);
              if (!rename($newfile, $filepath)) {
                  throw new restore_step_exception('Error while attempting to rename the gradebook step file.');
              }
          } else {
              if ($fr) {
                  fclose($fr);
              }
              if ($fw) {
                  fclose($fw);
              }
          }
      }
code_tokens: protected function rewrite_step_backup_file_for_legacy_freeze ( $ filepath ) { $ foundnode = false ; $ newfile = make_request_directory ( true ) . DIRECTORY_SEPARATOR . 'file.xml' ; $ fr = fopen ( $ filepath , 'r' ) ; $ fw = fopen ( $ newfile , 'w' ) ; if ( $ fr && $ fw ) { while ( ( $ line = fgets ( $ fr , 4096 ) ) !== false ) { if ( ! $ foundnode && strpos ( $ line , '<gradebook ' ) === 0 ) { $ foundnode = true ; $ matches = array ( ) ; $ pattern = '@calculations_freeze=.([0-9]+).@' ; if ( preg_match ( $ pattern , $ line , $ matches ) ) { $ freeze = $ matches [ 1 ] ; $ line = preg_replace ( $ pattern , '' , $ line ) ; $ line .= "  <attributes>\n    <calculations_freeze>$freeze</calculations_freeze>\n  </attributes>\n" ; } } fputs ( $ fw , $ line ) ; } if ( ! feof ( $ fr ) ) { throw new restore_step_exception ( 'Error while attempting to rewrite the gradebook step file.' ) ; } fclose ( $ fr ) ; fclose ( $ fw ) ; if ( ! rename ( $ newfile , $ filepath ) ) { throw new restore_step_exception ( 'Error while attempting to rename the gradebook step file.' ) ; } } else { if ( $ fr ) { fclose ( $ fr ) ; } if ( $ fw ) { fclose ( $ fw ) ; } } }
docstring: Rewrite step definition to handle the legacy freeze attribute.
  
  In previous backups the calculations_freeze property was stored as an attribute of the
  top level node <gradebook>. The backup API, however, do not process grandparent nodes.
  It only processes definitive children, and their parent attributes.
  
  We had:
  
  <gradebook calculations_freeze="20160511">
  <grade_categories>
  <grade_category id="10">
  <depth>1</depth>
  ...
  </grade_category>
  </grade_categories>
  ...
  </gradebook>
  
  And this method will convert it to:
  
  <gradebook >
  <attributes>
  <calculations_freeze>20160511</calculations_freeze>
  </attributes>
  <grade_categories>
  <grade_category id="10">
  <depth>1</depth>
  ...
  </grade_category>
  </grade_categories>
  ...
  </gradebook>
  
  Note that we cannot just load the XML file in memory as it could potentially be huge.
  We can also completely ignore if the node <attributes> is already in the backup
  file as it never existed before.
  
  @param string $filepath The absolute path to the XML file.
  @return void
docstring_tokens: Rewrite step definition to handle the legacy freeze attribute .