diff options
Diffstat (limited to 'lib/update_importer.rb')
| -rw-r--r-- | lib/update_importer.rb | 155 |
1 files changed, 82 insertions, 73 deletions
diff --git a/lib/update_importer.rb b/lib/update_importer.rb index 77d27b2..103ac5a 100644 --- a/lib/update_importer.rb +++ b/lib/update_importer.rb | |||
| @@ -5,9 +5,6 @@ require 'iconv' | |||
| 5 | class UpdateImporter | 5 | class UpdateImporter |
| 6 | 6 | ||
| 7 | def initialize path | 7 | def initialize path |
| 8 | Node.delete_all | ||
| 9 | Page.delete_all | ||
| 10 | |||
| 11 | @path = path | 8 | @path = path |
| 12 | 9 | ||
| 13 | unless Node.root | 10 | unless Node.root |
| @@ -42,7 +39,7 @@ class UpdateImporter | |||
| 42 | 39 | ||
| 43 | tmp_dir = dir.sub(@path, "").split(/\//).last | 40 | tmp_dir = dir.sub(@path, "").split(/\//).last |
| 44 | chaos_id = tmp_dir.split(/\./)[0] | 41 | chaos_id = tmp_dir.split(/\./)[0] |
| 45 | 42 | ||
| 46 | create_node_and_page( chaospage.root, lang, chaos_id ) | 43 | create_node_and_page( chaospage.root, lang, chaos_id ) |
| 47 | end | 44 | end |
| 48 | end | 45 | end |
| @@ -62,6 +59,8 @@ class UpdateImporter | |||
| 62 | node.move_to_child_of parent_node | 59 | node.move_to_child_of parent_node |
| 63 | end | 60 | end |
| 64 | 61 | ||
| 62 | |||
| 63 | |||
| 65 | create_node_for_page chaospage, node, date, lang | 64 | create_node_for_page chaospage, node, date, lang |
| 66 | end | 65 | end |
| 67 | 66 | ||
| @@ -78,108 +77,118 @@ class UpdateImporter | |||
| 78 | element = element.next_sibling | 77 | element = element.next_sibling |
| 79 | end | 78 | end |
| 80 | 79 | ||
| 81 | if node.pages.empty? | 80 | page = node.pages.first |
| 82 | 81 | ||
| 83 | I18n.locale = lang | 82 | I18n.locale = lang |
| 84 | 83 | ||
| 85 | page = node.pages.create!( | 84 | unless node.head |
| 85 | page.update_attributes( | ||
| 86 | :title => xhtml.elements['title'].get_text.to_s, | 86 | :title => xhtml.elements['title'].get_text.to_s, |
| 87 | :abstract => xhtml.elements['abstract'].get_text.to_s, | 87 | :abstract => xhtml.elements['abstract'].get_text.to_s, |
| 88 | :body => body | 88 | :body => body |
| 89 | ) | 89 | ) |
| 90 | else | ||
| 91 | page = node.pages.first | ||
| 92 | 90 | ||
| 93 | I18n.locale = lang | ||
| 94 | 91 | ||
| 95 | page.update_attributes( | 92 | if xhtml.elements['author'] |
| 96 | :title => xhtml.elements['title'].get_text.to_s, | 93 | user = User.find_by_login(xhtml.elements['author'].get_text.to_s) |
| 97 | :abstract => xhtml.elements['abstract'].get_text.to_s, | 94 | page.user = user |
| 98 | :body => body | 95 | else |
| 99 | ) | 96 | page.user = User.first |
| 97 | end | ||
| 100 | 98 | ||
| 99 | page.published_at = date.to_time | ||
| 100 | page.save! | ||
| 101 | |||
| 102 | puts page.published_at | ||
| 103 | |||
| 104 | page.tag_list.add("update") if page | ||
| 101 | end | 105 | end |
| 102 | 106 | ||
| 103 | if xhtml.elements['author'] | ||
| 104 | user = User.find_by_login(xhtml.elements['author'].get_text.to_s) | ||
| 105 | page.user = user | ||
| 106 | else | ||
| 107 | page.user = User.first | ||
| 108 | end | ||
| 109 | |||
| 110 | page.published_at = date.to_time | ||
| 111 | page.save! | ||
| 112 | |||
| 113 | puts page.published_at | ||
| 114 | |||
| 115 | page.tag_list.add("update") if page | ||
| 116 | |||
| 117 | if (flags = xhtml.elements['flags']) && page | 107 | if (flags = xhtml.elements['flags']) && page |
| 118 | page.tag_list.add("event") if flags.attributes['calendar'] | 108 | page.tag_list.add("event") if (flags.attributes['calendar'] && !node.head) |
| 119 | page.tag_list.add("pressemitteilung") if flags.attributes['pm'] | 109 | page.tag_list.add("pressemitteilung") if (flags.attributes['pm'] && !node.head) |
| 120 | 110 | ||
| 121 | if flags.attributes['calendar'] | 111 | if flags.attributes['calendar'] |
| 122 | cal = Vpim::Icalendar.create2 | 112 | event_options = { } |
| 123 | 113 | ||
| 114 | # Figuring out dtstart | ||
| 124 | dtstart = xhtml.elements['ical:DTSTART'] | 115 | dtstart = xhtml.elements['ical:DTSTART'] |
| 125 | dtisdate = dtstart.attributes['VALUE'] | 116 | dtisdate = dtstart.attributes['VALUE'] |
| 126 | raise "DTSTART not present in event" unless dtstart | 117 | raise "DTSTART not present in event" unless dtstart |
| 127 | if dtisdate && dtisdate == 'DATE' | 118 | if dtisdate && dtisdate == 'DATE' |
| 128 | dtstart = dtstart.text.to_date | 119 | # dtstart = dtstart.text.to_date |
| 120 | event_options[:allday] = true | ||
| 129 | else | 121 | else |
| 130 | dtstart = dtstart.text.to_time | 122 | # dtstart = dtstart.text.to_time |
| 123 | event_options[:allday] = false | ||
| 131 | end | 124 | end |
| 132 | 125 | ||
| 133 | dtend = xhtml.elements['ical:DTEND'] | 126 | event_options[:start_time] = dtstart.text |
| 134 | dtisdate = dtend.attributes['VALUE'] if dtend | 127 | |
| 128 | #Figuring out dtend | ||
| 135 | duration = xhtml.elements['ical:DURATION'] | 129 | duration = xhtml.elements['ical:DURATION'] |
| 136 | puts "WARNING: Neither DTEND nor DURATION present in event" unless dtend || duration | 130 | |
| 137 | # raise "Both DTEND and DURATION present in event" if dtend && duration | 131 | |
| 138 | if dtend | 132 | unless dtend = xhtml.elements['ical:DTEND'] |
| 139 | if dtisdate && dtisdate == 'DATE' | 133 | parsed_duration = Ical_occurrences.duration_to_fixnum(duration.text) |
| 140 | dtend = dtend.text.to_date | 134 | event_options[:end_time] = dtstart.text.to_time + parsed_duration |
| 141 | else | 135 | else |
| 142 | dtend = dtend.text.to_time | 136 | event_options[:end_time] = dtend.text |
| 143 | end | ||
| 144 | end | 137 | end |
| 145 | duration = duration.text if duration | 138 | |
| 146 | 139 | ||
| 140 | raise "WARNING: Neither DTEND nor DURATION present in event" unless dtend || duration | ||
| 141 | |||
| 142 | # Figuring out location data | ||
| 147 | location = xhtml.elements['ical:LOCATION'] | 143 | location = xhtml.elements['ical:LOCATION'] |
| 148 | localtrep = location.attributes['ALTREP'] if location | 144 | event_options[:location] = location.text if location |
| 149 | location = location.text if location | 145 | |
| 146 | # Figuring out url | ||
| 147 | if location | ||
| 148 | localtrep = location.attributes['ALTREP'] | ||
| 149 | event_options[:url] = localtrep if localtrep | ||
| 150 | end | ||
| 150 | 151 | ||
| 151 | geo = xhtml.elements['ical:GEO'] | 152 | # Figuring out geo data latitude / longitude |
| 152 | geo = geo.text if geo | 153 | geo = xhtml.elements['ical:GEO'] |
| 154 | event_options[:latitude], event_options[:longitude] = geo.text.split(";") if geo | ||
| 153 | 155 | ||
| 156 | # Figuring out RRule | ||
| 154 | if( rrule = xhtml.elements['ical:RRULE'] ) | 157 | if( rrule = xhtml.elements['ical:RRULE'] ) |
| 155 | rrtxt = '' | 158 | rrtxt = '' |
| 156 | rrule.each_element( ) { |subrule| | 159 | rrule.each_element( ) { |subrule| |
| 157 | rrtxt += subrule.name + '=' + subrule.text + ';' | 160 | rrtxt += subrule.name + '=' + subrule.text + ';' |
| 158 | } | 161 | } |
| 159 | rrtxt.chomp!(';') | 162 | rrtxt.chomp!(';') |
| 160 | end | 163 | |
| 161 | 164 | event_options[:rrule] = rrtxt | |
| 162 | hash = { 'CREATED' => page.published_at } | ||
| 163 | hash[ 'DTEND' ] = dtend if dtend | ||
| 164 | hash[ 'DURATION' ] = duration if duration | ||
| 165 | hash[ 'LOCATION' ] = location if location | ||
| 166 | hash[ 'GEO' ] = geo if geo | ||
| 167 | hash[ 'URL' ] = localtrep if localtrep | ||
| 168 | hash[ 'RRULE' ] = rrtxt if rrtxt | ||
| 169 | |||
| 170 | xevent = Vpim::Icalendar::Vevent.create( dtstart, hash ) | ||
| 171 | cal.push( xevent ) | ||
| 172 | puts cal.to_s | ||
| 173 | 165 | ||
| 166 | default_rrules = ["FREQ=WEEKLY;INTERVAL=1", "FREQ=MONTHLY;INTERVAL=1", "FREQ=YEARLY;INTERVAL=1"] | ||
| 167 | |||
| 168 | unless default_rrules.include? event_options[:rrule] | ||
| 169 | event_options[:custom_rrule] = true | ||
| 170 | end | ||
| 171 | end | ||
| 172 | |||
| 173 | puts event_options.inspect | ||
| 174 | |||
| 175 | # Creating or updating event data for node | ||
| 176 | unless tmp_event = node.event | ||
| 177 | tmp_event = Event.create! event_options.merge({:node_id => node.id}) | ||
| 178 | else | ||
| 179 | tmp_event.update_attributes event_options | ||
| 180 | end | ||
| 174 | end | 181 | end |
| 175 | end | 182 | end |
| 176 | 183 | ||
| 177 | page.save! | 184 | unless node.head |
| 178 | 185 | page.save! | |
| 179 | if node.head.nil? && page | 186 | |
| 180 | node.head = page | 187 | if node.head.nil? && page |
| 181 | node.draft = nil | 188 | node.head = page |
| 182 | node.save! | 189 | node.draft = nil |
| 190 | node.save! | ||
| 191 | end | ||
| 183 | end | 192 | end |
| 184 | end | 193 | end |
| 185 | 194 | ||
