text:
<!DOCTYPE html>

	<title>

            MJW Communications -

    Complete profile - Canadian Company Capabilities - Industries and Business - Industry Canada
  </title>

<meta charset="utf-8" />
<meta name="dcterms.language" title="ISO639-2" content="eng" />
<meta name="dcterms.title" content="" />
<meta name="description" content="" />
<meta name="dcterms.description" content="" />
<meta name="dcterms.type" content="report, data set" />
<meta name="dcterms.subject" content="businesses, industry" />
<meta name="dcterms.subject" content="businesses, industry" />
<meta name="dcterms.issued" title="W3CDTF" content="" />
<meta name="dcterms.modified" title="W3CDTF" content="" />
<meta name="keywords" content="" />

<meta name="dcterms.creator" content="" />
<meta name="author" content="" />
<meta name="dcterms.created" title="W3CDTF" content="" />
<meta name="dcterms.publisher" content="" />
<meta name="dcterms.audience" title="icaudience" content="" />
<meta name="dcterms.spatial" title="ISO3166-1" content="" />
<meta name="dcterms.spatial" title="gcgeonames" content="" />
<meta name="dcterms.format" content="HTML" />
<meta name="dcterms.identifier" title="ICsiteProduct" content="536" />

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta content="width=device-width,initial-scale=1" name="viewport">

<meta name = "format-detection" content = "telephone=no">

<meta name="robots" content="noarchive">

<link href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/assets/favicon.ico" rel="icon" type="image/x-icon">
<link rel="stylesheet" href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/css/wet-boew.min.css">

<link rel="stylesheet" href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/css/theme.min.css">

<link rel="stylesheet" href="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/css/noscript.min.css" />

<link rel="stylesheet" href="/eic/home.nsf/css/add_WET_4-0_Canada_Apps.css">

  	<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
  	<link href="/app/ccc/srch/css/print.css" media="print" rel="stylesheet" type="text/css" />

<meta property="legalName" content="Department_of_Industry">

Skip to main content

Skip to "About this site"

Language selection

Français

<object type="image/svg+xml" tabindex="-1" data="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/assets/sig-blk-en.svg"></object> Government of Canada

Search and menus

Search and menus

Topics menu

Jobs

Immigration

Travel

Business

Benefits

Health

Taxes

More services

You are here:

Home

Industries and Business

Canadian Company Capabilities

<input id="showMore" type="hidden" value='more'/>
<input id="showLess" type="hidden" value='less'/>

 Company profile - Canadian Company Capabilities

New Search |

<form name="searchForm" method="post" action="/app/ccc/srch/bscSrch.do">

          <input type="hidden" name="lang" value="eng" />
          <input type="hidden" name="profileId" value="" />
          <input type="hidden" name="prtl" value="1" />
          <input type="hidden" name="searchPage" value="%2Fapp%2Fccc%2Fsrch%2FcccBscSrch.do%3Flang%3Deng%26amp%3Bprtl%3D1%26amp%3Btagid%3D"  />

          <input type="hidden" name="V_SEARCH.scopeCategory" value="CCC.Root" />

          <input type="hidden" name="V_SEARCH.depth" value="1" />
          <input type="hidden" name="V_SEARCH.showStricts" value="false" />
          <input id="repeatSearchBtn" class="btn btn-link" type="submit" value="Return to search results" />

  </form>

| Previous Company

| Next Company

      Third-Party Information Liability Disclaimer

Some of the information on this Web page has been provided by external sources. The Government of Canada is not responsible for the accuracy, reliability or currency of the information supplied by external sources. Users wishing to rely upon this information should consult directly with the source of the information. Content provided by external sources is not subject to official languages, privacy and accessibility requirements.

      MJW Communications Group

Legal Name:

MJW Communications Group

Operating Name:

MJW Communications

http://www.mjwcommunications.com

mwallens@mjwcommunications.com

Mailing Address:

              	<address class="mrgn-bttm-md">

                   202-1177 Yonge St.

                    TORONTO,

                    Ontario

                  M4T 2Y4

                </address>

Location Address:

              	         <address class="mrgn-bttm-md">

                   202-1177 Yonge St.

                    TORONTO,

                    Ontario

                  M4T 2Y4

                         </address>

Tel.:
		      (416) 961-5924

Fax:
		      (416) 961-5924

Company Profile

            MJW Communications is a integrated marketing communications company that helps expanding or emerging companies make contacts and build relationships to increase awareness of the  company, products or services resulting in increased sales.   Services include: media relations, branding, marketing and communications.

				Full profile

				Full profile

				Contact information

                Marjorie
                Wallens

	                  Title:

  						Principal

                  Telephone:

                (416) 961-5924

                  Facsimile:

                (416) 961-7783

                    Email:

                  mwallens@mjwcommunications.com

  top of page

				Company description

            Country of Ownership:

              Canada

              Year Established:

            1994

            Exporting:

              No

                Primary Industry (NAICS):

				541619 - Other Management Consulting Services

            Primary Business Activity:

            Services

				Product / Service / Licensing

                              Service Name:

                            Marketing Communications

                            MJW Communications offers a practical approach to problem solving and creative ideas to help build market share for existing brands and support launches of products and services. Often the first step in the process is to develop a marketing communications plan based on solid research, communications expertise and a common sense approach to creating awareness.

                              Service Name:

                            Media Relations

                            Marjorie Wallens, principal of MJW Communications, is a former broadcaster, parliamentary correspondent and journalist. She is highly regarded for her media savvy, contacts and ability to gain media attention by positioning stories in the best possible way.

MJW Communications looks for ways to enhance the news value of your story by creating speaking & photo opportunities, special events, one-on-one interviews, writing op-ed page articles, arranging editorial board meetings, and creating interest and coverage in community news papers and trade publications.

Working with the media, MJW Communications also provides advice and guidance on issues / crisis management.

                              Service Name:

                            Employee Communications

                            Sound employee communications is critical to ensuring a productive and positive work environment. MJW Communications has a special expertise in developing employee communications strategies that help align corporate strategies to the workforce. From town hall meetings, special events and celebrations to employee newsletters, MJW Communications works with its clients to develop an effective program.

              FSC Code:

              T002-Communications Services (incl. exhibit Services)

              T004-Public Relations Services (incl. Writing Services, Event Planning and Management, Media Relations, Radio and TV Analysis, Press Services)

  top of page

					Sector information

                Key / Major clients:

              Sears Hearing Centres, STAPLES Business Depot, priszm brandz, ROBTV, International Association of Career Professionals, Total Wellness Consulting, Biovail, Metrick System,

                Success stories:

              STAPLES Business Depot, Canada

Media Relations

Record media coverage set STAPLES apart from its competitors

Description:

Back to School Campaigns 2004 & 2005

Seneca College of Applied Arts and Technology

Media Relations & Marketing and Communications

Seneca boosts awareness by 13%-Gains attention of government and business leaders.

Description:

Re-branding and a year long public awareness and stakeholder campaign gets results.

Project:

Seneca College of Applied Arts and Technology is the largest college in Canada with over 100,000 students. To support a campus expansion program Seneca required stronger brand recognition within the business and government sectors and a higher profile within the media. A year long campaign included re-branding its visual identity; repositioning Seneca to reflect a global image, extensive use of media and themed corporate events supported by a print and outdoor image advertising campaign

Results:

Awareness of Seneca was boosted by 13% in target audiences

priszm brandz

Corporate Communications

Building Corporate Profile nets results

Description:

A private company and the largest owner of KFC, Pizza Hut and Taco Bell restaurants in Canada, was eager to quickly build its image as an energetic and successful operation to strengthen its credibility within the Canadian and American financial communities and with suppliers.

Project:

MJW Communications developed a corporate communications strategy focusing on the company's senior leadership, namely its President. Favorable company and personal profile press in mainstream and trade media helped build an impressive record of media coverage and awareness. Meet and greet events designed to introduce potential suppliers and other stakeholders to the company also helped quickly launch the company as a 'player'.

Results:

Company went public as income trust in 2004.

Project:

MJW Communications was asked to build product awareness and enhance the company's corporate image during this critical sales period. Developing solid story angles with Canada wide appeal, creating print and video advertorials plus rigorous media outreach set Staples apart from its competitors.

Results:

Campaign achieved over 12 million impressions of national media coverage-a record high -in daily and community news papers including the Globe and Mail, Toronto Star & National Post and broadcast media.

Pizza Hut, KFC, Taco Bell

Employee Communications

Bringing two cultures and companies together helps builds new spirit of entrepreneurialism.Description:

Strategic events and news letters were created to help build 'national 'spirit of enterprise' in this newly merged company of 16,000.

Project:

When a new company in Canada was created that brought together some of the most high profile brands in the quick service industry- it also brought together 16,000 employees from two companies with very different corporate cultures. MJW Communications was asked to help convey the new spirit and culture of the company to a unified workforce. Employee newsletters, town hall meetings, sales award ceremonies, fundraising, and special events –were strategically created and designed to ensure employees were aligned with the new company's corporate strategy of growth.

Results:

A reinvigorated and more productive workforce enabled company to go public.

ROB TV- Report on Business Television

Marketing Communications

Audience ratings increased by 2%

Description:

Define and create ROBTV marketing and communications plan –to boost ratings and advertiser interest.

Project:

ROB TV, a specialty TV channel devoted to reporting Canadian business news and events, was celebrating its 5th anniversary. MJW Communications was asked to review its marketing strategy and develop a marketing communications plan to boost ratings and increase market share.

A comprehensive marketing and communications plan, based on practical tactics to strengthen existing market relationships combined with an outdoor billboard campaign and radio spots helped boost audience and advertiser interest. A 5th anniversary bash held at the TSX Exchange for Bay 'Streeters' and sponsors, underscored the success and growth of ROB TV from a fledgling specialty channel into a permanent and successful fixture in the television and business community.

Results:

Audience ratings increased by 2%.

Description:

Strategic events and news letters were created to help build 'national 'spirit of enterprise' in this newly merged company of 16,000.

Project:

When a new company in Canada was created that brought together some of the most high profile brands in the quick service industry- it also brought together 16,000 employees from two companies with very different corporate cultures. MJW Communications was asked to help convey the new spirit and culture of the company to a unified workforce. Employee newsletters, town hall meetings, sales award ceremonies, fundraising, and special events –were strategically created and designed to ensure employees were aligned with the new company's corporate strategy of growth.

Results:

A reinvigorated and more productive workforce enabled company to go public.

STAPLES Business Depot, Canada

Celebrity helps build new brand

Description:

STAPLES opened new concept print and copy centres in Montreal. Quebec singer and broadcaster Mitsou adds celebrity power.

Project:

MJW Communications was asked to develop a Grand Opening event for DOSSIER stores, in downtown Montréal, and create media awareness. Quebec celebrity Mitsou became the face of Dossier in an outdoor marketing campaign and host of a grand opening media and VIP event.

Results:

Dossier received coverage in local and trade media.

                Testimonial:

              'Marjorie's professionalism and knowledge is a tremendous help to the PR department…. She developed solid story angles and obtained TV and newspaper coverage setting us apart from our competitors resulting in over 12 million impressions over a two month period, a record high. She makes us all look good.'

PR Manager, a major North American Retailer

'We have had great success in boosting our presence among our audience and advertising clients, both measurable outcomes to which Ms. Wallens contributed greatly. Her industry experience, creative ideas and practical approach to problem-solving are valued assets and just as important…she understands her role as a consultant and how to manage her client relationships to everyone's satisfaction.'

General Manager, a national specialty television channel

'Marjorie's contribution has gone well beyond expectations, and we are now frequently sought out for comment and expert opinion by the general and trade media. She has been a true strategic partner to our Association providing invaluable input to our strategic planning process, and all communications and marketing strategies.'

Elaine Sigurdson, President, ACP International, Toronto Chapter

					Third-Party Information Liability Disclaimer

					Some of the information on this Web page has been provided by external sources. The Government of Canada is not responsible for the accuracy, reliability or currency of the information supplied by external sources. Users wishing to rely upon this information should consult directly with the source of the information. Content provided by external sources is not subject to official languages, privacy and accessibility requirements.

				Contacts

				Contact information

                Marjorie
                Wallens

	                  Title:

  						Principal

                  Telephone:

                (416) 961-5924

                  Facsimile:

                (416) 961-7783

                    Email:

                  mwallens@mjwcommunications.com

				Description

				Company description

            Country of Ownership:

              Canada

              Year Established:

            1994

            Exporting:

              No

                Primary Industry (NAICS):

				541619 - Other Management Consulting Services

            Primary Business Activity:

            Services

				Products, services and licensing

				Product / Service / Licensing

                              Service Name:

                            Marketing Communications

                            MJW Communications offers a practical approach to problem solving and creative ideas to help build market share for existing brands and support launches of products and services. Often the first step in the process is to develop a marketing communications plan based on solid research, communications expertise and a common sense approach to creating awareness.

                              Service Name:

                            Media Relations

                            Marjorie Wallens, principal of MJW Communications, is a former broadcaster, parliamentary correspondent and journalist. She is highly regarded for her media savvy, contacts and ability to gain media attention by positioning stories in the best possible way.

MJW Communications looks for ways to enhance the news value of your story by creating speaking & photo opportunities, special events, one-on-one interviews, writing op-ed page articles, arranging editorial board meetings, and creating interest and coverage in community news papers and trade publications.

Working with the media, MJW Communications also provides advice and guidance on issues / crisis management.

                              Service Name:

                            Employee Communications

                            Sound employee communications is critical to ensuring a productive and positive work environment. MJW Communications has a special expertise in developing employee communications strategies that help align corporate strategies to the workforce. From town hall meetings, special events and celebrations to employee newsletters, MJW Communications works with its clients to develop an effective program.

              FSC Code:

              T002-Communications Services (incl. exhibit Services)

              T004-Public Relations Services (incl. Writing Services, Event Planning and Management, Media Relations, Radio and TV Analysis, Press Services)

					Sector

					Sector information

                Key / Major clients:

              Sears Hearing Centres, STAPLES Business Depot, priszm brandz, ROBTV, International Association of Career Professionals, Total Wellness Consulting, Biovail, Metrick System,

                Success stories:

              STAPLES Business Depot, Canada

Media Relations

Record media coverage set STAPLES apart from its competitors

Description:

Back to School Campaigns 2004 & 2005

Seneca College of Applied Arts and Technology

Media Relations & Marketing and Communications

Seneca boosts awareness by 13%-Gains attention of government and business leaders.

Description:

Re-branding and a year long public awareness and stakeholder campaign gets results.

Project:

Seneca College of Applied Arts and Technology is the largest college in Canada with over 100,000 students. To support a campus expansion program Seneca required stronger brand recognition within the business and government sectors and a higher profile within the media. A year long campaign included re-branding its visual identity; repositioning Seneca to reflect a global image, extensive use of media and themed corporate events supported by a print and outdoor image advertising campaign

Results:

Awareness of Seneca was boosted by 13% in target audiences

priszm brandz

Corporate Communications

Building Corporate Profile nets results

Description:

A private company and the largest owner of KFC, Pizza Hut and Taco Bell restaurants in Canada, was eager to quickly build its image as an energetic and successful operation to strengthen its credibility within the Canadian and American financial communities and with suppliers.

Project:

MJW Communications developed a corporate communications strategy focusing on the company's senior leadership, namely its President. Favorable company and personal profile press in mainstream and trade media helped build an impressive record of media coverage and awareness. Meet and greet events designed to introduce potential suppliers and other stakeholders to the company also helped quickly launch the company as a 'player'.

Results:

Company went public as income trust in 2004.

Project:

MJW Communications was asked to build product awareness and enhance the company's corporate image during this critical sales period. Developing solid story angles with Canada wide appeal, creating print and video advertorials plus rigorous media outreach set Staples apart from its competitors.

Results:

Campaign achieved over 12 million impressions of national media coverage-a record high -in daily and community news papers including the Globe and Mail, Toronto Star & National Post and broadcast media.

Pizza Hut, KFC, Taco Bell

Employee Communications

Bringing two cultures and companies together helps builds new spirit of entrepreneurialism.Description:

Strategic events and news letters were created to help build 'national 'spirit of enterprise' in this newly merged company of 16,000.

Project:

When a new company in Canada was created that brought together some of the most high profile brands in the quick service industry- it also brought together 16,000 employees from two companies with very different corporate cultures. MJW Communications was asked to help convey the new spirit and culture of the company to a unified workforce. Employee newsletters, town hall meetings, sales award ceremonies, fundraising, and special events –were strategically created and designed to ensure employees were aligned with the new company's corporate strategy of growth.

Results:

A reinvigorated and more productive workforce enabled company to go public.

ROB TV- Report on Business Television

Marketing Communications

Audience ratings increased by 2%

Description:

Define and create ROBTV marketing and communications plan –to boost ratings and advertiser interest.

Project:

ROB TV, a specialty TV channel devoted to reporting Canadian business news and events, was celebrating its 5th anniversary. MJW Communications was asked to review its marketing strategy and develop a marketing communications plan to boost ratings and increase market share.

A comprehensive marketing and communications plan, based on practical tactics to strengthen existing market relationships combined with an outdoor billboard campaign and radio spots helped boost audience and advertiser interest. A 5th anniversary bash held at the TSX Exchange for Bay 'Streeters' and sponsors, underscored the success and growth of ROB TV from a fledgling specialty channel into a permanent and successful fixture in the television and business community.

Results:

Audience ratings increased by 2%.

Description:

Strategic events and news letters were created to help build 'national 'spirit of enterprise' in this newly merged company of 16,000.

Project:

When a new company in Canada was created that brought together some of the most high profile brands in the quick service industry- it also brought together 16,000 employees from two companies with very different corporate cultures. MJW Communications was asked to help convey the new spirit and culture of the company to a unified workforce. Employee newsletters, town hall meetings, sales award ceremonies, fundraising, and special events –were strategically created and designed to ensure employees were aligned with the new company's corporate strategy of growth.

Results:

A reinvigorated and more productive workforce enabled company to go public.

STAPLES Business Depot, Canada

Celebrity helps build new brand

Description:

STAPLES opened new concept print and copy centres in Montreal. Quebec singer and broadcaster Mitsou adds celebrity power.

Project:

MJW Communications was asked to develop a Grand Opening event for DOSSIER stores, in downtown Montréal, and create media awareness. Quebec celebrity Mitsou became the face of Dossier in an outdoor marketing campaign and host of a grand opening media and VIP event.

Results:

Dossier received coverage in local and trade media.

                Testimonial:

              'Marjorie's professionalism and knowledge is a tremendous help to the PR department…. She developed solid story angles and obtained TV and newspaper coverage setting us apart from our competitors resulting in over 12 million impressions over a two month period, a record high. She makes us all look good.'

PR Manager, a major North American Retailer

'We have had great success in boosting our presence among our audience and advertising clients, both measurable outcomes to which Ms. Wallens contributed greatly. Her industry experience, creative ideas and practical approach to problem-solving are valued assets and just as important…she understands her role as a consultant and how to manage her client relationships to everyone's satisfaction.'

General Manager, a national specialty television channel

'Marjorie's contribution has gone well beyond expectations, and we are now frequently sought out for comment and expert opinion by the general and trade media. She has been a true strategic partner to our Association providing invaluable input to our strategic planning process, and all communications and marketing strategies.'

Elaine Sigurdson, President, ACP International, Toronto Chapter

    Last Update Date 2015-11-19

Date Modified:

      <time>2017-03-02</time>

About government

Contact us

Departments and agencies

Public service and military

News

Treaties, laws and regulations

Government-wide reporting

Prime Minister

How government works

Open government

About this site

Social media

Mobile applications

About Canada.ca

Terms and conditions

Privacy

Top of Page

<object type="image/svg+xml" tabindex="-1" role="img" data="/utils/scripts/_WET_4-0/apps/themes-dist/gcweb/assets/wmms-blk.svg" aria-label="Symbol of the Government of Canada"></object>


text:
'use strict';

var Buffer = require('./buffer').Buffer;
var Transform = require('./_stream_transform');
var binding = process.binding('zlib');
var util = require('./util');
var assert = require('./assert').ok;
var kMaxLength = require('./buffer').kMaxLength;
var kRangeErrorMessage = 'Cannot create final Buffer. ' +
    'It would be larger than 0x' + kMaxLength.toString(16) + ' bytes.';

// zlib doesn't provide these, so kludge them in following the same
// const naming scheme zlib uses.
binding.Z_MIN_WINDOWBITS = 8;
binding.Z_MAX_WINDOWBITS = 15;
binding.Z_DEFAULT_WINDOWBITS = 15;

// fewer than 64 bytes per chunk is stupid.
// technically it could work with as few as 8, but even 64 bytes
// is absurdly low.  Usually a MB or more is best.
binding.Z_MIN_CHUNK = 64;
binding.Z_MAX_CHUNK = Infinity;
binding.Z_DEFAULT_CHUNK = (16 * 1024);

binding.Z_MIN_MEMLEVEL = 1;
binding.Z_MAX_MEMLEVEL = 9;
binding.Z_DEFAULT_MEMLEVEL = 8;

binding.Z_MIN_LEVEL = -1;
binding.Z_MAX_LEVEL = 9;
binding.Z_DEFAULT_LEVEL = binding.Z_DEFAULT_COMPRESSION;

// expose all the zlib constants
var bkeys = Object.keys(binding);
for (var bk = 0; bk < bkeys.length; bk++) {
  var bkey = bkeys[bk];
  if (bkey.match(/^Z/)) {
    Object.defineProperty(exports, bkey, {
      enumerable: true, value: binding[bkey], writable: false
    });
  }
}

// translation table for return codes.
var codes = {
  Z_OK: binding.Z_OK,
  Z_STREAM_END: binding.Z_STREAM_END,
  Z_NEED_DICT: binding.Z_NEED_DICT,
  Z_ERRNO: binding.Z_ERRNO,
  Z_STREAM_ERROR: binding.Z_STREAM_ERROR,
  Z_DATA_ERROR: binding.Z_DATA_ERROR,
  Z_MEM_ERROR: binding.Z_MEM_ERROR,
  Z_BUF_ERROR: binding.Z_BUF_ERROR,
  Z_VERSION_ERROR: binding.Z_VERSION_ERROR
};

var ckeys = Object.keys(codes);
for (var ck = 0; ck < ckeys.length; ck++) {
  var ckey = ckeys[ck];
  codes[codes[ckey]] = ckey;
}

Object.defineProperty(exports, 'codes', {
  enumerable: true, value: Object.freeze(codes), writable: false
});

exports.Deflate = Deflate;
exports.Inflate = Inflate;
exports.Gzip = Gzip;
exports.Gunzip = Gunzip;
exports.DeflateRaw = DeflateRaw;
exports.InflateRaw = InflateRaw;
exports.Unzip = Unzip;

exports.createDeflate = function(o) {
  return new Deflate(o);
};

exports.createInflate = function(o) {
  return new Inflate(o);
};

exports.createDeflateRaw = function(o) {
  return new DeflateRaw(o);
};

exports.createInflateRaw = function(o) {
  return new InflateRaw(o);
};

exports.createGzip = function(o) {
  return new Gzip(o);
};

exports.createGunzip = function(o) {
  return new Gunzip(o);
};

exports.createUnzip = function(o) {
  return new Unzip(o);
};

// Convenience methods.
// compress/decompress a string or buffer in one step.
exports.deflate = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new Deflate(opts), buffer, callback);
};

exports.deflateSync = function(buffer, opts) {
  return zlibBufferSync(new Deflate(opts), buffer);
};

exports.gzip = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new Gzip(opts), buffer, callback);
};

exports.gzipSync = function(buffer, opts) {
  return zlibBufferSync(new Gzip(opts), buffer);
};

exports.deflateRaw = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new DeflateRaw(opts), buffer, callback);
};

exports.deflateRawSync = function(buffer, opts) {
  return zlibBufferSync(new DeflateRaw(opts), buffer);
};

exports.unzip = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new Unzip(opts), buffer, callback);
};

exports.unzipSync = function(buffer, opts) {
  return zlibBufferSync(new Unzip(opts), buffer);
};

exports.inflate = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new Inflate(opts), buffer, callback);
};

exports.inflateSync = function(buffer, opts) {
  return zlibBufferSync(new Inflate(opts), buffer);
};

exports.gunzip = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new Gunzip(opts), buffer, callback);
};

exports.gunzipSync = function(buffer, opts) {
  return zlibBufferSync(new Gunzip(opts), buffer);
};

exports.inflateRaw = function(buffer, opts, callback) {
  if (typeof opts === 'function') {
    callback = opts;
    opts = {};
  }
  return zlibBuffer(new InflateRaw(opts), buffer, callback);
};

exports.inflateRawSync = function(buffer, opts) {
  return zlibBufferSync(new InflateRaw(opts), buffer);
};

function zlibBuffer(engine, buffer, callback) {
  var buffers = [];
  var nread = 0;

  engine.on('error', onError);
  engine.on('end', onEnd);

  engine.end(buffer);
  flow();

  function flow() {
    var chunk;
    while (null !== (chunk = engine.read())) {
      buffers.push(chunk);
      nread += chunk.length;
    }
    engine.once('readable', flow);
  }

  function onError(err) {
    engine.removeListener('end', onEnd);
    engine.removeListener('readable', flow);
    callback(err);
  }

  function onEnd() {
    var buf;
    var err = null;

    if (nread >= kMaxLength) {
      err = new RangeError(kRangeErrorMessage);
    } else {
      buf = Buffer.concat(buffers, nread);
    }

    buffers = [];
    engine.close();
    callback(err, buf);
  }
}

function zlibBufferSync(engine, buffer) {
  if (typeof buffer === 'string')
    buffer = new Buffer(buffer);
  if (!(buffer instanceof Buffer))
    throw new TypeError('Not a string or buffer');

  var flushFlag = binding.Z_FINISH;

  return engine._processChunk(buffer, flushFlag);
}

// generic zlib
// minimal 2-byte header
function Deflate(opts) {
  if (!(this instanceof Deflate)) return new Deflate(opts);
  Zlib.call(this, opts, binding.DEFLATE);
}

function Inflate(opts) {
  if (!(this instanceof Inflate)) return new Inflate(opts);
  Zlib.call(this, opts, binding.INFLATE);
}

// gzip - bigger header, same deflate compression
function Gzip(opts) {
  if (!(this instanceof Gzip)) return new Gzip(opts);
  Zlib.call(this, opts, binding.GZIP);
}

function Gunzip(opts) {
  if (!(this instanceof Gunzip)) return new Gunzip(opts);
  Zlib.call(this, opts, binding.GUNZIP);
}

// raw - no header
function DeflateRaw(opts) {
  if (!(this instanceof DeflateRaw)) return new DeflateRaw(opts);
  Zlib.call(this, opts, binding.DEFLATERAW);
}

function InflateRaw(opts) {
  if (!(this instanceof InflateRaw)) return new InflateRaw(opts);
  Zlib.call(this, opts, binding.INFLATERAW);
}

// auto-detect header.
function Unzip(opts) {
  if (!(this instanceof Unzip)) return new Unzip(opts);
  Zlib.call(this, opts, binding.UNZIP);
}

// the Zlib class they all inherit from
// This thing manages the queue of requests, and returns
// true or false if there is anything in the queue when
// you call the .write() method.

function Zlib(opts, mode) {
  this._opts = opts = opts || {};
  this._chunkSize = opts.chunkSize || exports.Z_DEFAULT_CHUNK;

  Transform.call(this, opts);

  if (opts.flush) {
    if (opts.flush !== binding.Z_NO_FLUSH &&
        opts.flush !== binding.Z_PARTIAL_FLUSH &&
        opts.flush !== binding.Z_SYNC_FLUSH &&
        opts.flush !== binding.Z_FULL_FLUSH &&
        opts.flush !== binding.Z_FINISH &&
        opts.flush !== binding.Z_BLOCK) {
      throw new Error('Invalid flush flag: ' + opts.flush);
    }
  }
  this._flushFlag = opts.flush || binding.Z_NO_FLUSH;

  if (opts.chunkSize) {
    if (opts.chunkSize < exports.Z_MIN_CHUNK ||
        opts.chunkSize > exports.Z_MAX_CHUNK) {
      throw new Error('Invalid chunk size: ' + opts.chunkSize);
    }
  }

  if (opts.windowBits) {
    if (opts.windowBits < exports.Z_MIN_WINDOWBITS ||
        opts.windowBits > exports.Z_MAX_WINDOWBITS) {
      throw new Error('Invalid windowBits: ' + opts.windowBits);
    }
  }

  if (opts.level) {
    if (opts.level < exports.Z_MIN_LEVEL ||
        opts.level > exports.Z_MAX_LEVEL) {
      throw new Error('Invalid compression level: ' + opts.level);
    }
  }

  if (opts.memLevel) {
    if (opts.memLevel < exports.Z_MIN_MEMLEVEL ||
        opts.memLevel > exports.Z_MAX_MEMLEVEL) {
      throw new Error('Invalid memLevel: ' + opts.memLevel);
    }
  }

  if (opts.strategy) {
    if (opts.strategy != exports.Z_FILTERED &&
        opts.strategy != exports.Z_HUFFMAN_ONLY &&
        opts.strategy != exports.Z_RLE &&
        opts.strategy != exports.Z_FIXED &&
        opts.strategy != exports.Z_DEFAULT_STRATEGY) {
      throw new Error('Invalid strategy: ' + opts.strategy);
    }
  }

  if (opts.dictionary) {
    if (!(opts.dictionary instanceof Buffer)) {
      throw new Error('Invalid dictionary: it should be a Buffer instance');
    }
  }

  this._handle = new binding.Zlib(mode);

  var self = this;
  this._hadError = false;
  this._handle.onerror = function(message, errno) {
    // there is no way to cleanly recover.
    // continuing only obscures problems.
    self._handle = null;
    self._hadError = true;

    var error = new Error(message);
    error.errno = errno;
    error.code = exports.codes[errno];
    self.emit('error', error);
  };

  var level = exports.Z_DEFAULT_COMPRESSION;
  if (typeof opts.level === 'number') level = opts.level;

  var strategy = exports.Z_DEFAULT_STRATEGY;
  if (typeof opts.strategy === 'number') strategy = opts.strategy;

  this._handle.init(opts.windowBits || exports.Z_DEFAULT_WINDOWBITS,
                    level,
                    opts.memLevel || exports.Z_DEFAULT_MEMLEVEL,
                    strategy,
                    opts.dictionary);

  this._buffer = new Buffer(this._chunkSize);
  this._offset = 0;
  this._closed = false;
  this._level = level;
  this._strategy = strategy;

  this.once('end', this.close);
}

util.inherits(Zlib, Transform);

Zlib.prototype.params = function(level, strategy, callback) {
  if (level < exports.Z_MIN_LEVEL ||
      level > exports.Z_MAX_LEVEL) {
    throw new RangeError('Invalid compression level: ' + level);
  }
  if (strategy != exports.Z_FILTERED &&
      strategy != exports.Z_HUFFMAN_ONLY &&
      strategy != exports.Z_RLE &&
      strategy != exports.Z_FIXED &&
      strategy != exports.Z_DEFAULT_STRATEGY) {
    throw new TypeError('Invalid strategy: ' + strategy);
  }

  if (this._level !== level || this._strategy !== strategy) {
    var self = this;
    this.flush(binding.Z_SYNC_FLUSH, function() {
      assert(!self._closed, 'zlib binding closed');
      self._handle.params(level, strategy);
      if (!self._hadError) {
        self._level = level;
        self._strategy = strategy;
        if (callback) callback();
      }
    });
  } else {
    process.nextTick(callback);
  }
};

Zlib.prototype.reset = function() {
  assert(!this._closed, 'zlib binding closed');
  return this._handle.reset();
};

// This is the _flush function called by the transform class,
// internally, when the last chunk has been written.
Zlib.prototype._flush = function(callback) {
  this._transform(new Buffer(0), '', callback);
};

Zlib.prototype.flush = function(kind, callback) {
  var ws = this._writableState;

  if (typeof kind === 'function' || (kind === undefined && !callback)) {
    callback = kind;
    kind = binding.Z_FULL_FLUSH;
  }

  if (ws.ended) {
    if (callback)
      process.nextTick(callback);
  } else if (ws.ending) {
    if (callback)
      this.once('end', callback);
  } else if (ws.needDrain) {
    var self = this;
    this.once('drain', function() {
      self.flush(callback);
    });
  } else {
    this._flushFlag = kind;
    this.write(new Buffer(0), '', callback);
  }
};

Zlib.prototype.close = function(callback) {
  if (callback)
    process.nextTick(callback);

  if (this._closed)
    return;

  this._closed = true;

  this._handle.close();

  process.nextTick(emitCloseNT, this);
};

function emitCloseNT(self) {
  self.emit('close');
}

Zlib.prototype._transform = function(chunk, encoding, cb) {
  var flushFlag;
  var ws = this._writableState;
  var ending = ws.ending || ws.ended;
  var last = ending && (!chunk || ws.length === chunk.length);

  if (chunk !== null && !(chunk instanceof Buffer))
    return cb(new Error('invalid input'));

  if (this._closed)
    return cb(new Error('zlib binding closed'));

  // If it's the last chunk, or a final flush, we use the Z_FINISH flush flag.
  // If it's explicitly flushing at some other time, then we use
  // Z_FULL_FLUSH. Otherwise, use Z_NO_FLUSH for maximum compression
  // goodness.
  if (last)
    flushFlag = binding.Z_FINISH;
  else {
    flushFlag = this._flushFlag;
    // once we've flushed the last of the queue, stop flushing and
    // go back to the normal behavior.
    if (chunk.length >= ws.length) {
      this._flushFlag = this._opts.flush || binding.Z_NO_FLUSH;
    }
  }

  this._processChunk(chunk, flushFlag, cb);
};

Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
  var availInBefore = chunk && chunk.length;
  var availOutBefore = this._chunkSize - this._offset;
  var inOff = 0;

  var self = this;

  var async = typeof cb === 'function';

  if (!async) {
    var buffers = [];
    var nread = 0;

    var error;
    this.on('error', function(er) {
      error = er;
    });

    assert(!this._closed, 'zlib binding closed');
    do {
      var res = this._handle.writeSync(flushFlag,
                                       chunk, // in
                                       inOff, // in_off
                                       availInBefore, // in_len
                                       this._buffer, // out
                                       this._offset, //out_off
                                       availOutBefore); // out_len
    } while (!this._hadError && callback(res[0], res[1]));

    if (this._hadError) {
      throw error;
    }

    if (nread >= kMaxLength) {
      this.close();
      throw new RangeError(kRangeErrorMessage);
    }

    var buf = Buffer.concat(buffers, nread);
    this.close();

    return buf;
  }

  assert(!this._closed, 'zlib binding closed');
  var req = this._handle.write(flushFlag,
                               chunk, // in
                               inOff, // in_off
                               availInBefore, // in_len
                               this._buffer, // out
                               this._offset, //out_off
                               availOutBefore); // out_len

  req.buffer = chunk;
  req.callback = callback;

  function callback(availInAfter, availOutAfter) {
    if (self._hadError)
      return;

    var have = availOutBefore - availOutAfter;
    assert(have >= 0, 'have should not go down');

    if (have > 0) {
      var out = self._buffer.slice(self._offset, self._offset + have);
      self._offset += have;
      // serve some output to the consumer.
      if (async) {
        self.push(out);
      } else {
        buffers.push(out);
        nread += out.length;
      }
    }

    // exhausted the output buffer, or used all the input create a new one.
    if (availOutAfter === 0 || self._offset >= self._chunkSize) {
      availOutBefore = self._chunkSize;
      self._offset = 0;
      self._buffer = new Buffer(self._chunkSize);
    }

    if (availOutAfter === 0) {
      // Not actually done.  Need to reprocess.
      // Also, update the availInBefore to the availInAfter value,
      // so that if we have to hit it a third (fourth, etc.) time,
      // it'll have the correct byte counts.
      inOff += (availInBefore - availInAfter);
      availInBefore = availInAfter;

      if (!async)
        return true;

      var newReq = self._handle.write(flushFlag,
                                      chunk,
                                      inOff,
                                      availInBefore,
                                      self._buffer,
                                      self._offset,
                                      self._chunkSize);
      newReq.callback = callback; // this same function
      newReq.buffer = chunk;
      return;
    }

    if (!async)
      return false;

    // finished with the chunk.
    cb();
  }
};

util.inherits(Deflate, Zlib);
util.inherits(Inflate, Zlib);
util.inherits(Gzip, Zlib);
util.inherits(Gunzip, Zlib);
util.inherits(DeflateRaw, Zlib);
util.inherits(InflateRaw, Zlib);
util.inherits(Unzip, Zlib);

redpajama_set_name: RedPajamaGithub


text:
Lidiun Plugins facilitam acrescentar funcionalidades ou ferramentas sem bagunçar o seu código.

Crie ou baixe publigs e divirta-se.

======
Acesso o site e leia mais: [http://www.lidiun.com.br](http://www.lidiun.com.br)

Leia a documentação em: [http://www.lidiun.com.br/documentation/](http://www.lidiun.com.br/documentation/)

Tenha uma visão geral aqui:[http://dyonenedi.github.io/lidiun-framework/](http://dyonenedi.github.io/lidiun-framework/)

# Relacionados
- Baixe o core do framework (Precisa conhecer o Lidiun): [Clique aqui](https://github.com/dyonenedi/lidiun-framework)

- Baixe uma aplicação default para o framework (Precisa conhecer o Lidiun): [Clique aqui](http://www.lidiun.com.br/downloading/app/)

- Baixe plugins para o framework (Precisa conhecer o Lidiun): [Clique aqui](https://github.com/dyonenedi/lidiun-plugin)

- Baixe um pacote com todas as opções acima (Qualquer desenvolvedor): [Clique aqui](http://www.lidiun.com.br/downloading/package/)


text:
rm -rf sv
mkdir sv
cd sv
cp ../../../../../ip/makestuff/pcie-hip/stratixv/pcie_sv.qsys .
$ALTERA/quartus/sopc_builder/bin/qsys-generate --family="Stratix V" --testbench=STANDARD --testbench-simulation=Verilog --allow-mixed-language-testbench-simulation pcie_sv.qsys
meld pcie_sv/testbench/pcie_sv_tb/simulation/pcie_sv_tb.v ../../pcie_sv_tb.sv &


text:
package com.webwoz.wizard.server.components;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.w3c.dom.Document;

import com.webwoz.wizard.server.MToutComp;

public class MToutMicrosoft extends MToutComp {

	private String uttAudioFile;
	private String uttMMFile;
	private String uttText;
	private HttpClient httpClient;

	public MToutMicrosoft(int mode) {
		super.setName("Microsoft Translate");
		super.setMode(mode);
	}

	public String translate(String inputText, String srcLang, String trgLang) {

		// initialize connection
		// adapt proxy and settings to fit your server environments
		initializeConnection("www-proxy.cs.tcd.ie", 8080);
		String translation = null;
		String query = inputText;
		String MICROSOFT_TRANSLATION_BASE_URL = "http://api.microsofttranslator.com/V2/Http.svc/Translate?";
		String appID = "226846CE16BC2542B7916B05CE9284CF4075B843";

		try {
			// encode text
			query = URLEncoder.encode(query, "UTF-8");
			// exchange + for space
			query = query.replace("+", "%20");
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
			System.out.println(ex);
		}

		String requestURL = MICROSOFT_TRANSLATION_BASE_URL + "appid=" + appID
				+ "&from=" + srcLang + "&to=" + trgLang + "&text=" + query;

		HttpGet getRequest = new HttpGet(requestURL);

		try {
			HttpResponse response = httpClient.execute(getRequest);
			HttpEntity responseEntity = response.getEntity();

			InputStream inputStream = responseEntity.getContent();

			Document myDocument = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder().parse(inputStream);

			XPathFactory factory = XPathFactory.newInstance();
			XPath xPath = factory.newXPath();

			translation = (String) xPath.evaluate("/string", myDocument,
					XPathConstants.STRING);

			inputStream.close();

			translation = translation.trim();

			// if the original string did not have a dot at the end, then
			// remove the dot that Microsoft Translator sometimes places at the
			// end!! (not so sure it still happens anyway!)
			if (!inputText.endsWith(".") && (translation.endsWith("."))) {
				translation = translation
						.substring(0, translation.length() - 1);
			}

			setUttText(translation);

		} catch (Exception ex) {
			ex.printStackTrace();
			translation = ex.toString();
			setUttText(translation);
			System.out.println(translation);
		}

		shutDownConnection();
		return translation;
	}

	/**
	 * Initializes the HTTP connection necessary for communicating with
	 * Microsoft Translator API. <br>
	 * An overloaded method should be implemented that does not take any proxy
	 * settings (in case no proxy connection is needed).
	 *
	 * @param proxyHostName
	 * @param proxyHostPort
	 */
	public void initializeConnection(String proxyHostName, int proxyHostPort) {
		SchemeRegistry supportedSchemes = new SchemeRegistry();
		// Register the "http" and "https" protocol schemes, they are
		// required by the default operator to look up socket factories.
		supportedSchemes.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		supportedSchemes.register(new Scheme("https", SSLSocketFactory
				.getSocketFactory(), 443));
		// prepare parameters
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params, "UTF-8");
		HttpProtocolParams.setUseExpectContinue(params, true);

		ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
				supportedSchemes);

		httpClient = new DefaultHttpClient(ccm, params);

		HttpHost proxyHost = new HttpHost(proxyHostName, proxyHostPort);

		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
				proxyHost);
	}

	/**
	 * Shuts down the HTTP connection.
	 */
	public void shutDownConnection() {
		httpClient.getConnectionManager().shutdown();
	}

	public String getResult(String text, String ctrl) {
		setUttAudio("");
		setUttMM("");

		return translate(text, ctrl.substring(7, 9), ctrl.substring(9, 11));
	}

	public void setUttAudio(String uttFile) {
		this.uttAudioFile = uttFile;
	}

	public String getUttAudio() {
		return uttAudioFile;
	}

	public void setUttMM(String uttMMFile) {
		this.uttMMFile = uttMMFile;
	}

	public String getUttMM() {
		return uttMMFile;
	}

	public void setUttText(String uttText) {
		this.uttText = uttText;
	}

	public String getUttText() {
		return uttText;
	}

}


text:
"""add_index_tenant_id

Revision ID: 4202e3047e47
Revises: start_neutron_fwaas
Create Date: 2015-02-10 17:17:47.846764

"""

# revision identifiers, used by Alembic.
revision = '4202e3047e47'
down_revision = 'start_neutron_fwaas'

from alembic import op

TABLES = ['firewall_rules', 'firewalls', 'firewall_policies']

def upgrade():
    for table in TABLES:
        op.create_index(op.f('ix_%s_tenant_id' % table),
                        table, ['tenant_id'], unique=False)


text:
{-# LANGUAGE OverloadedStrings, RecordWildCards, NamedFieldPuns, TupleSections, FlexibleContexts, FlexibleInstances, UndecidableInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, StandaloneDeriving, TypeSynonymInstances, TypeFamilies, CPP, RankNTypes #-}

module Database.MongoDB.GridFS
  ( Bucket
  , files, chunks
  , File
  , document, bucket
  -- ** Setup
  , openDefaultBucket
  , openBucket
  -- ** Query
  , findFile
  , findOneFile
  , fetchFile
  -- ** Delete
  , deleteFile
  -- ** Conduits
  , sourceFile
  , sinkFile
  )
  where

import Control.Applicative((<$>))

import Control.Monad(when)
import Control.Monad.Fail(MonadFail)
import Control.Monad.IO.Class
import Control.Monad.Trans(lift)

import Data.Conduit
import Data.Digest.Pure.MD5
import Data.Int
import Data.Tagged(Tagged, untag)
import Data.Text(Text, append)
import Data.Time.Clock(getCurrentTime)
import Database.MongoDB
import Prelude
import qualified Data.Bson as B
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L

defaultChunkSize :: Int64
-- ^ The default chunk size is 256 kB
defaultChunkSize = 256 * 1024

-- magic constant for md5Finalize
md5BlockSizeInBytes :: Int
md5BlockSizeInBytes = 64

data Bucket = Bucket {files :: Text, chunks :: Text}
-- ^ Files are stored in "buckets". You open a bucket with openDefaultBucket or openBucket

openDefaultBucket :: (Monad m, MonadIO m) => Action m Bucket
-- ^ Open the default 'Bucket' (named "fs")
openDefaultBucket = openBucket "fs"

openBucket :: (Monad m, MonadIO m) => Text -> Action m Bucket
-- ^ Open a 'Bucket'
openBucket name = do
  let filesCollection = name `append` ".files"
  let chunksCollection = name `append` ".chunks"
  ensureIndex $ (index filesCollection ["filename" =: (1::Int), "uploadDate" =: (1::Int)])
  ensureIndex $ (index chunksCollection ["files_id" =: (1::Int), "n" =: (1::Int)]) { iUnique = True, iDropDups = True }
  return $ Bucket filesCollection chunksCollection

data File = File {bucket :: Bucket, document :: Document}

getChunk :: (MonadFail m, MonadIO m) => File -> Int -> Action m (Maybe S.ByteString)
-- ^ Get a chunk of a file
getChunk (File bucket doc) i = do
  files_id <- B.look "_id" doc
  result <- findOne $ select ["files_id" := files_id, "n" =: i] $ chunks bucket
  let content = at "data" <$> result
  case content of
    Just (Binary b) -> return (Just b)
    _ -> return Nothing

findFile :: MonadIO m => Bucket -> Selector -> Action m [File]
-- ^ Find files in the bucket
findFile bucket sel = do
  cursor <- find $ select sel $ files bucket
  results <- rest cursor
  return $ File bucket <$> results

findOneFile :: MonadIO m => Bucket -> Selector -> Action m (Maybe File)
-- ^ Find one file in the bucket
findOneFile bucket sel = do
  mdoc <- findOne $ select sel $ files bucket
  return $ File bucket <$> mdoc

fetchFile :: MonadIO m => Bucket -> Selector -> Action m File
-- ^ Fetch one file in the bucket
fetchFile bucket sel = do
  doc <- fetch $ select sel $ files bucket
  return $ File bucket doc

deleteFile :: (MonadIO m, MonadFail m) => File -> Action m ()
-- ^ Delete files in the bucket
deleteFile (File bucket doc) = do
  files_id <- B.look "_id" doc
  delete $ select ["_id" := files_id] $ files bucket
  delete $ select ["files_id" := files_id] $ chunks bucket

putChunk :: (Monad m, MonadIO m) => Bucket -> ObjectId -> Int -> L.ByteString -> Action m ()
-- ^ Put a chunk in the bucket
putChunk bucket files_id i chunk = do
  insert_ (chunks bucket) ["files_id" =: files_id, "n" =: i, "data" =: Binary (L.toStrict chunk)]

sourceFile :: (MonadFail m, MonadIO m) => File -> Producer (Action m) S.ByteString
-- ^ A producer for the contents of a file
sourceFile file = yieldChunk 0 where
  yieldChunk i = do
    mbytes <- lift $ getChunk file i
    case mbytes of
      Just bytes -> yield bytes >> yieldChunk (i+1)
      Nothing -> return ()

-- Used to keep data during writing
data FileWriter = FileWriter
  { _fwChunkSize :: Int64
  , _fwBucket :: Bucket
  , _fwFilesId :: ObjectId
  , _fwChunkIndex :: Int
  , _fwSize :: Int64
  , _fwAcc :: L.ByteString
  , _fwMd5Context :: MD5Context
  , _fwMd5acc :: L.ByteString
  }

-- Finalize file, calculating md5 digest, saving the last chunk, and creating the file in the bucket
finalizeFile :: (Monad m, MonadIO m) => Text -> FileWriter -> Action m File
finalizeFile filename (FileWriter chunkSize bucket files_id i size acc md5context md5acc) = do
  let md5digest = finalizeMD5 md5context (L.toStrict md5acc)
  when (L.length acc > 0) $ putChunk bucket files_id i acc
  currentTimestamp <- liftIO $ getCurrentTime
  let doc = [ "_id" =: files_id
            , "length" =: size
            , "uploadDate" =: currentTimestamp
            , "md5" =: show (md5digest)
            , "chunkSize" =: chunkSize
            , "filename" =: filename
            ]
  insert_ (files bucket) doc
  return $ File bucket doc

-- finalize the remainder and return the MD5Digest.
finalizeMD5 :: MD5Context -> S.ByteString -> MD5Digest
finalizeMD5 ctx remainder =
  md5Finalize ctx2 (S.drop lu remainder) -- can only handle max md5BlockSizeInBytes length
  where
    l = S.length remainder
    r = l `mod` md5BlockSizeInBytes
    lu = l - r
    ctx2 = md5Update ctx (S.take lu remainder)

-- Write as many chunks as can be written from the file writer
writeChunks :: (Monad m, MonadIO m) => FileWriter -> L.ByteString -> Action m FileWriter
writeChunks (FileWriter chunkSize bucket files_id i size acc md5context md5acc) chunk = do
  -- Update md5 context
  let md5BlockLength = fromIntegral $ untag (blockLength :: Tagged MD5Digest Int)
  let md5acc_temp = (md5acc `L.append` chunk)
  let (md5context', md5acc') =
        if (L.length md5acc_temp < md5BlockLength)
        then (md5context, md5acc_temp)
        else let numBlocks = L.length md5acc_temp `div` md5BlockLength
                 (current, remainder) = L.splitAt (md5BlockLength * numBlocks) md5acc_temp
             in (md5Update md5context (L.toStrict current), remainder)
  -- Update chunks
  let size' = (size + L.length chunk)
  let acc_temp = (acc `L.append` chunk)
  if (L.length acc_temp < chunkSize)
    then return (FileWriter chunkSize bucket files_id i size' acc_temp md5context' md5acc')
    else do
      let (newChunk, acc') = L.splitAt chunkSize acc_temp
      putChunk bucket files_id i newChunk
      writeChunks (FileWriter chunkSize bucket files_id (i+1) size' acc' md5context' md5acc') L.empty

sinkFile :: (Monad m, MonadIO m) => Bucket -> Text -> Consumer S.ByteString (Action m) File
-- ^ A consumer that creates a file in the bucket and puts all consumed data in it
sinkFile bucket filename = do
  files_id <- liftIO $ genObjectId
  awaitChunk $ FileWriter defaultChunkSize bucket files_id 0 0 L.empty md5InitialContext L.empty
 where
  awaitChunk fw = do
    mchunk <- await
    case mchunk of
      Nothing -> lift (finalizeFile filename fw)
      Just chunk -> lift (writeChunks fw (L.fromStrict chunk)) >>= awaitChunk


text:
package org.apache.solr.util;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.highlight.*;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrException;
import org.apache.solr.request.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.request.DefaultSolrParams;
import org.apache.solr.request.AppendedSolrParams;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.*;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.logging.Level;
import java.util.regex.Pattern;

/**
 *
Utilities that may be of use to RequestHandlers.

 *
 *

 * Many of these functions have code that was stolen/mutated from
 * StandardRequestHandler.
 *

 *
 *
:TODO: refactor StandardRequestHandler to use these utilities

 *
 *
:TODO: Many "standard" functionality methods are not cognisant of
 * default parameter settings.
 */
public class SolrPluginUtils {

  /**
   * Set defaults on a SolrQueryRequest.
   *
   * RequestHandlers can use this method to ensure their defaults are
   * visible to other components such as the response writer
   */
  public static void setDefaults(SolrQueryRequest req, SolrParams defaults) {
    setDefaults(req, defaults, null, null);
  }

  /**
   * Set default-ish params on a SolrQueryRequest.
   *
   * RequestHandlers can use this method to ensure their defaults and
   * overrides are visible to other components such as the response writer
   *
   * @param req The request whose params we are interested i
   * @param defaults values to be used if no values are specified in the request params
   * @param appends values to be appended to those from the request (or defaults) when dealing with multi-val params, or treated as another layer of defaults for singl-val params.
   * @param invariants values which will be used instead of any request, or default values, regardless of context.
   */
  public static void setDefaults(SolrQueryRequest req, SolrParams defaults,
                                 SolrParams appends, SolrParams invariants) {

      SolrParams p = req.getParams();
      if (defaults != null) {
        p = new DefaultSolrParams(p,defaults);
      }
      if (appends != null) {
        p = new AppendedSolrParams(p,appends);
      }
      if (invariants != null) {
        p = new DefaultSolrParams(invariants,p);
      }
      req.setParams(p);
  }

  /** standard param for field list */
  @Deprecated
  public static String FL = SolrParams.FL;

  /**
   * SolrIndexSearch.numDocs(Query,Query) freaks out if the filtering
   * query is null, so we use this workarround.
   */
  public static int numDocs(SolrIndexSearcher s, Query q, Query f)
    throws IOException {

    return (null == f) ? s.getDocSet(q).size() : s.numDocs(q,f);

  }

  /**
   * Returns the param, or the default if it's empty or not specified.
   * @deprecated use SolrParam.get(String,String)
   */
  public static String getParam(SolrQueryRequest req,
                                String param, String def) {

    String v = req.getParam(param);
    // Note: parameters passed but given only white-space value are
    // considered equvalent to passing nothing for that parameter.
    if (null == v || "".equals(v.trim())) {
      return def;
    }
    return v;
  }

  /**
   * Treats the param value as a Number, returns the default if nothing is
   * there or if it's not a number.
   * @deprecated use SolrParam.getFloat(String,float)
   */
  public static Number getNumberParam(SolrQueryRequest req,
                                      String param, Number def) {

    Number r = def;
    String v = req.getParam(param);
    if (null == v || "".equals(v.trim())) {
      return r;
    }
    try {
      r = new Float(v);
    } catch (NumberFormatException e) {
      /* :NOOP" */
    }
    return r;
  }

  /**
   * Treats parameter value as a boolean.  The string 'false' is false;
   * any other non-empty string is true.
   * @deprecated use SolrParam.getBool(String,boolean)
   */
  public static boolean getBooleanParam(SolrQueryRequest req,
                                       String param, boolean def) {
    String v = req.getParam(param);
    if (null == v || "".equals(v.trim())) {
      return def;
    }
    return !"false".equals(v.trim());
  }

  private final static Pattern splitList=Pattern.compile(",| ");

  /** Split a value that may contain a comma, space of bar separated list. */
  public static String[] split(String value){
     return splitList.split(value.trim(), 0);
  }

  /**
   * Assumes the standard query param of "fl" to specify the return fields
   * @see #setReturnFields(String,SolrQueryResponse)
   */
  public static int setReturnFields(SolrQueryRequest req,
                                    SolrQueryResponse res) {

    return setReturnFields(req.getParam(FL), res);
  }

  /**
   * Given a space seperated list of field names, sets the field list on the
   * SolrQueryResponse.
   *
   * @return bitfield of SolrIndexSearcher flags that need to be set
   */
  public static int setReturnFields(String fl,
                                    SolrQueryResponse res) {
    int flags = 0;
    if (fl != null) {
      // TODO - this could become more efficient if widely used.
      // TODO - should field order be maintained?
      String[] flst = split(fl);
      if (flst.length > 0 && !(flst.length==1 && flst[0].length()==0)) {
        Set<String> set = new HashSet<String>();
        for (String fname : flst) {
          if("score".equalsIgnoreCase(fname))
            flags |= SolrIndexSearcher.GET_SCORES;
          set.add(fname);
        }
        res.setReturnFields(set);
      }
    }
    return flags;
  }

  /**
   * Pre-fetch documents into the index searcher's document cache.
   *
   * This is an entirely optional step which you might want to perform for
   * the following reasons:
   *
   *

   *
Locates the document-retrieval costs in one spot, which helps
   *     detailed performance measurement

   *
   *
Determines a priori what fields will be needed to be fetched by
   *     various subtasks, like response writing and highlighting.  This
   *     minimizes the chance that many needed fields will be loaded lazily.
   *     (it is more efficient to load all the field we require normally).

   *

   *
   * If lazy field loading is disabled, this method does nothing.
   */
  public static void optimizePreFetchDocs(DocList docs,
                                          Query query,
                                          SolrQueryRequest req,
                                          SolrQueryResponse res) throws IOException {
    SolrIndexSearcher searcher = req.getSearcher();
    if(!searcher.enableLazyFieldLoading) {
      // nothing to do
      return;
    }

    Set<String> fieldFilter = null;
    Set<String> returnFields = res.getReturnFields();
    if(returnFields != null) {
      // copy return fields list
      fieldFilter = new HashSet<String>(returnFields);
      // add highlight fields
      if(HighlightingUtils.isHighlightingEnabled(req)) {
        for(String field: HighlightingUtils.getHighlightFields(query, req, null))
          fieldFilter.add(field);
      }
      // fetch unique key if one exists.
      SchemaField keyField = req.getSearcher().getSchema().getUniqueKeyField();
      if(null != keyField)
          fieldFilter.add(keyField.getName());
    }

    // get documents
    DocIterator iter = docs.iterator();
    for (int i=0; i<docs.size(); i++) {
      searcher.doc(iter.nextDoc(), fieldFilter);
    }
  }

  /**
   *

   * Returns a NamedList containing many "standard" pieces of debugging
   * information.
   *

   *
   *

   *
rawquerystring - the 'q' param exactly as specified by the client
   *

   *
querystring - the 'q' param after any preprocessing done by the plugin
   *

   *
parsedquery - the main query executed formated by the Solr
   *     QueryParsing utils class (which knows about field types)
   *

   *
parsedquery_toString - the main query executed formated by it's
   *     own toString method (in case it has internal state Solr
   *     doesn't know about)
   *

   *
expain - the list of score explanations for each document in
   *     results against query.
   *

   *
otherQuery - the query string specified in 'explainOther' query param.
   *

   *
explainOther - the list of score explanations for each document in
   *     results against 'otherQuery'
   *

   *

   *
   * @param req the request we are dealing with
   * @param userQuery the users query as a string, after any basic
   *                  preprocessing has been done
   * @param query the query built from the userQuery
   *              (and perhaps other clauses) that identifies the main
   *              result set of the response.
   * @param results the main result set of the response
   * @deprecated Use doStandardDebug(SolrQueryRequest,String,Query,DocList) with setDefaults
   */
  public static NamedList doStandardDebug(SolrQueryRequest req,
                                          String userQuery,
                                          Query query,
                                          DocList results,
                                          CommonParams params)
    throws IOException {

    String debug = getParam(req, SolrParams.DEBUG_QUERY, params.debugQuery);

    NamedList dbg = null;
    if (debug!=null) {
      dbg = new NamedList();

      /* userQuery may have been pre-processes .. expose that */
      dbg.add("rawquerystring", req.getQueryString());
      dbg.add("querystring", userQuery);

      /* QueryParsing.toString isn't perfect, use it to see converted
       * values, use regular toString to see any attributes of the
       * underlying Query it may have missed.
       */
      dbg.add("parsedquery",QueryParsing.toString(query, req.getSchema()));
      dbg.add("parsedquery_toString", query.toString());

      dbg.add("explain", getExplainList
              (query, results, req.getSearcher(), req.getSchema()));
      String otherQueryS = req.getParam("explainOther");
      if (otherQueryS != null && otherQueryS.length() > 0) {
        DocList otherResults = doSimpleQuery
          (otherQueryS,req.getSearcher(), req.getSchema(),0,10);
        dbg.add("otherQuery",otherQueryS);
        dbg.add("explainOther", getExplainList
                (query, otherResults,
                 req.getSearcher(),
                 req.getSchema()));
      }
    }

    return dbg;
  }

  /**
   *

   * Returns a NamedList containing many "standard" pieces of debugging
   * information.
   *

   *
   *

   *
rawquerystring - the 'q' param exactly as specified by the client
   *

   *
querystring - the 'q' param after any preprocessing done by the plugin
   *

   *
parsedquery - the main query executed formated by the Solr
   *     QueryParsing utils class (which knows about field types)
   *

   *
parsedquery_toString - the main query executed formated by it's
   *     own toString method (in case it has internal state Solr
   *     doesn't know about)
   *

   *
expain - the list of score explanations for each document in
   *     results against query.
   *

   *
otherQuery - the query string specified in 'explainOther' query param.
   *

   *
explainOther - the list of score explanations for each document in
   *     results against 'otherQuery'
   *

   *

   *
   * @param req the request we are dealing with
   * @param userQuery the users query as a string, after any basic
   *                  preprocessing has been done
   * @param query the query built from the userQuery
   *              (and perhaps other clauses) that identifies the main
   *              result set of the response.
   * @param results the main result set of the response
   */
  public static NamedList doStandardDebug(SolrQueryRequest req,
                                          String userQuery,
                                          Query query,
                                          DocList results)
    throws IOException {

    String debug = req.getParam(SolrParams.DEBUG_QUERY);

    NamedList dbg = null;
    if (debug!=null) {
      dbg = new NamedList();

      /* userQuery may have been pre-processes .. expose that */
      dbg.add("rawquerystring", req.getQueryString());
      dbg.add("querystring", userQuery);

      /* QueryParsing.toString isn't perfect, use it to see converted
       * values, use regular toString to see any attributes of the
       * underlying Query it may have missed.
       */
      dbg.add("parsedquery",QueryParsing.toString(query, req.getSchema()));
      dbg.add("parsedquery_toString", query.toString());

      dbg.add("explain", getExplainList
              (query, results, req.getSearcher(), req.getSchema()));
      String otherQueryS = req.getParam("explainOther");
      if (otherQueryS != null && otherQueryS.length() > 0) {
        DocList otherResults = doSimpleQuery
          (otherQueryS,req.getSearcher(), req.getSchema(),0,10);
        dbg.add("otherQuery",otherQueryS);
        dbg.add("explainOther", getExplainList
                (query, otherResults,
                 req.getSearcher(),
                 req.getSchema()));
      }
    }

    return dbg;
  }

  /**
   * Generates an list of Explanations for each item in a list of docs.
   *
   * @param query The Query you want explanations in the context of
   * @param docs The Documents you want explained relative that query
   */
  public static NamedList getExplainList(Query query, DocList docs,
                                         SolrIndexSearcher searcher,
                                         IndexSchema schema)
    throws IOException {

    NamedList explainList = new NamedList();
    DocIterator iterator = docs.iterator();
    for (int i=0; i<docs.size(); i++) {
      int id = iterator.nextDoc();

      Explanation explain = searcher.explain(query, id);

      Document doc = searcher.doc(id);
      String strid = schema.printableUniqueKey(doc);
      String docname = "";
      if (strid != null) docname="id="+strid+",";
      docname = docname + "internal_docid="+id;

      explainList.add(docname, "\n" +explain.toString());
    }
    return explainList;
  }

  /**
   * Executes a basic query in lucene syntax
   */
  public static DocList doSimpleQuery(String sreq,
                                      SolrIndexSearcher searcher,
                                      IndexSchema schema,
                                      int start, int limit) throws IOException {
    List<String> commands = StrUtils.splitSmart(sreq,';');

    String qs = commands.size() >= 1 ? commands.get(0) : "";
    Query query = QueryParsing.parseQuery(qs, schema);

    // If the first non-query, non-filter command is a simple sort on an indexed field, then
    // we can use the Lucene sort ability.
    Sort sort = null;
    if (commands.size() >= 2) {
      QueryParsing.SortSpec sortSpec = QueryParsing.parseSort(commands.get(1), schema);
      if (sortSpec != null) {
        sort = sortSpec.getSort();
        if (sortSpec.getCount() >= 0) {
          limit = sortSpec.getCount();
        }
      }
    }

    DocList results = searcher.getDocList(query,(DocSet)null, sort, start, limit);
    return results;
  }

  /**
   * Given a string containing fieldNames and boost info,
   * converts it to a Map from field name to boost info.
   *
   *

   * Doesn't care if boost info is negative, you're on your own.
   *

   *

   * Doesn't care if boost info is missing, again: you're on your own.
   *

   *
   * @param in a String like "fieldOne^2.3 fieldTwo fieldThree^-0.4"
   * @return Map of fieldOne => 2.3, fieldTwo => null, fieldThree => -0.4
   */
  public static Map<String,Float> parseFieldBoosts(String in) {

    if (null == in || "".equals(in.trim())) {
      return new HashMap<String,Float>();
    }

    String[] bb = in.trim().split("\\s+");
    Map<String, Float> out = new HashMap<String,Float>(7);
    for (String s : bb) {
      String[] bbb = s.split("\\^");
      out.put(bbb[0], 1 == bbb.length ? null : Float.valueOf(bbb[1]));
    }
    return out;
  }

  /**
   * Given a string containing functions with optional boosts, returns
   * an array of Queries representing those functions with the specified
   * boosts.
   *

   * NOTE: intra-function whitespace is not allowed.
   *

   * @see #parseFieldBoosts
   */
  public static List<Query> parseFuncs(IndexSchema s, String in)
    throws ParseException {

    Map<String,Float> ff = parseFieldBoosts(in);
    List<Query> funcs = new ArrayList<Query>(ff.keySet().size());
    for (String f : ff.keySet()) {
      Query fq = QueryParsing.parseFunction(f, s);
      Float b = ff.get(f);
      if (null != b) {
        fq.setBoost(b);
      }
      funcs.add(fq);
    }
    return funcs;
  }

  /**
   * Checks the number of optional clauses in the query, and compares it
   * with the specification string to determine the proper value to use.
   *
   *

   * Details about the specification format can be found
   * here
   *

   *
   *
A few important notes...

   *

   *

   * If the calculations based on the specification determine that no
   * optional clauses are needed, BooleanQuerysetMinMumberShouldMatch
   * will never be called, but the usual rules about BooleanQueries
   * still apply at search time (a BooleanQuery containing no required
   * clauses must still match at least one optional clause)
   *

   *

   * No matter what number the calculation arrives at,
   * BooleanQuery.setMinShouldMatch() will never be called with a
   * value greater then the number of optional clauses (or less then 1)
   *

   *

   *
   *
:TODO: should optimize the case where number is same
   * as clauses to just make them all "required"
   *

   */
  public static void setMinShouldMatch(BooleanQuery q, String spec) {

    int optionalClauses = 0;
    for (BooleanClause c : q.getClauses()) {
      if (c.getOccur() == Occur.SHOULD) {
        optionalClauses++;
      }
    }

    int msm = calculateMinShouldMatch(optionalClauses, spec);
    if (0 < msm) {
      q.setMinimumNumberShouldMatch(msm);
    }
  }

  /**
   * helper exposed for UnitTests
   * @see #setMinShouldMatch
   */
  static int calculateMinShouldMatch(int optionalClauseCount, String spec) {

    int result = optionalClauseCount;

    if (-1 < spec.indexOf("<")) {
      /* we have conditional spec(s) */

      for (String s : spec.trim().split(" ")) {
        String[] parts = s.split("<");
        int upperBound = (new Integer(parts[0])).intValue();
        if (optionalClauseCount <= upperBound) {
          return result;
        } else {
          result = calculateMinShouldMatch
            (optionalClauseCount, parts[1]);
        }
      }
      return result;
    }

    /* otherwise, simple expresion */

    if (-1 < spec.indexOf("%")) {
      /* percentage */
      int percent = new Integer(spec.replace("%","")).intValue();
      float calc = (result * percent) / 100f;
      result = calc < 0 ? result + (int)calc : (int)calc;
    } else {
      int calc = (new Integer(spec)).intValue();
      result = calc < 0 ? result + calc : calc;
    }

    return (optionalClauseCount < result ?
            optionalClauseCount : (result < 0 ? 0 : result));

  }

  /**
   * Recursively walks the "from" query pulling out sub-queries and
   * adding them to the "to" query.
   *
   *

   * Boosts are multiplied as needed.  Sub-BooleanQueryies which are not
   * optional will not be flattened.  From will be mangled durring the walk,
   * so do not attempt to reuse it.
   *

   */
  public static void flattenBooleanQuery(BooleanQuery to, BooleanQuery from) {

    BooleanClause[] c = from.getClauses();
    for (int i = 0; i < c.length; i++) {

      Query ci = c[i].getQuery();
      ci.setBoost(ci.getBoost() * from.getBoost());

      if (ci instanceof BooleanQuery
          && !c[i].isRequired()
          && !c[i].isProhibited()) {

        /* we can recurse */
        flattenBooleanQuery(to, (BooleanQuery)ci);

      } else {
        to.add(c[i]);
      }
    }
  }

  /**
   * Escapes all special characters except '"', '-', and '+'
   *
   * @see QueryParser#escape
   */
  public static CharSequence partialEscape(CharSequence s) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      if (c == '\\' || c == '!' || c == '(' || c == ')' ||
          c == ':'  || c == '^' || c == '[' || c == ']' ||
          c == '{'  || c == '}' || c == '~' || c == '*' || c == '?'
          ) {
        sb.append('\\');
      }
      sb.append(c);
    }
    return sb;
  }

  /**
   * Returns it's input if there is an even (ie: balanced) number of
   * '"' characters -- otherwise returns a String in which all '"'
   * characters are striped out.
   */
  public static CharSequence stripUnbalancedQuotes(CharSequence s) {
    int count = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s.charAt(i) == '\"') { count++; }
    }
    if (0 == (count & 1)) {
      return s;
    }
    return s.toString().replace("\"","");
  }

  /**
   * A subclass of SolrQueryParser that supports aliasing fields for
   * constructing DisjunctionMaxQueries.
   */
  public static class DisjunctionMaxQueryParser extends SolrQueryParser {

    /** A simple container for storing alias info
     * @see #aliases
     */
    protected static class Alias {
      public float tie;
      public Map<String,Float> fields;
    }

    /**
     * Where we store a map from field name we expect to see in our query
     * string, to Alias object containing the fields to use in our
     * DisjunctionMaxQuery and the tiebreaker to use.
     */
    protected Map<String,Alias> aliases = new HashMap<String,Alias>(3);

    public DisjunctionMaxQueryParser(IndexSchema s, String defaultField) {
      super(s,defaultField);
    }
    public DisjunctionMaxQueryParser(IndexSchema s) {
      this(s,null);
    }

    /**
     * Add an alias to this query parser.
     *
     * @param field the field name that should trigger alias mapping
     * @param fieldBoosts the mapping from fieldname to boost value that
     *                    should be used to build up the clauses of the
     *                    DisjunctionMaxQuery.
     * @param tiebreaker to the tiebreaker to be used in the
     *                   DisjunctionMaxQuery
     * @see SolrPluginUtils#parseFieldBoosts
     */
    public void addAlias(String field, float tiebreaker,
                         Map<String,Float> fieldBoosts) {

      Alias a = new Alias();
      a.tie = tiebreaker;
      a.fields = fieldBoosts;
      aliases.put(field, a);
    }

    /**
     * Delegates to the super class unless the field has been specified
     * as an alias -- in which case we recurse on each of
     * the aliased fields, and the results are composed into a
     * DisjunctionMaxQuery.  (so yes: aliases which point at other
     * aliases should work)
     */
    protected Query getFieldQuery(String field, String queryText)
      throws ParseException {

      if (aliases.containsKey(field)) {

        Alias a = aliases.get(field);
        DisjunctionMaxQuery q = new DisjunctionMaxQuery(a.tie);

        /* we might not get any valid queries from delegation,
         * in which we should return null
         */
        boolean ok = false;

        for (String f : a.fields.keySet()) {

          Query sub = getFieldQuery(f,queryText);
          if (null != sub) {
            if (null != a.fields.get(f)) {
              sub.setBoost(a.fields.get(f));
            }
            q.add(sub);
            ok = true;
          }
        }
        return ok ? q : null;

      } else {
        return super.getFieldQuery(field, queryText);
      }
    }

  }

  /**
   * Determines the correct Sort based on the request parameter "sort"
   *
   * @return null if no sort is specified.
   */
  public static Sort getSort(SolrQueryRequest req) {

    String sort = req.getParam("sort");
    if (null == sort || sort.equals("")) {
      return null;
    }

    SolrException sortE = null;
    QueryParsing.SortSpec ss = null;
    try {
      ss = QueryParsing.parseSort(sort, req.getSchema());
    } catch (SolrException e) {
      sortE = e;
    }

    if ((null == ss) || (null != sortE)) {
      /* we definitely had some sort of sort string from the user,
       * but no SortSpec came out of it
       */
      SolrCore.log.log(Level.WARNING,"Invalid sort \""+sort+"\" was specified, ignoring", sortE);
      return null;
    }

    return ss.getSort();
  }

  /**
   * Builds a list of Query objects that should be used to filter results
   * @see SolrParams#FQ
   * @return null if no filter queries
   */
  public static List<Query> parseFilterQueries(SolrQueryRequest req) throws ParseException {
    String[] in = req.getParams().getParams(SolrParams.FQ);

    if (null == in || 0 == in.length) return null;

    List<Query> out = new LinkedList<Query>();
    SolrIndexSearcher s = req.getSearcher();
    /* Ignore SolrParams.DF - could have init param FQs assuming the
     * schema default with query param DF intented to only affect Q.
     * If user doesn't want schema default, they should be explicit in the FQ.
     */
    SolrQueryParser qp = new SolrQueryParser(s.getSchema(), null);
    for (String q : in) {
      if (null != q && 0 != q.trim().length()) {
        out.add(qp.parse(q));
      }
    }
    return out;
  }

  /**
   * A CacheRegenerator that can be used whenever the items in the cache
   * are not dependant on the current searcher.
   *
   *

   * Flat out copies the oldKey=>oldVal pair into the newCache
   *

   */
  public static class IdentityRegenerator implements CacheRegenerator {
    public boolean regenerateItem(SolrIndexSearcher newSearcher,
                                  SolrCache newCache,
                                  SolrCache oldCache,
                                  Object oldKey,
                                  Object oldVal)
      throws IOException {

      newCache.put(oldKey,oldVal);
      return true;
    }

  }

}

/**
 * Helper class which creates a single TokenStream out of values from a
 * multi-valued field.
 */
class MultiValueTokenStream extends TokenStream {
  private String fieldName;
  private String[] values;
  private Analyzer analyzer;
  private int curIndex;                  // next index into the values array
  private int curOffset;                 // offset into concatenated string
  private TokenStream currentStream;     // tokenStream currently being iterated
  private boolean orderTokenOffsets;

  /** Constructs a TokenStream for consecutively-analyzed field values
   *
   * @param fieldName name of the field
   * @param values array of field data
   * @param analyzer analyzer instance
   */
  public MultiValueTokenStream(String fieldName, String[] values,
                               Analyzer analyzer, boolean orderTokenOffsets) {
    this.fieldName = fieldName;
    this.values = values;
    this.analyzer = analyzer;
    curIndex = -1;
    curOffset = 0;
    currentStream = null;
    this.orderTokenOffsets=orderTokenOffsets;
  }

  /** Returns the next token in the stream, or null at EOS. */
  public Token next() throws IOException {
    int extra = 0;
    if(currentStream == null) {
      curIndex++;
      if(curIndex < values.length) {
        currentStream = analyzer.tokenStream(fieldName,
                                             new StringReader(values[curIndex]));
        if (orderTokenOffsets) currentStream = new TokenOrderingFilter(currentStream,10);
        // add extra space between multiple values
        if(curIndex > 0)
          extra = analyzer.getPositionIncrementGap(fieldName);
      } else {
        return null;
      }
    }
    Token nextToken = currentStream.next();
    if(nextToken == null) {
      curOffset += values[curIndex].length();
      currentStream = null;
      return next();
    }
    // create an modified token which is the offset into the concatenated
    // string of all values
    Token offsetToken = new Token(nextToken.termText(),
                                  nextToken.startOffset() + curOffset,
                                  nextToken.endOffset() + curOffset);
    offsetToken.setPositionIncrement(nextToken.getPositionIncrement() + extra*10);
    return offsetToken;
  }

  /**
   * Returns all values as a single String into which the Tokens index with
   * their offsets.
   */
  public String asSingleValue() {
    StringBuilder sb = new StringBuilder();
    for(String str : values)
      sb.append(str);
    return sb.toString();
  }

}

/**
 * A simple modification of SimpleFragmenter which additionally creates new
 * fragments when an unusually-large position increment is encountered
 * (this behaves much better in the presence of multi-valued fields).
 */
class GapFragmenter extends SimpleFragmenter {
  public static final int INCREMENT_THRESHOLD = 50;
  protected int fragOffsetAccum = 0;

  public GapFragmenter() {
  }

  public GapFragmenter(int fragsize) {
     super(fragsize);
  }

  /* (non-Javadoc)
   * @see org.apache.lucene.search.highlight.TextFragmenter#start(java.lang.String)
   */
  public void start(String originalText) {
    fragOffsetAccum = 0;
  }

  /* (non-Javadoc)
   * @see org.apache.lucene.search.highlight.TextFragmenter#isNewFragment(org.apache.lucene.analysis.Token)
   */
  public boolean isNewFragment(Token token) {
    boolean isNewFrag =
      token.endOffset() >= fragOffsetAccum + getFragmentSize() ||
      token.getPositionIncrement() > INCREMENT_THRESHOLD;
    if(isNewFrag) {
        fragOffsetAccum += token.endOffset() - fragOffsetAccum;
    }
    return isNewFrag;
  }
}

/** Orders Tokens in a window first by their startOffset ascending.
 * endOffset is currently ignored.
 * This is meant to work around fickleness in the highlighter only.  It
 * can mess up token positions and should not be used for indexing or querying.
 */
class TokenOrderingFilter extends TokenFilter {
  private final int windowSize;
  private final LinkedList<Token> queue = new LinkedList<Token>();
  private boolean done=false;

  protected TokenOrderingFilter(TokenStream input, int windowSize) {
    super(input);
    this.windowSize = windowSize;
  }

  public Token next() throws IOException {
    while (!done && queue.size() < windowSize) {
      Token newTok = input.next();
      if (newTok==null) {
        done=true;
        break;
      }

      // reverse iterating for better efficiency since we know the
      // list is already sorted, and most token start offsets will be too.
      ListIterator<Token> iter = queue.listIterator(queue.size());
      while(iter.hasPrevious()) {
        if (newTok.startOffset() >= iter.previous().startOffset()) {
          // insertion will be before what next() would return (what
          // we just compared against), so move back one so the insertion
          // will be after.
          iter.next();
          break;
        }
      }
      iter.add(newTok);
    }

    return queue.isEmpty() ? null : queue.removeFirst();
  }
}


text:
<?php
class Model {
	public $db;
	public $URL;

	function __construct() {
		global $URL;
		global $DATABASE;

		$this->URL = $URL;

		$DATABASE['dsn'] = sprintf('mysql:host=%s;dbname=%s', $DATABASE['host'], $DATABASE['db']);
		$this->db = new PDO($DATABASE['dsn'], $DATABASE['user'], $DATABASE['pass']);
		$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

		$this->session = new Session($this->db);
		$this->defineConstants();
	}

	/**
	 * Defines the constants needed
	 */
	private function defineConstants() {
		if (substr($GLOBALS['siteUrl'], -1) != '/') {
			$GLOBALS['siteUrl'] .= '/';
		}
		define('SITE_URL', $GLOBALS['siteUrl']);

		$GLOBALS['user'] = $this->_getLoggedUser();
		define('LOGGED_IN', (bool) $GLOBALS['user']);
		//define('LOGGED_IN_ADMIN', (isset($_SESSION['admin']['logged']) && $_SESSION['admin']['logged']));

		$GLOBALS['msg'] = $this->_getMsg();

		define('WORDS_PER_MINUTE',  240);
		define('POSTS_PER_PAGE',    8);
		define('COMMENTS_PER_PAGE', 6);

		define('IMG_UPLOAD_PATH', 'uploads/');
		define('IMG_ALLOWED_EXT', 'jpg,jpeg,gif,png,bmp');
		define('IMG_MAX_SIZE'   , 8388608);
	}

	/**
	 * Generate unique string
	 * @param  int $len Length of the uniq string
	 * @return string      Generated string
	 */
	public function uniq($len) {
		$chars    = 'abcdefghijklmnopqrstuvwxyz0123456789';
		$charsLen = strlen($chars)-1;

		$ret = '';
		for ($i = 0; $i < $len; ++$i) {
		    $ret .= $chars[mt_rand(0, $charsLen)];
		}

		return $ret;
	}

	/**
	 * Gets the message intented to be shown to the user
	 * @return string The message from session
	 */
	private function _getMsg() {
		$msg = '';
		if (isset($_SESSION['msg'])) {
			$msg = $_SESSION['msg'];
			unset($_SESSION['msg']);
		}

		return $msg;
	}

	/**
	 * Puts a message to be shown to the user on the next page
	 * @param  string $msg
	 */
	public function putMsg($msg) {
		$_SESSION['msg'] = $msg;
	}

	/**
	 * hash $pass
	 */
	public function hashPassword($pass) {
		$pass = md5($pass) . $pass;
		$pass = md5('sarpe' . $pass);

		return $pass;
	}

	/**
	 * clean form post data (array)
	 */
	public function cleanPostArray($array){
		$cleanArray = array();

		foreach($array as $key => $value){
			$cleanArray[$key] = $this->cleanPostValue($value);
		}

		return $cleanArray;
	}

	/**
	 * clean form post data (value)
	 */
	public function cleanPostValue($value){
		// o mica protectie pt a afisa corect ceea ce insereaza.
		// sunt diferiti factori care pot fute si asta impiedica
			// $value = htmlspecialchars_decode($value);

		$value = trim($value);
		$value = htmlspecialchars($value);

		return $value;
	}

	/**
	 * Redirects to a specified local link
	 */
	public function redirect($link = '') {
		header('Location: ' . SITE_URL . $link);
	}

	/**
	 * Gets logged user details
	 * Used in defineConstants and added to GLOBALS['user']
	 * @return [type] [description]
	 */
	private function _getLoggedUser() {
		if (!isset($_COOKIE['SID'])) {
            return false;
        }

        $sid = $this->session->get($_COOKIE['SID']);
        if (  !$sid
            || $sid['type'] != 'SID'
        ) { return false; }

        $ret = $this->getUserByUserId($sid['data']);
        return $ret;
	}

	/**
	 * Gets user data from db by the user id
	 * @param  int $userId
	 * @return array         Db rows
	 */
	public function getUserByUserId($userId) {
		$sth = $this->db->prepare('SELECT * FROM users WHERE id = ? LIMIT 1');
    	$sth->execute(array($userId));

        return $sth->fetch();
	}

    /**
     * Gets a post list by user id
     * @param  int $user_id     The id of the user
     * @param  string $post_status Post status, for example draft
     * @return array              Posts
     */
    public function getPostsByUserId($userId, $postStatus, $offset = 0) {
    	$offset   = $offset * POSTS_PER_PAGE;
    	$rowCount = POSTS_PER_PAGE;

    	$sql = '';
    	if ($userId != -1) {
    		$sql = 'u.id = :userId AND ';
    	}
        $sql = 'SELECT p.ID as ID, post_title, post_subtitle,
                    username, name, word_count, b.status as bookmark_status,
                    UNIX_TIMESTAMP(post_date) as post_date
                FROM posts p JOIN users u ON (post_author=u.id)
               		LEFT JOIN bookmarks b ON (b.post_id=p.ID AND b.user_id=:loggedUserId)
                WHERE ' . $sql . 'post_status = :postStatus
                ORDER BY post_date DESC LIMIT :offset, :rowCount';
        $sth = $this->db->prepare($sql);
        $sth->bindValue(':loggedUserId', $GLOBALS['user']['id'], PDO::PARAM_INT);
        if ($userId != -1) {
       	 	$sth->bindValue(':userId',   $userId,                PDO::PARAM_INT);
       	}
        $sth->bindValue(':postStatus',   $postStatus,            PDO::PARAM_STR);
        $sth->bindValue(':offset',       $offset,                PDO::PARAM_INT);
        $sth->bindValue(':rowCount',     $rowCount,              PDO::PARAM_INT);
        $sth->execute();

        $rows = array();
        while($row = $sth->fetch()) {
            $row['post_date_supertag'] = date('j<b\r>M', $row['post_date']);
            $row['reading_time'] = ceil($row['word_count'] / WORDS_PER_MINUTE);
			$row['bookmark_status'] = (($row['bookmark_status'] == 0) ? null : $row['bookmark_status']);

            $rows[] = $row;
        }

        return $rows;
    }

    /**
     * Checks the existence of an user
     * @param  string $username
     * @return bool
     */
    public function userExists($username) {
        $sth = $this->db->prepare('SELECT 1 FROM users WHERE username = ? LIMIT 1');
        $sth->execute(array($username));

        return (bool) $sth->fetch();
    }

	private function _getExtension($file) {
        return strtolower(substr($file, strrpos($file, '.')+1));
    }

	/**
	 * Uploads image
	 * @param  array $POST
	 * @param  array $FILES
	 * @return array        Upload status
	 */
	public function uploadImage($POST, $FILES){
		$data = array('status' => 1);

		/**
		 * Checks for size overflow as stated in the link below
		 * http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/
		 */
		if (   $_SERVER['REQUEST_METHOD'] == 'POST'
			&& $_SERVER['CONTENT_LENGTH'] > 0
			&& empty($_POST)
			&& empty($_FILES)
		) {
			$data['message'] = 'Your file is too large';
			$data['status']  = 0;

			return $data;
		}

		// Checks the existence of a file
        if (   $_FILES['image']['size'] == 0
            || $_FILES['image']['error'] == UPLOAD_ERR_NO_FILE) {
            $data['message'] = 'You must select a file';
        	$data['status']  = 0;

            return $data;
        }

        // Checks the size of the uploaded file
        if (   $_FILES['image']['size'] > IMG_MAX_SIZE
            || $_FILES['image']['error'] == UPLOAD_ERR_INI_SIZE) {
            $data['message'] = 'Your file is too large';
        	$data['status']  = 0;

            return $data;
        }

        // Check if the extension is allowed
        $ext = $this->_getExtension($_FILES['image']['name']);
        if (strpos(IMG_ALLOWED_EXT, $ext) === false) {
            $data['message'] = 'Your file type is not allowed';
            $data['status']  = 0;

            return $data;
        }

        // Generates an unique name for the file
        do {
        	$file = $this->uniq(10);
        } while (file_exists(IMG_UPLOAD_PATH . $file . '.' . $ext));
        $file = $file . '.' . $ext;

        // Makes the directory available
        if (!is_dir(IMG_UPLOAD_PATH)) {
            mkdir(IMG_UPLOAD_PATH, 0777, true);
        }

        // Uploads file
        if (!move_uploaded_file($_FILES['image']['tmp_name'], (IMG_UPLOAD_PATH . $file))) {
            $data['message'] = 'Something strange happened, try again later ' .
            				   'or if the problem persists, contact the admin';
            $data['status']  = 0;

            return;
        }

        $data['image-url'] = SITE_URL . IMG_UPLOAD_PATH . $file;

		return $data;
	}
}
?>


text:
class HkxObject;
class QGridLayout;
class TableWidget;
class hkbFootIkControlsModifier;
class LineEdit;
class DoubleSpinBox;
class CheckBox;
class ComboBox;
class GenericTableWidget;
class SpinBox;
class hkbVariableBindingSet;
class ExpressionDataArrayUI;
class QGroupBox;
class LegUI;
class QuadVariableWidget;

class FootIkControlsModifierUI final: public QStackedWidget
{
    Q_OBJECT
public:
    FootIkControlsModifierUI();
    FootIkControlsModifierUI& operator=(const FootIkControlsModifierUI&) = delete;
    FootIkControlsModifierUI(const FootIkControlsModifierUI &) = delete;
    ~FootIkControlsModifierUI() = default;
public:
    void loadData(HkxObject *data);
    void variableRenamed(const QString & name, int index);
    void connectToTables(GenericTableWidget *variables, GenericTableWidget *properties, GenericTableWidget *events);
signals:
    void viewVariables(int index, const QString & typeallowed, const QStringList &typesdisallowed);
    void viewProperties(int index, const QString & typeallowed, const QStringList &typesdisallowed);
    void viewEvents(int index, const QString & typeallowed, const QStringList &typesdisallowed);
    void modifierNameChanged(const QString & newName, int index);
private slots:
    void setName(const QString &newname);
    void setEnable();
    void setOnOffGain();
    void setGroundAscendingGain();
    void setGroundDescendingGain();
    void setFootPlantedGain();
    void setFootRaisedGain();
    void setFootUnlockGain();
    void setWorldFromModelFeedbackGain();
    void setErrorUpDownBias();
    void setAlignWorldFromModelGain();
    void setHipOrientationGain();
    void setMaxKneeAngleDifference();
    void setAnkleOrientationGain();
    void setErrorOutTranslation();
    void setAlignWithGroundRotation();
    void viewSelectedChild(int row, int column);
    void variableTableElementSelected(int index, const QString &name);
    void returnToWidget();
private:
    void addLeg();
    void removeLeg(int index);
    void loadDynamicTableRows();
    void toggleSignals(bool toggleconnections);
    void setBindingVariable(int index, const QString & name);
    void selectTableToView(bool viewproperties, const QString & path);
private:
    enum ACTIVE_WIDGET {
        MAIN_WIDGET,
        CHILD_WIDGET
    };
private:
    static const QStringList headerLabels;
    hkbFootIkControlsModifier *bsData;
    QGridLayout *topLyt;
    TableWidget *table;
    QGroupBox *groupBox;
    LegUI *legUI;
    LineEdit *name;
    CheckBox *enable;
    DoubleSpinBox *onOffGain;
    DoubleSpinBox *groundAscendingGain;
    DoubleSpinBox *groundDescendingGain;
    DoubleSpinBox *footPlantedGain;
    DoubleSpinBox *footRaisedGain;
    DoubleSpinBox *footUnlockGain;
    DoubleSpinBox *worldFromModelFeedbackGain;
    DoubleSpinBox *errorUpDownBias;
    DoubleSpinBox *alignWorldFromModelGain;
    DoubleSpinBox *hipOrientationGain;
    DoubleSpinBox *maxKneeAngleDifference;
    DoubleSpinBox *ankleOrientationGain;
    QuadVariableWidget *errorOutTranslation;
    QuadVariableWidget *alignWithGroundRotation;
};

#endif // FOOTIKCONTROLSMODIFIERUI_H