Difference between revisions of "Transform-changes.php"

From Organic Design wiki
m
(Render moves better)
Line 50: Line 50:
 
$td = '<td valign=top>';
 
$td = '<td valign=top>';
 
$s = '&nbsp;&nbsp;';
 
$s = '&nbsp;&nbsp;';
ksort($changes);
+
ksort( $changes );
foreach ( array_reverse($changes, SORT_STRING) as $t => $c ) {
+
foreach ( array_reverse( $changes, SORT_STRING ) as $t => $c ) {
 
$time = strftime('%H:%M', $t);
 
$time = strftime('%H:%M', $t);
if ($date != ($d = strftime('%d %b %Y', $t))) {
+
if ( $date != ( $d = strftime( '%d %b %Y', $t ) ) ) {
 
$html .= $date ? '<tr><td>&nbsp;<tr>' : '<tr>';
 
$html .= $date ? '<tr><td>&nbsp;<tr>' : '<tr>';
 
$html .= str_replace( ">Date", ">$d", $head );
 
$html .= str_replace( ">Date", ">$d", $head );
Line 60: Line 60:
 
$bgtag = ++$bg % 2 ? '' : ' bgcolor=#d4d4d4';
 
$bgtag = ++$bg % 2 ? '' : ' bgcolor=#d4d4d4';
 
$html .= "<tr$bgtag>$td$s$time$s$td$s&nbsp;$s";
 
$html .= "<tr$bgtag>$td$s$time$s$td$s&nbsp;$s";
if ($wt = $c[0][0]) {
+
if ( $wt = $c[0][0] ) {
 
$wiki = "<a href=\"".$c[0][1]."\" style=\"color:#009900;\">$wt:</a>";
 
$wiki = "<a href=\"".$c[0][1]."\" style=\"color:#009900;\">$wt:</a>";
if (!preg_match("/\\d+\\.\\d+\\.\\d+\\.\\d+/", $c[5]))
+
if ( !preg_match("/\\d+\\.\\d+\\.\\d+\\.\\d+/", $c[5]) )
$c[5] = str_replace('">', "\">$wt:", $c[5]);
+
$c[5] = str_replace( '">', "\">$wt:", $c[5] );
 
} else $wiki = '';
 
} else $wiki = '';
  
if (!$c[2]) {
+
if ( !$c[2] ) {
list($diff,$hist) = explode(' ', $c[1]);
+
list( $diff, $hist ) = explode( ' ', $c[1] );
preg_match("/(<a.+?<\\/a>)/", $c[7], $m);
+
preg_match( "/(<a.+?<\\/a>)/", $c[7], $m );
 
$c[4] = $m[1];
 
$c[4] = $m[1];
} else list(,$diff,$hist) = $c;
+
} else list(, $diff, $hist ) = $c;
if ($diff == 'diff') $diff = '';
+
if ( $diff == 'diff' ) $diff = '';
if (ereg('^href',$hist)) { $diff = "<a $hist>log</a>"; $hist = ''; }
+
if ( ereg('^href',$hist) ) { $diff = "<a $hist>log</a>"; $hist = ''; }
 +
 
 +
if ( ereg( 'undo', $c[1] ) ) {
 +
$c4 = str_replace( ' moved to', '', $c[3] );
 +
$c[3] = $c[4];
 +
$c[4] = $c4;
 +
}
  
if (ereg('undo', $c[1])) { $c[4] = "$c[3]&nbsp;$c[4]"; $c[3] = ''; }
 
 
$event = "$wiki$c[4]";
 
$event = "$wiki$c[4]";
  
if ($c[3]) $c[7] = "<i>($c[3])</i>&nbsp;$c[7]";
+
if ( $c[3] ) $c[7] = "<i>($c[3])</i>&nbsp;$c[7]";
 
$html .= "$td$s$event$s<td valign=top width=100%>$s$c[7]$s\n";
 
$html .= "$td$s$event$s<td valign=top width=100%>$s$c[7]$s\n";
 
$html .= "$td$s$c[5]";
 
$html .= "$td$s$c[5]";
Line 89: Line 94:
 
$wikis = array();
 
$wikis = array();
 
$wl = xwArticleContent("$tTitle/wiki.list", false);
 
$wl = xwArticleContent("$tTitle/wiki.list", false);
if (preg_match_all("/^\\*.*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\"/m", $wl, $wikis, PREG_SET_ORDER))
+
if ( preg_match_all( "/^\\*.*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\"/m", $wl, $wikis, PREG_SET_ORDER ) )
for ($i = 0; $i < count($wikis); $i++) array_shift($wikis[$i]);
+
for ( $i = 0; $i < count($wikis); $i++ ) array_shift( $wikis[$i] );
  
 
# get query string of this request
 
# get query string of this request
if (!$qs = $_SERVER['QUERY_STRING']) $qs = "title=Special:Recentchanges";
+
if ( !$qs = $_SERVER['QUERY_STRING'] ) $qs = "title=Special:Recentchanges";
  
 
# Merge each wikis changes into local table
 
# Merge each wikis changes into local table
 
$changes = array();
 
$changes = array();
foreach ($wikis as $wd) {
+
foreach ( $wikis as $wd ) {
$rc = file_get_contents($wd[1]."?$qs&xpath://view:=&printable=yes");
+
$rc = file_get_contents( $wd[1]."?$qs&xpath://view:=&printable=yes" );
extractChanges($changes, $rc, $wd);
+
extractChanges( $changes, $rc, $wd );
 
}
 
}
  
 
# Extract local changes and replace with merged table
 
# Extract local changes and replace with merged table
$article = preg_replace("/<h4>[0-9]+ [a-z]+ [0-9]+<\\/h4>\\s*<ul class=\"special\">.+(?=<div class=\"printfooter\">)/ise", '"<br>".renderChanges(extractChanges($changes,$article))', $article);
+
$article = preg_replace( "/<h4>[0-9]+ [a-z]+ [0-9]+<\\/h4>\\s*<ul class=\"special\">.+(?=<div class=\"printfooter\">)/ise", '"<br>".renderChanges(extractChanges($changes,$article))', $article );
 
#$article .= renderChanges( extractChanges( $changes, $article ) );
 
#$article .= renderChanges( extractChanges( $changes, $article ) );
  
 
global $wgSitename, $wgLang;
 
global $wgSitename, $wgLang;
 
$head = "<table border=0 cellpadding=0 cellspacing=0 width=100%><tr><td width=80% align=left><table border=0 cellpadding=0 cellspacing=0><tr><td><h1 class=firstHeading>&nbsp;$wgSitename Events & Changes</h1></table>\n";
 
$head = "<table border=0 cellpadding=0 cellspacing=0 width=100%><tr><td width=80% align=left><table border=0 cellpadding=0 cellspacing=0><tr><td><h1 class=firstHeading>&nbsp;$wgSitename Events & Changes</h1></table>\n";
$now = str_replace(' ','&nbsp;',$wgLang->timeanddate( wfTimestampNow(), true ));
+
$now = str_replace( ' ', '&nbsp;', $wgLang->timeanddate( wfTimestampNow(), true ) );
 
$head .= "<td width=20% align=right><a href=$script/Talk:Transform-changes.php><u>Talk:transform-changes.php</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>$now</b></a></table>\n";
 
$head .= "<td width=20% align=right><a href=$script/Talk:Transform-changes.php><u>Talk:transform-changes.php</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>$now</b></a></table>\n";
 
#$article = "$head$article";
 
#$article = "$head$article";
Line 114: Line 119:
 
# Add a 1 minute refresh into the headers
 
# Add a 1 minute refresh into the headers
 
#$GLOBALS['xwHeaders'] .= '<META HTTP-EQUIV="REFRESH" CONTENT="300" />';
 
#$GLOBALS['xwHeaders'] .= '<META HTTP-EQUIV="REFRESH" CONTENT="300" />';
#if (!isset($_REQUEST['printable'])) $_REQUEST['printable'] = true;
+
#if ( !isset( $_REQUEST['printable'] ) ) $_REQUEST['printable'] = true;
 
?>
 
?>

Revision as of 09:26, 7 March 2006

<?

  1. Merge local changes with changes from other specified MediaWikis

# Extract changes from a page into an array ready for rendering in a table function extractChanges(&$changes, &$page, $wd = array()) { global $xwIsAdmin, $xwUserGroups; list($wiki, $url, $tz) = $wd; #if ($wiki) xwMessage(strlen($page)." bytes of changes received from $wiki wiki.", 'green'); # Change exraction rules (for supporting TWiki and MoinMoin later) $cr = "/^\\((.*?(diff|log|undo)(<\\/a>)?)\\)(.+?\\((.+?<\\/a>)\\))?( \\. \\.\\s+(.*?)\\s+(()?<a.+?<\\/a>(<\\/strong>)?))?.*?; ([0-9]+:[0-9]+) \\. \\. (<a.+?<\\/a>).+?(<a.+?<\\/a>)(.+?\\((.+)\\))?/";

$pr = "/

([0-9]+ [a-z]+ [0-9]+)<\\/h4>\\s*
    (.+?)<\\/ul>/is"; # Used to make urls from other wiki's absolute if ($url) { $url = parse_url($url); $url = $url['scheme'].'://'.$url['host']; } $tr = 999; # patch to stop same time keys overwriting # First split the page by day sections preg_match_all($pr, $page, $m1, PREG_SET_ORDER); # Loop through changes in each day extracting relevent info into $changes array foreach ($m1 as $day) { preg_match_all("/
  • (.+?)<\\/li>/", $day[2], $m2); foreach ($m2[1] as $change) { $change = str_replace('href="', "href=\"$url", $change); if (preg_match($cr, $change, $m3)) { list(,$diff,,,,$hist,,$flags,$title,,,$time,$user,$talk,,$comment) = $m3; ereg('>.+</a>', $user, $uname); $user = ereg_replace($uname[0], str_replace(' ',' ',$uname[0]), $user); # Get read-perms for article if ( (!$readable = $xwIsAdmin) && ereg('>(.+)</a>', $title, $anchor) ) { $readable = xwGetListByTagname( xwArticleProperties($anchor[1]), 'read' ) + array('anyone'); $readable = count( array_intersect( $readable, $xwUserGroups ) ); } # Store the change if readable by current user if ( $readable ) $changes[(strtotime("$day[1] $time")+$tz*3600).'.00'.$tr--] = array($wd,$diff,$hist,$flags,$title,$user,$talk,$comment); } else xwMessage( "Couldn't match change: $change", 'red' ); } } return $changes; } # Render changes into said table function renderChanges(&$changes) { $head = ; foreach ( array( 'Date', 'Event / Activity / Article', 'Description', 'Account', 'Change', 'State' ) as $i ) { $colspan = $head ?  : ' colspan=2'; $head .= "<th$colspan align=center>
    • <a href=\"/wiki/index.php/Talk:transform-changes.php\">$i</a>
    \n";

    }

    $html = "
    \n"; $date = ; $td = '' : '';

    $html .= str_replace( ">Date", ">$d", $head ); $date = $d; $bg = 1; }; $bgtag = ++$bg % 2 ?  : ' bgcolor=#d4d4d4'; $html .= "<tr$bgtag>$td$s$time$s$td$s $s"; if ( $wt = $c[0][0] ) { $wiki = "<a href=\"".$c[0][1]."\" style=\"color:#009900;\">$wt:</a>"; if ( !preg_match("/\\d+\\.\\d+\\.\\d+\\.\\d+/", $c[5]) ) $c[5] = str_replace( '">', "\">$wt:", $c[5] ); } else $wiki = ;

    if ( !$c[2] ) { list( $diff, $hist ) = explode( ' ', $c[1] ); preg_match( "/(<a.+?<\\/a>)/", $c[7], $m ); $c[4] = $m[1]; } else list(, $diff, $hist ) = $c; if ( $diff == 'diff' ) $diff = ; if ( ereg('^href',$hist) ) { $diff = "<a $hist>log</a>"; $hist = ; }

    if ( ereg( 'undo', $c[1] ) ) { $c4 = str_replace( ' moved to', , $c[3] ); $c[3] = $c[4]; $c[4] = $c4; }

    $event = "$wiki$c[4]";

    if ( $c[3] ) $c[7] = "($c[3]) $c[7]";

    $html .= "$td$s$event$s
    ';

    $s = '  '; ksort( $changes ); foreach ( array_reverse( $changes, SORT_STRING ) as $t => $c ) { $time = strftime('%H:%M', $t); if ( $date != ( $d = strftime( '%d %b %Y', $t ) ) ) {

    $html .= $date ? '
     
    $s$c[7]$s\n";

    $html .= "$td$s$c[5]"; $html .= "$td$s$diff"; $html .= "$td$s$hist"; }

    return "$html\n
    ";

    }

    1. Get the list of wikis for which changes are merged

    $wikis = array(); $wl = xwArticleContent("$tTitle/wiki.list", false); if ( preg_match_all( "/^\\*.*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\"/m", $wl, $wikis, PREG_SET_ORDER ) ) for ( $i = 0; $i < count($wikis); $i++ ) array_shift( $wikis[$i] );

    1. get query string of this request

    if ( !$qs = $_SERVER['QUERY_STRING'] ) $qs = "title=Special:Recentchanges";

    1. Merge each wikis changes into local table

    $changes = array(); foreach ( $wikis as $wd ) { $rc = file_get_contents( $wd[1]."?$qs&xpath://view:=&printable=yes" ); extractChanges( $changes, $rc, $wd ); }

    1. Extract local changes and replace with merged table
    $article = preg_replace( "/

    [0-9]+ [a-z]+ [0-9]+<\\/h4>\\s*
      .+(?=
      )/ise", '"
      ".renderChanges(extractChanges($changes,$article))', $article );
      1. $article .= renderChanges( extractChanges( $changes, $article ) );

      global $wgSitename, $wgLang;

      $head = "

       $wgSitename Events & Changes

      \n";

      $now = str_replace( ' ', ' ', $wgLang->timeanddate( wfTimestampNow(), true ) );

      $head .= "
      <a href=$script/Talk:Transform-changes.php>Talk:transform-changes.php        $now</a>
      \n";
      1. $article = "$head$article";
      1. Add a 1 minute refresh into the headers
      2. $GLOBALS['xwHeaders'] .= '<META HTTP-EQUIV="REFRESH" CONTENT="300" />';
      3. if ( !isset( $_REQUEST['printable'] ) ) $_REQUEST['printable'] = true;

      ?>