Class: Milkode::SearchContents

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/cdweb/lib/search_contents.rb

Defined Under Namespace

Classes: MatchRecord

Constant Summary collapse

DISP_NUM =

1ページの表示数

20
LIMIT_NUM =

最大検索ファイル数

50
NTH =

表示範囲

3
COL_LIMIT =

1行の桁制限

200
MATH_FILE_DISP =

マッチファイルの最大表示数

3
MATH_FILE_LIMIT =

マッチファイルの検索リミット数

MATH_FILE_DISP + 1
DEFAULT_WIDE_MATCH_RANGE =

未指定時のワイド検索範囲

7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params, query, suburl) ⇒ SearchContents

Returns a new instance of SearchContents.



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
92
93
94
95
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 29

def initialize(path, params, query, suburl)
  @path             = path
  @params           = params
  @q                = query
  @page             = params[:page].to_i || 0
  @offset           = params[:offset].to_i
  @line             = params[:line].to_i
  @is_onematch      = params[:onematch]  == 'on'
  @is_sensitive     = params[:sensitive] == 'on'
  @suburl           = suburl
  @homeurl          = @suburl + "/home/"

  @searcher_fuzzy_gotoline = nil

  # 検索1 : クエリーそのまま
  @records, @total_records = Database.instance.search(@q.keywords, @q.multi_match_keywords, @q.packages, path, @q.fpaths, @q.suffixs, @q.fpath_or_packages, @offset, LIMIT_NUM)
  grep_contents(@q.keywords, @q.wide_match_range)

  # 検索2 : マッチしなかった時におすすめクエリーがある場合

  # gotolineモード (test_cdstk.rb:55)
  if @match_records.empty? && recommended_fuzzy_gotoline?
    # 専用の Searcher を作成
    @searcher_fuzzy_gotoline = SearchFuzzyGotoLine.new(@path, @params, @q)

    # 結果をコピーする
    @total_records = @searcher_fuzzy_gotoline.total_records
    @match_records = @searcher_fuzzy_gotoline.match_records
    @next_index    = @searcher_fuzzy_gotoline.next_index
    @end_index     = @searcher_fuzzy_gotoline.end_index
    @next_line     = nil
  end

  # ワイド検索範囲
  if @match_records.empty? && recommended_wide_match_range?
    grep_contents(@q.keywords, DEFAULT_WIDE_MATCH_RANGE)

    # 検索範囲0の自動マッチは混乱をまねくのでやめる
    # if @match_records.empty?
    #   grep_contents(@q.keywords, 0)
    # end
  end

  # 先頭をファイル名とみなす自動マッチは混乱をまねくのでやめる
  # if @match_records.empty? && recommended_fpath_or_packages?
  #   # おすすめクエリーに変換
  #   q2 = @q.conv_head_keyword_to_fpath_or_packages
    
  #   # 検索
  #   @records, @total_records = Database.instance.search(q2.keywords, q2.multi_match_keywords, q2.packages, path, q2.fpaths, q2.suffixs, q2.fpath_or_packages, @offset, LIMIT_NUM)
    
  #   # 再grep
  #   grep_contents(q2.keywords, q2.wide_match_range)
  # end
  
  # 検索3 : マッチするファイル
  @match_files = []
  if @offset == 0 && @line == 0
    t = 0

    if (@path != "")
      @match_files, t = Database.instance.search([], @q.multi_match_keywords, @q.packages, path, @q.fpaths + @q.keywords, @q.suffixs, @q.fpath_or_packages, @offset, MATH_FILE_LIMIT)
    else
      @match_files, t = Database.instance.search([], @q.multi_match_keywords, @q.packages, path, @q.fpaths, @q.suffixs, @q.fpath_or_packages + @q.keywords, @offset, MATH_FILE_LIMIT)
    end
  end
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



17
18
19
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 17

def page
  @page
end

#total_recordsObject (readonly)

Returns the value of attribute total_records.



16
17
18
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 16

def total_records
  @total_records
end

Instance Method Details

#data_rangeObject



105
106
107
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 105

def data_range
  @offset..(@offset + @end_index)
end

#directjump?Boolean

Returns:

  • (Boolean)


243
244
245
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 243

def directjump?
  @searcher_fuzzy_gotoline && @searcher_fuzzy_gotoline.directjump?
end

#directjump_urlObject



247
248
249
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 247

def directjump_url
  @searcher_fuzzy_gotoline.directjump_url
end

#html_contentsObject



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
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 109

def html_contents
  match_groups = @match_records.reduce([]) do |g, m|
    if (g.empty?)
      g << [m]
    else
      prev = g[-1][-1]

      if (m.match_line.index - prev.match_line.index <= NTH * 2 &&
          m.record.shortpath == prev.record.shortpath)
        g[-1] << m          # グループの末尾に追加
        g
      else
        g << [m]            # 新規グループ
      end
    end

    # 近接マッチ無効
    # g << [m]
  end

  @prev = nil
  
  <<EOF
#{recommended_contents}
#{match_groups.map{|g|result_match_record(g)}.join}
EOF
end

#html_paginationObject



228
229
230
231
232
233
234
235
236
237
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 228

def html_pagination
  return "" if @q.empty?
  return "" if next_offset >= @total_records

  return <<EOF
<div class='pagination pagination-centered'>
#{pagination_link(next_offset, @next_line, "next >>")}
</div>
EOF
end

#match_files_contentsObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 210

def match_files_contents
  unless @match_files.empty?
    is_and_more = @match_files.size >= MATH_FILE_LIMIT
    @match_files = @match_files[0..MATH_FILE_DISP-1]
    conv_query = (@path != "") ? @q.conv_keywords_to_fpath : @q.conv_keywords_to_fpath_or_packages
    tmpp = @params.clone
    tmpp[:query] = conv_query.query_string
    url = Mkurl.new(@path, tmpp).inherit_query_shead
    <<EOF
#{@match_files.map {|record| result_record(DocumentRecord.new(record))}.join}
#{"<a href='#{url}'>...and more</a></a>" if is_and_more}
<hr>
EOF
  else
    ""
  end
end

#match_numObject



239
240
241
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 239

def match_num
  @match_records.size
end

#next_offsetObject



101
102
103
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 101

def next_offset
  @offset + @next_index
end

#queryObject



97
98
99
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 97

def query
  @q.query_string
end


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 137

def recommended_contents
  contents = []

  str = recommended_query_contents
  contents << str unless str.empty?

  str = match_files_contents
  contents << str unless str.empty?

  unless contents.empty?
    contents.join
  else
    ""
  end
end

Returns:

  • (Boolean)


161
162
163
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 161

def recommended_fpath_or_packages?
  @q.keywords.size >= 2 && @q.only_keywords
end

Returns:

  • (Boolean)


153
154
155
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 153

def recommended_fuzzy_gotoline?
  @q.keywords.size == 1 && @q.only_keywords && Util::fuzzy_gotoline_keyword?(@q.keywords[0])
end


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
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 165

def recommended_query_contents
  result = []
  
  if recommended_fuzzy_gotoline?
    conv_query   = @q.conv_fuzzy_gotoline
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    url          = Mkurl.new(@path, tmpp).inherit_query_shead
    result << "<dt class='result-file'>#{img_icon('document-new-4.png', @suburl)}<a href='#{url}'>#{conv_query.query_string}</a></dt>"
  end

  if recommended_wide_match_range?
    conv_query   = @q.conv_wide_match_range(0)
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    w0_url        = Mkurl.new(@path, tmpp).inherit_query_shead

    conv_query   = @q.conv_wide_match_range(1)
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    w1_url        = Mkurl.new(@path, tmpp).inherit_query_shead

    conv_query   = @q.conv_wide_match_range(DEFAULT_WIDE_MATCH_RANGE)
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    url          = Mkurl.new(@path, tmpp).inherit_query_shead

    result << "<dt class='result-file'>#{img_icon('document-new-4.png', @suburl)}<a href='#{url}'>#{conv_query.query_string}</a> (<a href='#{w0_url}'>w:0</a>, <a href='#{w1_url}'>w:1</a>)</dt>"
  end
  
  if recommended_fpath_or_packages?
    conv_query   = @q.conv_head_keyword_to_fpath_or_packages
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    url          = Mkurl.new(@path, tmpp).inherit_query_shead
    result << "<dt class='result-file'>#{img_icon('document-new-4.png', @suburl)}<a href='#{url}'>#{conv_query.query_string}</a></dt>"
  end

  unless result.empty?
    result.join("\n") + "<hr>\n"
  else
    ""
  end
end

Returns:

  • (Boolean)


157
158
159
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 157

def recommended_wide_match_range?
  @q.keywords.size >= 2 && @q.wide_match_range_empty?
end