[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/importoldchangelog/ -> action.php (source)

   1  <?php
   2  // must be run within Dokuwiki
   3  if(!defined('DOKU_INC')) die();
   4  
   5  if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
   6  require_once (DOKU_PLUGIN.'action.php');
   7  
   8  class action_plugin_importoldchangelog extends DokuWiki_Action_Plugin {
   9  
  10      function getInfo(){
  11          return array(
  12              'author' => 'Ben Coburn',
  13              'email'  => 'btcoburn@silicodon.net',
  14              'date'   => '2006-10-29',
  15              'name'   => 'Import Old Changelog',
  16              'desc'   => 'Imports and converts the single file changelog '.
  17                          'from the 2006-03-09 release to the new format. '.
  18                          'Also reconstructs missing changelog data from  '.
  19                          'old revisions kept in the attic.',
  20              'url'    => 'http://dokuwiki.org/plugin:importoldchangelog'
  21              );
  22      }
  23  
  24      function register(&$controller) {
  25          $controller->register_hook('TEMPORARY_CHANGELOG_UPGRADE_EVENT', 'BEFORE', $this, 'run_import');
  26      }
  27  
  28      function importOldLog($line, &$logs) {
  29          global $lang;
  30          /*
  31          // Note: old log line format
  32          //$info['date']  = $tmp[0];
  33          //$info['ip']    = $tmp[1];
  34          //$info['id']    = $tmp[2];
  35          //$info['user']  = $tmp[3];
  36          //$info['sum']   = $tmp[4];
  37          */
  38          $oldline = @explode("\t", $line);
  39          if ($oldline!==false && count($oldline)>1) {
  40              // trim summary
  41              $tmp = substr($oldline[4], 0, 1);
  42              $wasMinor = ($tmp==='*');
  43              if ($tmp==='*' || $tmp===' ') {
  44                  $sum = rtrim(substr($oldline[4], 1), "\n");
  45              } else {
  46                  // no is_minor prefix in summary
  47                  $sum = rtrim($oldline[4], "\n");
  48              }
  49              // guess line type
  50              $type = DOKU_CHANGE_TYPE_EDIT;
  51              if ($wasMinor) { $type = DOKU_CHANGE_TYPE_MINOR_EDIT; }
  52              if ($sum===$lang['created']) { $type = DOKU_CHANGE_TYPE_CREATE; }
  53              if ($sum===$lang['deleted']) { $type = DOKU_CHANGE_TYPE_DELETE; }
  54              // build new log line
  55              $tmp = array();
  56              $tmp['date']  = (int)$oldline[0];
  57              $tmp['ip']    = $oldline[1];
  58              $tmp['type']  = $type;
  59              $tmp['id']    = $oldline[2];
  60              $tmp['user']  = $oldline[3];
  61              $tmp['sum']   = $sum;
  62              $tmp['extra'] = '';
  63              // order line by id
  64              if (!isset($logs[$tmp['id']])) { $logs[$tmp['id']] = array(); }
  65              $logs[$tmp['id']][$tmp['date']] = $tmp;
  66          }
  67      }
  68  
  69      function importFromAttic(&$logs) {
  70          global $conf, $lang;
  71          $base = $conf['olddir'];
  72          $stack = array('');
  73          $context = ''; // namespace
  74          while (count($stack)>0){
  75              $context = array_pop($stack);
  76              $dir = dir($base.'/'.str_replace(':', '/', $context));
  77  
  78              while (($file = $dir->read()) !== false) {
  79                  if ($file==='.' || $file==='..') { continue; }
  80                  $matches = array();
  81                  if (preg_match('/([^.]*)\.([^.]*)\..*/', $file, $matches)===1) {
  82                      $id = (($context=='')?'':$context.':').$matches[1];
  83                      $date = $matches[2];
  84  
  85                      // check if page & revision are already logged
  86                      if (!isset($logs[$id])) { $logs[$id] = array(); }
  87                      if (!isset($logs[$id][$date])) {
  88                          $tmp = array();
  89                          $tmp['date']  = (int)$date;
  90                          $tmp['ip']    = '127.0.0.1'; // original ip lost
  91                          $tmp['type']  = DOKU_CHANGE_TYPE_EDIT;
  92                          $tmp['id']    = $id;
  93                          $tmp['user']  = ''; // original user lost
  94                          $tmp['sum']   = '('.$lang['restored'].')'; // original summary lost
  95                          $tmp['extra'] = '';
  96                          $logs[$id][$date] = $tmp;
  97                      }
  98  
  99                  } else if (is_dir($dir->path.'/'.$file)) {
 100                      array_push($stack, (($context=='')?'':$context.':').$file);
 101                  }
 102  
 103              }
 104  
 105              $dir->close();
 106          }
 107  
 108      }
 109  
 110      function savePerPageChanges($id, &$changes, &$recent) {
 111          ksort($changes); // ensure correct order of changes from attic
 112          foreach ($changes as $date => $tmp) {
 113              $changes[$date] = implode("\t", $tmp)."\n";
 114              $recent[$date] = &$changes[$date];
 115          }
 116          io_saveFile(metaFN($id, '.changes'), implode('', $changes));
 117      }
 118  
 119      function savePerPageMetadata($id, &$changes) {
 120          global $auth;
 121          ksort($changes); // order by date
 122          $meta = array();
 123          // Run through history and populate the metadata array
 124          foreach ($changes as $date => $tmp) {
 125              $user = $tmp['user'];
 126              if ($tmp['type'] === DOKU_CHANGE_TYPE_CREATE) {
 127                  $meta['date']['created'] = $tmp['date'];
 128                  if ($user) {
 129                      $userinfo = $auth->getUserData($user);
 130                      $meta['creator'] = $userinfo['name'];
 131                  }
 132              } else if ($tmp['type'] === DOKU_CHANGE_TYPE_EDIT) {
 133                  $meta['date']['modified'] = $tmp['date'];
 134                  if ($user) {
 135                      $userinfo = $auth->getUserData($user);
 136                      $meta['contributor'][$user] = $userinfo['name'];
 137                  }
 138              }
 139          }
 140          p_set_metadata($id, $meta, true);
 141      }
 142  
 143      function resetTimer() {
 144          // Add 5 minutes to the script execution timer...
 145          // This should be much more than needed.
 146          @set_time_limit(5*60);
 147          // Note: Has no effect in safe-mode!
 148      }
 149  
 150      function run_import(&$event, $args) {
 151          global $conf;
 152          register_shutdown_function('importoldchangelog_plugin_shutdown');
 153          touch($conf['changelog'].'_importing'); // changelog importing lock
 154          io_saveFile($conf['changelog'], ''); // pre-create changelog
 155          io_lock($conf['changelog']);  // hold onto the lock
 156          // load old changelog
 157          $this->resetTimer();
 158          $log = array();
 159          $oldlog = file($conf['changelog_old']);
 160          foreach ($oldlog as $line) {
 161              $this->importOldLog($line, $log);
 162          }
 163          unset($oldlog); // free memory
 164          // look in the attic for unlogged revisions
 165          $this->resetTimer();
 166          $this->importFromAttic($log);
 167          // save per-page changelogs
 168          $this->resetTimer();
 169          $recent = array();
 170          foreach ($log as $id => $page) {
 171              $this->savePerPageMetadata($id, $page);
 172              $this->savePerPageChanges($id, $page, $recent);
 173          }
 174          // save recent changes cache
 175          $this->resetTimer();
 176          ksort($recent); // ensure correct order of recent changes
 177          io_unlock($conf['changelog']); // hand off the lock to io_saveFile
 178          io_saveFile($conf['changelog'], implode('', $recent));
 179          @unlink($conf['changelog'].'_importing'); // changelog importing unlock
 180      }
 181  
 182  }
 183  
 184  function importoldchangelog_plugin_shutdown() {
 185      global $conf;
 186      $path = array();
 187      $path['changelog'] = $conf['changelog'];
 188      $path['importing'] = $conf['changelog'].'_importing';
 189      $path['failed']    = $conf['changelog'].'_failed';
 190      $path['import_ok'] = $conf['changelog'].'_import_ok';
 191      io_unlock($path['changelog']); // guarantee unlocking
 192      if (@file_exists($path['importing'])) {
 193          // import did not finish
 194          rename($path['importing'], $path['failed']) or trigger_error('Importing changelog failed.', E_USER_WARNING);
 195          @unlink($path['import_ok']);
 196      } else {
 197          // import successful
 198          touch($path['import_ok']);
 199          @unlink($path['failed']);
 200          plugin_disable('importoldchangelog'); // only needs to run once
 201      }
 202  }
 203  
 204  


Generated: Tue Dec 2 01:30:01 2008 Cross-referenced by PHPXref 0.7