r70553 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70552‎ | r70553 | r70554 >
Date:11:14, 6 August 2010
Author:reedy
Status:deferred (Comments)
Tags:
Comment:
* (bug 24553) Give stylize a path ignore pattern

Using MIT licensed args handling (really should be using Maintenance, but it's in a different tree, which people might not have... And it doesn't really make sense for the file to be in phase3/maintenance imho)

If anyones got any sane ideas about the above, I'll happily follow this up and change it

Also expose backup option to paramaters

Add --help option too
Modified paths:
  • /trunk/tools/code-utils/stylize.php (modified) (history)

Diff [purge]

Index: trunk/tools/code-utils/stylize.php
@@ -17,27 +17,48 @@
1818 array_shift( $argv );
1919
2020 if ( count( $argv ) ) {
21 - foreach ( $argv as $dirOrFile ) {
 21+ $args = new Args( $argv );
 22+
 23+ if ( $args->flag( 'help' ) ) {
 24+ echo "Usage: php createAndPromote.php [--backup|--help|--ignore=<regextoexclude>] <files/directories>
 25+ backup : Creates a backup of modified files
 26+ help : This message!
 27+ ignore : Regex of files not to stylize e.g. .*\.i18n\.php
 28+ <files/directories> : Files/directories to stylize
 29+";
 30+
 31+ return;
 32+ }
 33+
 34+ $ignore = $args->flag( 'ignore' );
 35+ $backup = $args->flag( 'backup' );
 36+
 37+ foreach ( $args->args as $dirOrFile ) {
2238 if ( is_dir( $dirOrFile ) ) {
23 - stylize_recursively( $dirOrFile );
 39+ stylize_recursively( $dirOrFile, $ignore, $backup );
2440 } else {
25 - stylize_file( $dirOrFile );
 41+ stylize_file( $dirOrFile, $backup );
2642 }
2743 }
2844 } else {
2945 stylize_file( '-' );
3046 }
3147
32 -function stylize_recursively( $dir ) {
 48+function stylize_recursively( $dir, $ignore = false, $backup = false ) {
3349 $dir = trim( $dir, "\/" );
3450
3551 foreach ( glob( "$dir/*" ) as $dirOrFile ) {
 52+ if ( $ignore && preg_match( '/' . $ignore . '$/', $dirOrFile ) ) {
 53+ echo "Ignoring $dirOrFile\n";
 54+ continue;
 55+ }
 56+
3657 if ( is_dir( $dirOrFile ) ) { // It's a directory, so call this function again.
37 - stylize_recursively( $dirOrFile );
 58+ stylize_recursively( $dirOrFile, $ignore, $backup );
3859 } elseif ( is_file( $dirOrFile ) ) { // It's a file, so let's stylize it.
3960 // Only stylize php and js files, omitting minified js files.
4061 if ( preg_match( '/\.(php|php5|js)$/', $dirOrFile ) && !preg_match( '/\.(min\.js)$/', $dirOrFile ) ) {
41 - stylize_file( $dirOrFile, false );
 62+ stylize_file( $dirOrFile, $backup );
4263 }
4364 }
4465 }
@@ -304,3 +325,76 @@
305326 return preg_replace( "#[\t ]*\n#", "\n", $s );
306327 }
307328 }
 329+
 330+/**
 331+ * From
 332+ * http://code.google.com/p/tylerhall/source/browse/trunk/class.args.php
 333+ * http://clickontyler.com/blog/2008/11/parse-command-line-arguments-in-php/ - MIT License
 334+
 335+ Copyright (c) 2008 Tyler Hall
 336+
 337+ Permission is hereby granted, free of charge, to any person obtaining a copy
 338+ of this software and associated documentation files (the "Software"), to deal
 339+ in the Software without restriction, including without limitation the rights
 340+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 341+ copies of the Software, and to permit persons to whom the Software is
 342+ furnished to do so, subject to the following conditions:
 343+
 344+ The above copyright notice and this permission notice shall be included in
 345+ all copies or substantial portions of the Software.
 346+
 347+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 348+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 349+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 350+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 351+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 352+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 353+ THE SOFTWARE
 354+ */
 355+class Args {
 356+ private $flags;
 357+ public $args;
 358+
 359+ public function __construct( $argv ) {
 360+ $this->flags = array();
 361+ $this->args = array();
 362+
 363+ for ( $i = 0; $i < count( $argv ); $i++ ) {
 364+ $str = $argv[$i];
 365+
 366+ // --foo
 367+ if ( strlen( $str ) > 2 && substr( $str, 0, 2 ) == '--' ) {
 368+ $str = substr( $str, 2 );
 369+ $parts = explode( '=', $str );
 370+ $this->flags[$parts[0]] = true;
 371+
 372+ // Does not have an =, so choose the next arg as its value
 373+ if ( count( $parts ) == 1 && isset( $argv[$i + 1] ) && preg_match( '/^--?.+/', $argv[$i + 1] ) == 0 ) {
 374+ $this->flags[$parts[0]] = $argv[$i + 1];
 375+ } elseif ( count( $parts ) == 2 ) { // Has a =, so pick the second piece
 376+ $this->flags[$parts[0]] = $parts[1];
 377+ }
 378+ } elseif ( strlen( $str ) == 2 && $str[0] == '-' ) { // -a
 379+ $this->flags[$str[1]] = true;
 380+ if ( isset( $argv[$i + 1] ) && preg_match( '/^--?.+/', $argv[$i + 1] ) == 0 )
 381+ $this->flags[$str[1]] = $argv[$i + 1];
 382+ } elseif ( strlen( $str ) > 1 && $str[0] == '-' ) { // -abcdef
 383+ for ( $j = 1; $j < strlen( $str ); $j++ )
 384+ $this->flags[$str[$j]] = true;
 385+ }
 386+ }
 387+
 388+ for ( $i = count( $argv ) - 1; $i >= 0; $i-- ) {
 389+ if ( preg_match( '/^--?.+/', $argv[$i] ) == 0 )
 390+ $this->args[] = $argv[$i];
 391+ else
 392+ break;
 393+ }
 394+
 395+ $this->args = array_reverse( $this->args );
 396+ }
 397+
 398+ public function flag( $name ) {
 399+ return isset( $this->flags[$name] ) ? $this->flags[$name] : false;
 400+ }
 401+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r71737Followup r70553, fixup whitespace on help...reedy20:10, 26 August 2010
r71802Fix script name in usage, from r70553...simetrical20:12, 27 August 2010

Comments

#Comment by Simetrical (talk | contribs)   20:19, 8 August 2010

IMO, this can be in maintenance/. Why not?

#Comment by Platonides (talk | contribs)   20:28, 8 August 2010

Anyone having checked tools/code-utils will also have checked phase3.

#Comment by Simetrical (talk | contribs)   19:28, 26 August 2010

In addition to using an incompatible argument-parsing library . . . the output of --help is a little weird.  :)

#Comment by Simetrical (talk | contribs)   20:13, 27 August 2010

I meant the fact that it says "createAndPromote.php", not the whitespace. Oh well, I fixed it myself in r71802.

Status & tagging log