public
Description: YAML RDoc Generator and Rails Website for RDoc
Homepage: http://rannotate.rubyforge.org/
Clone URL: git://github.com/conorh/rannotate.git
imported local changes

git-svn-id: 
file:///Users/conorhunt/Code/Rails/svndavrepo/rannotate/trunk@153 
016420e5-f504-0410-9c4b-e4e4ac7be119
conor (author)
Mon Apr 23 21:25:40 -0700 2007
commit  70c83cc50dd35737f6193565fe28461434c39707
tree    2d072307ff98864f92f10a25759eecc79c3ea4d6
parent  9f87402f533a3060d814921e08195a7803d855b8
...
68
69
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -68,3 +68,30 @@ task "doc:api:rails" do
0
   
0
   RDoc::RDoc.new.document(params + dirs)
0
 end
0
+
0
+task "doc:api:rails_vendor" do
0
+ params = []
0
+ params << "--fmt=yaml"
0
+ params << "--opname=rails-1.1.2"
0
+ dirs = []
0
+ dirs << 'vendor/rails/railties/README'
0
+ dirs << 'vendor/rails/railties/CHANGELOG'
0
+ dirs << 'vendor/rails/railties/MIT-LICENSE'
0
+ dirs << 'vendor/rails/activerecord/README'
0
+ dirs << 'vendor/rails/activerecord/CHANGELOG'
0
+ dirs << 'vendor/rails/activerecord/lib/'
0
+ dirs << 'vendor/rails/actionpack/README'
0
+ dirs << 'vendor/rails/actionpack/CHANGELOG'
0
+ dirs << 'vendor/rails/actionpack/lib/'
0
+ dirs << 'vendor/rails/actionmailer/README'
0
+ dirs << 'vendor/rails/actionmailer/CHANGELOG'
0
+ dirs << 'vendor/rails/actionmailer/lib/'
0
+ dirs << 'vendor/rails/actionwebservice/README'
0
+ dirs << 'vendor/rails/actionwebservice/CHANGELOG'
0
+ dirs << 'vendor/rails/actionwebservice/lib/'
0
+# dirs << 'vendor/rails/activesupport/README'
0
+ dirs << 'vendor/rails/activesupport/CHANGELOG'
0
+ dirs << 'vendor/rails/activesupport/lib/'
0
+
0
+ RDoc::RDoc.new.document(params + dirs)
0
+end
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
6
7
...
27
28
29
30
31
32
 
33
34
35
...
63
64
65
66
67
 
 
 
 
 
 
 
 
 
 
 
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
71
...
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
...
76
77
78
 
 
 
79
80
81
82
...
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
0
@@ -2,6 +2,55 @@ class Admin::NotesController < ApplicationController
0
   before_filter :login_required
0
   helper :notes
0
   layout 'admin/admin.rhtml'
0
+
0
+ def show
0
+ # Start main code
0
+ s = File.open("d:\\rails_site_notes.xml").read
0
+ array = s.scan(/<entry>.*?<title type="text">New note for - ([^<]*)<\/t.*?d>([^<]*)<\/p.*?l">(.*?)<\/summ.*?e>([^<]*)</m)
0
+
0
+ array.each do |entry|
0
+ n = Note.new
0
+
0
+ if(s == "Index page")
0
+ container_name = "index"
0
+ note_type = "index"
0
+ note_group = "index"
0
+ elsif(s =~ /Method/)
0
+ m = s.match(/Method ([a-zA-z0-9_]+) of ([a-zA-z0-9_:]+)/)
0
+ container_name = m[1]
0
+ note_type = "RaMethod"
0
+ note_group = m[0] # method name
0
+ elseif(s =~ / section of /)
0
+ m = s.match(/([a-zA-z0-9_]+) section of ([a-zA-z0-9_:]+)/)
0
+ container_name = m[1]
0
+ type = get_note_type(m[0])
0
+ note_type = type
0
+ note_group = type
0
+ else
0
+ container_name = m
0
+ type = get_container_type(m)
0
+ note_type = type
0
+ note_group = type
0
+ end
0
+
0
+ n.container_name = container_name
0
+ n.note_type = note_type
0
+ n.note_group = note_group
0
+ n.email = entry[4]
0
+ n.text = entry[3]
0
+ n.ip_address = "127.0.0.1"
0
+ n.created_at = entry[1]
0
+ n.updated_at = entry[1]
0
+ n.total_votes = 0
0
+ n_version = "1.8.4"
0
+ n.status = 0
0
+
0
+ n.save
0
+ end
0
+
0
+ puts "done!"
0
+ end
0
+
0
   
0
   def index
0
    @note_filter = NoteFilter.new(@params['note_filter'])
0
@@ -27,9 +76,7 @@ class Admin::NotesController < ApplicationController
0
    @note = Note.find(@params['id'])
0
   end
0
   
0
- def show
0
- @note = Note.find(@params['id'])
0
- end
0
+
0
   
0
   # update a note and expire the cache if necessary
0
   def update
0
@@ -63,8 +110,35 @@ class Admin::NotesController < ApplicationController
0
          Note.delete(note.id)
0
       end
0
   end  
0
-
0
-private
0
+
0
+ # show the list of notes waiting for moderation
0
+ def list_moderate
0
+ @notes = Note.find(:all, :conditions => ["approved = ?", false])
0
+ end
0
+
0
+ def moderate
0
+ # get the list of notes to approve
0
+ # get the list of notes to delete
0
+ # get the list of notes to hide
0
+ end
0
    
0
 
0
+def get_contanier_type(type_string)
0
+ container = RaContainer.find_by_name(type_string)
0
+ return container.class.to_s
0
+end
0
+
0
+def get_note_type(type_string)
0
+ case type_string
0
+ when "Method" then return "RaMethod"
0
+ when "Children" then return "RaChildren"
0
+ when "InFiles" then return "RaInFile"
0
+ when "Attributes" then return "RaAttribute"
0
+ when "Constants" then return "RaConstant"
0
+ when "Includes" then return "RaInclude"
0
+ when "Requires" then return "RaRequire"
0
+ when "Aliases" then return "RaAlias"
0
+ end
0
+end
0
+
0
 end
0
\ No newline at end of file
...
 
 
1
2
3
...
13
14
15
16
17
18
 
 
 
19
20
21
 
22
23
24
...
29
30
31
32
33
34
35
36
37
38
39
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42
43
44
45
 
 
46
47
48
...
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
...
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
...
1
2
3
4
5
...
15
16
17
 
 
 
18
19
20
21
22
 
23
24
25
26
...
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
...
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
...
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
310
311
312
313
314
315
316
0
@@ -1,3 +1,5 @@
0
+require 'pp'
0
+
0
 class Admin::UploadController < ApplicationController
0
   before_filter :login_required
0
   layout 'admin/admin.rhtml'
0
@@ -13,12 +15,12 @@ class Admin::UploadController < ApplicationController
0
      # entry in the hash contains a list of all the library versions
0
      @libraries = Hash.new
0
      all_libs.each do |l|
0
- unless @libraries[l.name] then @libraries[l.name] = Array.new end
0
- @libraries[l.name].push(l)
0
- end
0
+ unless @libraries[l.name] then @libraries[l.name] = Array.new end
0
+ @libraries[l.name].push(l)
0
+ end
0
   end
0
   
0
- # delete a library and all of its contents
0
+ # delete libraries and all of their associated contents
0
   def delete  
0
     library_ids = @params[:id]
0
         
0
@@ -29,20 +31,33 @@ class Admin::UploadController < ApplicationController
0
     
0
      library = RaLibrary.find(library_id)
0
      name = library.name
0
-
0
- RaCodeObject.connection.delete("DELETE ra_code_objects FROM ra_code_objects, ra_containers WHERE ra_code_objects.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
- RaInFile.connection.delete("DELETE ra_in_files FROM ra_in_files, ra_containers WHERE ra_in_files.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
- RaSourceCode.connection.delete("DELETE ra_source_codes FROM ra_source_codes, ra_methods, ra_containers WHERE ra_source_codes.id = ra_methods.ra_source_code_id AND ra_methods.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
- RaComment.connection.delete("DELETE ra_comments FROM ra_comments, ra_methods, ra_containers WHERE ra_comments.id = ra_methods.ra_comment_id AND ra_methods.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
- RaMethod.connection.delete("DELETE ra_methods FROM ra_methods, ra_containers WHERE ra_methods.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
- RaComment.connection.delete("DELETE ra_comments FROM ra_comments, ra_containers WHERE ra_comments.id = ra_containers.ra_comment_id AND ra_containers.ra_library_id =" + library_id)
0
- RaContainer.connection.delete("DELETE FROM ra_containers WHERE ra_library_id = " + library_id)
0
- RaLibrary.connection.delete("DELETE FROM ra_libraries WHERE id =" + library_id)
0
+
0
+ # ok this really should be more activerecord'ish
0
+ # but I didn't have enough time to do that, so for now we know it works on
0
+ # two DBs, mysql and postgres
0
+ begin
0
+ RaCodeObject.connection.delete("DELETE ra_code_objects FROM ra_code_objects, ra_containers WHERE ra_code_objects.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
+ RaInFile.connection.delete("DELETE ra_in_files FROM ra_in_files, ra_containers WHERE ra_in_files.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
+ RaSourceCode.connection.delete("DELETE ra_source_codes FROM ra_source_codes, ra_methods, ra_containers WHERE ra_source_codes.id = ra_methods.ra_source_code_id AND ra_methods.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
+ RaComment.connection.delete("DELETE ra_comments FROM ra_comments, ra_methods, ra_containers WHERE ra_comments.id = ra_methods.ra_comment_id AND ra_methods.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
+ RaMethod.connection.delete("DELETE ra_methods FROM ra_methods, ra_containers WHERE ra_methods.ra_container_id = ra_containers.id AND ra_containers.ra_library_id = " + library_id)
0
+ RaComment.connection.delete("DELETE ra_comments FROM ra_comments, ra_containers WHERE ra_comments.id = ra_containers.ra_comment_id AND ra_containers.ra_library_id =" + library_id)
0
+ RaContainer.connection.delete("DELETE FROM ra_containers WHERE ra_library_id = " + library_id)
0
+ RaLibrary.connection.delete("DELETE FROM ra_libraries WHERE id =" + library_id)
0
+ rescue
0
+ RaCodeObject.connection.delete("DELETE FROM ra_code_objects WHERE id IN (SELECT ra.id FROM ra_code_objects AS ra, ra_containers AS rc, ra_libraries AS rl WHERE ra.ra_container_id = rc.id AND rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ RaInFile.connection.delete("DELETE FROM ra_in_files WHERE id in (SELECT ra.id FROM ra_in_files AS ra, ra_containers AS rc, ra_libraries AS rl WHERE ra.ra_container_id = rc.id AND rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ RaSourceCode.connection.delete("DELETE FROM ra_source_codes WHERE id IN (SELECT rs.id FROM ra_source_codes AS rs, ra_methods AS rm, ra_containers AS rc, ra_libraries AS rl WHERE rs.id = rm.ra_source_code_id AND rm.ra_container_id = rc.id AND rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ RaComment.connection.delete("DELETE FROM ra_comments WHERE id IN (SELECT rs.id FROM ra_comments AS rs, ra_methods AS rm, ra_containers AS rc, ra_libraries AS rl WHERE rs.id = rm.ra_comment_id AND rm.ra_container_id = rc.id AND rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ RaMethod.connection.delete("DELETE FROM ra_methods WHERE id IN (SELECT ra.id FROM ra_methods AS ra, ra_containers AS rc, ra_libraries AS rl WHERE ra.ra_container_id = rc.id AND rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ RaComment.connection.delete("DELETE FROM ra_comments WHERE id IN (SELECT rs.id FROM ra_comments AS rs, ra_containers AS rc, ra_libraries AS rl WHERE rs.id = rc.ra_comment_id AND rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ RaContainer.connection.delete("DELETE FROM ra_containers WHERE id IN (SELECT rc.id FROM ra_containers AS rc, ra_libraries AS rl WHERE rc.ra_library_id = rl.id AND rl.id =" + library_id + ")")
0
+ end
0
     
0
      # make sure that the highest library version is still marked as the current version
0
      libraries = RaLibrary.find(:all, :conditions => ["name = ?", name])    
0
- max_lib = nil
0
- max_ver = 0
0
+ max_lib = nil
0
+ max_ver = 0
0
      libraries.each do |lib|
0
      if(lib.version > max_ver)
0
      max_lib = lib
0
@@ -60,139 +75,179 @@ class Admin::UploadController < ApplicationController
0
     redirect_to :action => :list
0
   end
0
   
0
- # Try and avoid errors for PostgreSQL and maybe other databases too.
0
- # If it fails, we log an exception instead of putting up an error page
0
- # because we can still complete the mission, just slower?
0
- def autocommit(state=true)
0
- begin
0
- ActiveRecord::Base.connection.execute( 'SET AUTOCOMMIT=' + state ? '1' : '0' )
0
- rescue
0
- begin
0
- ActiveRecord::Base.connection.execute( state ? 'BEGIN' : 'COMMIT' )
0
- rescue Exception => e
0
- logger.info "Oops. #{e.to_s}\n" + e.backtrace.join + "\n"
0
+ # given a directory import all files from that directory
0
+ def mass_import
0
+ dir = params[:directory]
0
+ files = Dir.glob(dir + "/*.out")
0
+
0
+ @success = []
0
+ @total_count = 0
0
+
0
+ for file in files
0
+ do_import(file)
0
+ if(@error)
0
+ if(params[:ignore])
0
+ @success << "failed: " + file + " " + @error
0
+ next
0
+ else
0
+ @success << "failed: " + file + " " + @error
0
+ break
0
+ end
0
    end
0
+ @success << "success: " + file
0
    end
0
+
0
+ render :action => :create
0
   end
0
   
0
+ # import a single file
0
   def import
0
-
0
- init_classes()
0
-
0
- @connection = ActiveRecord::Base.connection
0
+ @total_count = 0
0
+ do_import(params['doc_file'], true)
0
+ if(@error)
0
+ @success = [@error]
0
+ else
0
+ @success = ["success: " + params['doc_file'].original_filename]
0
+ end
0
+
0
+ render :action => :create
0
+ end
0
+
0
+private
0
 
0
- if(@connection.class.to_s =~ /mysql/i)
0
- # check to see if the connection is MySQL, if so then we have created some
0
- # optimizations for MySql inserts
0
- @connection_type = "mysql"
0
- else
0
- @connection_type = "other"
0
- end
0
-
0
- # Lookup hash used to map ids in the YAML file to database IDs
0
- lookup = {}
0
+ # import a single library into the database
0
+ def do_import(file, tempfile = false)
0
+ init_classes()
0
+
0
+ @connection = ActiveRecord::Base.connection
0
+
0
+ if(@connection.class.to_s =~ /mysql/i)
0
+ # check to see if the connection is MySQL, if so then we have created some
0
+ # optimizations for MySql inserts
0
+ @connection_type = "mysql"
0
+ else
0
+ @connection_type = "other"
0
+ end
0
     
0
- # Lookup hash used to keep columns around so that they do not need to be
0
- # recreated each time
0
- @col_hash = Hash.new
0
+ # Lookup hash used to map ids in the YAML file to database IDs
0
+ lookup = {}
0
     
0
- file_name = @params['doc_file'].original_filename
0
- match = /([a-zA-Z]+)-([0-9]+)\.([0-9]+)\.([0-9]+)\.out/.match(file_name)
0
- if(!match)
0
- @error = "File name must have the format name-major.minor.release.out, ex: rails-1.2.1.out"
0
- render :action => :index
0
- return
0
- end
0
+ # Lookup hash used to keep columns around so that they do not need to be
0
+ # recreated each time
0
+ @col_hash = Hash.new
0
+
0
+ # should we be reading the file passed in, or is it a file stream already
0
+ if(tempfile)
0
+ file_name = file.original_filename
0
+ else
0
+ file_name = file
0
+ end
0
     
0
+ match = /([a-zA-Z0-9]+)-([0-9]+)\.([0-9]+)\.([0-9]+)\.out/.match(file_name)
0
+ if(!match)
0
+ @error = "File name must have the format name-major.minor.release.out, ex: rails-1.2.1.out"
0
+ return
0
+ end
0
 
0
- ActiveRecord::Base.connection.transaction do
0
- # Create a new library object
0
- @library = RaLibrary.new({:name => match[1], :major => match[2], :minor => match[3], :release => match[4]})
0
- @library.current = false
0
- @library.date = DateTime.now
0
-
0
- if(@library.find_lib == nil)
0
- @library.calc_version
0
+ # measure the time it takes to do the DB inserts
0
+ start = Time.now
0
+ count = 0
0
+ # Set the logging level to ERROR so that we do not output SQL when we are debugging
0
+ # that really slows things down
0
+ loglevel = logger.level
0
+ logger.level = Logger::ERROR
0
+
0
+ ActiveRecord::Base.connection.transaction do
0
+ autocommit(false)
0
+ # Create a new library object
0
+ @library = RaLibrary.new({:name => match[1], :major => match[2], :minor => match[3], :release => match[4]})
0
+ @library.current = false
0
+ @library.date = DateTime.now
0
+
0
+ if(@library.find_lib == nil)
0
+ @library.calc_version
0
+ @library.save!
0
+ # ok now we need to see if the library version that we uploaded is the most recent version of this library
0
+ higher_version = RaLibrary.find(:first, :conditions => ["name = ? AND version > ?", @library.name, @library.version])
0
+ if(higher_version == nil)
0
+ # if our version is the most recent remove the current status from all the current entries
0
+ currents = RaLibrary.find(:all, :conditions => ["name = ? AND current = ?", @library.name, true])
0
+ if(currents)
0
+ currents.each do |c|
0
+ c.current = false
0
+ c.save!
0
+ end
0
+ end
0
+ end
0
+ # now set the status of this library to be most recent and save it
0
+ @library.current = true
0
        @library.save!
0
- # ok now we need to see if the library version that we uploaded is the most recent version of this library
0
- higher_version = RaLibrary.find(:first, :conditions => ["name = ? AND version > ?", @library.name, @library.version])
0
- if(higher_version == nil)
0
- # if our version is the most recent remove the current status from all the current entries
0
- currents = RaLibrary.find(:all, :conditions => ["name = ? AND current = ?", @library.name, true])
0
- if(currents)
0
- currents.each do |c|
0
- c.current = false
0
- c.save!
0
- end
0
- end
0
- # now set the status of this library to be most recent and save it
0
- @library.current = true
0
- @library.save!
0
- end
0
-
0
- # measure the time it takes to do the DB inserts
0
- start = Time.now
0
+ else
0
+ @error = "Could not import library " + file_name + " already exists"
0
+ return
0
+ end
0
+
0
+ # Read in the YAML file
0
+ if(!tempfile)
0
+ file = File.open(file)
0
+ end
0
+
0
+ yp = YAML::load_documents( file ) { |doc|
0
+ # if this is a library record then merge the attributes
0
+ # with our library and save it (can provide more information)
0
+ if(doc.class.to_s == RaLibrary.to_s)
0
+ # if there is a 'main' attribute it will be the name of the
0
+ # file that should be displayed as the home page for this library
0
+ # this should be translated to the container id
0
+ if(doc.attributes['home_page'] != nil)
0
+ home_page = RaContainer.find(:first, :conditions => ["full_name = ? AND ra_library_id = ?", doc.home_page, @library.id])
0
+ if(home_page != nil)
0
+ @library.main_container_id = home_page.id
0
+ end
0
+ end
0
+
0
+ attr = doc.attributes
0
+ attr.delete("home_page")
0
+ @library.update_attributes(attr)
0
+ else
0
+ # Each record has a unique id. This id changes though when we insert it into the
0
+ # database. This would not be a problem except that all of the records are associated
0
+ # by these ids so when a record is inserted into the DB we need to make sure the associations are
0
+ # updated. get_ids takes care of this updating.
0
+ #
0
+ # IMPORTANT NOTE: This requires that the YAML is written out in the correct order so that we always insert
0
+ # a record before we need its id for an association
0
+ id = doc.id
0
+ get_ids(doc, lookup)
0
+ lookup[id] = insert_object(doc)
0
+ count = count + 1
0
+ end
0
+ }
0
      
0
- # Set the logging level to ERROR so that we do not output SQL when we are debugging
0
- # that really slows things down
0
- loglevel = logger.level
0
- logger.level = Logger::ERROR
0
-
0
- autocommit(true)
0
-
0
- count = 0
0
- # Read in the YAML file
0
- yp = YAML::load_documents( @params['doc_file'] ) { |doc|
0
-
0
- # Each record has a unique id. This id changes though when we insert it into the
0
- # database. This would not be a problem except that all of the records are associated
0
- # by these ids so when a record is inserted into the DB we need to make sure the associations are
0
- # updated. get_ids takes care of this updating.
0
- #
0
- # IMPORTANT NOTE: This requires that the YAML is written out in the correct order so that we always insert
0
- # a record before we need its id for an association.
0
- id = doc.id
0
- get_ids(doc, lookup)
0
- lookup[id] = insert_object(doc)
0
- count = count + 1
0
- }
0
-
0
- autocommit(false)
0
-
0
- @total_time = Time.now - start
0
- @total_count = count
0
- logger.level = loglevel
0
- else
0
- @error = "Could not import library, already exists"
0
- end
0
- end
0
-
0
- render :action => :create
0
- end
0
-
0
-private
0
-
0
- # Insert an active record object into the DB
0
- def insert_object(obj)
0
-
0
- if(@connection_type == "mysql")
0
- obj.id = nil
0
- # We don't want to use the ActiveRecord::Base.save method because it is slow
0
- # ~1300 inserts on a MySQL DB MyIASM table on windows is 5.7 seconds with this method
0
- # and 17.5 seconds using .save
0
- unless(@col_hash[obj.class.to_s])
0
- @col_hash[obj.class.to_s] = column_list(obj.attributes.keys)
0
- end
0
- columns = @col_hash[obj.class.to_s]
0
- values = value_list(obj.attributes.values)
0
-
0
- val = @connection.insert "INSERT INTO #{obj.class.table_name} (#{columns}) VALUES (#{values})"
0
- else
0
- # slower but works on PostgreSQL and other DBs where they cannot have a NULL id in the insert
0
- obj.save
0
- obj.id
0
+ if(!tempfile)
0
+ file.close
0
+ end
0
+ autocommit(true)
0
    end
0
- end
0
+
0
+ logger.level = loglevel
0
+ @total_time = Time.now - start
0
+ @total_count += count
0
+ end
0
+
0
+ def insert_object(obj)
0
+ # We don't want to use the ActiveRecord::Base.save method because it is slow
0
+ # ~1300 inserts on a MySQL DB MyIASM table on windows is 5.7 seconds with this method
0
+ # and 17.5 seconds using .save
0
+ unless(@col_hash[obj.class.to_s])
0
+ @col_hash[obj.class.to_s] = column_list(obj.attributes.keys.reject! { |key| key =~ /^id$/ })
0
+ end
0
+ columns = @col_hash[obj.class.to_s]
0
+ attributes = obj.attributes
0
+ attributes.delete('id') # remove the id column
0
+ values = value_list(attributes.values) if attributes
0
+ val = @connection.insert "INSERT INTO #{obj.class.table_name} (#{columns}) VALUES (#{values})" if values
0
+ end
0
   
0
   # Create a list of column names from an array of strings
0
   def column_list(keys)
0
@@ -202,36 +257,60 @@ private
0
 
0
   # Create a list of insertable values from an array of strings
0
   def value_list(values)
0
- values.map { |v| ActiveRecord::Base.connection.quote(v).gsub('\\n', "\n").gsub('\\r', "\r") }.join(", ")
0
+ values.map { |v| ActiveRecord::Base.connection.quote(v).gsub('\n', "\\n").gsub('\r', "\\r") }.join(", ")
0
   end    
0
     
0
   # Lookup the database ids for all the associations for this type of object
0
- def get_ids(doc, lookup)
0
-
0
+ def get_ids(doc, lookup)
0
     case(doc.class.superclass.to_s)
0
       when RaCodeObject.to_s
0
         doc.ra_container_id = lookup[doc.ra_container_id]
0
+ doc.file_id = lookup[doc.file_id]
0
       when RaContainer.to_s
0
         doc.parent_id = lookup[doc.parent_id]
0
         doc.ra_comment_id = lookup[doc.ra_comment_id]
0
- doc.ra_library_id = @library.id
0
+ doc.ra_library_id = @library.id
0
     end
0
     
0
+ puts "#{doc.class.to_s} - #{doc.id}"
0
+ pp doc.attributes
0
+ puts "**"
0
+
0
     case(doc.class.to_s)
0
+ when RaClass.to_s
0
+ doc.file_id = lookup[doc.file_id]
0
+ when RaModule.to_s
0
+ doc.file_id = lookup[doc.file_id]
0
       when RaMethod.to_s
0
         doc.ra_container_id = lookup[doc.ra_container_id]
0
         doc.ra_comment_id = lookup[doc.ra_comment_id]
0
         doc.ra_source_code_id = lookup[doc.ra_source_code_id]
0
+ doc.file_id = lookup[doc.file_id]
0
       when RaInFile.to_s
0
         doc.ra_container_id = lookup[doc.ra_container_id]
0
     end
0
   end
0
   
0
- # if none of these classes has been previously used then the
0
- # YAML load will fail because it will not find the class to be able to
0
- # instantiate it, not sure why this happens, but this here fixes that issue
0
- # by creating an instance of each class needed
0
- def init_classes()
0
+ # Try and avoid errors for PostgreSQL and maybe other databases too.
0
+ # If it fails, we log an exception instead of putting up an error page
0
+ # because we can still complete the mission, just slower?
0
+ def autocommit(state=true)
0
+ begin
0
+ ActiveRecord::Base.connection.execute('SET AUTOCOMMIT = ' + (state ? '1' : '0') )
0
+ rescue
0
+ begin
0
+ ActiveRecord::Base.connection.execute( state ? 'BEGIN' : 'COMMIT' )
0
+ rescue Exception => e
0
+ logger.info "Oops. #{e.to_s}\n" + e.backtrace.join + "\n"
0
+ end
0
+ end
0
+ end
0
+
0
+ # if none of these classes has been previously used then the
0
+ # YAML load will fail because it will not find the class to be able to
0
+ # instantiate it, not sure why this happens, but this here fixes that issue
0
+ # by creating an instance of each class needed
0
+ def init_classes()
0
    c = RaCodeObject.new
0
    c = RaInFile.new
0
    c = RaFile.new
...
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
...
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