[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/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://wiki.splitbrain.org/wiki:changelog'
  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 = 'E';
  51              if ($wasMinor) { $type = 'e'; }
  52              if ($sum===$lang['created']) { $type = 'C'; }
  53              if ($sum===$lang['deleted']) { $type = 'D'; }
  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']  = 'E';
  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 resetTimer() {
 120          // Add 5 minutes to the script execution timer...
 121          // This should be much more than needed.
 122          @set_time_limit(5*60);
 123          // Note: Has no effect in safe-mode!
 124      }
 125  
 126      function run_import(&$event, $args) {
 127          global $conf;
 128          register_shutdown_function('importoldchangelog_plugin_shutdown');
 129          touch($conf['changelog'].'_importing'); // changelog importing lock
 130          io_saveFile($conf['changelog'], ''); // pre-create changelog
 131          io_lock($conf['changelog']);  // hold onto the lock
 132          // load old changelog
 133          $this->resetTimer();
 134          $log = array();
 135          $oldlog = file($conf['changelog_old']);
 136          foreach ($oldlog as $line) {
 137              $this->importOldLog($line, $log);
 138          }
 139          unset($oldlog); // free memory
 140          // look in the attic for unlogged revisions
 141          $this->resetTimer();
 142          $this->importFromAttic($log);
 143          // save per-page changelogs
 144          $this->resetTimer();
 145          $recent = array();
 146          foreach ($log as $id => $page) {
 147              $this->savePerPageChanges($id, $page, $recent);
 148          }
 149          // save recent changes cache
 150          $this->resetTimer();
 151          ksort($recent); // ensure correct order of recent changes
 152          io_unlock($conf['changelog']); // hand off the lock to io_saveFile
 153          io_saveFile($conf['changelog'], implode('', $recent));
 154          @unlink($conf['changelog'].'_importing'); // changelog importing unlock
 155      }
 156  
 157  }
 158  
 159  function importoldchangelog_plugin_shutdown() {
 160      global $conf;
 161      $path = array();
 162      $path['changelog'] = $conf['changelog'];
 163      $path['importing'] = $conf['changelog'].'_importing';
 164      $path['failed']    = $conf['changelog'].'_failed';
 165      $path['import_ok'] = $conf['changelog'].'_import_ok';
 166      io_unlock($path['changelog']); // guarantee unlocking
 167      if (@file_exists($path['importing'])) {
 168          // import did not finish
 169          rename($path['importing'], $path['failed']) or trigger_error('Importing changelog failed.', E_USER_WARNING);
 170          @unlink($path['import_ok']);
 171      } else {
 172          // import successful
 173          touch($path['import_ok']);
 174          @unlink($path['failed']);
 175          plugin_disable('importoldchangelog'); // only needs to run once
 176      }
 177  }
 178  
 179  


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7