[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/wiki/ewiki/plugins/moodle/ -> diff.php (source)

   1  <?php // $Id: diff.php,v 1.8 2007/06/08 13:45:10 sam_marshall Exp $
   2  
   3   # this is the "stupid diff", which shows up changes between two
   4   # saved versions of a WikiPage; even if working very unclean it
   5   # allows to see what has changed
   6   # it is accessible through the "info about page" action
   7  
   8  
   9  
  10   $ewiki_plugins["action"]["diff"] = "ewiki_page_stupid_diff";
  11   $ewiki_config["action_links"]["info"]["diff"] = "diff";
  12  
  13  
  14  
  15   function ewiki_page_stupid_diff($id, $data, $action) {
  16      global $wiki, $moodle_format;
  17  
  18      if ($uu=$GLOBALS["ewiki_diff_versions"]) {
  19         list($new_ver, $old_ver) = $uu;
  20         $data = ewiki_database("GET", array("id" => $id, "version" => $new_ver));
  21      }
  22      else {
  23         $new_ver = $data["version"];
  24         $old_ver = $new_ver - 1;
  25      }
  26      if ($old_ver > 0) {
  27         $data0 = ewiki_database("GET", array("id" => $id, "version" => $old_ver));
  28      }
  29      
  30      $a->new_ver=$new_ver;
  31      $a->old_ver=$old_ver;
  32      $a->pagename=$id;
  33      $o = ewiki_make_title($id, get_string("differences","wiki",$a));
  34  
  35      # Different handling for html: closes Bug #1530 - Wiki diffs useless when using HTML editor    
  36      if($wiki->htmlmode==2) {
  37          /// first do the formatiing to get normal display format without filters
  38          $options = new object();
  39          $options->smiley = false;
  40          $options->filter = false;
  41          $content0 = format_text($data0['content'], $moodle_format, $options);
  42          $content = format_text($data['content'], $moodle_format, $options);
  43  
  44          /// Remove all new line characters. They will be placed at HTML line breaks.
  45          $content0 = preg_replace('/\n|\r/i', ' ', $content0);
  46          $content0 = preg_replace('/(\S)\s+(\S)/', '$1 $2', $content0); // Remove multiple spaces.
  47          $content = preg_replace('/\n|\r/i', ' ', $content);
  48          $content = preg_replace('/(\S)\s+(\S)/', '$1 $2', $content);
  49          
  50          /// Replace <p>&nbsp;</p>
  51          $content0 = preg_replace('#(<p( [^>]*)?>(&nbsp;|\s+)</p>)|(<p( [^>]*)?></p>)#i', "\n", $content0);
  52          $content = preg_replace('#(<p( [^>]*)?>(&nbsp;|\s+)</p>)|(<p( [^>]*)?></p>)#i', "\n", $content);
  53          
  54  
  55          /// Place new line characters at logical HTML positions.
  56          $htmlendings = array('+(<br.*?>)+iU', '+(<p( [^>]*)?>)+iU', '+(</p>)+i', '+(<hr.*?>)+iU', '+(<ol.*?>)+iU',
  57                               '+(</ol>)+i', '+(<ul.*?>)+iU', '+(</ul>)+i', '+(<li.*?>)+iU', '+(</li>)+i', 
  58                               '+(</tr>)+i', '+(<div.*?>)+iU', '+(</div>)+i');
  59          $htmlrepl = array("\n\$1\n", "\n\$1\n", "\n\$1\n", "\n\$1\n", "\n\$1\n",
  60                            "\n\$1\n", "\n\$1\n", "\n\$1\n", "\n\$1\n", "\n\$1\n",
  61                            "\n\$1\n", "\n\$1\n", "\n\$1\n");
  62          $content0 = preg_replace($htmlendings, $htmlrepl, $content0);
  63          $content = preg_replace($htmlendings, $htmlrepl, $content);
  64      } else {
  65        $content0=$data0["content"];
  66        $content=$data["content"];
  67      }
  68      $txt0 = preg_split("+\s*\n+", trim($content0));
  69      $txt2 = preg_split("+\s*\n+", trim($content));
  70  
  71      $diff0 = array_diff($txt0, $txt2);
  72      $diff2 = array_diff($txt2, $txt0);
  73  
  74      foreach ($txt2 as $i => $line) {
  75         $i2 = $i;
  76         while ($rm = $diff0[$i2++]) {          
  77            if($wiki->htmlmode == 2) {
  78              if ($rm == '<br />') { //ugly hack to fix line breaks
  79                  $rm = '';
  80              }
  81              $o .= "<b>-</b><font color=\"#990000\">".format_text($rm, $moodle_format, $options)."</font><br />\n";
  82            } else {
  83              $o .= "<b>-</b><font color=\"#990000\"><tt>".s($rm)."</tt></font><br />\n";
  84            }
  85            unset($diff0[$i2-1]);
  86         }
  87  
  88         if (in_array($line, $diff2)) {
  89            if($wiki->htmlmode == 2) {
  90              if ($line == '<br />') { //ugly hack to fix line breaks
  91                  $line = '';
  92              }
  93              $o .= "<b>+</b><font color=\"#009900\">".format_text($line, $moodle_format, $options)."</font><br />\n";
  94            } else {
  95              $o .= "<b>+</b><font color=\"#009900\"><tt>".s($line)."</tt></font><br />\n";
  96            }
  97         }
  98         else {
  99            if($wiki->htmlmode == 2) {
 100              $o .= format_text($line, $moodle_format, $options)."\n";
 101            } else {
 102              $o .= "&nbsp; ".s($line)."<br />\n";
 103            }
 104         }
 105  
 106      }
 107  
 108      foreach ($diff0 as $rm) {
 109         $o .= "<b>-</b><font color=\"#990000\"> <tt>".s($rm)."</tt></font><br />\n";
 110      }
 111  
 112      return($o);
 113   }
 114  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7