r60976 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60975‎ | r60976 | r60977 >
Date:19:06, 12 January 2010
Author:nikerabbit
Status:resolved (Comments)
Tags:
Comment:
Channeled output support for maintenance scripts.

Channeled output (for the lack of better name) allows outputting stuff into same line, channel, so that each channel change starts a new line.
Modified paths:
  • /trunk/phase3/maintenance/Maintenance.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/Maintenance.php
@@ -98,6 +98,7 @@
9999 */
100100 public function __construct() {
101101 $this->addDefaultParams();
 102+ register_shutdown_function( array( $this, 'outputChanneled' ), false );
102103 }
103104
104105 /**
@@ -214,14 +215,14 @@
215216 * Throw some output to the user. Scripts can call this with no fears,
216217 * as we handle all --quiet stuff here
217218 * @param $out String The text to show to the user
 219+ * @param $channel Mixed Unique identifier for the channel. See function outputChanneled.
218220 */
219 - protected function output( $out ) {
 221+ protected function output( $out, $channel = null ) {
220222 if( $this->mQuiet ) {
221223 return;
222224 }
223 - $f = fopen( 'php://stdout', 'w' );
224 - fwrite( $f, $out );
225 - fclose( $f );
 225+ $out = preg_replace( '/\n\z/', '', $out );
 226+ $this->outputChanneled( $out, $channel );
226227 }
227228
228229 /**
@@ -231,6 +232,7 @@
232233 * @param $die boolean If true, go ahead and die out.
233234 */
234235 protected function error( $err, $die = false ) {
 236+ $this->outputChanneled( false );
235237 if ( php_sapi_name() == 'cli' ) {
236238 fwrite( STDERR, $err . "\n" );
237239 } else {
@@ -241,7 +243,46 @@
242244 if( $die ) die();
243245 }
244246
 247+ private $atLineStart = true;
 248+ private $lastChannel = null;
 249+
245250 /**
 251+ * Message outputter with channeled message support. Messages on the
 252+ * same channel are concatenated, but any intervening messages in another
 253+ * channel start a new line.
 254+ * @param $msg String The message without trailing newline
 255+ * @param $channel Channel identifier or null for no channel. Channel comparison uses ===.
 256+ */
 257+ public function outputChanneled( $msg, $channel = null ) {
 258+ $handle = fopen( 'php://stdout', 'w' );
 259+
 260+ if ( $msg === false ) {
 261+ // For cleanup
 262+ if ( !$this->atLineStart ) fwrite( $handle, "\n" );
 263+ fclose( $handle );
 264+ return;
 265+ }
 266+
 267+ // End the current line if necessary
 268+ if ( !$this->atLineStart && $channel !== $this->lastChannel ) {
 269+ fwrite( $handle, "\n" );
 270+ }
 271+
 272+ fwrite( $handle, $msg );
 273+
 274+ $this->atLineStart = false;
 275+ if ( $channel === null ) {
 276+ // For unchanneled messages, output trailing newline immediately
 277+ fwrite( $handle, "\n" );
 278+ $this->atLineStart = true;
 279+ }
 280+ $this->lastChannel = $channel;
 281+
 282+ // Cleanup handle
 283+ fclose( $handle );
 284+ }
 285+
 286+ /**
246287 * Does the script need different DB access? By default, we give Maintenance
247288 * scripts normal rights to the DB. Sometimes, a script needs admin rights
248289 * access for a reason and sometimes they want no access. Subclasses should

Follow-up revisions

RevisionCommit summaryAuthorDate
r61169Avoid r60976 breakage of unexpected line breaks inside maybeHelpplatonides15:35, 17 January 2010

Comments

#Comment by Platonides (talk | contribs)   15:36, 17 January 2010

This inserts new lines where it shouldn't (see --help). It probably shouldn't be placing a new line on $channel === null

#Comment by Nikerabbit (talk | contribs)   16:33, 17 January 2010

--help for what script?

#Comment by Platonides (talk | contribs)   17:35, 17 January 2010

Any script, since --help is handled at Maintenance.php I found it while reviewing getText.php Look at r61169. maybeHelp() called output() with partial lines. The extra new lines inserted here broke the output. This is changing the meaning of output(), so instead of fixing the callers it's better to fix the callee.

#Comment by Nikerabbit (talk | contribs)   21:26, 17 January 2010

Since the interface was inconsistent (::error appends \n but ::output doesn't), and because it is easy to fix callers to use a channel, I'd fix the callers instead of hacking callee. It was of course my oversight not to do that in the first place.

Status & tagging log