jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/359984 )

Change subject: Keep short array syntax after run of magic-export.php
......................................................................


Keep short array syntax after run of magic-export.php

alias files were changed to use short array syntax.
A export from translatewiki.net should not revert these changes.

This patch set changed the export script to avoid difference like this,
also looks for new line at the end or the ignore phpcs tag

Change-Id: I5f2d115f023e62092240fd402cb762cf4077fc78
---
M scripts/magic-export.php
1 file changed, 78 insertions(+), 20 deletions(-)

Approvals:
  jenkins-bot: Verified
  Nikerabbit: Looks good to me, approved



diff --git a/scripts/magic-export.php b/scripts/magic-export.php
index 9e97027..76f6557 100644
--- a/scripts/magic-export.php
+++ b/scripts/magic-export.php
@@ -26,6 +26,7 @@
 
        protected $handles = [];
        protected $messagesOld = [];
+       protected $extraInformation = [];
 
        public function __construct() {
                parent::__construct();
@@ -100,6 +101,12 @@
 
                        $inFile = $group->replaceVariables( $inFile, 'en' );
                        $outFile =  $this->target . '/' . $outFile;
+                       $varName = null;
+
+                       if ( !is_readable( $inFile ) ) {
+                               $this->error( "File '$inFile' not readable." );
+                               continue;
+                       }
 
                        include $inFile;
                        switch ( $this->type ) {
@@ -107,9 +114,11 @@
                                        if ( isset( $aliases ) ) {
                                                
$this->messagesOld[$group->getId()] = $aliases;
                                                unset( $aliases );
+                                               $varName = '$aliases';
                                        } elseif ( isset( $specialPageAliases ) 
) {
                                                
$this->messagesOld[$group->getId()] = $specialPageAliases;
                                                unset( $specialPageAliases );
+                                               $varName = 
'$specialPageAliases';
                                        } else {
                                                $this->error( "File '$inFile' 
does not contain an aliases array." );
                                                continue;
@@ -122,51 +131,83 @@
                                        }
                                        $this->messagesOld[$group->getId()] = 
$magicWords;
                                        unset( $magicWords );
+                                       $varName = '$magicWords';
                                        break;
                        }
 
                        wfMkdirParents( dirname( $outFile ), null, __METHOD__ );
                        $this->handles[$group->getId()] = fopen( $outFile, 'w' 
);
-                       fwrite( $this->handles[$group->getId()], 
$this->readHeader( $inFile ) );
+                       $headerInformation = $this->readHeader( $inFile, 
$varName );
+                       fwrite( $this->handles[$group->getId()], 
$headerInformation['fileBegin'] );
+                       $this->extraInformation[$group->getId()] = 
$headerInformation;
 
                        $this->output( "\t{$group->getId()}\n" );
                }
        }
 
-       protected function readHeader( $file ) {
+       protected function readHeader( $file, $varName ) {
                $data = file_get_contents( $file );
 
                // Seek first '*/'.
-               $end = strpos( $data, '*/' ) + 2;
+               $end = strpos( $data, '*/' );
 
-               if ( $end === false ) {
-                       return "<?php\n";
+               // But not when it is the english comment
+               $varPos = strpos( $data, $varName );
+               if ( $varPos && $end && $varPos <= $end ) {
+                       $end = false;
                }
 
-               // Grab header.
-               return substr( $data, 0, $end );
+               if ( $end === false ) {
+                       $fileBegin = "<?php\n";
+               } else {
+                       // Grab header.
+                       $fileBegin = substr( $data, 0, $end + 2 );
+               }
+
+               // preserve the phpcs codingStandardsIgnoreFile, if exists
+               $preserveIgnoreTag = strpos( $data, 
'@codingStandardsIgnoreFile' ) !== false;
+
+               // preserve the long array syntax, if varName is written with it
+               $preserveLongArraySyntax = preg_match(
+                       '/' . preg_quote( $varName ) . 
'\s*=\s*array\s*\(\s*\)\s*;/',
+                       $data
+               );
+
+               // avoid difference by the last character
+               $preserveNewlineAtEnd = substr( $data, -1 ) === "\n";
+
+               return [
+                       'fileBegin' => $fileBegin,
+                       'preserveIgnoreTag' => $preserveIgnoreTag,
+                       'preserveLongArraySyntax' => $preserveLongArraySyntax,
+                       'preserveNewlineAtEnd' => $preserveNewlineAtEnd,
+               ];
        }
 
        /**
         * Write the opening of the files for each output file handle.
         */
        protected function writeHeaders() {
-               foreach ( $this->handles as $handle ) {
+               foreach ( $this->handles as $group => $handle ) {
+                       $arraySyntax = 
$this->extraInformation[$group]['preserveLongArraySyntax']
+                               ? 'array()'
+                               : '[]';
                        switch ( $this->type ) {
                                case 'special':
+                                       $ignoreTag = 
$this->extraInformation[$group]['preserveIgnoreTag']
+                                               ? "\n// 
@codingStandardsIgnoreFile"
+                                               : '';
                                        fwrite( $handle, <<<PHP
+$ignoreTag
 
-// @codingStandardsIgnoreFile
-
-\$specialPageAliases = array();
+\$specialPageAliases = $arraySyntax;
 PHP
                                        );
                                        break;
                                case 'magic':
                                        fwrite( $handle, <<<PHP
 
-
-\$magicWords = array();
+\$magicWords = $arraySyntax;
 PHP
                                        );
                                        break;
@@ -241,6 +282,13 @@
                                                $messagesOut[$key] = 
$this->messagesOld[$group][$l][$key];
                                        }
                                }
+                               if ( 
$this->extraInformation[$group]['preserveLongArraySyntax'] ) {
+                                       $arrayStart = 'array(';
+                                       $arrayEnd = ')';
+                               } else {
+                                       $arrayStart = '[';
+                                       $arrayEnd = ']';
+                               }
 
                                // If there are messages to write, write them.
                                if ( count( $messagesOut ) > 0 ) {
@@ -248,35 +296,39 @@
                                        switch ( $this->type ) {
                                                case 'special':
                                                        $out .= "\n\n/** 
{$namesEn[$l]} ({$namesNative[$l]}) " .
-                                                               
"*/\n\$specialPageAliases['{$l}'] = array(\n";
+                                                               
"*/\n\$specialPageAliases['{$l}'] = {$arrayStart}\n";
                                                        break;
                                                case 'magic':
                                                        $out .= "\n\n/** 
{$namesEn[$l]} ({$namesNative[$l]}) *" .
-                                                               
"/\n\$magicWords['{$l}'] = array(\n";
+                                                               
"/\n\$magicWords['{$l}'] = {$arrayStart}\n";
                                                        break;
                                        }
                                        foreach ( $messagesOut as $key => 
$translations ) {
+                                               if ( !is_array( $translations ) 
) {
+                                                       $this->error( "$l in 
$group has not an array..." );
+                                                       continue;
+                                               }
                                                foreach ( $translations as $id 
=> $translation ) {
                                                        $translations[$id] = 
addslashes( $translation );
-                                                       if ( $this->type === 
'magic' && $translation == 0 ) {
+                                                       if ( $this->type === 
'magic' && $translation === 0 ) {
                                                                unset( 
$translations[$id] );
                                                        }
                                                }
                                                $translations = implode( "', 
'", $translations );
                                                switch ( $this->type ) {
                                                        case 'special':
-                                                               $out .= 
"\t'$key' => array( '$translations' ),\n";
+                                                               $out .= 
"\t'$key' => $arrayStart '$translations' $arrayEnd,\n";
                                                                break;
                                                        case 'magic':
                                                                if ( 
$this->messagesOld[$group]['en'][$key][0] === 0 ) {
-                                                                       $out .= 
"\t'$key' => array( 0, '$translations' ),\n";
+                                                                       $out .= 
"\t'$key' => $arrayStart 0, '$translations' $arrayEnd,\n";
                                                                } else {
-                                                                       $out .= 
"\t'$key' => array( '$translations' ),\n";
+                                                                       $out .= 
"\t'$key' => $arrayStart '$translations' $arrayEnd,\n";
                                                                }
                                                                break;
                                                }
                                        }
-                                       $out .= ');';
+                                       $out .= "$arrayEnd;";
                                        fwrite( $handle, $out );
                                }
                        }
@@ -288,6 +340,12 @@
         */
        protected function writeFooters() {
                $this->output( "Writing file footers...\n" );
+               foreach ( $this->handles as $group => $handle ) {
+                       if ( 
$this->extraInformation[$group]['preserveNewlineAtEnd'] ) {
+                               // php files should end with a newline
+                               fwrite( $handle, "\n" );
+                       }
+               }
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/359984
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I5f2d115f023e62092240fd402cb762cf4077fc78
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Raimond Spekking <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Umherirrender <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to