mw_utils.rb 3.78 KB
Newer Older
1 2 3 4
require 'open-uri'
require 'uri'
require 'net/http'
require 'net/https'
5
require 'mediawiki_api'
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

#Adding method to mediawiki_api client
module MediawikiApi

  class Client

    def get_page_content(page_name)
      get_conn = Faraday.new(url: MW::BASE_URL + "index.php/#{page_name}") do |faraday|
        faraday.request :multipart
        faraday.request :url_encoded
        faraday.use :cookie_jar, jar: @cookies
        faraday.use FaradayMiddleware::FollowRedirects
        faraday.adapter Faraday.default_adapter
      end
      params = {
        :token_type => false,
        :action => 'raw'
      }
      params[:token] = get_token(:csrf)
      res = get_conn.send(:get, '', params)
      res.body
    end
  end
29

30
end
31 32 33 34 35 36

#Defines global Grid5000 helpers (TODO move to its own file once it is big enough)
module G5K

  SITES = %w{grenoble lille luxembourg lyon nancy nantes rennes sophia}

37
  # This method compacts an array of integers as follows
38 39
  # nodeset([2,3,4,7,9,10,12]) returns the string '[2-4,<wbr>7,<wbr>9-10,<wbr>12]'
  # where <wbr> is a hidden tag that enables carriage return in wikimedia
40 41 42 43 44 45 46 47
  def self.nodeset(a)
    l = a.length
    return '' if l == 0
    a = a.compact.uniq.sort
    a0 = a[0]
    s = "[#{a0}"
    i = 1
    while i < l
48
      fast_forward = (i < l and a[i] - a0 == 1) ? true : false
49 50
      (a0 = a[i] and i+=1 ) while (i < l and a[i] - a0 == 1) # fast forward
      if fast_forward
51
        s += (i != l) ? "-#{a0},<wbr>#{a[i]}" : "-#{a0}"
52
      else
53
        s += ",<wbr>#{a[i]}"
54 55 56 57 58 59
      end
      a0 = a[i]
      i += 1
    end
    s += ']'
  end
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

  def self.get_size(x)
    gbytes = (x.to_f / 2**30).floor
    if gbytes < 2**10
      gbytes.to_s + '&nbsp;GB'
    else
      (x.to_f / 2**40).round(3).to_s + '&nbsp;TB'
    end
  end
  
  def self.get_rate(x)
    return '' if (x == 0 || x.nil?)
    mbps = (x.to_f / 10**6).floor
    if mbps < 1000
      mbps.to_s + '&nbsp;Mbps'
    else
      (x.to_f / 10**9).floor.to_s + '&nbsp;Gbps'
    end
  end
79
  
80 81
end

82 83 84
#Defines MediaWiki helpers
module MW

85 86 87
  BASE_URL = "https://www.grid5000.fr/mediawiki/"

  API_URL = BASE_URL + "api.php"
88 89 90 91 92 93 94 95 96 97 98 99 100

  TABLE_START = "{|"

  TABLE_END = "|}"

  TABLE_ROW = "|-"

  TABLE_HEADER = "!"

  INLINE_CELL = "||"

  TABLE_CELL = "|"

101 102 103 104
  UNSORTED_INLINE_CELL = "!!"

  UNSORTED_TABLE_CELL = "!"
  
105 106
  LINE_FEED = "\n"

107 108 109 110
  LIST_ITEM = "*"

  NUMBERED_LIST_ITEM = "#"

111 112 113 114 115 116 117 118
  HTML_LINE_FEED = "<br />"

  def self.generate_table(table_options, columns, rows)
    table_text = MW::TABLE_START + table_options

    table_text += MW::LINE_FEED + MW::TABLE_ROW + MW::LINE_FEED

    columns.each { |col|
119 120 121 122 123
      if col.kind_of?(Hash)
        table_text += MW::TABLE_HEADER + col[:attributes] + MW::TABLE_CELL + col[:text] + MW::LINE_FEED
      else
        table_text += MW::TABLE_HEADER + MW::TABLE_CELL + col + MW::LINE_FEED
      end
124 125 126
    }

    rows.each { |row|
127 128 129 130 131 132 133 134
      if row.kind_of?(Hash) and row[:sort] == false
        row = row[:columns]
        table_cell = MW::UNSORTED_TABLE_CELL
        inline_cell = MW::UNSORTED_INLINE_CELL
      else
        table_cell = MW::TABLE_CELL
        inline_cell = MW::INLINE_CELL
      end
135 136 137
      table_text += MW::TABLE_ROW + MW::LINE_FEED
      row.each_with_index{ |cell, i|
        if (i == 0)
138
          table_text += table_cell
139
        else
140
          table_text += inline_cell
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        end
        table_text += cell.to_s
      }
      table_text += MW::LINE_FEED
    }
    table_text += MW::LINE_FEED + MW::TABLE_END
    return table_text
  end

  def self.small(text)
    "<small>" + text + "</small>"
  end

  def self.big(text)
    "<big>" + text + "</big>"
  end

  def self.italic(text)
    "''" + text + "''"
  end
161
 
162 163 164
  def self.bold(text)
    "'''" + text + "'''"
  end
165 166 167 168 169 170 171 172 173

  def self.heading(text, level = 1)
    "#{'=' * level} " + text + " #{'=' * level}"
  end

  def self.code(text)
    "<code>" + text + "</code>"
  end   

174
end