docs: function getRatedFragment(params, config, conn) {
  
    // defaults
    config = config || require('../config/config.js')
  
    // main
    return new Promise((resolve, reject) => {
      debug('getRatedFragment', params)
  
      if (!conn) {
        var conn = wc_db.getConnection(config.db)
      }
  
      var tagJoins = ''
      var tagClause = ''
      if (params.tag) {
        tagJoins = ' join MediaTag mt on mt.media_id = a.id join Tag t on t.id = mt.tag_id '
        tagClause = ' and tag = :tag '
      }
  
      var webidJoins = ''
      var webidClause = ''
      if (params.webid) {
        webidJoins = ' join User u on u.id = f.user_id '
        webidClause = ' and u.uri = :webid '
      }
  
      var safeClause = ''
      if (params.safe !== undefined && params.safe !== null) {
        if (parseInt(params.safe) === 0) {
          safeClause = ''
        } else {
          safeClause = ' and a.safe = :safe '
        }
      } else {
        safeClause = ' and a.safe = 1 '
      }
  
      var sql = "select m.length, a.uri, a.id, max(f.end) end, a.contentType, max(f.rating) mrat, m.subtitlesURI, m.charenc from Media a join Fragment f on  a.id = f.id join Meta m on m.id = a.id " + webidJoins + tagJoins + " where contentType = 'video' " + webidClause + tagClause + "  " + safeClause + " group by f.id having mrat >= 9 and m.length >  end  order by RAND() limit 1;"
  
      debug(sql)
  
      conn.query(sql, { replacements: { "tag" : params.tag, "webid" : params.webid, "safe" : params.safe } }).then(function(ret){
        debug('getRatedFragment', ret)
        return resolve({"ret" : ret, "conn" : conn})
      }).catch(function(err) {
        return reject({"err" : err, "conn" : conn})
      })
  
    })
  
  }