eZ Publish  [4.0]
changelog2xmltext.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <22-Mar-2004 13:00:25 amos>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 //include_once( 'lib/ezutils/classes/ezcli.php' );
00031 //include_once( 'kernel/classes/ezscript.php' );
00032 
00033 require 'autoload.php';
00034 
00035 $cli = eZCLI::instance();
00036 $script = eZScript::instance( array( 'description' => ( "eZ Publish Changelog converter\n\n" .
00037                                                         "Converts a Changelog into XML text format usable in eZ Publish\n" .
00038                                                         "The result is printed to the standard output" ),
00039                                      'use-session' => false,
00040                                      'use-modules' => true,
00041                                      'use-extensions' => true ) );
00042 
00043 $script->startup();
00044 
00045 $options = $script->getOptions( "",
00046                                 "[changelog]",
00047                                 false, false,
00048                                 array( 'log' => false,
00049                                        'siteaccess' => false ) );
00050 $script->initialize();
00051 
00052 if ( count( $options['arguments'] ) < 1 )
00053 {
00054     $cli->error( "Missing changelog file" );
00055     $script->shutdown( 1 );
00056 }
00057 
00058 $changelogFilename = $options['arguments'][0];
00059 
00060 $fd = fopen( $changelogFilename, 'r' ) or $script->shutdown( 1, "Couldn't not open file $changelogFilename" );
00061 $text = fread( $fd, filesize( $changelogFilename ) );
00062 fclose( $fd );
00063 
00064 $lines = explode( "\n", $text );
00065 
00066 function isChangeEntry( $line, &$changeText )
00067 {
00068     if ( preg_match( "/^- (.+)$/", $line, $matches ) )
00069     {
00070         $changeText = $matches[1];
00071         return true;
00072     }
00073     return false;
00074 }
00075 
00076 function addListItem( &$listLines, $changeText )
00077 {
00078     if ( $changeText !== false )
00079     {
00080         $changeText = str_replace( array( "<", ">",   ),
00081                                    array( "[", "]" ),
00082                                    $changeText );
00083         $methodText = 'http|https|ftp|sftp';
00084         $elements = preg_split( "#((?:(?:$methodText)://)(?:(?:[a-zA-Z0-9_-]+\\.)*[a-zA-Z0-9_-]+(?:(?:[\/a-zA-Z0-9_+$-]+\\.)*(?:[\/a-zA-Z0-9_+$-])*))(?:\#[a-zA-Z0-9-]+)?\?*(?:[a-zA-Z0-9_-]+=[a-zA-Z0-9_-]+&*)*)#m",
00085                                 $changeText,
00086                                 false,
00087                                 PREG_SPLIT_DELIM_CAPTURE );
00088         $newElements = array();
00089         $i = 0;
00090         foreach ( $elements as $element )
00091         {
00092             if ( ( $i % 2 ) == 1 )
00093             {
00094                 $element = "<link href=\"$element\">$element</link>";
00095             }
00096             $newElements[] = $element;
00097             ++$i;
00098         }
00099         $changeText = implode( '', $newElements );
00100 
00101         $elements = preg_split( "/bugs? *# *([0-9]+)/im",
00102                                 $changeText,
00103                                 false,
00104                                 PREG_SPLIT_DELIM_CAPTURE );
00105         $newElements = array();
00106         $i = 0;
00107         foreach ( $elements as $element )
00108         {
00109             if ( ( $i % 2 ) == 1 )
00110             {
00111                 $element = "<link href=\"/bugs/view/$element\">bug #$element</link>";
00112             }
00113             $newElements[] = $element;
00114             ++$i;
00115         }
00116         $changeText = implode( '', $newElements );
00117 
00118         $elements = preg_split( "/enhancements? *# *([0-9]+)/im",
00119                                 $changeText,
00120                                 false,
00121                                 PREG_SPLIT_DELIM_CAPTURE );
00122         $newElements = array();
00123         $i = 0;
00124         foreach ( $elements as $element )
00125         {
00126             if ( ( $i % 2 ) == 1 )
00127             {
00128                 $element = "<link href=\"/bugs/view/$element\">enhancement #$element</link>";
00129             }
00130             $newElements[] = $element;
00131             ++$i;
00132         }
00133         $changeText = implode( '', $newElements );
00134 
00135         $changeText = preg_replace( "# *\( *?(:?manually +)?merged +from +[a-z0-9.-]+(?:/[a-z0-9.-]+)*[,/]? +(\([0-9](?:[.-][0-9a-z]+)*\))? *(?:rev|erv)(?:ision|\.)? *[0-9]+ *\)#i", '', $changeText );
00136 
00137         $listLines[] = array( 'type' => 'li',
00138                               'text' => $changeText );
00139     }
00140 }
00141 
00142 function createList( &$newLines, &$listLines )
00143 {
00144     if ( count( $listLines ) > 0 )
00145     {
00146         $ulEntry = array( 'type' => 'ul',
00147                           'items' => $listLines );
00148         $newLines[] = $ulEntry;
00149         $listLines = array();
00150     }
00151 }
00152 
00153 function isEmptyLine( $line )
00154 {
00155     return strlen( trim( $line ) ) == 0;
00156 }
00157 
00158 $newLines = array();
00159 $lineNumber = 0;
00160 $listCounter = 0;
00161 $listLines = array();
00162 $currentListEntry = false;
00163 $lastSection = null;
00164 foreach ( $lines as $line )
00165 {
00166     $line = trim( $line, "\r" );
00167 
00168     ++$lineNumber;
00169     if ( $lineNumber == 1 )
00170     {
00171         $newLines[] = array( 'type' => 'title',
00172                              'name' => $line );
00173         $newLines[] = '';
00174         continue;
00175     }
00176     if ( $listCounter > 0 )
00177     {
00178         if ( isChangeEntry( $line, $changeText ) )
00179         {
00180             addListItem( $listLines, $currentListEntry );
00181             $currentListEntry = $changeText;
00182         }
00183         else if ( isEmptyLine( $line ) )
00184         {
00185             addListItem( $listLines, $currentListEntry );
00186             $currentListEntry = false;
00187             $listCounter = 0;
00188         }
00189         else
00190         {
00191             $currentListEntry .= ' ' . trim( $line );
00192         }
00193     }
00194     else if ( preg_match( "/^(\*?)(.+):/", $line, $matches ) )
00195     {
00196         addListItem( $listLines, $currentListEntry );
00197         $currentListEntry = false;
00198         createList( $lastSection['items'], $listLines );
00199 
00200         $header = $matches[2];
00201         $headerLevel = 1;
00202         if ( !$matches[1] )
00203             $headerLevel += 1;
00204         $section = array( 'type' => 'section',
00205                           'level' => $headerLevel,
00206                           'name' => "$header",
00207                           'items' => array() );
00208         $newLines[] = $section;
00209         unset( $lastSection );
00210         $lastSection =& $newLines[count( $newLines ) - 1];
00211         $listCounter = 1;
00212     }
00213 }
00214 addListItem( $listLines, $currentListEntry );
00215 $currentListEntry = false;
00216 if ( $lastSection !== null )
00217 {
00218     createList( $lastSection['items'], $listLines );
00219 }
00220 
00221 function dumpToText( $nodes )
00222 {
00223     $text = '';
00224     foreach ( $nodes as $node )
00225     {
00226         if ( is_string( $node ) )
00227         {
00228             $text .= $node . "\n";
00229         }
00230         else
00231         {
00232             if ( !isset( $node['type'] ) )
00233             {
00234                     var_dump( $node );
00235             }
00236             $type = $node['type'];
00237             switch ( $type )
00238             {
00239                 case 'title':
00240                 {
00241                     $text .= $node['name'] . "\n";
00242                 } break;
00243                 case 'section':
00244                 {
00245                     if ( count( $node['items'] ) > 0 )
00246                     {
00247                         $text .= "<header level='" . $node['level'] . "'>" . $node['name'] . "</header>\n";
00248                         $text .= dumpToText( $node['items'] );
00249                     }
00250                 } break;
00251                 case 'ul':
00252                 {
00253                     $text .= "<ul>\n";
00254                     $text .= dumpToText( $node['items'] );
00255                     $text .= "</ul>\n\n";
00256                 } break;
00257                 case 'li':
00258                 {
00259                     $text .= "  <li>" . $node['text'] . "</li>\n";
00260                 } break;
00261                 default:
00262                 {
00263                     $cli->error( "Unknown type [$type]\n" );
00264                     $script->shutdown( 1 );
00265                 }
00266             }
00267         }
00268     }
00269     return $text;
00270 }
00271 
00272 $newText = dumpToText( $newLines );
00273 
00274 $cli->output( $newText, false );
00275 
00276 $script->shutdown();
00277 
00278 ?>