\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Migrating identities}
This is a small MySQL script that migrates email addresses and full names from old IMP 2 databases to the identity arrays used in Horde 3/IMP 4. By tweaking the sub-select statement, you can use the same script to migrate existing user data from any external source. Please adapt it to your needs before running it.

<pre><code class="language-sql">
INSERT INTO horde\_prefs
    (pref\_uid, pref\_scope, pref\_name, pref\_value)
    SELECT
        SUBSTRING(user, 1, LOCATE('@', user) - 1),
        'horde',
        'identities',
        CONCAT('a:1:\{i:0;a:5:\{s:2:"id";s:16:"Default Identity";s:8:"fullname";s:',
               LENGTH(IFNULL(fullname, '')), ':"', IFNULL(fullname, ''),
               '";s:9:"from\_addr";s:', LENGTH(IFNULL(replyto, '')), ':"', IFNULL(replyto, ''),
               '";s:16:"default\_identity";s:1:"0";s:9:"signature";s:', LENGTH(IFNULL(sig, '')), ':"', IFNULL(sig, ''), '";\}\}')
        FROM imp\_pref WHERE user LIKE '\%@localhost' OR user LIKE '\%@example.com';
</code></pre>
This code takes all entries from the \texttt{imp\_pref} table that have the user name ending with \texttt{@localhost} or \texttt{@example.com}. This domain part is then stripped from the user name (\texttt{SUBSTRING(...)}) and a string representing a serialized PHP array is build (\texttt{CONCAT(...)}) from the \texttt{fullname}, \texttt{replyto}, and \texttt{sig} columns and inserted into the \texttt{horde\_prefs} table. The \texttt{IFNULL(...)} expressions make sure that this works with both, empty and NULL values in the table fields.

Please note that this script only makes sense for \textit{one-time} migration of existing data. If you have this kind of user data in an external source permanently, e.g. in a company addressbook, LDAP directory or similar, you should use preference hooks to create the user identities instead.

\end{document}
