File: tm_migrate.cpp

package info (click to toggle)
poedit 1.8.11-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,116 kB
  • ctags: 2,239
  • sloc: cpp: 20,600; sh: 4,213; makefile: 210; xml: 35
file content (309 lines) | stat: -rw-r--r-- 8,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 *  This file is part of Poedit (https://poedit.net)
 *
 *  Copyright (C) 2013-2016 Vaclav Slavik
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef DONT_MIGRATE_LEGACY_TM

#include "transmem.h"

#include "logcapture.h"
#include "errors.h"

#include <wx/app.h>
#include <wx/string.h>
#include <wx/log.h>
#include <wx/intl.h>
#include <wx/utils.h>
#include <wx/filename.h>
#include <wx/process.h>
#include <wx/config.h>
#include <wx/stdpaths.h>
#include <wx/msgdlg.h>
#include <wx/progdlg.h>
#include <wx/intl.h>
#include <wx/sstream.h>

#include <expat.h>

namespace
{

wxString GetLegacyDatabaseDirInternal()
{
    wxString data;
#if defined(__UNIX__) && !defined(__WXOSX__)
    if (!wxGetEnv("XDG_DATA_HOME", &data))
        data = wxGetHomeDir() + "/.local/share";
    data += "/poedit";
#else
    data = wxStandardPaths::Get().GetUserDataDir();
#endif

    data += wxFILE_SEP_PATH;
    data += "TM";

    if (wxFileName::DirExists(data))
        return data;
    else
        return wxString();
}

wxString GetLegacyDatabaseDir()
{
    wxString path = wxConfig::Get()->Read("/TM/database_path", "");
    if (path.empty() || !wxFileName::DirExists(path))
        path = GetLegacyDatabaseDirInternal();
#ifdef __WXMSW__
    return wxFileName(path).GetShortPath();
#else
    return path;
#endif
}


wxString GetDumpToolPath()
{
#if defined(__WXOSX__) || defined(__WXMSW__)
    wxFileName path(wxStandardPaths::Get().GetExecutablePath());
    path.SetName("dump-legacy-tm");
  #ifdef __WXMSW__
    path.SetExt("exe");
    return path.GetShortPath();
  #else
    return path.GetFullPath();
  #endif
#else
    return wxStandardPaths::Get().GetInstallPrefix() + "/lib/poedit/poedit-dump-legacy-tm";
#endif
}


class DumpProcess : public wxProcess
{
public:
    DumpProcess() : wxProcess(), terminated(false), status(0) {}

    virtual void OnTerminate(int, int status_)
    {
        status = status_;
        terminated = true;
    }

    bool terminated;
    int status;
};


// Note: Expat is used for parsing, because the data may potentially be huge and
//       streaming the content avoids excessive memory usage.

struct ExpatContext
{
    wxProgressDialog *progress;
    Language lang;
    int count;
    std::shared_ptr<TranslationMemory::Writer> tm;
};

void XMLCALL OnStartElement(void *data, const char *name, const char **attrs)
{
    ExpatContext& ctxt = *static_cast<ExpatContext*>(data);

    if ( strcmp(name, "language") == 0 )
    {
        for ( int i = 0; attrs[i]; i += 2 )
        {
            if (strcmp(attrs[i], "lang") == 0)
                ctxt.lang = Language::TryParse(wxString::FromUTF8(attrs[i+1]).ToStdWstring());
        }
    }
    if ( strcmp(name, "i") == 0 )
    {
        wxString s, t;
        for ( int i = 0; attrs[i]; i += 2 )
        {
            if (strcmp(attrs[i], "s") == 0)
                s = wxString::FromUTF8(attrs[i+1]);
            else if (strcmp(attrs[i], "t") == 0)
                t = wxString::FromUTF8(attrs[i+1]);
            if (!s.empty() && !t.empty())
            {
                static const auto srclang = Language::English();
                ctxt.tm->Insert(srclang, ctxt.lang, s.ToStdWstring(), t.ToStdWstring());
                if (ctxt.count++ % 47 == 0)
                    ctxt.progress->Pulse(wxString::Format(_("Importing translations: %d"), ctxt.count));
            }
        }
    }
}

void XMLCALL OnEndElement(void*, const char*)
{
    // nothing to do
}

void DoMigrate(const wxString& path, const wxString& languages)
{
    LogCapture log;

    const wxString tool = GetDumpToolPath();
    wxLogTrace("poedit.tm", "TM migration - tool: '%s'", tool);
    wxLogVerbose("%s \"%s\" \"%s\"", tool, path, languages);

    wxProgressDialog progress(_("Poedit Update"), _("Preparing migration..."));

    wxCharBuffer tool_utf8(tool.utf8_str());
    wxCharBuffer path_utf8(path.utf8_str());
    wxCharBuffer langs_utf8(languages.utf8_str());
    char *argv_utf8[] = { tool_utf8.data(), path_utf8.data(), langs_utf8.data(), nullptr };

    DumpProcess callback;
    callback.Redirect();
    long executeStatus = wxExecute(argv_utf8, wxEXEC_ASYNC, &callback);
    if (executeStatus <= 0)
        throw Exception(log.text);

    auto sout = callback.GetInputStream();
    auto serr = callback.GetErrorStream();

    ExpatContext ctxt;
    ctxt.progress = &progress;
    ctxt.count = 0;
    ctxt.tm = TranslationMemory::Get().GetWriter();

    XML_Parser parser = XML_ParserCreate(NULL);
    XML_SetUserData(parser, &ctxt);
    XML_SetElementHandler(parser, OnStartElement, OnEndElement);

    while (!callback.terminated)
    {
#if defined(__UNIX__)
        if ( wxTheApp )
            wxTheApp->CheckSignal();
#elif defined(__WXMSW__)
        wxYield(); // so that OnTerminate() is called
#endif

        wxMilliSleep(1);

        while (callback.IsInputAvailable())
        {
            char buf[4096];
            sout->Read(buf, 4096);
            size_t read = sout->LastRead();
            XML_Parse(parser, buf, (int)read, XML_FALSE);
        }
        while (callback.IsErrorAvailable())
        {
            wxStringOutputStream logstream(&log.text);
            serr->Read(logstream);
        }
    }
    XML_Parse(parser, "", 0, XML_TRUE);
    XML_ParserFree(parser);

    if (callback.status == 0)
    {
        progress.Pulse(_("Finalizing..."));
        progress.Pulse();
        ctxt.tm->Commit();
    }

    if (callback.status != 0)
    {
        log.Append(wxString::Format(_("Migration exit status: %d"), callback.status));
        throw Exception(log.text);
    }
}

} // anonymous namespace

/**
    Migrates existing BerkeleyDB-based translation memory into the new Lucene
    format.
    
    @return false if the user declined to do it, true otherwise (even on failure!)
 */
bool MigrateLegacyTranslationMemory()
{
    if (wxConfig::Get()->ReadBool("/TM/legacy_migration_failed", false))
        return true; // failed migration shouldn't prevent Poedit from working

    const wxString languages = wxConfig::Get()->Read("/TM/languages", "");
    if (languages.empty())
        return true; // no migration to perform

    const wxString path = GetLegacyDatabaseDir();
    wxLogTrace("poedit.tm", "TM migration - path: '%s', languages: '%s'", path, languages);
    if (path.empty())
        return true; // no migration to perform

    {
        wxMessageDialog dlg
        (
            nullptr,
            _("Poedit needs to convert your translation memory to a new format."),
            _("Poedit Update"),
            wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
        );
        dlg.SetExtendedMessage(_("This must be done before Poedit can start. It may take a few minutes if you have lots of translations stored, but should normally be much faster."));
        dlg.SetYesNoLabels(_("Proceed"), _("Quit"));
        if (dlg.ShowModal() != wxID_YES)
            return false;
    }

    try
    {
        DoMigrate(path, languages);

        // migration successed, remove old TM:
        if (path == GetLegacyDatabaseDirInternal())
        {
            wxLogNull null;
            wxFileName::Rmdir(path, wxPATH_RMDIR_RECURSIVE);
        }
        // else: points to a user directory; play it safe and don't delete

        wxConfigBase::Get()->DeleteGroup("/TM");
    }
    catch (Exception& e)
    {
        wxConfig::Get()->Write("/TM/legacy_migration_failed", true);
        wxMessageDialog dlg
        (
            nullptr,
            _("Translation memory migration failed."),
            _("Poedit Update"),
            wxOK | wxICON_ERROR
        );
        dlg.SetExtendedMessage(wxString::Format(
            _(L"Your translation memory data couldn't be migrated. The error was:\n\n%s\nPlease email help@poedit.net and we’ll get it fixed."),
            e.What()));
        dlg.ShowModal();
    }

    return true;
}

#endif // !DONT_MIGRATE_LEGACY_TM