cvs: php-gtk-doc /scripts merge_updates.php
| From: | Andrei Zmievski | Date: | Sat, 08 Apr 2006 06:24:35 +0000 |
| Subject: | cvs: php-gtk-doc /scripts merge_updates.php | ||
| Groups: | php.gtk.doc | ||
| Request: | Send a blank email to php-gtk-doc+get-2192@lists.php.net to get a copy of this message | ||
andrei Sat Apr 8 06:24:35 2006 UTC
Modified files:
/php-gtk-doc/scripts merge_updates.php
Log:
Simplify.
http://cvs.php.net/viewcvs.cgi/php-gtk-doc/scripts/merge_updates.php?r1=1.1&r2=1.2&diff_format=u Index: php-gtk-doc/scripts/merge_updates.php diff -u php-gtk-doc/scripts/merge_updates.php:1.1 php-gtk-doc/scripts/merge_updates.php:1.2 --- php-gtk-doc/scripts/merge_updates.php:1.1 Sun Mar 26 02:44:13 2006 +++ php-gtk-doc/scripts/merge_updates.php Sat Apr 8 06:24:35 2006 @@ -1,145 +1,170 @@ -#!/usr/local/bin/php -<?php - -/** - * Class for combining a current list of manual file - * updates with an existing list. - * - * Updated files should be listed as a set of HTML - * links. The resulting file is an HTML fragment - * that consists of precisely that, and nothing more. - * - * @original author Scott Mattocks - * @altered by Steph Fox to accommodate XSLT changes - */ - -// Check number of args -if ($GLOBALS["argc"] != 3) { - exit("Purpose: Merge pre-formatted contents of new updates file into pre-existing updates file.\r\n" - .'Usage: merge-updates.php [ newupdatesfile ] [ outfile ] ' ."\r\n" - ); -} - -// Not really max. It can be overridden by -// very busy scribblers: see mergeUpdates() -define ('MAX_UPDATES_TO_SHOW', 15); - -class ManualUpdates { - - /** - * File containing the new update data. - * @var string - */ - var $newfile; - /** - * Pre-existing updates file; used for output. - * @var string - */ - var $outfile; - /** - * Array that will contain the new update data. - * @var array - */ - var $newarray = array(); - /** - * Array that will contain the current data. - * @var array - */ - var $oldarray = array(); - /** - * Array that will carry the merged info. - * @var array - */ - var $output = array(); - var $maximum; - - /** - * PHP 4 constructor. - * Sets files name and content variables - * - * @param string updatesfile, string outputfile - * @return void - */ - function ManualUpdates($newupdates, $outupdates) - { - $this->newfile = $newupdates; - $this->outfile = $outupdates; - $this->newarray = $this->_getUpdates($this->newfile); - $this->oldarray = $this->_getUpdates($this->outfile); - } - - /** - * Value checking. - * Picks up file contents. - * - * @param string filename - * @return array filecontents - */ - function _getUpdates($filename) - { - if (!is_readable($filename)) { - trigger_error('Cannot get updates from ' . $filename, E_USER_WARNING); - trigger_error($filename . ' does not exist or is not readable by this user.', E_USER_ERROR); - exit; - } - return file($filename); - } - - /** - * Merges and sorts the arrays without array_intersect_key() or even a useful timestamp <sigh /> - * Rewrite this function when we get PHP 5 and/or `date %s` - * Sets output array - * - * @param none - * @return void - */ - function mergeUpdates() { - // We want to show MAX_UPDATES_TO_SHOW or the total of - // tonight's updates, whichever is the greater. Apparently. - $this->maximum = max(MAX_UPDATES_TO_SHOW, sizeof($this->newarray)); - $merged = array_merge($this->newarray, $this->oldarray); - - // Lines aren't unique. Titles are... - foreach ($merged as $line) { - $stripped = strip_tags($line); - $titles[] = substr($stripped, strpos($stripped, ' ')); - } - - $unique = array_unique($titles); - - for ($i = 0; $i < sizeof($merged); $i++) { - if (array_key_exists($i, $unique) && sizeof($this->output) < $this->maximum) { - $this->output[] = $merged[$i]; - } else { - unset($merged[$i]); - } - } - } - - /** - * Writes the updated output array to file, the old-fashioned way - * - * @param none - * @return void - */ - function writeUpdatesToFile() - { - $fp = fopen($this->outfile, 'w'); - - foreach ($this->output as $line) { - fwrite($fp, $line); - } - fclose($fp); - } -} - -// Set the parameter vars -$newupdates = $argv[1]; -$outupdates = $argv[2]; - -// Create the current list of updates. -$mu =& new ManualUpdates($newupdates, $outupdates); -$mu->mergeUpdates(); -$mu->writeUpdatesToFile(); - -?> +#!/usr/local/bin/php +<?php + +/** + * Class for combining a current list of manual file + * updates with an existing list. + * + * Updated files should be listed as a set of HTML + * links. The resulting file is an HTML fragment + * that consists of precisely that, and nothing more. + * + * @original author Scott Mattocks + * @altered by Steph Fox to accommodate XSLT changes + */ + +// Check number of args +if ($argc < 3 || $argc > 4) { + exit("Purpose: Merge pre-formatted contents of new updates file with old updates file.\r\n" + .'Usage: merge-updates.php <new file> <old file> [ <outfile> ] ' ."\r\n" + ); +} + +// Not really max. It can be overridden by +// very busy scribblers: see mergeUpdates() +define ('MAX_UPDATES_TO_SHOW', 15); + +class ManualUpdates { + + /** + * File containing the new update data. + * @var string + */ + var $newfile; + /** + * Pre-existing updates file; can used for output. + * @var string + */ + var $oldfile; + + /** + * File to use for output. + * @var string + */ + var $outfile; + + /** + * Array that will contain the new update data. + * @var array + */ + var $newarray = array(); + /** + * Array that will contain the current data. + * @var array + */ + var $oldarray = array(); + /** + * Array that will carry the merged info. + * @var array + */ + var $output = array(); + var $maximum; + + /** + * PHP 4 constructor. + * Sets files name and content variables + * + * @param string updatesfile, string outputfile + * @return void + */ + function ManualUpdates($newupdates, $oldupdates, $outupdates) + { + $this->newfile = $newupdates; + $this->oldfile = $oldupdates; + $this->outfile = $outupdates; + $this->newarray = $this->_getUpdates($this->newfile, 1); + $this->oldarray = $this->_getUpdates($this->oldfile, 0); + } + + /** + * Value checking. + * Picks up file contents. + * + * @param string filename + * @return array filecontents + */ + function _getUpdates($filename, $new) + { + if (!is_readable($filename)) { + trigger_error('Cannot get updates from ' . $filename, E_USER_WARNING); + trigger_error($filename . ' does not exist or is not readable by this user.', E_USER_ERROR); + exit; + } + + $updates = array(); + $data = file_get_contents($filename); + preg_match_all('!^.+href="([^"]+)".+date="([^"]+)">([^<]+).*$!m', $data, $match, PREG_SET_ORDER); + foreach ($match as $m) { + $updates[$m[1]] = array($m[3], $m[2], $m[0], $new); + } + return $updates; + } + + /** + * Merges and sorts the updates arrays + * Sets output array + * + * @param none + * @return void + */ + function mergeUpdates() { + $maximum = max(MAX_UPDATES_TO_SHOW, count($this->newarray)); + $this->output = array_merge($this->oldarray, $this->newarray); + uasort($this->output, array(&$this, 'sortByDate')); + $this->output = array_slice($this->output, 0, $maximum); + return; + } + + function sortByDate($a, $b) + { + /* if dates are the same */ + if ((int)$a[1] == (int)$b[1]) { + /* and 'new' flags are the same */ + if ($a[3] == $b[3]) { + /* sort by title */ + return strcmp($a[0], $b[0]); + } else { + /* + * otherwise sort by 'new' flags so that new entries show up at + * the top + */ + return ($a[3] < $b[3]) ? 1 : -1; + } + } + + /* sort by date */ + return ((int)$a[1] < (int)$b[1]) ? 1 : -1; + } + + /** + * Writes the updated output array to file, the old-fashioned way + * + * @param none + * @return void + */ + function writeUpdatesToFile() + { + $fp = fopen($this->outfile, 'w'); + + foreach ($this->output as $line) { + fwrite($fp, $line[2]."\n"); + } + fclose($fp); + } +} + +// Set the parameter vars +$newupdates = $argv[1]; +$oldupdates = $argv[2]; +if ($argc > 3) { + $outupdates = $argv[3]; +} else { + $outupdates = $oldupdates; +} + +// Create the current list of updates. +$mu =& new ManualUpdates($newupdates, $oldupdates, $outupdates); +$mu->mergeUpdates(); +$mu->writeUpdatesToFile(); + +?>
http://cvs.php.net/viewcvs.cgi/php-gtk-doc/scripts/merge_updates.php?r1=1.1&r2=1.2&diff_format=u Index: php-gtk-doc/scripts/merge_updates.php diff -u php-gtk-doc/scripts/merge_updates.php:1.1 php-gtk-doc/scripts/merge_updates.php:1.2 --- php-gtk-doc/scripts/merge_updates.php:1.1 Sun Mar 26 02:44:13 2006 +++ php-gtk-doc/scripts/merge_updates.php Sat Apr 8 06:24:35 2006 @@ -1,145 +1,170 @@ -#!/usr/local/bin/php -<?php - -/** - * Class for combining a current list of manual file - * updates with an existing list. - * - * Updated files should be listed as a set of HTML - * links. The resulting file is an HTML fragment - * that consists of precisely that, and nothing more. - * - * @original author Scott Mattocks - * @altered by Steph Fox to accommodate XSLT changes - */ - -// Check number of args -if ($GLOBALS["argc"] != 3) { - exit("Purpose: Merge pre-formatted contents of new updates file into pre-existing updates file.\r\n" - .'Usage: merge-updates.php [ newupdatesfile ] [ outfile ] ' ."\r\n" - ); -} - -// Not really max. It can be overridden by -// very busy scribblers: see mergeUpdates() -define ('MAX_UPDATES_TO_SHOW', 15); - -class ManualUpdates { - - /** - * File containing the new update data. - * @var string - */ - var $newfile; - /** - * Pre-existing updates file; used for output. - * @var string - */ - var $outfile; - /** - * Array that will contain the new update data. - * @var array - */ - var $newarray = array(); - /** - * Array that will contain the current data. - * @var array - */ - var $oldarray = array(); - /** - * Array that will carry the merged info. - * @var array - */ - var $output = array(); - var $maximum; - - /** - * PHP 4 constructor. - * Sets files name and content variables - * - * @param string updatesfile, string outputfile - * @return void - */ - function ManualUpdates($newupdates, $outupdates) - { - $this->newfile = $newupdates; - $this->outfile = $outupdates; - $this->newarray = $this->_getUpdates($this->newfile); - $this->oldarray = $this->_getUpdates($this->outfile); - } - - /** - * Value checking. - * Picks up file contents. - * - * @param string filename - * @return array filecontents - */ - function _getUpdates($filename) - { - if (!is_readable($filename)) { - trigger_error('Cannot get updates from ' . $filename, E_USER_WARNING); - trigger_error($filename . ' does not exist or is not readable by this user.', E_USER_ERROR); - exit; - } - return file($filename); - } - - /** - * Merges and sorts the arrays without array_intersect_key() or even a useful timestamp <sigh /> - * Rewrite this function when we get PHP 5 and/or `date %s` - * Sets output array - * - * @param none - * @return void - */ - function mergeUpdates() { - // We want to show MAX_UPDATES_TO_SHOW or the total of - // tonight's updates, whichever is the greater. Apparently. - $this->maximum = max(MAX_UPDATES_TO_SHOW, sizeof($this->newarray)); - $merged = array_merge($this->newarray, $this->oldarray); - - // Lines aren't unique. Titles are... - foreach ($merged as $line) { - $stripped = strip_tags($line); - $titles[] = substr($stripped, strpos($stripped, ' ')); - } - - $unique = array_unique($titles); - - for ($i = 0; $i < sizeof($merged); $i++) { - if (array_key_exists($i, $unique) && sizeof($this->output) < $this->maximum) { - $this->output[] = $merged[$i]; - } else { - unset($merged[$i]); - } - } - } - - /** - * Writes the updated output array to file, the old-fashioned way - * - * @param none - * @return void - */ - function writeUpdatesToFile() - { - $fp = fopen($this->outfile, 'w'); - - foreach ($this->output as $line) { - fwrite($fp, $line); - } - fclose($fp); - } -} - -// Set the parameter vars -$newupdates = $argv[1]; -$outupdates = $argv[2]; - -// Create the current list of updates. -$mu =& new ManualUpdates($newupdates, $outupdates); -$mu->mergeUpdates(); -$mu->writeUpdatesToFile(); - -?> +#!/usr/local/bin/php +<?php + +/** + * Class for combining a current list of manual file + * updates with an existing list. + * + * Updated files should be listed as a set of HTML + * links. The resulting file is an HTML fragment + * that consists of precisely that, and nothing more. + * + * @original author Scott Mattocks + * @altered by Steph Fox to accommodate XSLT changes + */ + +// Check number of args +if ($argc < 3 || $argc > 4) { + exit("Purpose: Merge pre-formatted contents of new updates file with old updates file.\r\n" + .'Usage: merge-updates.php <new file> <old file> [ <outfile> ] ' ."\r\n" + ); +} + +// Not really max. It can be overridden by +// very busy scribblers: see mergeUpdates() +define ('MAX_UPDATES_TO_SHOW', 15); + +class ManualUpdates { + + /** + * File containing the new update data. + * @var string + */ + var $newfile; + /** + * Pre-existing updates file; can used for output. + * @var string + */ + var $oldfile; + + /** + * File to use for output. + * @var string + */ + var $outfile; + + /** + * Array that will contain the new update data. + * @var array + */ + var $newarray = array(); + /** + * Array that will contain the current data. + * @var array + */ + var $oldarray = array(); + /** + * Array that will carry the merged info. + * @var array + */ + var $output = array(); + var $maximum; + + /** + * PHP 4 constructor. + * Sets files name and content variables + * + * @param string updatesfile, string outputfile + * @return void + */ + function ManualUpdates($newupdates, $oldupdates, $outupdates) + { + $this->newfile = $newupdates; + $this->oldfile = $oldupdates; + $this->outfile = $outupdates; + $this->newarray = $this->_getUpdates($this->newfile, 1); + $this->oldarray = $this->_getUpdates($this->oldfile, 0); + } + + /** + * Value checking. + * Picks up file contents. + * + * @param string filename + * @return array filecontents + */ + function _getUpdates($filename, $new) + { + if (!is_readable($filename)) { + trigger_error('Cannot get updates from ' . $filename, E_USER_WARNING); + trigger_error($filename . ' does not exist or is not readable by this user.', E_USER_ERROR); + exit; + } + + $updates = array(); + $data = file_get_contents($filename); + preg_match_all('!^.+href="([^"]+)".+date="([^"]+)">([^<]+).*$!m', $data, $match, PREG_SET_ORDER); + foreach ($match as $m) { + $updates[$m[1]] = array($m[3], $m[2], $m[0], $new); + } + return $updates; + } + + /** + * Merges and sorts the updates arrays + * Sets output array + * + * @param none + * @return void + */ + function mergeUpdates() { + $maximum = max(MAX_UPDATES_TO_SHOW, count($this->newarray)); + $this->output = array_merge($this->oldarray, $this->newarray); + uasort($this->output, array(&$this, 'sortByDate')); + $this->output = array_slice($this->output, 0, $maximum); + return; + } + + function sortByDate($a, $b) + { + /* if dates are the same */ + if ((int)$a[1] == (int)$b[1]) { + /* and 'new' flags are the same */ + if ($a[3] == $b[3]) { + /* sort by title */ + return strcmp($a[0], $b[0]); + } else { + /* + * otherwise sort by 'new' flags so that new entries show up at + * the top + */ + return ($a[3] < $b[3]) ? 1 : -1; + } + } + + /* sort by date */ + return ((int)$a[1] < (int)$b[1]) ? 1 : -1; + } + + /** + * Writes the updated output array to file, the old-fashioned way + * + * @param none + * @return void + */ + function writeUpdatesToFile() + { + $fp = fopen($this->outfile, 'w'); + + foreach ($this->output as $line) { + fwrite($fp, $line[2]."\n"); + } + fclose($fp); + } +} + +// Set the parameter vars +$newupdates = $argv[1]; +$oldupdates = $argv[2]; +if ($argc > 3) { + $outupdates = $argv[3]; +} else { + $outupdates = $oldupdates; +} + +// Create the current list of updates. +$mu =& new ManualUpdates($newupdates, $oldupdates, $outupdates); +$mu->mergeUpdates(); +$mu->writeUpdatesToFile(); + +?>