Skip to content
Snippets Groups Projects
migrate-hooks 1.70 KiB
#!/usr/bin/env php
<?php

include __DIR__ . '/hooks-by-category.php';

$cache_dir = __DIR__ . '/wiki_cache';
$root_dir = dirname(dirname(__DIR__));
$hooks_dir = "$root_dir/docs/hooks";


function clean_markdown($markdown) {
  // add domain name to all hyperlinks that point to the wiki
  $pattern = '@\]\((/confluence[^)]*)\)@';
  $replace = '](https://wiki.civicrm.org$1)';
  $markdown = preg_replace($pattern,$replace,$markdown);

  // set all headings of level 3 and above to level 2
  $pattern = '@^(##)(#+) (.*)$@m';
  $replace = '## $3';
  $markdown = preg_replace($pattern,$replace,$markdown);

  return $markdown;
}



# create hooks directory if needed
if ( !is_dir($hooks_dir) ) {
  mkdir($hooks_dir);
}
chdir($hooks_dir);
foreach ($hooks_by_category as $category => $hooks) {
  foreach ($hooks as $hook) {
    $hook_name = $hook['name'];
    $markdown_file = "$hook_name.md";
    $html = "$cache_dir/$hook_name";
    if ( file_exists($html) && !file_exists($markdown_file) ) {
      echo "converting $hook_name ... ";
      $conv_output_array = array();
      $conv_status = 1;
      exec("webpage2md $html", $conv_output_array, $conv_status);
      if( $conv_status == 0 ) {
        $conv_output = implode("\n",$conv_output_array);
        $conv_output = clean_markdown($conv_output);
        file_put_contents($markdown_file, $conv_output);
        echo "done" . PHP_EOL;
      }
      else {
        echo "ERROR CONVERTING $hook_name" . PHP_EOL;
        echo "$conv_output";
      }
    }
    else if ( !file_exists($html) ) {
      echo "WARNING: $hook_name not yet fetched" . PHP_EOL;
    }
    else if ( file_exists($markdown_file) ) {
      echo "ignoring $hook_name (markdown file already exists)" . PHP_EOL;
    }
  }
}