source: trunk/scripts/migrate-newsletters-to-html @ 1184

Revision 1005, 3.0 KB checked in by bogdan2412, 3 years ago (diff)

Updated database to work with latest code

  • Property svn:executable set to *
Line 
1#! /usr/bin/env php
2<?php
3require_once(dirname($argv[0]) . "/utilities.php");
4require_once(IA_ROOT_DIR . "common/textblock.php");
5require_once(IA_ROOT_DIR . "common/db/textblock.php");
6
7// Timestamp since all newsletters are HTML.
8define("IA_NEWSLETTER_HTML_EPOCH", "2009-01-10 00:00:00");
9log_assert(is_db_date(IA_NEWSLETTER_HTML_EPOCH));
10// Basic HTTP URL Regexp;
11define("IA_RE_HTTP_URL", 'https?:\/\/[^\s\(\)]*');
12
13function newsletter_is_plaintext($textblock) {
14    log_assert_valid(textblock_validate($textblock));
15    $timestamp = $textblock['timestamp'];
16    return ($timestamp && is_db_date($timestamp) &&
17        db_date_parse($timestamp) < db_date_parse(IA_NEWSLETTER_HTML_EPOCH));
18}
19
20// Convert plaintext newsletter body to textile format.
21function newsletter_plaintext_to_textile($plaintext) {
22    // Indent paragraphs with leading whitespace.
23
24    $paragraphs = preg_split("/(\n\r?){2,}/", $plaintext);
25    $textile = '';
26    foreach ($paragraphs as $paragraph) {
27        $p_stripped = ltrim($paragraph);
28        $num_spaces = strlen($paragraph) - strlen($p_stripped);
29        if ($num_spaces > 0) {
30            $indent = (double)$num_spaces / 2;
31            $textile .= "p{padding-left: {$indent}em;}. ";
32        }
33        $textile .= $p_stripped."\n\n";
34    }
35
36    // Insert headings.
37
38    $textile = preg_replace('/^==(.+)==\s*$/m', "h2. \\1\n", $textile);
39
40    // Convert placeholder %tags% to textile macros.
41
42    $tags = array(
43        '"%full_name%"' => '==UserInfo(user="%username%" info="fullname")==',
44        '%full_name%' => '==UserInfo(user="%username%" info="fullname")==',
45        '%url_unsubscribe%' => '\'account/%username%\':account/%username%',
46        '%your_rating_is%' => '==NewsletterRating(username="%username%")==',
47    );
48    foreach ($tags as $tag_name => $tag_value) {
49        $textile = str_replace($tag_name, $tag_value, $textile);
50    }
51
52    // Convert links
53
54    $textile = preg_replace('|%url_infoarena%/?utilizator/('.IA_RE_USER_NAME.')|',
55            ' ==User(user="\1" type="link")== ', $textile);
56    $textile = preg_replace('|%url_infoarena%/?('.IA_RE_PAGE_NAME.')\.|',
57            '\'\1\':\1.', $textile);
58    $textile = preg_replace('|%url_infoarena%/?('.IA_RE_PAGE_NAME.')|',
59            '\'\1\':\1', $textile);
60    $textile = preg_replace('|%url_infoarena%/?|', '\'infoarena\':home', $textile);
61    $textile = preg_replace('|('.IA_RE_HTTP_URL.')|', '\'\1\':\1', $textile);
62    return $textile;
63}
64
65
66db_connect();
67foreach (textblock_get_by_prefix('newsletter/', true) as $textblock) {
68    if (!newsletter_is_plaintext($textblock)) {
69        log_print('Skipping '.$textblock['name']);
70        continue;
71    }
72    log_print('Converting '.$textblock['name']);
73    $new_text = newsletter_plaintext_to_textile($textblock['text']);
74    textblock_add_revision($textblock['name'], $textblock['title'],
75            $new_text, $textblock['user_id'], $textblock['security'],
76            $textblock['forum_topic'], null, $textblock['creation_timestamp'],
77            getattr($textblock, 'remote_ip_info'));
78}
79
80?>
Note: See TracBrowser for help on using the repository browser.