function Project::getReleaseNotes

8.0.x Project.php Project::getReleaseNotes($version = NULL, $print_status = TRUE, $tmpfile = NULL)
7.x Project.php Project::getReleaseNotes($version = NULL, $print_status = TRUE, $tmpfile = NULL)
master Project.php Project::getReleaseNotes($version = NULL, $print_status = TRUE, $tmpfile = NULL)

Prints release notes for given projects.

Parameters

string $version: Version of the release to get notes.

bool $print_status: Whether to print a informative note.

string $tmpfile: If provided, a file that contains contents to show before the release notes.

File

lib/Drush/UpdateService/Project.php, line 553

Namespace

Drush\UpdateService

Class

Project
Representation of a project's release info from the update service.

Code

function getReleaseNotes($version = NULL, $print_status = TRUE, $tmpfile = NULL) {
  $project_name = $this->parsed['short_name'];
  if (!isset($tmpfile)) {
    $tmpfile = drush_tempnam('rln-' . $project_name . '.');
  }

  // Select versions to show.
  $versions = array();
  if (!is_null($version)) {
    $versions[] = $version;
  }
  else {
    // If requested project is installed,
    // show release notes for the installed version and all newer versions.
    if (isset($this->parsed['recommended'], $this->parsed['installed'])) {
      $releases = array_reverse($this->parsed['releases']);
      foreach ($releases as $version => $release) {
        if ($release['date'] >= $this->parsed['releases'][$this->parsed['installed']]['date']) {
          $release += array('version_extra' => '');
          $this->parsed['releases'][$this->parsed['installed']] += array('version_extra' => '');
          if ($release['version_extra'] == 'dev' && $this->parsed['releases'][$this->parsed['installed']]['version_extra'] != 'dev') {
            continue;
          }
          $versions[] = $version;
        }
      }
    }
    else {
      // Project is not installed and user did not specify a version,
      // so show the release notes for the recommended version.
      $versions[] = $this->parsed['recommended'];
    }
  }

  foreach ($versions as $version) {
    if (!isset($this->parsed['releases'][$version]['release_link'])) {
      drush_log(dt("Project !project does not have release notes for version !version.", array('!project' => $project_name, '!version' => $version)), LogLevel::WARNING);
      continue;
    }

    // Download the release node page and get the html as xml to explore it.
    $release_link = $this->parsed['releases'][$version]['release_link'];
    $filename = drush_download_file($release_link, drush_tempnam($project_name));
    @$dom = \DOMDocument::loadHTMLFile($filename);
    if ($dom) {
      drush_log(dt("Successfully parsed and loaded the HTML contained in the release notes' page for !project (!version) project.", array('!project' => $project_name, '!version' => $version)), LogLevel::NOTICE);
    }
    else {
      drush_log(dt("Error while requesting the release notes page for !project project.", array('!project' => $project_name)), LogLevel::ERROR);
      continue;
    }
    $xml = simplexml_import_dom($dom);

    // Extract last update time and the notes.
    $last_updated = $xml->xpath('//div[contains(@class,"views-field-changed")]');
    $last_updated = $last_updated[0]->asXML();
    $notes = $xml->xpath('//div[contains(@class,"field-name-body")]');
    $notes = (!empty($notes)) ? $notes[0]->asXML() : dt("There're no release notes.");

    // Build the notes header.
    $header = array();
    $header[] = '<hr>';
    $header[] = dt("> RELEASE NOTES FOR '!name' PROJECT, VERSION !version:", array('!name' => strtoupper($project_name), '!version' => $version));
    $header[] = dt("> !last_updated.", array('!last_updated' => trim(drush_html_to_text($last_updated))));
    if ($print_status) {
      $header[] = '> ' . implode(', ', $this->parsed['releases'][$version]['release_status']);
    }
    $header[] = '<hr>';

    // Finally add the release notes for the requested project to the tmpfile.
    $content = implode("\n", $header) . "\n" . $notes . "\n";
    #TODO# accept $html as a method argument
    if (!drush_get_option('html', FALSE)) {
      $content = drush_html_to_text($content, array('br', 'p', 'ul', 'ol', 'li', 'hr'));
    }
    file_put_contents($tmpfile, $content, FILE_APPEND);
  }

  #TODO# don't print! Just return the filename
  drush_print_file($tmpfile);
}