Write language files atomically
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 8 Sep 2014 16:13:13 +0000 (18:13 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 8 Sep 2014 16:27:32 +0000 (18:27 +0200)
wcfsetup/install/files/lib/data/language/LanguageEditor.class.php

index 476e2d7ffd290cce9c4a03c046e797822ced9718..92611f3b767ee66b8d8ee4d550faaa22beae03a7 100644 (file)
@@ -9,7 +9,7 @@ use wcf\data\IEditableCachedObject;
 use wcf\system\cache\builder\LanguageCacheBuilder;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\exception\SystemException;
-use wcf\system\io\File;
+use wcf\system\io\AtomicWriter;
 use wcf\system\language\LanguageFactory;
 use wcf\system\Regex;
 use wcf\system\WCF;
@@ -85,22 +85,27 @@ class LanguageEditor extends DatabaseObjectEditor implements IEditableCachedObje
                                continue;
                        }
                        
-                       $content = "<?php\n/**\n* WoltLab Community Framework\n* language: ".$this->languageCode."\n* encoding: UTF-8\n* category: ".$category->languageCategory."\n* generated at ".gmdate("r")."\n* \n* DO NOT EDIT THIS FILE\n*/\n";
+                       $filename = WCF_DIR.'language/'.$this->languageID.'_'.$category->languageCategory.'.php';
+                       $writer = new AtomicWriter($filename);
+                       
+                       $writer->write("<?php\n/**\n* WoltLab Community Framework\n* language: ".$this->languageCode."\n* encoding: UTF-8\n* category: ".$category->languageCategory."\n* generated at ".gmdate("r")."\n* \n* DO NOT EDIT THIS FILE\n*/\n");
                        foreach ($languageItems as $languageItem => $languageItemValue) {
-                               $content .= "\$this->items['".$languageItem."'] = '".str_replace("'", "\'", $languageItemValue)."';\n";
+                               $writer->write("\$this->items['".$languageItem."'] = '".str_replace("'", "\'", $languageItemValue)."';\n");
                                
                                // compile dynamic language variables
                                if ($category->languageCategory != 'wcf.global' && strpos($languageItemValue, '{') !== false) {
+                                       $writer->write("\$this->dynamicItems['".$languageItem."'] = '");
+                                       
                                        $output = LanguageFactory::getInstance()->getScriptingCompiler()->compileString($languageItem, $languageItemValue);
-                                       $content .= "\$this->dynamicItems['".$languageItem."'] = '".str_replace("'", "\'", $output['template'])."';\n";
+                                       $writer->write(str_replace("'", "\'", $output['template']));
+                                       
+                                       $writer->write("';\n");
                                }
                        }
                        
-                       $filename = WCF_DIR.'language/'.$this->languageID.'_'.$category->languageCategory.'.php';
-                       $file = new File($filename);
+                       $writer->flush();
+                       $writer->close();
                        FileUtil::makeWritable($filename);
-                       $file->write($content . '?>');
-                       $file->close();
                }
        }