~ubuntu-branches/ubuntu/utopic/tdiary/utopic

« back to all changes in this revision

Viewing changes to contrib2/util/image-gallery/image-gallery.rb.euc-jp

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2011-04-11 21:53:16 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110411215316-ih4gt4q8p29d2wf8
Tags: 3.0.1-1
* New upstream release (Closes: #542801, #594947)
* debian/control:
 - Bumped up Standards-Version to 3.9.1.
 - Updated version dependency.
* debian/tdiary-setup.rb: Followed the upstream changes, incorporating js and
  index.fcgi

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
# image-gallery.rb $Revision: 2.0.0 $
 
3
#
 
4
# Copyright (c) 2005-2010 N.KASHIJUKU <n-kashi[at]whi.m-net.ne.jp>
 
5
# You can redistribute it and/or modify it under GPL2.
 
6
 
 
7
if FileTest::symlink?( __FILE__ ) then
 
8
  org_path = File::dirname( File::readlink( __FILE__ ) )
 
9
else
 
10
  org_path = File::dirname( __FILE__ )
 
11
end
 
12
$:.unshift( org_path.untaint )
 
13
require 'tdiary'
 
14
require 'pstore'
 
15
require 'date'
 
16
 
 
17
 
 
18
# class TDiaryGallery
 
19
#
 
20
module TDiary
 
21
  class ImageData
 
22
    attr_reader :file, :url, :title, :subtitle, :date, :width, :height, :type
 
23
    attr_writer :file, :url, :title, :subtitle, :date, :width, :height, :type
 
24
  end
 
25
 
 
26
  
 
27
  class TDiaryGallery < ::TDiary::TDiaryBase
 
28
    MAX_PAGES = 20
 
29
    ORDER_OPTIONS = [
 
30
    ["asc", "��������"],
 
31
    ["desc", "�Ť���"],
 
32
    ]
 
33
    MODE_OPTIONS = [
 
34
    ["list", "�ꥹ��"],
 
35
    ["slide", "���饤��"],
 
36
    ]
 
37
 
 
38
    def initialize( cgi, rhtml, conf )
 
39
      super
 
40
      @img_version = "2.0.0"
 
41
      @image_hash = Hash[]
 
42
      @image_num = 0
 
43
      @image_keys = []
 
44
      @images = []
 
45
      @exifstr = []
 
46
      @t_page_title = ""
 
47
 
 
48
      get_conf(conf)
 
49
      parse_args(cgi)
 
50
      format_form
 
51
      read_cache
 
52
      make_image_data
 
53
      check_name_filter_dateformat
 
54
      make_page_title
 
55
    end
 
56
 
 
57
    private
 
58
 
 
59
    def get_conf(conf)
 
60
      @column = conf.options['image-gallery.column'].to_i
 
61
      @column = 3 if @column == 0
 
62
      @line   = conf.options['image-gallery.line'].to_i
 
63
      @line   = 4 if @line == 0
 
64
      @num = @line * @column
 
65
      @width  = conf.options['image-gallery.width']
 
66
      @width  = "160" if @width == nil
 
67
      @vwidth = conf.options['image-gallery.vwidth']
 
68
      @vwidth = "640" if @vwidth == nil 
 
69
      @show_exif = conf.options['image-gallery.show_exif']
 
70
      @show_exif = false if @show_exif == nil
 
71
      @use_mid_image = conf.options['image-gallery.use_mid_image']
 
72
      @use_mid_image = false if @use_mid_image == nil
 
73
    end
 
74
 
 
75
    def parse_args(cgi)
 
76
      @start = cgi["start"].to_i
 
77
      @order = @cgi["order"].empty? ? "asc" : @cgi["order"]
 
78
      @name_filter  = @cgi["name"].empty? ? "" : @cgi["name"].strip
 
79
      @title_filter = @cgi["title"].empty? ? "" : @cgi["title"].strip
 
80
      @subtitle_filter = @cgi["subtitle"].empty? ? "" : @cgi["subtitle"].strip
 
81
      @mode = @cgi["mode"].empty? ? "list" : @cgi["mode"].strip
 
82
      @mode = "list" if @mode != "viewer" and @mode != "slide" and @mode != "fslide"
 
83
      @key   = cgi["key"].empty? ? "" : cgi["key"].strip
 
84
      @page_title = cgi["pagetitle"].empty? ? "" : @cgi["pagetitle"].strip
 
85
      @show_inputfield = true;
 
86
      show_inputfield = @cgi["showinputfield"].strip
 
87
      @show_inputfield = false if show_inputfield == "false"
 
88
    end
 
89
 
 
90
    def read_cache
 
91
      db = PStore.new("#{cache_path}/gallery/image-gallery2.dat")
 
92
      db.transaction do
 
93
        @image_hash = db["recent_image_hash"]
 
94
        @image_keys = db["recent_image_keys"]
 
95
        @image_url  = db["recent_image_url"]
 
96
        @image_dir  = db["recent_image_dir"]
 
97
        db.abort
 
98
      end
 
99
    end
 
100
 
 
101
    def make_image_data
 
102
      if @name_filter != "" or @title_filter != "" or @subtitle_filter != ""
 
103
        @image_keys.reject! { |key|
 
104
          image = @image_hash[key]
 
105
          (@name_filter  != "" and image.file.match(@name_filter) == nil) or
 
106
          (@title_filter != "" and image.title.match(@title_filter) == nil) or
 
107
          (@subtitle_filter != "" and image.subtitle.match(@subtitle_filter) == nil)
 
108
        }
 
109
      end
 
110
      @image_num = @image_keys.length
 
111
      @image_keys = @image_keys.reverse if @order == "asc"
 
112
 
 
113
      if @mode == "list" or @mode == "slide" or @mode == "fslide"
 
114
        if @key != ""
 
115
          index = @image_keys.index(@key)
 
116
          if index != nil
 
117
            @start = (index / @num) * @num
 
118
          else
 
119
            return
 
120
          end
 
121
        end
 
122
        @num.times do |i|
 
123
          index = @start + i
 
124
          break if @image_keys[index] == nil
 
125
          @images.push(@image_hash[@image_keys[index]])
 
126
        end
 
127
      elsif @mode == "viewer"
 
128
        if @key != ""
 
129
          index = @image_keys.index(@key)
 
130
          if index != nil
 
131
            @start = index
 
132
          else
 
133
            @start = 0
 
134
          end
 
135
        end
 
136
        @images.push(@image_hash[@image_keys[@start]])
 
137
        width, height = @images[0].width.to_i, @images[0].height.to_i
 
138
        size = ((width > height ? width : height) > @vwidth.to_i) ? @vwidth : width
 
139
        if width > height
 
140
          @sizestr = %Q[width="#{size}" height="#{(size.to_i*height/width).to_s}"]
 
141
        else
 
142
          @sizestr = %Q[width="#{(size.to_i*width/height).to_s}" height="#{size}"]
 
143
        end
 
144
        if @show_exif and @images[0].type == "jpg"
 
145
          begin
 
146
            require 'exifparser'
 
147
            @exifstr = read_exif_data("#{@image_dir}/#{@images[0].file}")
 
148
          rescue
 
149
            exp = []
 
150
            exp.push(($!).to_s)
 
151
            ($!).backtrace.each do |btinfo|
 
152
              exp.push(btinfo)
 
153
            end
 
154
            @exifstr = exp
 
155
          end
 
156
        end
 
157
      end
 
158
    end
 
159
 
 
160
    def check_name_filter_dateformat
 
161
      @page_year  = ""
 
162
      @page_month = ""
 
163
      @page_day   = ""
 
164
      @page_date  = nil
 
165
      return if @name_filter == ""
 
166
      begin
 
167
        if @name_filter.index(/[\d]{8}/) != nil
 
168
          @page_year  = @name_filter[0,4]
 
169
          @page_month = @name_filter[4,2]
 
170
          @page_day   = @name_filter[6,2]
 
171
          @page_date  = Date.new(@page_year.to_i, @page_month.to_i, @page_day.to_i)
 
172
 
 
173
        elsif @name_filter.index(/[\d]{6}/) != nil
 
174
          @page_year  = @name_filter[0,4]
 
175
          @page_month = @name_filter[4,2]
 
176
          @page_date  = Date.new(@page_year.to_i, @page_month.to_i, 1)
 
177
 
 
178
        elsif @name_filter.index(/[\d]{4}/) != nil
 
179
          @page_year  = @name_filter[0,4]
 
180
          @page_date  = Date.new(@page_year.to_i, 1, 1)
 
181
        end
 
182
      rescue
 
183
        @page_year  = ""
 
184
        @page_month = ""
 
185
        @page_day   = ""
 
186
        @page_date  = nil
 
187
        return
 
188
      end
 
189
    end
 
190
 
 
191
    def make_page_title
 
192
      return if @page_title == ""
 
193
      @t_page_title = String.new(@page_title)
 
194
      @t_page_title.gsub!("@year",  @page_year)
 
195
      @t_page_title.gsub!("@month", @page_month)
 
196
      @t_page_title.gsub!("@day",   @page_day)
 
197
      begin
 
198
        @t_page_title.gsub!("@subtitle", @images[0].subtitle)
 
199
      rescue
 
200
      end
 
201
    end
 
202
    
 
203
    def format_links(count)
 
204
      page_count = (count - 1) / @num + 1
 
205
      current_page = @start / @num + 1
 
206
      first_page = current_page - (MAX_PAGES / 2 - 1)
 
207
      if first_page < 1
 
208
        first_page = 1
 
209
      end
 
210
      last_page = first_page + MAX_PAGES - 1
 
211
      if last_page > page_count
 
212
        last_page = page_count
 
213
      end
 
214
      buf = "<p id=\"navi\" class=\"infobar\">\n"
 
215
      if current_page > 1
 
216
        buf << format_link("&laquo;��Ƭ��&nbsp;", 0, 0, @mode)
 
217
        buf << format_link("&lt;����", @start - @num, @num, @mode)
 
218
      end
 
219
      if first_page > 1
 
220
        buf << "... "
 
221
      end
 
222
      for i in first_page..last_page
 
223
        if i == current_page
 
224
          buf << "#{i} "
 
225
        else
 
226
          buf << format_link(i.to_s, (i - 1) * @num, @num, @mode)
 
227
        end
 
228
      end
 
229
      if last_page < page_count
 
230
        buf << "... "
 
231
      end
 
232
      if current_page < page_count
 
233
        buf << format_link("����&gt;", @start + @num, @num, @mode)
 
234
        buf.concat(format_link("&nbsp;�Ǹ��&raquo;", (page_count - 1) * @num, 0, @mode))
 
235
      end
 
236
      buf << "</p>\n"
 
237
      return buf
 
238
    end
 
239
 
 
240
    def format_link(label, start, num, mode)
 
241
      return format('<a href="%s?mode=%s;%sstart=%d">%s</a> ',
 
242
              _(@cgi.script_name ? @cgi.script_name : ''),
 
243
              mode,
 
244
              make_cgi_param,
 
245
              start, label)
 
246
    end
 
247
 
 
248
    def format_links_viewer
 
249
      buf = "<p id=\"vnavi\" class=\"infobar\">\n"
 
250
      if @start == 0
 
251
        buf << "&laquo;����"
 
252
      else
 
253
        buf << format_link_viewer("&laquo;����", @image_keys[@start - 1])
 
254
      end
 
255
      buf << "&nbsp;&nbsp;|&nbsp;&nbsp;"
 
256
      buf << format_link("������", (@start / @num) * @num, 0, "list")
 
257
      buf << "&nbsp;&nbsp;|&nbsp;&nbsp;"
 
258
      if @start == @image_keys.length - 1
 
259
        buf << "����&raquo;"
 
260
      else
 
261
        buf << format_link_viewer("����&raquo;", @image_keys[@start + 1])
 
262
      end
 
263
      buf << "</p>\n"
 
264
      return buf
 
265
    end
 
266
 
 
267
    def format_link_viewer(label, key)
 
268
      return format('<a href="%s?%smode=viewer;key=%s">%s</a>',
 
269
              _(@cgi.script_name ? @cgi.script_name : ''),
 
270
              make_cgi_param,
 
271
              key, label)
 
272
    end
 
273
 
 
274
    def format_link_viewer_image(key)
 
275
      return format('%s?%smode=viewer;key=%s',
 
276
              _(@cgi.script_name ? @cgi.script_name : ''),
 
277
              make_cgi_param,
 
278
              key)
 
279
    end
 
280
 
 
281
    def format_links_date
 
282
      return "" unless @name_filter != "" and @title_filter == "" and  @subtitle_filter == ""
 
283
 
 
284
      begin
 
285
        buf = "<p id=\"ynavi\" class=\"infobar\">\n"
 
286
        if @page_day != ""
 
287
          yesterday = (@page_date - 1).strftime("%Y%m%d")
 
288
          tomorrow  = (@page_date + 1).strftime("%Y%m%d")
 
289
          buf << format_link_date(%Q[&laquo;#{(@page_date - 1).to_s}], yesterday)
 
290
          buf << format('&nbsp;&nbsp;|&nbsp;&nbsp;<a href="%s?mode=%s;order=%s">%s</a>&nbsp;&nbsp;|&nbsp;&nbsp;', _(@cgi.script_name ? @cgi.script_name : ''), _(@mode), _(@order), '������')
 
291
          buf << format_link_date(%Q[#{(@page_date + 1).to_s}&raquo;], tomorrow)
 
292
 
 
293
        elsif @page_month != ""
 
294
          prevmonth = (@page_date << 1).strftime("%Y%m")
 
295
          nextmonth = (@page_date >> 1).strftime("%Y%m")
 
296
          buf << format_link_date(%Q[&laquo;#{(@page_date << 1).to_s[0,7]}], prevmonth)
 
297
          buf << format('&nbsp;&nbsp;|&nbsp;&nbsp;<a href="%s?mode=%s;order=%s">%s</a>&nbsp;&nbsp;|&nbsp;&nbsp;', _(@cgi.script_name ? @cgi.script_name : ''), _(@mode), _(@order), '������')
 
298
          buf << format_link_date(%Q[#{(@page_date >> 1).to_s[0,7]}&raquo;], nextmonth)
 
299
 
 
300
        elsif @page_year != ""
 
301
          year = @page_year.to_i
 
302
          buf << format_link_date(%Q[&laquo;#{(year - 1).to_s}], (year - 1).to_s)
 
303
          buf << format('&nbsp;&nbsp;|&nbsp;&nbsp;<a href="%s?mode=%s;order=%s">%s</a>&nbsp;&nbsp;|&nbsp;&nbsp;', _(@cgi.script_name ? @cgi.script_name : ''), _(@mode), _(@order), '������')
 
304
          buf << format_link_date(%Q[#{(year + 1).to_s}&raquo;], (year + 1).to_s)
 
305
        end
 
306
 
 
307
        buf << "</p>\n"
 
308
        return buf
 
309
      rescue
 
310
        return ""
 
311
      end
 
312
    end
 
313
 
 
314
    def format_link_date(label, name_filter)
 
315
      cgi_params = make_cgi_param
 
316
      if cgi_params.gsub!(/name=[^;]*;/, "name=#{CGI::escape(name_filter)};") == nil
 
317
        cgi_params = "name=" + CGI::escape(name_filter) + ";" + cgi_params
 
318
      end
 
319
      return format('<a href="%s?mode=%s;%s">%s</a>',
 
320
              _(@cgi.script_name ? @cgi.script_name : ''),
 
321
              @mode,
 
322
              cgi_params,
 
323
              label)
 
324
    end
 
325
 
 
326
    def format_link_viewer_date(label, name_filter)
 
327
      return format('<a href="%s?mode=list;order=desc;name=%s">%s</a>',
 
328
              _(@cgi.script_name ? @cgi.script_name : ''),
 
329
              name_filter,
 
330
              label)
 
331
    end
 
332
 
 
333
    def format_link_viewer_category(subtitle)
 
334
      buf = ""
 
335
      subtitle.scan(/\[[^\]]*\]/).each do |category|
 
336
        tag = category[1..-2]
 
337
        next if tag[0] == ?[
 
338
        buf << format('[<a href="%s?mode=list;order=desc;subtitle=%s">%s</a>]',
 
339
              _(@cgi.script_name ? @cgi.script_name : ''),
 
340
              CGI::escape("\\[" + tag + "\\]"), tag)
 
341
      end
 
342
      return buf
 
343
    end
 
344
 
 
345
    def format_link_list_category(images)
 
346
      categories = []
 
347
      images.each do |image|
 
348
        categories |= image.subtitle.scan(/\[[^\]]*\]/)
 
349
      end
 
350
      buf = ""
 
351
      categories.each do |category|
 
352
        tag = category[1..-2]
 
353
        next if tag[0] == ?[
 
354
        buf << format('[<a href="%s?mode=list;order=desc;subtitle=%s">%s</a>]',
 
355
              _(@cgi.script_name ? @cgi.script_name : ''),
 
356
              CGI::escape("\\[" + tag + "\\]"), tag)
 
357
      end
 
358
      return buf
 
359
    end
 
360
 
 
361
    def get_other_mode_link
 
362
      case @mode
 
363
      when "list"
 
364
        format_link("[SLIDESHOW]", @start, @num, "slide") + format_link("[SLIDESHOW(FullScreen)]", @start, @num, "fslide")
 
365
      when "slide"
 
366
        format_link("[LIST]", @start, @num, "list") + format_link("[SLIDESHOW(FullScreen)]", @start, @num, "fslide")
 
367
      when "fslide"
 
368
        format_link("[LIST]", @start, @num, "list") + format_link("[SLIDESHOW]", @start, @num, "slide")
 
369
      end
 
370
    end
 
371
 
 
372
    def make_cgi_param
 
373
      buf = ""
 
374
      buf << "name=#{CGI::escape(@name_filter)};"         if @name_filter != ""
 
375
      buf << "title=#{CGI::escape(@title_filter)};"       if @title_filter != ""
 
376
      buf << "subtitle=#{CGI::escape(@subtitle_filter)};" if @subtitle_filter != ""
 
377
      buf << "pagetitle=#{CGI::escape(@page_title)};"     if @page_title != ""
 
378
      buf << "showinputfield=false;" if not @show_inputfield
 
379
      buf << "order=#{@order};"
 
380
      return buf
 
381
    end
 
382
 
 
383
    def format_options(options, value)
 
384
      return options.collect { |val, label|
 
385
        if val == value
 
386
          "<option value=\"#{_(val)}\" selected>#{_(label)}</option>"
 
387
        else
 
388
          "<option value=\"#{_(val)}\">#{_(label)}</option>"
 
389
        end
 
390
      }.join("\n")
 
391
    end
 
392
 
 
393
    def format_form
 
394
      @order_options = format_options(ORDER_OPTIONS, @order)
 
395
      @mode_options  = format_options(MODE_OPTIONS,  @mode )
 
396
    end
 
397
 
 
398
    def _(str)
 
399
      CGI::escapeHTML(str)
 
400
    end
 
401
 
 
402
    def read_exif_data(file)
 
403
      exifstr = []
 
404
      str = ""
 
405
      exif = ExifParser.new(file)
 
406
      return exifstr if exif == nil
 
407
 
 
408
      exifstr.push("-- IFD0 (main image) --")
 
409
      exif.each(:IFD0) do |tag|
 
410
        next if tag.name == "Unknown"
 
411
        str = "#{tag.name} : #{tag.to_s}"
 
412
        exifstr.push(str)
 
413
      end
 
414
 
 
415
      exifstr.push("-- Exif SubIFD --")
 
416
      exif.each(:Exif) do |tag|
 
417
        next if tag.name == "Unknown"
 
418
        str = "#{tag.name} : #{tag.to_s}"
 
419
        exifstr.push(str)
 
420
      end
 
421
 
 
422
      exifstr.push("-- MakerNote --")
 
423
      exif.each(:MakerNote) do |tag|
 
424
        next if tag.name == "Unknown" or tag.name == "NikonCameraSerialNumber"
 
425
        str = "#{tag.name} : #{tag.to_s}"
 
426
        exifstr.push(str)
 
427
      end
 
428
 
 
429
      exifstr.push("-- GPS --")
 
430
      exif.each(:GPS) do |tag|
 
431
        next if tag.name == "Unknown"
 
432
        str = "#{tag.name} : #{tag.to_s}"
 
433
        exifstr.push(str)
 
434
      end
 
435
 
 
436
      return exifstr
 
437
 
 
438
    end
 
439
 
 
440
 
 
441
    def js_start_gallery
 
442
      if @mode == "fslide"
 
443
      <<-EOS
 
444
      <script type="text/javascript">
 
445
      function startGallery() {
 
446
        var myGallery = new gallery($('myGallery'), {
 
447
          timed: true,
 
448
          fullScreen: true
 
449
        });
 
450
      }
 
451
      window.addEvent('domready',startGallery);
 
452
      </script>
 
453
      EOS
 
454
      elsif @mode == "slide"
 
455
      <<-EOS2
 
456
      <script type="text/javascript">
 
457
      function startGallery() {
 
458
        var myGallery = new gallery($('myGallery'), {
 
459
          timed: true,
 
460
        });
 
461
      }
 
462
      window.addEvent('domready',startGallery);
 
463
      </script>
 
464
      EOS2
 
465
      end
 
466
    end
 
467
  end
 
468
end
 
469
 
 
470
begin
 
471
  @cgi = CGI::new
 
472
  if TDiary::Config.instance_method(:initialize).arity > 0
 
473
    # for tDiary 2.1 or later
 
474
    conf = TDiary::Config::new(@cgi)
 
475
  else
 
476
    # for tDiary 2.0 or earlier
 
477
    conf = TDiary::Config::new
 
478
  end
 
479
  tdiary = TDiary::TDiaryGallery::new( @cgi, 'gallery.rhtml', conf )
 
480
 
 
481
  head = {
 
482
    'type' => 'text/html',
 
483
    'Vary' => 'User-Agent'
 
484
  }
 
485
  body = tdiary.eval_rhtml
 
486
  head['charset'] = conf.encoding
 
487
  head['Content-Length'] = body.size.to_s
 
488
  head['Pragma'] = 'no-cache'
 
489
  head['Cache-Control'] = 'no-cache'
 
490
 
 
491
  print @cgi.header( head )
 
492
  print body
 
493
rescue Exception
 
494
  if @cgi then
 
495
    print @cgi.header( 'type' => 'text/plain' )
 
496
  else
 
497
    print "Content-Type: text/plain\n\n"
 
498
  end
 
499
  puts "#$! (#{$!.class})"
 
500
  puts ""
 
501
  puts $@.join( "\n" )
 
502
end