Attention une mise à jour du serveur va être effectuée le lundi 17 mai entre 13h et 13h30. Cette mise à jour va générer une interruption du service de quelques minutes.

mw_utils.rb 5.4 KB
Newer Older
1 2 3
require 'active_support'
require 'active_support/core_ext/object/deep_dup'

4
require 'pp'
5 6 7 8
require 'open-uri'
require 'uri'
require 'net/http'
require 'net/https'
9
require 'mediawiki_api'
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

#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)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      res.body
    end
    
    def get_file_content(file_name)
      get_conn = Faraday.new(url: MW::BASE_URL + "images/#{file_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)
47 48
      res.body
    end
49 50 51 52 53 54 55

    def update_file(filename, path, content_type, comment, ignorewarnings, text = nil)
      file = Faraday::UploadIO.new(path, content_type)
      action(:upload,
             filename: filename, file: file, comment: comment, text: text,
             ignorewarnings: ignorewarnings)
    end
56
  end
57

58
end
59 60 61 62

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

63
  # This method compacts an array of integers as follows
64 65
  # 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
66 67 68
  def self.nodeset(a)
    l = a.length
    return '' if l == 0
69
    return a.first.to_s if l == 1
70 71 72 73 74
    a = a.compact.uniq.sort
    a0 = a[0]
    s = "[#{a0}"
    i = 1
    while i < l
75
      fast_forward = (i < l and a[i] - a0 == 1) ? true : false
76 77
      (a0 = a[i] and i+=1 ) while (i < l and a[i] - a0 == 1) # fast forward
      if fast_forward
78
        s += (i != l) ? "-#{a0},<wbr>#{a[i]}" : "-#{a0}"
79
      else
80
        s += ",<wbr>#{a[i]}"
81 82 83 84 85 86
      end
      a0 = a[i]
      i += 1
    end
    s += ']'
  end
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  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
106 107 108 109

  def self.pluralize(count, word)
    return (count == 1 || word[-1] == 's') ? word : word + 's'
  end
110
  
111 112 113
  @@global_hash = nil
  def self.get_global_hash
    if @@global_hash.nil?
114
      @@global_hash = load_yaml_file_hierarchy
115 116
    end
    # return a deep copy of global_hash
117
    return @@global_hash.deep_dup
118 119 120
  end

  SITES = get_global_hash['sites'].keys.sort
121 122
end

123 124 125
#Defines MediaWiki helpers
module MW

126 127 128
  BASE_URL = "https://www.grid5000.fr/mediawiki/"

  API_URL = BASE_URL + "api.php"
129 130 131 132 133 134 135 136 137 138 139 140 141

  TABLE_START = "{|"

  TABLE_END = "|}"

  TABLE_ROW = "|-"

  TABLE_HEADER = "!"

  INLINE_CELL = "||"

  TABLE_CELL = "|"

142 143 144 145
  UNSORTED_INLINE_CELL = "!!"

  UNSORTED_TABLE_CELL = "!"
  
146 147
  LINE_FEED = "\n"

148 149 150 151
  LIST_ITEM = "*"

  NUMBERED_LIST_ITEM = "#"

152 153 154 155 156 157 158 159
  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|
160 161 162 163 164
      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
165 166 167
    }

    rows.each { |row|
168 169 170 171 172 173 174 175
      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
176 177 178
      table_text += MW::TABLE_ROW + MW::LINE_FEED
      row.each_with_index{ |cell, i|
        if (i == 0)
179
          table_text += table_cell
180
        else
181
          table_text += inline_cell
182 183 184 185 186 187 188 189 190
        end
        table_text += cell.to_s
      }
      table_text += MW::LINE_FEED
    }
    table_text += MW::LINE_FEED + MW::TABLE_END
    return table_text
  end

191 192 193 194 195 196 197 198 199 200 201
  def self.generate_hash_table(hash)
    format = " valign=\"top\" style=\"background-color: #f9f9f9; padding: 0px 10px 0px 3px;\" "
    table = [MW::TABLE_START, MW::TABLE_ROW]
    hash.each { |k, v|
      table << MW::TABLE_CELL + format + MW::TABLE_CELL + "'''#{k}:'''"
      table << MW::TABLE_CELL + " #{v}<br/>"
      table << MW::TABLE_ROW
    }
    (table << MW::TABLE_END).join(MW::LINE_FEED)
  end

202 203 204 205 206 207 208 209 210 211 212
  def self.small(text)
    "<small>" + text + "</small>"
  end

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

  def self.italic(text)
    "''" + text + "''"
  end
213
 
214 215 216
  def self.bold(text)
    "'''" + text + "'''"
  end
217 218 219 220 221 222 223 224 225

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

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

226
end