1

代码被困在 REXML 行上的无限循环中。我已经调试过了,奇怪的是,那行代码一次又一次地运行,永远。没有任何意义。

此方法来自 mediawiki-gateway gem https://github.com/jpatokal/mediawiki-gateway

# Get a list of pages with matching content in given namespaces
#
# [key] Search key
# [namespaces] Array of namespace names to search (defaults to main only)
# [limit] Maximum number of hits to ask for (defaults to 500; note that Wikimedia Foundation wikis allow only 50 for normal users)
#
# Returns array of page titles (empty if no matches)
def search(key, namespaces=nil, limit=@options[:limit])
  titles = []
  offset = nil
  in_progress = true

  form_data = { 'action' => 'query',
    'list' => 'search',
    'srwhat' => 'text',
    'srsearch' => key,
    'srlimit' => limit
  }
  if namespaces
    namespaces = [ namespaces ] unless namespaces.kind_of? Array
    form_data['srnamespace'] = namespaces.map! do |ns| namespaces_by_prefix[ns] end.join('|')
  end
  begin
    form_data['sroffset'] = offset if offset
    res, offset = make_api_request(form_data, '//query-continue/search/@sroffset')
    titles += REXML::XPath.match(res, "//p").map { |x| x.attributes["title"] }
  end while offset
  titles
end

重现步骤如下

打开一个irb和代码

require 'media_wiki'

options = {
      :limit => 50
}
@mw = MediaWiki::Gateway.new('http://en.wikipedia.org/w/api.php', options)
@mw.search('Strange Loop')

不幸的是,它永远不会结束。

4

0 回答 0