Kit Lo | 6d440cc | 2013-03-02 19:56:45 -0500 | [diff] [blame] | 1 | <?php |
| 2 | /******************************************************************************* |
| 3 | * Copyright (c) 2013 IBM Corporation and others. |
| 4 | * All rights reserved. This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Kit Lo (IBM) - [402215] Extract Orion JavaScript files for translation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | error_reporting(E_ALL); ini_set("display_errors", true); |
| 14 | |
| 15 | header("Content-type: text/plain"); |
| 16 | include("../../html/global.php"); |
| 17 | InitPage(""); |
| 18 | |
| 19 | $headless = 1; |
| 20 | |
| 21 | # See http://wiki.eclipse.org/Babel_/_Large_Contribution_Import_Process |
| 22 | # |
| 23 | # !! IMPORTANT !! |
| 24 | # Set to 1 unless the translations were authored (and tested/reviewed in context) by professionals |
| 25 | # This doesn't mean all incoming translations will be fuzzy -- |
| 26 | # only those that are 'replacing' a non-fuzzy one |
| 27 | $fuzzy = 1; |
| 28 | |
| 29 | require(dirname(__FILE__) . "/../file/file.class.php"); |
| 30 | require_once("/home/data/httpd/babel.eclipse.org/html/json_encode.php"); |
| 31 | |
| 32 | $pageTitle = "Babel - Import Translation archive"; |
| 33 | $pageKeywords = "import,properties,translation,language,nlpack,pack,eclipse,babel"; |
| 34 | |
| 35 | $USER = getGenieUser()->userid; |
| 36 | |
| 37 | # TODO |
| 38 | $indir = "/tmp/tmp-babel/import"; |
| 39 | chdir($indir); |
| 40 | # sub-structure: ./XX/eclipse/plugins/ where XX is the iso code for the language |
| 41 | exec('find . -type f', $lines); |
| 42 | |
| 43 | # loop through files |
| 44 | foreach ($lines as $line) { |
| 45 | $line = trim($line); |
| 46 | # echo $line . "\n"; |
| 47 | if(preg_match("/^\.\/([a-zA-Z0-9._-]+)\/([0-9.]+)\/([a-zA-Z0-9_-]+)\/(.+\.js)$/", $line, $matches)) { |
| 48 | $PROJECT_ID = $matches[1]; |
| 49 | $VERSION = $matches[2]; |
| 50 | $language = $matches[3]; |
| 51 | $file = $matches[4]; |
| 52 | |
| 53 | $file_id = 0; |
| 54 | $language_id = 0; |
| 55 | |
| 56 | $SQL = "SELECT F.file_id, L.language_id |
| 57 | FROM files AS F, languages AS L WHERE F.is_active = 1 |
| 58 | AND F.project_id = '" . $PROJECT_ID . "' AND F.version = '" . $VERSION . "' |
| 59 | AND F.name LIKE '%" . $file . "' AND L.iso_code = '" . $language . "'"; |
| 60 | $rs = mysql_query($SQL, $dbh); |
| 61 | if($myrow = mysql_fetch_assoc($rs)) { |
| 62 | $file_id = $myrow['file_id']; |
| 63 | $language_id = $myrow['language_id']; |
| 64 | # echo " Found file: " . $file_id . "\n"; |
| 65 | } |
| 66 | |
| 67 | if($file_id > 0 && $language_id > 0) { |
| 68 | # Get the file contents |
| 69 | $fh = fopen($line, 'r'); |
| 70 | $size = filesize($line); |
| 71 | |
| 72 | # echo $file . " - file size: " . $size . " language: " . $language . "\n"; |
| 73 | $content = fread($fh, $size); |
| 74 | # echo $content . "<br/>"; |
| 75 | fclose($fh); |
| 76 | $file_contents = ereg_replace("\r\n?", "\n", $content); |
| 77 | $file_contents = preg_replace("/NON-NLS-(.*)/", "", $file_contents); |
| 78 | $file_contents = preg_replace("/\\/\\/\\$/", "", $file_contents); |
| 79 | $file_contents = preg_replace("/((.*?(\n))+.*?)define\(/", "define(", $file_contents); |
| 80 | $file_contents = preg_replace("/define\(((.*?(\n))+.*?)\)\;/", "$1", $file_contents); |
| 81 | $jsons = new Services_JSON(); |
| 82 | $lines = $jsons->decode($file_contents); |
| 83 | foreach($lines as $key => $value) { |
| 84 | # Get the matching string name |
| 85 | $SQL = "SELECT s.string_id, s.value, tr.value as tr_last, tr.possibly_incorrect as tr_last_fuzzy, trv.value as ever_tr_value |
| 86 | FROM strings as s |
| 87 | left join translations as tr on (s.string_id = tr.string_id |
| 88 | and tr.language_id = $language_id |
| 89 | and tr.is_active) |
| 90 | left join translations as trv on (s.string_id = trv.string_id |
| 91 | and trv.language_id = $language_id |
| 92 | and trv.value = '" . addslashes(unescape($value)) . "') |
| 93 | WHERE s.is_active = 1 AND s.non_translatable <> 1 AND s.file_id = " . $file_id . " AND s.name = '" . $key . "'"; |
| 94 | $rs_string = mysql_query($SQL, $dbh); |
| 95 | if ($rs_string) { |
| 96 | $myrow_string = mysql_fetch_assoc($rs_string); |
| 97 | if($myrow_string['string_id'] > 0 # There is an English string |
| 98 | && $value != "" # With a non-null English value |
| 99 | && $myrow_string['ever_tr_value'] == "" # That's never been translated to this incoming value |
| 100 | && $value != $myrow_string['value'] # And the proposed translation is different from the English value |
| 101 | ) { |
| 102 | $insert_as_fuzzy = 0; |
| 103 | if($myrow_string['tr_last'] != "" && $fuzzy == 1 && $myrow_string['tr_last_fuzzy'] == 0) { |
| 104 | # This incoming translation is replacing an existing value that is *not* marked as fuzzy |
| 105 | # And the $fuzzy == 1, so we may be replacing a known good value !! |
| 106 | $insert_as_fuzzy = 1; |
| 107 | } |
| 108 | else { |
| 109 | ## Nothing. Insert as non-fuzzy. |
| 110 | ## If this is replacing a fuzzy value, then that's a good thing |
| 111 | } |
| 112 | # echo " Language: " . $language . " - Found string with ID: " . $myrow_string['string_id'] . " value: " . $myrow_string['value'] . " never translated to: " . $value . "\n"; |
| 113 | $SQL = "UPDATE translations set is_active = 0 where string_id = " . $myrow_string['string_id'] . " and language_id = '" . $language_id . "'"; |
| 114 | mysql_query($SQL, $dbh); |
| 115 | $SQL = "INSERT INTO translations (translation_id, string_id, language_id, version, value, possibly_incorrect, is_active, userid, created_on) |
| 116 | VALUES ( |
| 117 | NULL, " . $myrow_string['string_id'] . ", |
| 118 | " . $language_id . ", 0, '" . addslashes(unescape($value)) . "', $insert_as_fuzzy, 1, " . $USER . ", NOW() |
| 119 | )"; |
| 120 | mysql_query($SQL, $dbh); |
| 121 | # echo $SQL; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | echo "Done.\n\n"; |
| 129 | ?> |