Newer
Older
pukiwikiplugin / merge_schedules.inc.php
<?php
function plugin_merge_schedules_convert(){

  // config
  $time2 = time()-60*60*24*10;
  $start_month = date('n', $time2); // 10日前
  $wikiname1 = '年中行事';
  $wikiname2 = '行事日程' . ($start_month < 4 ? date('Y',$time2)-1 : date('Y',$time2));
  $wikiname3 = '行事日程' . ($start_month < 4 ? date('Y',$time2) : date('Y',$time2)+1);

  // load1
  $wikisrc1 = get_source($wikiname1);
  $schedule1 = array();    
  foreach($wikisrc1 as $line){
    if(preg_match('/^- (\d{1,2})月(.*)/', $line, $matches)){
      $schedule1["$matches[1]"] .= '● ' . trim($matches[2]) . "\n";
    }
  }
  
  // load2
  $wikisrc2 = get_source($wikiname2);
  if(is_page($wikiname3)){
    $wikisrc2 = array_merge($wikisrc2, get_source($wikiname3));
  }
  $schedule2 = array();
  $state = 0;
  $months = array();
  $current_month = 0;
  foreach($wikisrc2 as $line){
    switch($state) {
    case 0:
      //if (preg_match('/^\d{4}年度?/', $line)){
      if (preg_match('/^\/\/ *start/', $line)){
        $state = 1;
      }
      break;
    case 1:
      if (preg_match('/-(\d{1,2})月/', $line, $matches)) {
        $current_month = $matches[1];
        if ($current_month == $start_month) {
          if (!in_array($current_month, $months)) {
            array_push($months, $current_month);
          }
          $state = 2;
        }
      }
      break;
    case 2:
      //if(preg_match('/^----/', $line)){
      if(preg_match('/^\/\/ *end/', $line)){
        $state = 3;
      } else {
        if(preg_match('/-(\d{1,2})月/', $line, $matches)){
          $current_month = $matches[1];
          if(!in_array($current_month, $months)){
            array_push($months, $current_month);
          }
        } else {
          $last_month = end($months);
          if(preg_match('/[^\s]/', $line) && $current_month == $last_month){
            $schedule2[$last_month] .= $line;
          }
        }
      }
      break;
    case 3:
      if (preg_match('/^\/\/ *start/', $line)){
        $state = 2;
      }
      break;
    }
  }
  
  // merge
  $schedule = "||準備室年中行事|学部学科行事日程|\n";
  $m = $start_month;
  for ($i = 1; $i <= 12; $i++) {
    if (in_array($m, $months)) {
      $schedule .= str_replace("\n", '&br;', '|' . $m . '月|' . rtrim($schedule1["$m"]) . '|' . rtrim($schedule2["$m"]) . '|') . "\n";
    }
    $m++;
    if ($m > 12) {
      $m = $m - 12;
    }
  }
  
  return convert_html($schedule);
}
?>