r63718 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63717‎ | r63718 | r63719 >
Date:00:44, 14 March 2010
Author:jeroendedauw
Status:resolved (Comments)
Tags:
Comment:
Added suport for stylizing directories recursivly
Modified paths:
  • /trunk/tools/code-utils/stylize.php (modified) (history)

Diff [purge]

Index: trunk/tools/code-utils/stylize.php
@@ -3,23 +3,46 @@
44 /**
55 * A PHP code beautifier aimed at adding lots of spaces to files that lack them,
66 * in keeping with MediaWiki's spacey site style.
 7+ *
 8+ * @author tstarling
 9+ * @author Jeroen De Dauw
710 */
811
912
1013 if ( php_sapi_name() != 'cli' ) {
11 - print "This script must be run from the command line\n";
 14+ echo "This script must be run from the command line\n";
1215 exit( 1 );
1316 }
 17+
1418 array_shift( $argv );
 19+
1520 if ( count( $argv ) ) {
16 - foreach ( $argv as $filename ) {
17 - stylize_file( $filename );
 21+ foreach ( $argv as $dirOrFile ) {
 22+ if ( is_dir( $dirOrFile ) ) {
 23+ stylize_recursivly( $dirOrFile );
 24+ } else {
 25+ stylize_file( $dirOrFile );
 26+ }
1827 }
1928 } else {
2029 stylize_file( '-' );
2130 }
2231
23 -function stylize_file( $filename ) {
 32+function stylize_recursivly( $dir ) {
 33+ foreach ( glob("$dir/*") as $dirOrFile ) {
 34+ if ( is_dir( $dirOrFile ) ) { // It's a directory, so call this function again.
 35+ stylize_recursivly( $dirOrFile );
 36+ } elseif ( is_file( $dirOrFile ) ) { // It's a file, so let's stylize it.
 37+ // Only stylize php and js files, omitting minified js files.
 38+ if ( preg_match( '/\.(php|php5|js)$/', $dirOrFile ) && !preg_match( '/\.(min\.js)$/', $dirOrFile ) ) {
 39+ stylize_file( $dirOrFile, false );
 40+ }
 41+ }
 42+ }
 43+}
 44+
 45+function stylize_file( $filename, $backup = true ) {
 46+ echo "Stylizing file $filename\n";
2447 if ( $filename == '-' ) {
2548 $s = file_get_contents( '/dev/stdin' );
2649 if ( $s === false ) {
@@ -35,7 +58,7 @@
3659 }
3760 $stylizer = new Stylizer( $s );
3861 $s = $stylizer->stylize();
39 - rename( $filename, "$filename~" );
 62+ if ( $backup ) rename( $filename, "$filename~" );
4063 file_put_contents( $filename, $s );
4164 }
4265 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r63736Fix r63718 and r63719: correct spelling of "stylize_recursivly()", fix syntax...catrope12:44, 14 March 2010
r68066Trim trailing / or \ from path to prettify output, followup r63718...reedy12:33, 15 June 2010

Comments

#Comment by Reedy (talk | contribs)   20:32, 17 April 2010

The recursive version eats memory

Running against the extensions dir, with 32mb of memory, it did 4 files, 64mb it did 16

Also, on recursive version, path shows wrongly if input is appended with /

reedy@ubuntu64-esxi:~$ php mediawiki/trunk/tools/code-utils/stylize.php mediawiki/trunk/extensions/ Stylizing file mediawiki/trunk/extensions//ABC/ABC.i18n.php Stylizing file mediawiki/trunk/extensions//ABC/ABC.php

reedy@ubuntu64-esxi:~$ php mediawiki/trunk/tools/code-utils/stylize.php mediawiki/trunk/extensions Stylizing file mediawiki/trunk/extensions/ABC/ABC.i18n.php Stylizing file mediawiki/trunk/extensions/ABC/ABC.php


reedy@ubuntu64-esxi:~$ php mediawiki/trunk/tools/code-utils/stylize.php mediawiki/trunk/phase3/index.php Stylizing file mediawiki/trunk/phase3/index.php

#Comment by Jeroen De Dauw (talk | contribs)   20:38, 17 April 2010

Never looked at memory usage, just added this stuff and updated a bunch of extensions. Feel free to fix issues tho. I'll have a look when I need the script again, but that can take quite a while.

#Comment by Reedy (talk | contribs)   22:20, 17 April 2010

Will have a poke if I get a chance

Status & tagging log