text:
package database

import (
	"bytes"
	"encoding/gob"
	"errors"
	"flag"
	"fmt"
	"log"
	"math"
	"net/url"
	"os"
	"path"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/garyburd/redigo/redis"
	"github.com/shopkeep/gddo/doc"
	"github.com/shopkeep/gddo/gosrc"
	"github.com/golang/snappy"
)

type Database struct {
	Pool interface {
		Get() redis.Conn
	}
}

type Package struct {
	Path     string `json:"path"`
	Synopsis string `json:"synopsis,omitempty"`
}

type byPath []Package

func (p byPath) Len() int           { return len(p) }
func (p byPath) Less(i, j int) bool { return p[i].Path < p[j].Path }
func (p byPath) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

var (
	redisServer      = flag.String("db-server", "redis://127.0.0.1:6379", "URI of Redis server.")
	redisIdleTimeout = flag.Duration("db-idle-timeout", 250*time.Second, "Close Redis connections after remaining idle for this duration.")
	redisLog         = flag.Bool("db-log", false, "Log database commands")
)

func dialDb() (c redis.Conn, err error) {
	u, err := url.Parse(*redisServer)
	if err != nil {
		return nil, err
	}

	defer func() {
		if err != nil && c != nil {
			c.Close()
		}
	}()

	c, err = redis.Dial("tcp", u.Host)
	if err != nil {
		return
	}

	if *redisLog {
		l := log.New(os.Stderr, "", log.LstdFlags)
		c = redis.NewLoggingConn(c, l, "")
	}

	if u.User != nil {
		if pw, ok := u.User.Password(); ok {
			if _, err = c.Do("AUTH", pw); err != nil {
				return
			}
		}
	}
	return
}

// New creates a database configured from command line flags.
func New() (*Database, error) {
	pool := &redis.Pool{
		Dial:        dialDb,
		MaxIdle:     10,
		IdleTimeout: *redisIdleTimeout,
	}

	if c := pool.Get(); c.Err() != nil {
		return nil, c.Err()
	} else {
		c.Close()
	}

	return &Database{Pool: pool}, nil
}

// Exists returns true if package with import path exists in the database.
func (db *Database) Exists(path string) (bool, error) {
	c := db.Pool.Get()
	defer c.Close()
	return redis.Bool(c.Do("HEXISTS", "ids", path))
}

var putScript = redis.NewScript(0, `
    local path = ARGV[1]
    local synopsis = ARGV[2]
    local score = ARGV[3]
    local gob = ARGV[4]
    local terms = ARGV[5]
    local etag = ARGV[6]
    local kind = ARGV[7]
    local nextCrawl = ARGV[8]

    local id = redis.call('HGET', 'ids', path)
    if not id then
        id = redis.call('INCR', 'maxPackageId')
        redis.call('HSET', 'ids', path, id)
    end

    if etag ~= '' and etag == redis.call('HGET', 'pkg:' .. id, 'clone') then
        terms = ''
        score = 0
    end

    local update = {}
    for term in string.gmatch(redis.call('HGET', 'pkg:' .. id, 'terms') or '', '([^ ]+)') do
        update[term] = 1
    end

    for term in string.gmatch(terms, '([^ ]+)') do
        update[term] = (update[term] or 0) + 2
    end

    for term, x in pairs(update) do
        if x == 1 then
            redis.call('SREM', 'index:' .. term, id)
        elseif x == 2 then
            redis.call('SADD', 'index:' .. term, id)
        end
    end

    redis.call('SREM', 'badCrawl', path)
    redis.call('SREM', 'newCrawl', path)

    if nextCrawl ~= '0' then
        redis.call('ZADD', 'nextCrawl', nextCrawl, id)
        redis.call('HSET', 'pkg:' .. id, 'crawl', nextCrawl)
    end

    return redis.call('HMSET', 'pkg:' .. id, 'path', path, 'synopsis', synopsis, 'score', score, 'gob', gob, 'terms', terms, 'etag', etag, 'kind', kind)
`)

var addCrawlScript = redis.NewScript(0, `
    for i=1,#ARGV do
        local pkg = ARGV[i]
        if redis.call('HEXISTS', 'ids',  pkg) == 0  and redis.call('SISMEMBER', 'badCrawl', pkg) == 0 then
            redis.call('SADD', 'newCrawl', pkg)
        end
    end
`)

func (db *Database) AddNewCrawl(importPath string) error {
	if !gosrc.IsValidRemotePath(importPath) {
		return errors.New("bad path")
	}
	c := db.Pool.Get()
	defer c.Close()
	_, err := addCrawlScript.Do(c, importPath)
	return err
}

// Put adds the package documentation to the database.
func (db *Database) Put(pdoc *doc.Package, nextCrawl time.Time, hide bool) error {
	c := db.Pool.Get()
	defer c.Close()

	score := 0.0
	if !hide {
		score = documentScore(pdoc)
	}
	terms := documentTerms(pdoc, score)

	var gobBuf bytes.Buffer
	if err := gob.NewEncoder(&gobBuf).Encode(pdoc); err != nil {
		return err
	}

	gobBytes := snappy.Encode(nil, gobBuf.Bytes())

	// Truncate large documents.
	if len(gobBytes) > 400000 {
		pdocNew := *pdoc
		pdoc = &pdocNew
		pdoc.Truncated = true
		pdoc.Vars = nil
		pdoc.Funcs = nil
		pdoc.Types = nil
		pdoc.Consts = nil
		pdoc.Examples = nil
		gobBuf.Reset()
		if err := gob.NewEncoder(&gobBuf).Encode(pdoc); err != nil {
			return err
		}
		gobBytes = snappy.Encode(nil, gobBuf.Bytes())
	}

	kind := "p"
	switch {
	case pdoc.Name == "":
		kind = "d"
	case pdoc.IsCmd:
		kind = "c"
	}

	t := int64(0)
	if !nextCrawl.IsZero() {
		t = nextCrawl.Unix()
	}

	_, err := putScript.Do(c, pdoc.ImportPath, pdoc.Synopsis, score, gobBytes, strings.Join(terms, " "), pdoc.Etag, kind, t)
	if err != nil {
		return err
	}

	if nextCrawl.IsZero() {
		// Skip crawling related packages if this is not a full save.
		return nil
	}

	paths := make(map[string]bool)
	for _, p := range pdoc.Imports {
		if gosrc.IsValidRemotePath(p) {
			paths[p] = true
		}
	}
	for _, p := range pdoc.TestImports {
		if gosrc.IsValidRemotePath(p) {
			paths[p] = true
		}
	}
	for _, p := range pdoc.XTestImports {
		if gosrc.IsValidRemotePath(p) {
			paths[p] = true
		}
	}
	if pdoc.ImportPath != pdoc.ProjectRoot && pdoc.ProjectRoot != "" {
		paths[pdoc.ProjectRoot] = true
	}
	for _, p := range pdoc.Subdirectories {
		paths[pdoc.ImportPath+"/"+p] = true
	}

	args := make([]interface{}, 0, len(paths))
	for p := range paths {
		args = append(args, p)
	}
	_, err = addCrawlScript.Do(c, args...)
	return err
}

var setNextCrawlEtagScript = redis.NewScript(0, `
    local root = ARGV[1]
    local etag = ARGV[2]
    local nextCrawl = ARGV[3]

    local pkgs = redis.call('SORT', 'index:project:' .. root, 'GET', '#',  'GET', 'pkg:*->etag')

    for i=1,#pkgs,2 do
        if pkgs[i+1] == etag then
            redis.call('ZADD', 'nextCrawl', nextCrawl, pkgs[i])
            redis.call('HSET', 'pkg:' .. pkgs[i], 'crawl', nextCrawl)
        end
    end
`)

// SetNextCrawlEtag sets the next crawl time for all packages in the project with the given etag.
func (db *Database) SetNextCrawlEtag(projectRoot string, etag string, t time.Time) error {
	c := db.Pool.Get()
	defer c.Close()
	_, err := setNextCrawlEtagScript.Do(c, normalizeProjectRoot(projectRoot), etag, t.Unix())
	return err
}

// bumpCrawlScript sets the crawl time to now. To avoid continuously crawling
// frequently updated repositories, the crawl is scheduled in the future.
var bumpCrawlScript = redis.NewScript(0, `
    local root = ARGV[1]
    local now = tonumber(ARGV[2])
    local nextCrawl = now + 7200
    local pkgs = redis.call('SORT', 'index:project:' .. root, 'GET', '#')

    for i=1,#pkgs do
        local v = redis.call('HMGET', 'pkg:' .. pkgs[i], 'crawl', 'kind')
        local t = tonumber(v[1] or 0)
        if t == 0 or now < t then
            redis.call('HSET', 'pkg:' .. pkgs[i], 'crawl', now)
        end
        local nextCrawl = now + 86400
        if v[2] == 'p' then
            nextCrawl = now + 7200
        end
        t = tonumber(redis.call('ZSCORE', 'nextCrawl', pkgs[i]) or 0)
        if t == 0 or nextCrawl < t then
            redis.call('ZADD', 'nextCrawl', nextCrawl, pkgs[i])
        end
    end
`)

func (db *Database) BumpCrawl(projectRoot string) error {
	c := db.Pool.Get()
	defer c.Close()
	_, err := bumpCrawlScript.Do(c, normalizeProjectRoot(projectRoot), time.Now().Unix())
	return err
}

// getDocScript gets the package documentation and update time for the
// specified path. If path is "-", then the oldest document is returned.
var getDocScript = redis.NewScript(0, `
    local path = ARGV[1]

    local id
    if path == '-' then
        local r = redis.call('ZRANGE', 'nextCrawl', 0, 0)
        if not r or #r == 0 then
            return false
        end
        id = r[1]
    else
        id = redis.call('HGET', 'ids', path)
        if not id then
            return false
        end
    end

    local gob = redis.call('HGET', 'pkg:' .. id, 'gob')
    if not gob then
        return false
    end

    local nextCrawl = redis.call('HGET', 'pkg:' .. id, 'crawl')
    if not nextCrawl then
        nextCrawl = redis.call('ZSCORE', 'nextCrawl', id)
        if not nextCrawl then
            nextCrawl = 0
        end
    end

    return {gob, nextCrawl}
`)

func (db *Database) getDoc(c redis.Conn, path string) (*doc.Package, time.Time, error) {
	r, err := redis.Values(getDocScript.Do(c, path))
	if err == redis.ErrNil {
		return nil, time.Time{}, nil
	} else if err != nil {
		return nil, time.Time{}, err
	}

	var p []byte
	var t int64

	if _, err := redis.Scan(r, &p, &t); err != nil {
		return nil, time.Time{}, err
	}

	p, err = snappy.Decode(nil, p)
	if err != nil {
		return nil, time.Time{}, err
	}

	var pdoc doc.Package
	if err := gob.NewDecoder(bytes.NewReader(p)).Decode(&pdoc); err != nil {
		return nil, time.Time{}, err
	}

	nextCrawl := pdoc.Updated
	if t != 0 {
		nextCrawl = time.Unix(t, 0).UTC()
	}

	return &pdoc, nextCrawl, err
}

var getSubdirsScript = redis.NewScript(0, `
    local reply
    for i = 1,#ARGV do
        reply = redis.call('SORT', 'index:project:' .. ARGV[i], 'ALPHA', 'BY', 'pkg:*->path', 'GET', 'pkg:*->path', 'GET', 'pkg:*->synopsis', 'GET', 'pkg:*->kind')
        if #reply > 0 then
            break
        end
    end
    return reply
`)

func (db *Database) getSubdirs(c redis.Conn, path string, pdoc *doc.Package) ([]Package, error) {
	var reply interface{}
	var err error

	switch {
	case isStandardPackage(path):
		reply, err = getSubdirsScript.Do(c, "go")
	case pdoc != nil:
		reply, err = getSubdirsScript.Do(c, pdoc.ProjectRoot)
	default:
		var roots []interface{}
		projectRoot := path
		for i := 0; i < 5; i++ {
			roots = append(roots, projectRoot)
			if j := strings.LastIndex(projectRoot, "/"); j < 0 {
				break
			} else {
				projectRoot = projectRoot[:j]
			}
		}
		reply, err = getSubdirsScript.Do(c, roots...)
	}

	values, err := redis.Values(reply, err)
	if err != nil {
		return nil, err
	}

	var subdirs []Package
	prefix := path + "/"

	for len(values) > 0 {
		var pkg Package
		var kind string
		values, err = redis.Scan(values, &pkg.Path, &pkg.Synopsis, &kind)
		if err != nil {
			return nil, err
		}
		if (kind == "p" || kind == "c") && strings.HasPrefix(pkg.Path, prefix) {
			subdirs = append(subdirs, pkg)
		}
	}

	return subdirs, err
}

// Get gets the package documenation and sub-directories for the the given
// import path.
func (db *Database) Get(path string) (*doc.Package, []Package, time.Time, error) {
	c := db.Pool.Get()
	defer c.Close()

	pdoc, nextCrawl, err := db.getDoc(c, path)
	if err != nil {
		return nil, nil, time.Time{}, err
	}

	if pdoc != nil {
		// fixup for speclal "-" path.
		path = pdoc.ImportPath
	}

	subdirs, err := db.getSubdirs(c, path, pdoc)
	if err != nil {
		return nil, nil, time.Time{}, err
	}
	return pdoc, subdirs, nextCrawl, nil
}

func (db *Database) GetDoc(path string) (*doc.Package, time.Time, error) {
	c := db.Pool.Get()
	defer c.Close()
	return db.getDoc(c, path)
}

var deleteScript = redis.NewScript(0, `
    local path = ARGV[1]

    local id = redis.call('HGET', 'ids', path)
    if not id then
        return false
    end

    for term in string.gmatch(redis.call('HGET', 'pkg:' .. id, 'terms') or '', '([^ ]+)') do
        redis.call('SREM', 'index:' .. term, id)
    end

    redis.call('ZREM', 'nextCrawl', id)
    redis.call('SREM', 'newCrawl', path)
    redis.call('ZREM', 'popular', id)
    redis.call('DEL', 'pkg:' .. id)
    return redis.call('HDEL', 'ids', path)
`)

// Delete deletes the documenation for the given import path.
func (db *Database) Delete(path string) error {
	c := db.Pool.Get()
	defer c.Close()
	_, err := deleteScript.Do(c, path)
	return err
}

func packages(reply interface{}, all bool) ([]Package, error) {
	values, err := redis.Values(reply, nil)
	if err != nil {
		return nil, err
	}
	result := make([]Package, 0, len(values)/3)
	for len(values) > 0 {
		var pkg Package
		var kind string
		values, err = redis.Scan(values, &pkg.Path, &pkg.Synopsis, &kind)
		if err != nil {
			return nil, err
		}
		if !all && kind == "d" {
			continue
		}
		if pkg.Path == "C" {
			pkg.Synopsis = "Package C is a \"pseudo-package\" used to access the C namespace from a cgo source file."
		}
		result = append(result, pkg)
	}
	return result, nil
}

func (db *Database) getPackages(key string, all bool) ([]Package, error) {
	c := db.Pool.Get()
	defer c.Close()
	reply, err := c.Do("SORT", key, "ALPHA", "BY", "pkg:*->path", "GET", "pkg:*->path", "GET", "pkg:*->synopsis", "GET", "pkg:*->kind")
	if err != nil {
		return nil, err
	}
	return packages(reply, all)
}

func (db *Database) GoIndex() ([]Package, error) {
	return db.getPackages("index:project:go", false)
}

func (db *Database) GoSubrepoIndex() ([]Package, error) {
	return db.getPackages("index:project:subrepo", false)
}

func (db *Database) Index() ([]Package, error) {
	return db.getPackages("index:all:", false)
}

func (db *Database) Project(projectRoot string) ([]Package, error) {
	return db.getPackages("index:project:"+normalizeProjectRoot(projectRoot), true)
}

func (db *Database) AllPackages() ([]Package, error) {
	c := db.Pool.Get()
	defer c.Close()
	values, err := redis.Values(c.Do("SORT", "nextCrawl", "DESC", "BY", "pkg:*->score", "GET", "pkg:*->path", "GET", "pkg:*->kind"))
	if err != nil {
		return nil, err
	}
	result := make([]Package, 0, len(values)/2)
	for len(values) > 0 {
		var pkg Package
		var kind string
		values, err = redis.Scan(values, &pkg.Path, &kind)
		if err != nil {
			return nil, err
		}
		if kind == "d" {
			continue
		}
		result = append(result, pkg)
	}
	return result, nil
}

var packagesScript = redis.NewScript(0, `
    local result = {}
    for i = 1,#ARGV do
        local path = ARGV[i]
        local synopsis = ''
        local kind = 'u'
        local id = redis.call('HGET', 'ids',  path)
        if id then
            synopsis = redis.call('HGET', 'pkg:' .. id, 'synopsis')
            kind = redis.call('HGET', 'pkg:' .. id, 'kind')
        end
        result[#result+1] = path
        result[#result+1] = synopsis
        result[#result+1] = kind
    end
    return result
`)

func (db *Database) Packages(paths []string) ([]Package, error) {
	var args []interface{}
	for _, p := range paths {
		args = append(args, p)
	}
	c := db.Pool.Get()
	defer c.Close()
	reply, err := packagesScript.Do(c, args...)
	if err != nil {
		return nil, err
	}
	pkgs, err := packages(reply, false)
	sort.Sort(byPath(pkgs))
	return pkgs, err
}

func (db *Database) ImporterCount(path string) (int, error) {
	c := db.Pool.Get()
	defer c.Close()
	return redis.Int(c.Do("SCARD", "index:import:"+path))
}

func (db *Database) Importers(path string) ([]Package, error) {
	return db.getPackages("index:import:"+path, false)
}

func (db *Database) Block(root string) error {
	c := db.Pool.Get()
	defer c.Close()
	if _, err := c.Do("SADD", "block", root); err != nil {
		return err
	}
	keys, err := redis.Strings(c.Do("HKEYS", "ids"))
	if err != nil {
		return err
	}
	for _, key := range keys {
		if key == root || strings.HasPrefix(key, root) && key[len(root)] == '/' {
			if _, err := deleteScript.Do(c, key); err != nil {
				return err
			}
		}
	}
	return nil
}

var isBlockedScript = redis.NewScript(0, `
    local path = ''
    for s in string.gmatch(ARGV[1], '[^/]+') do
        path = path .. s
        if redis.call('SISMEMBER', 'block', path) == 1 then
            return 1
        end
        path = path .. '/'
    end
    return  0
`)

func (db *Database) IsBlocked(path string) (bool, error) {
	c := db.Pool.Get()
	defer c.Close()
	return redis.Bool(isBlockedScript.Do(c, path))
}

type queryResult struct {
	Path     string
	Synopsis string
	Score    float64
}

type byScore []*queryResult

func (p byScore) Len() int           { return len(p) }
func (p byScore) Less(i, j int) bool { return p[j].Score < p[i].Score }
func (p byScore) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func (db *Database) Query(q string) ([]Package, error) {
	terms := parseQuery(q)
	if len(terms) == 0 {
		return nil, nil
	}
	c := db.Pool.Get()
	defer c.Close()
	n, err := redis.Int(c.Do("INCR", "maxQueryId"))
	if err != nil {
		return nil, err
	}
	id := "tmp:query-" + strconv.Itoa(n)

	args := []interface{}{id}
	for _, term := range terms {
		args = append(args, "index:"+term)
	}
	c.Send("SINTERSTORE", args...)
	c.Send("SORT", id, "DESC", "BY", "nosort", "GET", "pkg:*->path", "GET", "pkg:*->synopsis", "GET", "pkg:*->score")
	c.Send("DEL", id)
	c.Flush()
	c.Receive()                              // SINTERSTORE
	values, err := redis.Values(c.Receive()) // SORT
	if err != nil {
		return nil, err
	}
	c.Receive() // DEL

	var queryResults []*queryResult
	if err := redis.ScanSlice(values, &queryResults, "Path", "Synopsis", "Score"); err != nil {
		return nil, err
	}

	for _, qr := range queryResults {
		c.Send("SCARD", "index:import:"+qr.Path)
	}
	c.Flush()

	for _, qr := range queryResults {
		importCount, err := redis.Int(c.Receive())
		if err != nil {
			return nil, err
		}

		qr.Score *= math.Log(float64(10 + importCount))

		if isStandardPackage(qr.Path) {
			if strings.HasSuffix(qr.Path, q) {
				// Big bump for exact match on standard package name.
				qr.Score *= 10000
			} else {
				qr.Score *= 1.2
			}
		}

		if q == path.Base(qr.Path) {
			qr.Score *= 1.1
		}
	}

	sort.Sort(byScore(queryResults))

	pkgs := make([]Package, len(queryResults))
	for i, qr := range queryResults {
		pkgs[i].Path = qr.Path
		pkgs[i].Synopsis = qr.Synopsis
	}

	return pkgs, nil
}

type PackageInfo struct {
	PDoc  *doc.Package
	Score float64
	Kind  string
	Size  int
}

// Do executes function f for each document in the database.
func (db *Database) Do(f func(*PackageInfo) error) error {
	c := db.Pool.Get()
	defer c.Close()
	cursor := 0
	c.Send("SCAN", cursor, "MATCH", "pkg:*")
	c.Flush()
	for {
		// Recieve previous SCAN.
		values, err := redis.Values(c.Receive())
		if err != nil {
			return err
		}
		var keys [][]byte
		if _, err := redis.Scan(values, &cursor, &keys); err != nil {
			return err
		}
		if cursor == 0 {
			break
		}
		for _, key := range keys {
			c.Send("HMGET", key, "gob", "score", "kind", "path", "terms", "synopis")
		}
		c.Send("SCAN", cursor, "MATCH", "pkg:*")
		c.Flush()
		for _ = range keys {
			values, err := redis.Values(c.Receive())
			if err != nil {
				return err
			}

			var (
				pi       PackageInfo
				p        []byte
				path     string
				terms    string
				synopsis string
			)

			if _, err := redis.Scan(values, &p, &pi.Score, &pi.Kind, &path, &terms, &synopsis); err != nil {
				return err
			}

			if p == nil {
				continue
			}

			pi.Size = len(path) + len(p) + len(terms) + len(synopsis)

			p, err = snappy.Decode(nil, p)
			if err != nil {
				return fmt.Errorf("snappy decoding %s: %v", path, err)
			}

			if err := gob.NewDecoder(bytes.NewReader(p)).Decode(&pi.PDoc); err != nil {
				return fmt.Errorf("gob decoding %s: %v", path, err)
			}
			if err := f(&pi); err != nil {
				return fmt.Errorf("func %s: %v", path, err)
			}
		}
	}
	return nil
}

var importGraphScript = redis.NewScript(0, `
    local path = ARGV[1]

    local id = redis.call('HGET', 'ids', path)
    if not id then
        return false
    end

    return redis.call('HMGET', 'pkg:' .. id, 'synopsis', 'terms')
`)

// DepLevel specifies the level of depdenencies to show in an import graph.
type DepLevel int

const (
	ShowAllDeps      DepLevel = iota // show all dependencies
	HideStandardDeps                 // don't show dependencies of standard libraries
	HideStandardAll                  // don't show standard libraries at all
)

func (db *Database) ImportGraph(pdoc *doc.Package, level DepLevel) ([]Package, [][2]int, error) {

	// This breadth-first traversal of the package's dependencies uses the
	// Redis pipeline as queue. Links to packages with invalid import paths are
	// only included for the root package.

	c := db.Pool.Get()
	defer c.Close()
	if err := importGraphScript.Load(c); err != nil {
		return nil, nil, err
	}

	nodes := []Package{{Path: pdoc.ImportPath, Synopsis: pdoc.Synopsis}}
	edges := [][2]int{}
	index := map[string]int{pdoc.ImportPath: 0}

	for _, path := range pdoc.Imports {
		if level >= HideStandardAll && isStandardPackage(path) {
			continue
		}
		j := len(nodes)
		index[path] = j
		edges = append(edges, [2]int{0, j})
		nodes = append(nodes, Package{Path: path})
		importGraphScript.Send(c, path)
	}

	for i := 1; i < len(nodes); i++ {
		c.Flush()
		r, err := redis.Values(c.Receive())
		if err == redis.ErrNil {
			continue
		} else if err != nil {
			return nil, nil, err
		}
		var synopsis, terms string
		if _, err := redis.Scan(r, &synopsis, &terms); err != nil {
			return nil, nil, err
		}
		nodes[i].Synopsis = synopsis
		for _, term := range strings.Fields(terms) {
			if strings.HasPrefix(term, "import:") {
				path := term[len("import:"):]
				if level >= HideStandardDeps && isStandardPackage(path) {
					continue
				}
				j, ok := index[path]
				if !ok {
					j = len(nodes)
					index[path] = j
					nodes = append(nodes, Package{Path: path})
					importGraphScript.Send(c, path)
				}
				edges = append(edges, [2]int{i, j})
			}
		}
	}
	return nodes, edges, nil
}

func (db *Database) PutGob(key string, value interface{}) error {
	var buf bytes.Buffer
	if err := gob.NewEncoder(&buf).Encode(value); err != nil {
		return err
	}
	c := db.Pool.Get()
	defer c.Close()
	_, err := c.Do("SET", "gob:"+key, buf.Bytes())
	return err
}

func (db *Database) GetGob(key string, value interface{}) error {
	c := db.Pool.Get()
	defer c.Close()
	p, err := redis.Bytes(c.Do("GET", "gob:"+key))
	if err == redis.ErrNil {
		return nil
	} else if err != nil {
		return err
	}
	return gob.NewDecoder(bytes.NewReader(p)).Decode(value)
}

var incrementPopularScoreScript = redis.NewScript(0, `
    local path = ARGV[1]
    local n = ARGV[2]
    local t = ARGV[3]

    local id = redis.call('HGET', 'ids', path)
    if not id then
        return
    end

    local t0 = redis.call('GET', 'popular:0') or '0'
    local f = math.exp(tonumber(t) - tonumber(t0))
    redis.call('ZINCRBY', 'popular', tonumber(n) * f, id)
    if f > 10 then
        redis.call('SET', 'popular:0', t)
        redis.call('ZUNIONSTORE', 'popular', 1, 'popular', 'WEIGHTS', 1.0 / f)
        redis.call('ZREMRANGEBYSCORE', 'popular', '-inf', 0.05)
    end
`)

const popularHalfLife = time.Hour * 24 * 7

func (db *Database) incrementPopularScoreInternal(path string, delta float64, t time.Time) error {
	// nt = n0 * math.Exp(-lambda * t)
	// lambda = math.Ln2 / thalf
	c := db.Pool.Get()
	defer c.Close()
	const lambda = math.Ln2 / float64(popularHalfLife)
	scaledTime := lambda * float64(t.Sub(time.Unix(1257894000, 0)))
	_, err := incrementPopularScoreScript.Do(c, path, delta, scaledTime)
	return err
}

func (db *Database) IncrementPopularScore(path string) error {
	return db.incrementPopularScoreInternal(path, 1, time.Now())
}

var popularScript = redis.NewScript(0, `
    local stop = ARGV[1]
    local ids = redis.call('ZREVRANGE', 'popular', '0', stop)
    local result = {}
    for i=1,#ids do
        local values = redis.call('HMGET', 'pkg:' .. ids[i], 'path', 'synopsis', 'kind')
        result[#result+1] = values[1]
        result[#result+1] = values[2]
        result[#result+1] = values[3]
    end
    return result
`)

func (db *Database) Popular(count int) ([]Package, error) {
	c := db.Pool.Get()
	defer c.Close()
	reply, err := popularScript.Do(c, count-1)
	if err != nil {
		return nil, err
	}
	pkgs, err := packages(reply, false)
	return pkgs, err
}

var popularWithScoreScript = redis.NewScript(0, `
    local ids = redis.call('ZREVRANGE', 'popular', '0', -1, 'WITHSCORES')
    local result = {}
    for i=1,#ids,2 do
        result[#result+1] = redis.call('HGET', 'pkg:' .. ids[i], 'path')
        result[#result+1] = ids[i+1]
        result[#result+1] = 'p'
    end
    return result
`)

func (db *Database) PopularWithScores() ([]Package, error) {
	c := db.Pool.Get()
	defer c.Close()
	reply, err := popularWithScoreScript.Do(c)
	if err != nil {
		return nil, err
	}
	pkgs, err := packages(reply, false)
	return pkgs, err
}

func (db *Database) PopNewCrawl() (string, bool, error) {
	c := db.Pool.Get()
	defer c.Close()

	var subdirs []Package

	path, err := redis.String(c.Do("SPOP", "newCrawl"))
	switch {
	case err == redis.ErrNil:
		err = nil
		path = ""
	case err == nil:
		subdirs, err = db.getSubdirs(c, path, nil)
	}
	return path, len(subdirs) > 0, err
}

func (db *Database) AddBadCrawl(path string) error {
	c := db.Pool.Get()
	defer c.Close()
	_, err := c.Do("SADD", "badCrawl", path)
	return err
}

var incrementCounterScript = redis.NewScript(0, `
    local key = 'counter:' .. ARGV[1]
    local n = tonumber(ARGV[2])
    local t = tonumber(ARGV[3])
    local exp = tonumber(ARGV[4])

    local counter = redis.call('GET', key)
    if counter then
        counter = cjson.decode(counter)
        n = n + counter.n * math.exp(counter.t - t)
    end

    redis.call('SET', key, cjson.encode({n = n; t = t}))
    redis.call('EXPIRE', key, exp)
    return tostring(n)
`)

const counterHalflife = time.Hour

func (db *Database) incrementCounterInternal(key string, delta float64, t time.Time) (float64, error) {
	// nt = n0 * math.Exp(-lambda * t)
	// lambda = math.Ln2 / thalf
	c := db.Pool.Get()
	defer c.Close()
	const lambda = math.Ln2 / float64(counterHalflife)
	scaledTime := lambda * float64(t.Sub(time.Unix(1257894000, 0)))
	return redis.Float64(incrementCounterScript.Do(c, key, delta, scaledTime, (4*counterHalflife)/time.Second))
}

func (db *Database) IncrementCounter(key string, delta float64) (float64, error) {
	return db.incrementCounterInternal(key, delta, time.Now())
}


text:
<?php

namespace MCUCourseCLI;

class Config {

  protected static $instance = null;

  protected $workPath = null;
  protected $commandPath = null;

  protected $config = array();

  private function __construct($workPath = null)
  {
    $this->workPath = getcwd();
    $this->commandPath = __DIR__;

    if($workPath && file_exists($workPath)) {
      $this->workPath = $workPath;
    }

    $this->parseConfigFile();
  }

  public static function getInstance($workPath = null)
  {
    if(self::$instance && self::$instance instanceof Config) {
      return self::$instance;
    }

    self::$instance = new Config($workPath);
    return self::$instance;

  }

  public function getAll()
  {
    return $this->config;
  }

  public function get($key)
  {
    return $this->config[$key];
  }

  public function getVersion()
  {
    return $this->config['VERSION'];
  }

  public function getWorkPath()
  {
    return $this->workPath;
  }
  public function getCommandPath()
  {
    return $this->commandPath;
  }

  private function parseConfigFile()
  {
    $defaultConfig = parse_ini_file(__DIR__ . '/../../config.ini');

    $userConfigFile = $this->workPath . '/.mcuConfig';
    $userConfig = array();
    if(file_exists($userConfigFile)) {
      $userConfig = parse_ini_file($userConfigFile);
    }

    $this->config = array_merge($defaultConfig, $userConfig);
    $this->config['VERSION'] = $defaultConfig['VERSION']; // Prevent user change version
  }

}

redpajama_set_name: RedPajamaGithub


text:
<?php

function customError($errno, $errstr) {
	echo(json_encode(array("success"=>false,"errors"=>array("reason"=>"<b>Error:</b> [".$errno."] ".$errstr))));
	exit;
}

function _prep_password($password)
{
	$obj =& get_instance();
	// Salt up the hash pipe
	// Encryption key as suffix.
	return sha1($password.$obj->config->item('encryption_key'));
	//return $this->obj->encryption->sha1($password.$this->obj->config->item('encryption_key'));
}

function is_ajax() {
	return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}


text:
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}

module DBus.Error where

import           Control.Exception as Ex
import           Data.Text         (Text)
import           Data.Typeable     (Typeable)

import           DBus.Types

data DBusError = CouldNotConnect String
               | DBusParseError String
               | MarshalError String
                 deriving (Show, Eq, Typeable)

instance Ex.Exception DBusError

errorFailed :: Text -> MsgError
errorFailed msg = (MsgError "org.freedesktop.DBus.Error.Failed"
                   (Just msg)
                   [])

noSuchInterface :: MsgError
noSuchInterface = errorFailed "No such interface"

noSuchProperty :: MsgError
noSuchProperty = errorFailed "No such property"

propertyNotReadable :: MsgError
propertyNotReadable = errorFailed "Property is not readable"

propertyNotWriteable :: MsgError
propertyNotWriteable = errorFailed "Property is not writeable"

argTypeMismatch :: MsgError
argTypeMismatch = errorFailed "Argument type mismatch"


text:
using System;
using System.Diagnostics.Contracts;
using System.Security.Cryptography;
using EnvCrypt.Core.EncryptionAlgo;
using EnvCrypt.Core.Key.Aes;
using EnvCrypt.Core.Key.XmlPoco;
using EnvCrypt.Core.Utils;

namespace EnvCrypt.Core.Key.Mapper.Xml.ToKeyPoco
{
    class XmlToAesKeyMapper : IExternalRepresentationToKeyMapper<XmlPoco.EnvCryptKey, AesKey>
    {
        public const EnvCryptAlgoEnum AlgorithmType = EnvCryptAlgoEnum.Rsa;

        private readonly IKeyDetailsPersistConverter _strConverter;

        public XmlToAesKeyMapper(IKeyDetailsPersistConverter strConverter)
        {
            _strConverter = strConverter;
        }

        public AesKey Map(XmlPoco.EnvCryptKey fromExternalRepresentationPoco)
        {
            //      Map works ensures that at least the public key will be returned
            Contract.Ensures(Contract.Result<AesKey>().Iv != null);
            Contract.Ensures(Contract.Result<AesKey>().Iv.Length > 0);
            Contract.Ensures(Contract.Result<AesKey>().Key != null);
            Contract.Ensures(Contract.Result<AesKey>().Key.Length > 0);
            //
            if (fromExternalRepresentationPoco.Aes == null)
            {
                throw new EnvCryptException("AES element must be present in key");
            }
            // There will be only 1 key per XML POCO
            if (string.IsNullOrWhiteSpace(fromExternalRepresentationPoco.Name))
            {
                throw new EnvCryptException("ECKey name in XML must not be empty");
            }
            if (fromExternalRepresentationPoco.Aes.Length != 1)
            {
                throw new EnvCryptException("ECKey XML must contain only one AES (1) key");
            }

            if (string.IsNullOrWhiteSpace(fromExternalRepresentationPoco.Aes[0].Iv))
            {
                throw new EnvCryptException("ECKey XML must contain at the IV for AES");
            }
            if (string.IsNullOrWhiteSpace(fromExternalRepresentationPoco.Aes[0].Key))
            {
                throw new EnvCryptException("ECKey XML must contain the Key for AES");
            }

            var xmlData = fromExternalRepresentationPoco.Aes[0];
            var ret = new AesKey()
            {
                Name = fromExternalRepresentationPoco.Name
            };
            ret.Iv = _strConverter.Decode(xmlData.Iv);
            ret.Key = _strConverter.Decode(xmlData.Key);
            return ret;
        }
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<labels>
	Esdeveniment
	Esdeveniments
	Crea un esdeveniment
	Edita l'esdeveniment
	Envia notificacions per correu electrònic
	No envïis correu opcional
	Elimina l'esdeveniment
	Esdeveniment guardat
	Error en guardar l'esdeveniment
	Crea un esdeveniment
	General
	Absència
	Aniversari
	Vacances
	Educació
	Reunió
	Feina exerna
	Treball a distància
	Reciclatge de paper
	Reciclatge de cartró
	Compensació
	Fita
	Recordatori
	Personal
	Realment voleu eliminar aquest esdeveniment?
	L'esdeveniment ha estat eliminat.
	No s'ha pogut eliminar l'esdeveniment.
	Assistents
	Part d'una sèrie d'esdeveniments
	Tipus d'esdeveniments sol·lapables
	Bloqueja tipus d'esdeveniments
	Creador
	Tipus d'esdeveniment
	Tot el dia
	És públic?
	Esdeveniment privat
	Assistents
	Període de temps
	Data d'inici
	A les
	Data final
	A les
	Lloc
	El meu esdeveniment
	Assistents
	[Cap persona seleccionada]
	Projecte
	Tasca
	Activa recordatori per correu electrònic
	Temps abans d'enviar un recordatori per correu
	Activa recordatori amb una finestra emergent
	Temps abans de mostrar una finestra emergent
	Recorda-m'ho per correu electrònic
	Recorda-m'ho per finestra emergent
	Tipus d'esdeveniment
	Títol de l'esdeveniment
	Aniversaris les properes setmanes
	Vacances les properes setmanes
	Cap esdeveniment a la vista
	L'esdeveniment acaba a la vegada o abans que comenci
	No té permís d'assignar un esdeveniment marcat com a privat a algú altre.
	Aquesta persona ja està ocupada en aquesta hora:
	Necessiteu assignar almenys una persona interna per l'esdeveniment
	Administrar esdeveniments
	Mostra esdeveniments
	Edita l'esdeveniment
	Edita sèries
	Edita sèries (només les properes)
	Elimina l'esdeveniment
	Elimina la sèrie d'esdeveniments
	Afegeix esdeveniment
	Mostra al calendari
	Dia
	Setmana
	Mes
	Correu recordatori
	Finestra recordatori
	No ho recordis
	abans
	A la hora de l'esdeveniment
	No s'ha trobat cap persona amb aquest nom.
	Voleu guardar l'esdeveniment de totes maneres?
	Crea un esdeveniment
	S'ha modificat l'esdeveniment
	S'ha cancel·lat l'esdeveniment
	Esdeveniment creat per
	Monstra l'esdeveniment al calendari de Gestió
	Correu enviat per
	Data prèvia de l'esdeveniment abans d'eliminar-se
	Envia la informació de l'esdeveniment per correu
	No envïis correu i no m'ho tornis a preguntar.
	Envia l'esdeveniment per correu
	Títol
	S'ha creat el nou esdeveniment
	S'ha modificat l'esdeveniment
	S'ha eliminat l'esdeveniment
	Els avisos s'han desactivat. Podeu reactivar-los al vostre perfil.
	S'ha enviat per correu la notificació automàtica.
	S'ha enviat la notificació per correu.
	No teniu accés a aquest esdeveniment
	Nova data i hora guardades
</labels>


text:
<component name="libraryTable">
  <library name="OneRepMax" type="javaScript">
    <properties>
      <sourceFilesUrls>
        <item url="file://$PROJECT_DIR$/src" />
      </sourceFilesUrls>
    </properties>
    <CLASSES>
      <root url="file://$PROJECT_DIR$/src" />
    </CLASSES>
    <SOURCES />
  </library>
</component>


text:
#include <stdio.h>
#include <stdlib.h>

#include <windows.h>
#include <commctrl.h>

#include "taskmgr.h"

typedef	void (WINAPI *RUNFILEDLG)(
HWND    hwndOwner,
HICON   hIcon,
LPCSTR  lpstrDirectory,
LPCSTR  lpstrTitle,
LPCSTR  lpstrDescription,
UINT    uFlags);

/*
 * Flags for RunFileDlg
 */

#define	RFF_NOBROWSE		0x01	/* Removes the browse button. */
#define	RFF_NODEFAULT		0x02	/* No default item selected. */
#define	RFF_CALCDIRECTORY	0x04	/* Calculates the working directory from the file name. */
#define	RFF_NOLABEL			0x08	/* Removes the edit box label. */
#define	RFF_NOSEPARATEMEM	0x20	/* Removes the Separate Memory Space check box (Windows NT only). */

void TaskManager_OnFileNew(void)
{
    HMODULE            hShell32;
    RUNFILEDLG        RunFileDlg;
    OSVERSIONINFOW    versionInfo;
    static const WCHAR wszShell32[] = {'S','H','E','L','L','3','2','.','D','L','L',0};

    hShell32 = LoadLibraryW(wszShell32);
    RunFileDlg = (void *)GetProcAddress(hShell32, (LPCSTR)61);

    /* Show "Run..." dialog */
    if (RunFileDlg)
    {
        HICON hIcon = LoadIconW(GetModuleHandleW(NULL), MAKEINTRESOURCEW(IDI_TASKMANAGER));
        versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
        GetVersionExW(&versionInfo);

        if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
        {
            WCHAR wTitle[64];
            LoadStringW(GetModuleHandleW(NULL), IDS_RUNDLG_CAPTION, wTitle, 64);
            RunFileDlg(hMainWnd, hIcon, NULL, (LPCSTR)wTitle, NULL, RFF_CALCDIRECTORY);
        }
        else
        {
            char szTitle[64];
            LoadStringA(GetModuleHandleW(NULL), IDS_RUNDLG_CAPTION, szTitle, 64);
            RunFileDlg(hMainWnd, hIcon, NULL, szTitle, NULL, RFF_CALCDIRECTORY);
        }
    }

    FreeLibrary(hShell32);
}


text:
package com.google.sps.data;

import java.util.stream.Collectors;
import com.google.appengine.api.datastore.GeoPt;
import com.google.sps.data.ProductWithAddress;

import java.util.*;
import java.lang.*;

public class MapsLibrary {
    private static Double oLat;
    private static Double oLng;
    private static Double dLat;
    private static Double dLng;

    //Radius of Earth in miles
    private static int EARTH_RADIUS = 3956;

    public static Double distance(GeoPt origin, GeoPt destination) {
        //Convert coordinates to radians instead of degrees
        oLat = new Float(origin.getLatitude()).doubleValue();
        oLng = new Float(origin.getLongitude()).doubleValue();
        dLat = new Float(destination.getLatitude()).doubleValue();
        dLng = new Float(destination.getLongitude()).doubleValue();

        // Haversine formula
        Double latDiff = dLat - oLat;
        Double lngDiff = dLng - oLng;
        Double ans = Math.pow(Math.sin(latDiff / 2), 2)
                  + Math.cos(oLat) * Math.cos(dLat)
                  * Math.pow(Math.sin(lngDiff / 2),2);

        ans = 2 * Math.asin(Math.sqrt(ans));

        return ans * EARTH_RADIUS;
    }

    public static List<ProductEntity> sortByLocation(List<ProductWithAddress> productList,
      Account account) {
          productList.sort((product1, product2) -> {
              Double dist1 = distance(account.getLatLng(), product1.getAddress().getLatLng());
              Double dist2 = distance(account.getLatLng(), product2.getAddress().getLatLng());

              if (dist1 < dist2) {
                  return -1;
              }

              if (dist1 > dist2) {
                  return 1;
              }

              return 0;
          });

          return productList.stream().map(product -> product.getProduct()).
            collect(Collectors.toList());
    }
}


text:
class User {

    constructor() {
        this.id = 0;
        this.uid = null;
        this.firstName = null;
        this.lastName = null;
    }

    getFullName() {
        return this.firstName + ' ' + this.lastName;
    }
}

export default User;


text:
module Rebase.Data.Bifunctor.Product
(
  module Data.Bifunctor.Product
)
where

import Data.Bifunctor.Product


text:
import java.util.LinkedList;

public class LongestValidParentheses {
    static public int longestValidParentheses(String s) {
        LinkedList<Integer> stack = new LinkedList<>();
        int result = 0;
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ')' && stack.size() > 1 && s.charAt(stack.peek()) == '(') {
                stack.pop();
                result = Math.max(result, i - stack.peek());
            } else {
                stack.push(i);
            }
        }
        return result;
    }

    public static void main(String [ ] args) {

        // 2
        System.out.println(longestValidParentheses("(()"));
        // 4
        System.out.println(longestValidParentheses(")()())"));
    }
}


text:
<?php

namespace LucidFrame\Core;

/**
 * Core utility for pagination
 */
class Pager
{
    /** @var int The current page no. */
    private $page = 1;
    /** @var int The customized query string name for "page" */
    private $pageQueryStr = 'page';
    /** @var int No. of items per page to display */
    private $itemsPerPage = 15;
    /** @var int How many page no. to show in the pagination */
    private $pageNumLimit = 5;
    /** @var string The absolute image directory path where the navigaion arrow images reside */
    private $imagePath = '';
    /** @var boolean AJAX pager or not */
    private $ajax = false;
    /** @var string The URL to request if it is different than the current URL; it must be relative to APP ROOT */
    private $url = '';
    /** @var int Total number of records for the pager */
    private $total = 0;
    /** @var int The calculated offset for the page */
    private $offset = 0;
    /** @var boolean The page is enabled or not */
    private $enabled = true;
    /** @var string HTML tag for the pagination display; default is table. ul and div are also allowed. */
    private $htmlTag = 'table';
    /** @var string HTML tag for internal use */
    private $parentOpenTag;
    /** @var string HTML tag for internal use */
    private $parentCloseTag;
    /** @var string HTML tag for internal use */
    private $childTag;
    /** @var array The array of calculated result pages and offset */
    private $result;
    /** @var string The session name for the last page number */
    private $name = 'lc_last_page';
    /** @var callable The callback function for customized display */
    private $displayCallback;
    /** @var string CSS class name for pagination container */
    private $className = 'lc-pager';

    /**
     * Constructor
     * @param string $pageQueryStr The customized page query string name
     */
    public function __construct($pageQueryStr = '')
    {
        if ($pageQueryStr) {
            $this->pageQueryStr = $pageQueryStr;
        }
        $page = _arg($this->pageQueryStr);
        $this->page = $page ?: 1;
    }

    /**
     * Setter functions for the properties
     * @param string $key The property name
     * @param mixed $value The value to be set to the property
     * @return object Pager
     */
    public function set($key, $value = '')
    {
        if (isset($this->$key)) {
            $this->$key = $value;
        }
        if ($key == 'htmlTag') {
            $this->setHtmlTag($value);
        }

        return $this;
    }

    /**
     * Getter functions for the properties
     * @param string $key The property name
     * @return mixed The value of the property
     */
    public function get($key)
    {
        return isset($this->$key) ? $this->$key : '';
    }

    /**
     * Setter functions for the property "htmlTag"
     *
     * @param string $tag The HTML tag - table, ul or div
     * @return object Pager
     * @internal
     * @ignore
     */
    private function setHtmlTag($tag = 'table')
    {
        if (!in_array($tag, array('table', 'ul', 'div'))) {
            $this->htmlTag = 'table';
        }

        switch ($this->htmlTag) {
            case 'table':
                $this->parentOpenTag = '
className . '" border="0" cellpadding="0" cellspacing="0">
';
                $this->parentCloseTag = '

';
                $this->childTag = 'td';
                break;

            case 'ul':
                $this->parentOpenTag = '
className . '">';
                $this->parentCloseTag = '
';
                $this->childTag = 'li';
                break;

            case 'div':
                $this->parentOpenTag = '
className . '">';
                $this->parentCloseTag = '
';
                $this->childTag = 'div';
                break;
        }

        return $this;
    }

    /**
     * Pager calculation function
     * Before calling this function, the following property must be set:
     *
     * - $page
     * - $itemsPerPage
     * - $pageNumLimit
     * - $total
     *
     * The array of the offsets
     *  Array(
     *      [offset] => xx
     *      [thisPage] => xx
     *      [beforePages] => Array()
     *      [afterPages] => Array()
     *      [firstPageEnable] => xx
     *      [prePageEnable] => xx
     *      [nextPageNo] => xx
     *      [nextPageEnable] => xx
     *      [lastPageNo] => xx
     *      [lastPageEnable] => xx
     *  )
     *
     * @return object Pager
     */
    public function calculate()
    {

        if (!($this->page && $this->itemsPerPage && $this->pageNumLimit && $this->total)) {
            $this->enabled = false;
            return $this;
        }

        if (!is_numeric($this->page)) {
            $this->page = 1;
        }
        $this->offset = ($this->page - 1) * $this->itemsPerPage;

        $nav = array();
        $nav['offset'] = $this->offset;
        $nav['thisPage'] = $this->page;

        $maxPage = ceil($this->total / $this->itemsPerPage);
        if ($this->page <= $this->pageNumLimit) {
            $startPage = 1;
        } else {
            $startPage = (floor(($this->page - 1) / $this->pageNumLimit) * $this->pageNumLimit) + 1;
        }

        $j = 0;
        $k = 0;
        $nav['beforePages'] = array();
        $nav['afterPages'] = array();
        for ($pageCount = 0, $i = $startPage; $i <= $maxPage; $i++) {
            if ($i < $this->page) {
                $nav['beforePages'][$j] = $i;
                $j++;
            }

            if ($i > $this->page) {
                $nav['afterPages'][$k] = $i;
                $k++;
            }

            $pageCount++;
            if ($pageCount == $this->pageNumLimit) {
                # display page number only.
                break;
            }
        }

        # First Page
        if ($this->page > 1) {
            $nav['firstPageNo'] = 1;
            $nav['firstPageEnable'] = 1;
        } else {
            $nav['firstPageEnable'] = 0;
        }

        # Previous Page
        if ($this->page > 1) {
            $nav['prePageNo'] = $this->page - 1;
            $nav['prePageEnable'] = 1;
        } else {
            $nav['prePageEnable'] = 0;
        }

        # Next page no.
        if ($this->page < $maxPage) {
            $nav['nextPageNo'] = $this->page + 1;
            $nav['nextPageEnable'] = 1;

            $nav['lastPageNo'] = $maxPage;
            $nav['lastPageEnable'] = 1;

        } else {
            $nav['nextPageEnable'] = 0;
            $nav['lastPageEnable'] = 0;
        }
        # Display multi page or not
        if (($maxPage <= 1) || ($this->page > $maxPage)) {
            $this->enabled = false;
        } else {
            $this->enabled = true;
        }

        # if page count is less than page num limit, fill page num till page num limit
        if ($maxPage > $this->pageNumLimit) {
            $allPagesCount = count($nav['beforePages']) + count($nav['afterPages']) + 1;
            if ($allPagesCount < $this->pageNumLimit) {
                $page = $this->page - 1;
                $filledPageCount = $this->pageNumLimit - $allPagesCount;
                if (isset($nav['beforePages'])) {
                    $filledPageCount += count($nav['beforePages']);
                }
                $x = 0;
                while ($filledPageCount != $x) {
                    $filledPages[] = $page;
                    $page--;
                    $x++;
                }
                $nav['beforePages'] = array();
                sort($filledPages);
                $nav['beforePages'] = $filledPages;
            }
        }

        $this->result = $nav;
        return $this;
    }

    /**
     * Display the pagination
     * @param callable $callback The callback function for customized display
     * @return void
     */
    public function display($callback = null)
    {
        if ($callback) {
            $this->displayCallback = $callback;
        }

        session_set($this->name, $this->page);

        $url = $this->url ?: null;
        $ajax = $this->ajax;
        $imagePath = isset($this->imagePath) ? $this->imagePath : '';

        $this->setHtmlTag($this->htmlTag);

        if ($this->enabled && $this->result) {
            if ($this->displayCallback) {
                $this->result['url'] = $url;
                $this->result['ajax'] = $ajax;
                call_user_func($this->displayCallback, $this->result);
                return;
            }

            extract($this->result);

            echo $this->parentOpenTag;
            # first
            if ($firstPageEnable) {
                echo '<' . $this->childTag . ' class="first-enabled">';
                if ($ajax) {
                    echo '';
                } else {
                    echo 'pageQueryStr => $firstPageNo)) . '">';
                }
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('First') . '';
                }
                echo '';
                echo '</' . $this->childTag . '>';
            } else {
                echo '<' . $this->childTag . ' class="first-disabled">';
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('First') . '';
                }
                echo '</' . $this->childTag . '>';
            }
            # prev
            if ($prePageEnable) {
                echo '<' . $this->childTag . ' class="prev-enabled">';
                if ($ajax) {
                    echo '';
                } else {
                    echo 'pageQueryStr => $prePageNo)) . '">';
                }
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('« Prev') . '';
                }
                echo '';
                echo '</' . $this->childTag . '>';
            } else {
                echo '<' . $this->childTag . ' class="prev-disabled">';
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('« Prev') . '';
                }
                echo '</' . $this->childTag . '>';
            }
            echo '<' . $this->childTag . ' class="pages">';

            # before pages
            if (isset($beforePages) && $beforePages) {
                foreach ($beforePages as $oneBeforePage) {
                    echo '';
                    if ($ajax) {
                        echo '' . $oneBeforePage . '';
                    } else {
                        echo 'pageQueryStr => $oneBeforePage)) . '">';
                        echo $oneBeforePage;
                        echo '';
                    }
                    echo '';
                }
            }
            echo '' . $thisPage . '';

            # after pages
            if (isset($afterPages) && $afterPages) {
                foreach ($afterPages as $oneAfterPage) {
                    echo '';
                    if ($ajax) {
                        echo '' . $oneAfterPage . '';
                    } else {
                        echo 'pageQueryStr => $oneAfterPage)) . '">';
                        echo $oneAfterPage;
                        echo '';
                    }
                    echo '';
                }
            }
            echo '</' . $this->childTag . '>';

            # next
            if ($nextPageEnable) {
                echo '<' . $this->childTag . ' class="next-enabled">';
                if ($ajax) {
                    echo '';
                } else {
                    echo 'pageQueryStr => $nextPageNo)) . '">';
                }
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('Next »') . '';
                }
                echo '';
                echo '</' . $this->childTag . '>';
            } else {
                echo '<' . $this->childTag . ' class="next-disabled">';
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('Next »') . '';
                }
                echo '</' . $this->childTag . '>';
            }

            # last
            if ($lastPageEnable) {
                echo '<' . $this->childTag . ' class="last-enabled">';
                if ($ajax) {
                    echo '';
                } else {
                    echo 'pageQueryStr => $lastPageNo)) . '">';
                }
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('Last') . '';
                }
                echo '';
                echo '</' . $this->childTag . '>';
            } else {
                echo '<' . $this->childTag . ' class="last-disabled">';
                if ($imagePath) {
                    echo '';
                } else {
                    echo '' . _t('Last') . '';
                }
                echo '</' . $this->childTag . '>';
            }

            echo $this->parentCloseTag;
        }
    }
}


text:
using PVM.Core.Definition;
using PVM.Persistence.Sql.Model;

namespace PVM.Persistence.Sql.Transform
{
    public interface IWorkflowDefinitionTransformer
    {
        WorkflowDefinitionModel Transform(IWorkflowDefinition workflowDefinition);
        IWorkflowDefinition TransformBack(WorkflowDefinitionModel model);
    }
}


text:
package edu.umd.smile.gui;

public interface ContextMenuListener<T> {
    public void onContextMenu(String itemName, T eventObject);
}


text:
package io.druid.initialization;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import io.druid.curator.CuratorModule;
import io.druid.curator.discovery.DiscoveryModule;
import io.druid.guice.AWSModule;
import io.druid.guice.AnnouncerModule;
import io.druid.guice.CoordinatorDiscoveryModule;
import io.druid.guice.DruidProcessingConfigModule;
import io.druid.guice.DruidSecondaryModule;
import io.druid.guice.ExpressionModule;
import io.druid.guice.ExtensionsConfig;
import io.druid.guice.FirehoseModule;
import io.druid.guice.IndexingServiceDiscoveryModule;
import io.druid.guice.JacksonConfigManagerModule;
import io.druid.guice.JavaScriptModule;
import io.druid.guice.LifecycleModule;
import io.druid.guice.LocalDataStorageDruidModule;
import io.druid.guice.MetadataConfigModule;
import io.druid.guice.ModulesConfig;
import io.druid.guice.ParsersModule;
import io.druid.guice.ServerModule;
import io.druid.guice.ServerViewModule;
import io.druid.guice.StartupLoggingModule;
import io.druid.guice.StorageNodeModule;
import io.druid.guice.annotations.Client;
import io.druid.guice.annotations.EscalatedClient;
import io.druid.guice.annotations.Json;
import io.druid.guice.annotations.Smile;
import io.druid.guice.http.HttpClientModule;
import io.druid.guice.security.AuthenticatorModule;
import io.druid.guice.security.AuthorizerModule;
import io.druid.guice.security.DruidAuthModule;
import io.druid.guice.security.EscalatorModule;
import io.druid.java.util.common.ISE;
import io.druid.java.util.common.logger.Logger;
import io.druid.metadata.storage.derby.DerbyMetadataStorageDruidModule;
import io.druid.segment.writeout.SegmentWriteOutMediumModule;
import io.druid.server.emitter.EmitterModule;
import io.druid.server.initialization.AuthenticatorMapperModule;
import io.druid.server.initialization.AuthorizerMapperModule;
import io.druid.server.initialization.jetty.JettyServerModule;
import io.druid.server.metrics.MetricsModule;
import org.apache.commons.io.FileUtils;
import org.eclipse.aether.artifact.DefaultArtifact;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 */
public class Initialization
{
  private static final Logger log = new Logger(Initialization.class);
  private static final ConcurrentMap<File, URLClassLoader> loadersMap = new ConcurrentHashMap<>();

  private final static Map<Class, Collection> extensionsMap = Maps.newHashMap();

  /**
   * @param clazz service class
   * @param <T> the service type
   *
   * @return Returns a collection of implementations loaded.
   */
  public static <T> Collection<T> getLoadedImplementations(Class<T> clazz)
  {
    @SuppressWarnings("unchecked")
    Collection<T> retVal = extensionsMap.get(clazz);
    if (retVal == null) {
      return Sets.newHashSet();
    }
    return retVal;
  }

  @VisibleForTesting
  static void clearLoadedImplementations()
  {
    extensionsMap.clear();
  }

  @VisibleForTesting
  static Map<File, URLClassLoader> getLoadersMap()
  {
    return loadersMap;
  }

  /**
   * Look for implementations for the given class from both classpath and extensions directory, using {@link
   * ServiceLoader}. A user should never put the same two extensions in classpath and extensions directory, if he/she
   * does that, the one that is in the classpath will be loaded, the other will be ignored.
   *
   * @param config        Extensions configuration
   * @param serviceClass  The class to look the implementations of (e.g., DruidModule)
   *
   * @return A collection that contains implementations (of distinct concrete classes) of the given class. The order of
   * elements in the returned collection is not specified and not guaranteed to be the same for different calls to
   * getFromExtensions().
   */
  public synchronized static <T> Collection<T> getFromExtensions(ExtensionsConfig config, Class<T> serviceClass)
  {
    Collection<T> modulesToLoad = new ServiceLoadingFromExtensions<>(config, serviceClass).implsToLoad;
    extensionsMap.put(serviceClass, modulesToLoad);
    return modulesToLoad;
  }

  private static class ServiceLoadingFromExtensions<T>
  {
    private final ExtensionsConfig extensionsConfig;
    private final Class<T> serviceClass;
    private final List<T> implsToLoad = new ArrayList<>();
    private final Set<String> implClassNamesToLoad = new HashSet<>();

    private ServiceLoadingFromExtensions(ExtensionsConfig extensionsConfig, Class<T> serviceClass)
    {
      this.extensionsConfig = extensionsConfig;
      this.serviceClass = serviceClass;
      if (extensionsConfig.searchCurrentClassloader()) {
        addAllFromCurrentClassLoader();
      }
      addAllFromFileSystem();
    }

    private void addAllFromCurrentClassLoader()
    {
      ServiceLoader
          .load(serviceClass, Thread.currentThread().getContextClassLoader())
          .forEach(impl -> tryAdd(impl, "classpath"));
    }

    private void addAllFromFileSystem()
    {
      for (File extension : getExtensionFilesToLoad(extensionsConfig)) {
        log.info("Loading extension [%s] for class [%s]", extension.getName(), serviceClass);
        try {
          final URLClassLoader loader = getClassLoaderForExtension(extension);
          ServiceLoader.load(serviceClass, loader).forEach(impl -> tryAdd(impl, "local file system"));
        }
        catch (Exception e) {
          throw Throwables.propagate(e);
        }
      }
    }

    private void tryAdd(T serviceImpl, String extensionType)
    {
      final String serviceImplName = serviceImpl.getClass().getCanonicalName();
      if (serviceImplName == null) {
        log.warn(
            "Implementation [%s] was ignored because it doesn't have a canonical name, "
            + "is it a local or anonymous class?",
            serviceImpl.getClass().getName()
        );
      } else if (!implClassNamesToLoad.contains(serviceImplName)) {
        log.info(
            "Adding implementation [%s] for class [%s] from %s extension",
            serviceImplName,
            serviceClass,
            extensionType
        );
        implClassNamesToLoad.add(serviceImplName);
        implsToLoad.add(serviceImpl);
      }
    }
  }

  /**
   * Find all the extension files that should be loaded by druid.
   *

   * If user explicitly specifies druid.extensions.loadList, then it will look for those extensions under root
   * extensions directory. If one of them is not found, druid will fail loudly.
   *

   * If user doesn't specify druid.extension.toLoad (or its value is empty), druid will load all the extensions
   * under the root extensions directory.
   *
   * @param config ExtensionsConfig configured by druid.extensions.xxx
   *
   * @return an array of druid extension files that will be loaded by druid process
   */
  public static File[] getExtensionFilesToLoad(ExtensionsConfig config)
  {
    final File rootExtensionsDir = new File(config.getDirectory());
    if (rootExtensionsDir.exists() && !rootExtensionsDir.isDirectory()) {
      throw new ISE("Root extensions directory [%s] is not a directory!?", rootExtensionsDir);
    }
    File[] extensionsToLoad;
    final LinkedHashSet<String> toLoad = config.getLoadList();
    if (toLoad == null) {
      extensionsToLoad = rootExtensionsDir.listFiles();
    } else {
      int i = 0;
      extensionsToLoad = new File[toLoad.size()];
      for (final String extensionName : toLoad) {
        File extensionDir = new File(extensionName);
        if (!extensionDir.isAbsolute()) {
          extensionDir = new File(rootExtensionsDir, extensionName);
        }

        if (!extensionDir.isDirectory()) {
          throw new ISE(
              "Extension [%s] specified in \"druid.extensions.loadList\" didn't exist!?",
              extensionDir.getAbsolutePath()
          );
        }
        extensionsToLoad[i++] = extensionDir;
      }
    }
    return extensionsToLoad == null ? new File[]{} : extensionsToLoad;
  }

  /**
   * Find all the hadoop dependencies that should be loaded by druid
   *
   * @param hadoopDependencyCoordinates e.g.["org.apache.hadoop:hadoop-client:2.3.0"]
   * @param extensionsConfig            ExtensionsConfig configured by druid.extensions.xxx
   *
   * @return an array of hadoop dependency files that will be loaded by druid process
   */
  public static File[] getHadoopDependencyFilesToLoad(
      List<String> hadoopDependencyCoordinates,
      ExtensionsConfig extensionsConfig
  )
  {
    final File rootHadoopDependenciesDir = new File(extensionsConfig.getHadoopDependenciesDir());
    if (rootHadoopDependenciesDir.exists() && !rootHadoopDependenciesDir.isDirectory()) {
      throw new ISE("Root Hadoop dependencies directory [%s] is not a directory!?", rootHadoopDependenciesDir);
    }
    final File[] hadoopDependenciesToLoad = new File[hadoopDependencyCoordinates.size()];
    int i = 0;
    for (final String coordinate : hadoopDependencyCoordinates) {
      final DefaultArtifact artifact = new DefaultArtifact(coordinate);
      final File hadoopDependencyDir = new File(rootHadoopDependenciesDir, artifact.getArtifactId());
      final File versionDir = new File(hadoopDependencyDir, artifact.getVersion());
      // find the hadoop dependency with the version specified in coordinate
      if (!hadoopDependencyDir.isDirectory() || !versionDir.isDirectory()) {
        throw new ISE("Hadoop dependency [%s] didn't exist!?", versionDir.getAbsolutePath());
      }
      hadoopDependenciesToLoad[i++] = versionDir;
    }
    return hadoopDependenciesToLoad;
  }

  /**
   * @param extension The File instance of the extension we want to load
   *
   * @return a URLClassLoader that loads all the jars on which the extension is dependent
   *
   * @throws MalformedURLException
   */
  public static URLClassLoader getClassLoaderForExtension(File extension) throws MalformedURLException
  {
    URLClassLoader loader = loadersMap.get(extension);
    if (loader == null) {
      final Collection<File> jars = FileUtils.listFiles(extension, new String[]{"jar"}, false);
      final URL[] urls = new URL[jars.size()];
      int i = 0;
      for (File jar : jars) {
        final URL url = jar.toURI().toURL();
        log.info("added URL[%s]", url);
        urls[i++] = url;
      }
      loadersMap.putIfAbsent(extension, new URLClassLoader(urls, Initialization.class.getClassLoader()));
      loader = loadersMap.get(extension);
    }
    return loader;
  }

  public static List<URL> getURLsForClasspath(String cp)
  {
    try {
      String[] paths = cp.split(File.pathSeparator);

      List<URL> urls = new ArrayList<>();
      for (String path : paths) {
        File f = new File(path);
        if ("*".equals(f.getName())) {
          File parentDir = f.getParentFile();
          if (parentDir.isDirectory()) {
            File[] jars = parentDir.listFiles(
                new FilenameFilter()
                {
                  @Override
                  public boolean accept(File dir, String name)
                  {
                    return name != null && (name.endsWith(".jar") || name.endsWith(".JAR"));
                  }
                }
            );
            for (File jar : jars) {
              urls.add(jar.toURI().toURL());
            }
          }
        } else {
          urls.add(new File(path).toURI().toURL());
        }
      }
      return urls;
    }
    catch (IOException ex) {
      throw Throwables.propagate(ex);
    }
  }

  public static Injector makeInjectorWithModules(final Injector baseInjector, Iterable<? extends Module> modules)
  {
    final ModuleList defaultModules = new ModuleList(baseInjector);
    defaultModules.addModules(
        // New modules should be added after Log4jShutterDownerModule
        new Log4jShutterDownerModule(),
        new DruidAuthModule(),
        new LifecycleModule(),
        EmitterModule.class,
        HttpClientModule.global(),
        HttpClientModule.escalatedGlobal(),
        new HttpClientModule("druid.broker.http", Client.class),
        new HttpClientModule("druid.broker.http", EscalatedClient.class),
        new CuratorModule(),
        new AnnouncerModule(),
        new AWSModule(),
        new MetricsModule(),
        new SegmentWriteOutMediumModule(),
        new ServerModule(),
        new DruidProcessingConfigModule(),
        new StorageNodeModule(),
        new JettyServerModule(),
        new ExpressionModule(),
        new DiscoveryModule(),
        new ServerViewModule(),
        new MetadataConfigModule(),
        new DerbyMetadataStorageDruidModule(),
        new JacksonConfigManagerModule(),
        new IndexingServiceDiscoveryModule(),
        new CoordinatorDiscoveryModule(),
        new LocalDataStorageDruidModule(),
        new FirehoseModule(),
        new ParsersModule(),
        new JavaScriptModule(),
        new AuthenticatorModule(),
        new AuthenticatorMapperModule(),
        new EscalatorModule(),
        new AuthorizerModule(),
        new AuthorizerMapperModule(),
        new StartupLoggingModule()
    );

    ModuleList actualModules = new ModuleList(baseInjector);
    actualModules.addModule(DruidSecondaryModule.class);
    for (Object module : modules) {
      actualModules.addModule(module);
    }

    Module intermediateModules = Modules.override(defaultModules.getModules()).with(actualModules.getModules());

    ModuleList extensionModules = new ModuleList(baseInjector);
    final ExtensionsConfig config = baseInjector.getInstance(ExtensionsConfig.class);
    for (DruidModule module : Initialization.getFromExtensions(config, DruidModule.class)) {
      extensionModules.addModule(module);
    }

    return Guice.createInjector(Modules.override(intermediateModules).with(extensionModules.getModules()));
  }

  private static class ModuleList
  {
    private final Injector baseInjector;
    private final ModulesConfig modulesConfig;
    private final ObjectMapper jsonMapper;
    private final ObjectMapper smileMapper;
    private final List<Module> modules;

    public ModuleList(Injector baseInjector)
    {
      this.baseInjector = baseInjector;
      this.modulesConfig = baseInjector.getInstance(ModulesConfig.class);
      this.jsonMapper = baseInjector.getInstance(Key.get(ObjectMapper.class, Json.class));
      this.smileMapper = baseInjector.getInstance(Key.get(ObjectMapper.class, Smile.class));
      this.modules = Lists.newArrayList();
    }

    private List<Module> getModules()
    {
      return Collections.unmodifiableList(modules);
    }

    public void addModule(Object input)
    {
      if (input instanceof DruidModule) {
        if (!checkModuleClass(input.getClass())) {
          return;
        }
        baseInjector.injectMembers(input);
        modules.add(registerJacksonModules(((DruidModule) input)));
      } else if (input instanceof Module) {
        if (!checkModuleClass(input.getClass())) {
          return;
        }
        baseInjector.injectMembers(input);
        modules.add((Module) input);
      } else if (input instanceof Class) {
        if (!checkModuleClass((Class<?>) input)) {
          return;
        }
        if (DruidModule.class.isAssignableFrom((Class) input)) {
          modules.add(registerJacksonModules(baseInjector.getInstance((Class<? extends DruidModule>) input)));
        } else if (Module.class.isAssignableFrom((Class) input)) {
          modules.add(baseInjector.getInstance((Class<? extends Module>) input));
          return;
        } else {
          throw new ISE("Class[%s] does not implement %s", input.getClass(), Module.class);
        }
      } else {
        throw new ISE("Unknown module type[%s]", input.getClass());
      }
    }

    private boolean checkModuleClass(Class<?> moduleClass)
    {
      String moduleClassName = moduleClass.getCanonicalName();
      if (moduleClassName != null && modulesConfig.getExcludeList().contains(moduleClassName)) {
        log.info("Not loading module [%s] because it is present in excludeList", moduleClassName);
        return false;
      }
      return true;
    }

    public void addModules(Object... object)
    {
      for (Object o : object) {
        addModule(o);
      }
    }

    private DruidModule registerJacksonModules(DruidModule module)
    {
      for (com.fasterxml.jackson.databind.Module jacksonModule : module.getJacksonModules()) {
        jsonMapper.registerModule(jacksonModule);
        smileMapper.registerModule(jacksonModule);
      }
      return module;
    }
  }
}


text:
require 'rails_helper'

RSpec.describe OccupationsController, type: :controller do

end


text:
module ROM
  module Commands
    class Lazy
      class Create < Lazy
        def call(*args)
          first = args.first
          last = args.last
          size = args.size

          if size > 1 && last.is_a?(Array)
            last.map.with_index do |parent, index|
              children = evaluator.call(first, index)
              command_proc[command, parent, children].call(children, parent)
            end.reduce(:concat)
          else
            input = evaluator.call(first)
            command.call(input, *args[1..size-1])
          end
        end
      end
    end
  end
end


text:
require 'freak'
require 'minitest/spec'
require 'minitest/autorun'


text:
/* TEMPLATE GENERATED TESTCASE FILE
Filename: CWE78_OS_Command_Injection__char_file_w32spawnl_22b.c
Label Definition File: CWE78_OS_Command_Injection.strings.label.xml
Template File: sources-sink-22b.tmpl.c
*/
/*
 * @description
 * CWE: 78 OS Command Injection
 * BadSource: file Read input from a file
 * GoodSource: Fixed string
 * Sink: w32spawnl
 *    BadSink : execute command with spawnl
 * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.
 *
 * */

#include "std_testcase.h"

#include <wchar.h>

#ifdef _WIN32
#define COMMAND_INT_PATH "%WINDIR%\\system32\\cmd.exe"
#define COMMAND_INT "cmd.exe"
#define COMMAND_ARG1 "/c"
#define COMMAND_ARG2 "dir "
#define COMMAND_ARG3 data
#else /* NOT _WIN32 */
#include <unistd.h>
#define COMMAND_INT_PATH "/bin/sh"
#define COMMAND_INT "sh"
#define COMMAND_ARG1 "-c"
#define COMMAND_ARG2 "ls "
#define COMMAND_ARG3 data
#endif

#ifdef _WIN32
#define FILENAME "C:\\temp\\file.txt"
#else
#define FILENAME "/tmp/file.txt"
#endif

#ifndef OMITBAD

/* The global variable below is used to drive control flow in the source function */
extern int CWE78_OS_Command_Injection__char_file_w32spawnl_22_badGlobal;

char * CWE78_OS_Command_Injection__char_file_w32spawnl_22_badSource(char * data)
{
    if(CWE78_OS_Command_Injection__char_file_w32spawnl_22_badGlobal)
    {
        {
            /* Read input from a file */
            size_t dataLen = strlen(data);
            FILE * pFile;
            /* if there is room in data, attempt to read the input from a file */
            if (100-dataLen > 1)
            {
                pFile = fopen(FILENAME, "r");
                if (pFile != NULL)
                {
                    /* POTENTIAL FLAW: Read data from a file */
                    if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                    {
                        printLine("fgets() failed");
                        /* Restore NUL terminator if fgets fails */
                        data[dataLen] = '\0';
                    }
                    fclose(pFile);
                }
            }
        }
    }
    return data;
}

#endif /* OMITBAD */

#ifndef OMITGOOD

/* The global variables below are used to drive control flow in the source functions. */
extern int CWE78_OS_Command_Injection__char_file_w32spawnl_22_goodG2B1Global;
extern int CWE78_OS_Command_Injection__char_file_w32spawnl_22_goodG2B2Global;

/* goodG2B1() - use goodsource and badsink by setting the static variable to false instead of true */
char * CWE78_OS_Command_Injection__char_file_w32spawnl_22_goodG2B1Source(char * data)
{
    if(CWE78_OS_Command_Injection__char_file_w32spawnl_22_goodG2B1Global)
    {
        /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
        printLine("Benign, fixed string");
    }
    else
    {
        /* FIX: Append a fixed string to data (not user / external input) */
        strcat(data, "*.*");
    }
    return data;
}

/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if in the source function */
char * CWE78_OS_Command_Injection__char_file_w32spawnl_22_goodG2B2Source(char * data)
{
    if(CWE78_OS_Command_Injection__char_file_w32spawnl_22_goodG2B2Global)
    {
        /* FIX: Append a fixed string to data (not user / external input) */
        strcat(data, "*.*");
    }
    return data;
}

#endif /* OMITGOOD */


text:
<?php

declare(strict_types=1);

namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field\Renderer;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
use Magento\Store\Model\StoreManagerInterface;

class Actions extends Select
{
    /**
     * @var array
     */
    public $options = null;
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    /**
     * Countries constructor.
     *
     * @param Context $context
     * @param StoreManagerInterface $storeManager
     * @param array $data
     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->storeManager = $storeManager;
    }

    /**
     * Render block HTML
     *
     * @return string
     */
    public function _toHtml()
    {
        if (!$this->getOptions()) {
            foreach ($this->getAllActions() as $action) {
                $this->addOption($action['value'], $action['label']);
            }
        }

        return parent::_toHtml();
    }

    /**
     * Get all countries
     *
     * @return array
     */
    private function getAllActions(): ?array
    {
        if (!$this->options) {
            $this->options[] = ['value' => '', 'label' => ''];
            $this->options[] = $this->getFormatOptions();
            $this->options[] = $this->getPriceOptions();
        }

        return $this->options;
    }

    /**
     * @return array
     */
    private function getFormatOptions(): array
    {
        return [
            'label' => __('Formatting'),
            'value' => [
                [
                    'label' => __('Striptags'),
                    'value' => 'striptags'
                ],
                [
                    'label' => __('Round'),
                    'value' => 'round'
                ],
                [
                    'label' => __('Number'),
                    'value' => 'number'
                ],
            ],
            'optgroup-name' => __('Formatting'),
        ];
    }

    /**
     * @return array
     */
    private function getPriceOptions(): array
    {
        $priceOptions = [];

        try {
            $storeId = (int)$this->getRequest()->getParam('store');
            $availableCurrencies = $this->storeManager->getStore($storeId)->getAvailableCurrencyCodes();
        } catch (NoSuchEntityException $exception) {
            return $priceOptions;
        }

        foreach ($availableCurrencies as $currencyCode) {
            $priceOptions[] = [
                'label' => $currencyCode,
                'value' => 'currency_' . $currencyCode
            ];
        }

        return [
            'label' => __('Render in currency'),
            'value' => $priceOptions,
            'optgroup-name' => __('Render in currency'),
        ];
    }

    /**
     * Sets name for input element
     *
     * @param $value
     *
     * @return mixed
     */
    public function setInputName($value)
    {
        return $this->setName($value);
    }
}


text:
using System;
using Windows.System;
using Cimbalino.Toolkit.Services;
using GalaSoft.MvvmLight.Command;
using Kliva.Models;
using Kliva.Services;
using Kliva.Services.Interfaces;
using Kliva.Views;

namespace Kliva.ViewModels
{
    public class LoginViewModel : KlivaBaseViewModel
    {
        private IStravaService _stravaService;
        private IMessageBoxService _messageBoxService;

        private RelayCommand _loginCommand;
        public RelayCommand LoginCommand => _loginCommand ?? (_loginCommand = new RelayCommand(async () =>
        {
            IsBusy = true;
            await _stravaService.GetAuthorizationCode();
            IsBusy = false;
        }));

        private RelayCommand _viewLoadedCommand;
        public RelayCommand ViewLoadedCommand => _viewLoadedCommand ?? (_viewLoadedCommand = new RelayCommand(ViewLoaded));

        private RelayCommand _newAccountCommand;
        public RelayCommand NewAccountCommand => _newAccountCommand ?? (_newAccountCommand = new RelayCommand(async () => await Launcher.LaunchUriAsync(new Uri(Constants.STRAVA_NEW_ACCOUNT))));

        public LoginViewModel(INavigationService navigationService, IMessageBoxService messageBoxService, IStravaService stravaService) : base(navigationService)
        {
            _stravaService = stravaService;
            _messageBoxService = messageBoxService;
        }

        private void ViewLoaded()
        {
            _stravaService.StatusEvent += OnStravaStatusEvent;
        }

        private async void OnStravaStatusEvent(object sender, StravaServiceEventArgs e)
        {
            switch (e.Status)
            {
                case StravaServiceStatus.Success:
                    _stravaService.StatusEvent -= OnStravaStatusEvent;

                    //Remove the current 'login page' back entry and navigate to the main page
                    NavigationService.Navigate<MainPage>();
                    NavigationService.RemoveBackEntry();

                    IsBusy = false;
                    break;
                case StravaServiceStatus.Failed:
                    IsBusy = false;

                    //TODO: Glenn - Add more text value through resources
                    await _messageBoxService.ShowAsync("Something happend...", "Error");
                    break;
            }
        }
    }
}


text:
package io.muoncore.spring.methodinvocation.parameterhandlers;

import io.muoncore.protocol.rpc.client.requestresponse.Request;
import io.muoncore.protocol.rpc.client.requestresponse.server.RequestWrapper;

import java.lang.reflect.Parameter;

public class ResourceMethodArgumentTransformerFactory {
    public static MethodArgumentTransformer createMethodParameterHandler(Parameter parameter) {
        if (RequestWrapper.class.equals(parameter.getType())) {
            return new PassThroughArgumentTransformer(parameter);
        } if (Request.class.equals(parameter.getType())) {
            return new RequestArgumentTransformer(parameter);
        } else if (parameter.isAnnotationPresent(io.muoncore.spring.annotations.parameterhandlers.Parameter.class)) {
            return new ParameterEventArgumentTransformer(parameter);
        } else {
            return new PayloadArgumentTransformer(parameter);
        }
    }
}