# HG changeset patch # User Dan # Date 1226779349 18000 # Node ID 04c2f743c6ec542e88e1d4d8612b62791cabbeaf # Parent d86ea89358ec1c98b225baa0d1ee0f121650eedc Added script for importing stats databases diff -r d86ea89358ec -r 04c2f743c6ec libprogress.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libprogress.php Sat Nov 15 15:02:29 2008 -0500 @@ -0,0 +1,311 @@ +bar_left = $bar_left; + $this->bar_right = $bar_right; + $this->color_bar = $this->color_to_code($color_bar); + $this->color_empty = $this->color_to_code($color_empty); + $this->color_text = $this->color_to_code($color_text); + $this->color_emptytext = $this->color_to_code($color_emptytext); + + if ( isset($_SERVER['COLUMNS']) ) + { + $this->term_width = intval($_SERVER['COLUMNS']); + } + $this->bar_width = $this->term_width - strlen($this->bar_left) - strlen($this->bar_right); + + $this->update_text_quiet($bar_text); + } + + /** + * Updates the text on the progress bar and recalculates the position without redrawing. + * @param string Text in the bar. If omitted, blanked. + */ + + public function update_text_quiet($bar_text = '') + { + $this->bar_text = strval($bar_text); + + if ( !empty($this->bar_text) ) + { + $this->text_pos = round(( $this->bar_width / 2 ) - ( strlen($this->bar_text) / 2 )); + } + } + + /** + * Updates the text on the progress bar, recalculates the position, and redraws. + * @param string Text in the bar. If omitted, blanked. + */ + + function update_text($bar_text = '') + { + $this->update_text_quiet($bar_text); + $this->set($this->bar_pos); + } + + /** + * Starts output of the bar. + */ + + function start() + { + echo self::CARRIAGE_RETURN; + echo $this->bar_left; + } + + /** + * Closes the bar. + */ + + function end() + { + $this->set($this->bar_pos, $this->bar_width); + echo "\n"; + } + + /** + * Sets the position of the bar. + * @param int Position in %. If a second parameter is set, this is treated as a numerator with the second parameter being the denominator and that is used to calculate position. + * @param int Optional. Total number of units to allow fraction usage instead of percentage. + */ + + function set($pos, $max = 100) + { + // if our pos is higher than 100%, reduce it + if ( $pos > $max ) + $pos = $max; + + // arithmetic one-liner + // this is where we should stop showing the "full" color and instead use "empty" + $bar_pos = round($this->bar_width * ( $pos / $max )); + $this->bar_pos = 100 * ( $pos / $max ); + + // reset the cursor + echo self::CARRIAGE_RETURN . $this->bar_left; + + // print everything out + for ( $i = 0; $i < $this->bar_width; $i++ ) + { + $char = ' '; + $hide = true; + if ( !empty($this->bar_text) ) + { + // we have some text to display in the middle; see where we are. + $show_text = ( $i >= $this->text_pos && $i < ( $this->text_pos + strlen($this->bar_text) ) ); + if ( $show_text ) + { + $char = substr($this->bar_text, $i - $this->text_pos, 1); + if ( strlen($char) < 1 ) + $char = ' '; + else + $hide = false; + } + } + // determine color + if ( $i > $bar_pos ) + { + $hide ? $this->set_color_empty_hide() : $this->set_color_empty_show(); + } + else + { + $hide ? $this->set_color_full_hide() : $this->set_color_full_show(); + } + echo $char; + } + $this->set_color_reset(); + echo $this->bar_right; + } + + # + # PRIVATE METHODS + # + + function set_color_full_hide() + { + if ( $this->color_state == self::COLOR_STATE_FULL_HIDE ) + return; + $this->color_state = self::COLOR_STATE_FULL_HIDE; + + $fgcolor = 30 + $this->color_bar; + $bgcolor = $fgcolor + 10; + echo self::SHELL_ESCAPE . "[0;{$fgcolor};{$bgcolor};8m"; + } + + function set_color_full_show() + { + if ( $this->color_state == self::COLOR_STATE_FULL_SHOW ) + return; + $this->color_state = self::COLOR_STATE_FULL_SHOW; + + $fgcolor = 30 + $this->color_text; + $bgcolor = 40 + $this->color_bar; + echo self::SHELL_ESCAPE . "[0;1;{$fgcolor};{$bgcolor}m"; + } + + function set_color_empty_hide() + { + if ( $this->color_state == self::COLOR_STATE_EMPTY_HIDE ) + return; + $this->color_state = self::COLOR_STATE_EMPTY_HIDE; + + $fgcolor = 30 + $this->color_empty; + $bgcolor = $fgcolor + 10; + echo self::SHELL_ESCAPE . "[0;{$fgcolor};{$bgcolor};8m"; + } + + function set_color_empty_show() + { + if ( $this->color_state == self::COLOR_STATE_EMPTY_SHOW ) + return; + $this->color_state = self::COLOR_STATE_EMPTY_SHOW; + + $fgcolor = 30 + $this->color_emptytext; + $bgcolor = 40 + $this->color_empty; + echo self::SHELL_ESCAPE . "[0;1;{$fgcolor};{$bgcolor}m"; + } + + function set_color_reset() + { + if ( $this->color_state == self::COLOR_STATE_RESET ) + return; + $this->color_state = self::COLOR_STATE_RESET; + + echo self::SHELL_ESCAPE . "[0m"; + } + + /** + * Converts a color name to an ASCII color code. Valid color names are black, red, green, yellow, blue, magenta, cyan, and white. + * @param string Color name + * @return int + */ + + private function color_to_code($color) + { + static $colors = array( + 'black' => 0, + 'red' => 1, + 'green' => 2, + 'yellow' => 3, + 'blue' => 4, + 'magenta' => 5, + 'cyan' => 6, + 'white' => 7 + ); + return ( isset($colors[$color]) ) ? $colors[$color] : $colors['white']; + } +} diff -r d86ea89358ec -r 04c2f743c6ec stats/import-stats.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stats/import-stats.php Sat Nov 15 15:02:29 2008 -0500 @@ -0,0 +1,53 @@ +start(); +$pbar->set(0); +$i = 0; +foreach ( $filelist as $dh ) +{ + $pbar->update_text_quiet($dh); + $pbar->set(++$i, count($filelist)); + require($dh); + foreach ( $stats_data['messages'] as $channel => &$data ) + { + foreach ( $data as &$message ) + { + stats_log_message($channel, $message['nick'], $message['time']); + } + } + if ( isset($stats_data['anonymous']) ) + { + foreach ( $stats_data['anonymous'] as $user => $_ ) + { + stats_anonymize_user_now($user); + } + } +} + +$pbar->end(); +