Add capability for DOM-based template expansion, and use DOM fragments for extensions

This patch adds the capability to expand individual transclusions all the way
to DOM, which enforces properly nested templates. This is also needed for
DOM-based template re-expansion, which the VE folks would like to use for
template editing.

In normal parsing, DOM-based expansion is currently disabled by default for
transclusions, and enabled by default for extensions. The decision whether to
balance a particular transclusion can be based on a statistics-based
classification of templates into balanced and unbalanced ones. The advantage
of this approach is consistency in behavior for old revisions. Another
alternative is to wrap unbalanced transclusions into <domparse> tags which
disable DOM-based parsing for all transclusions in its content. This has the
advantage that all special cases can be handled after tag insertion, and that
balancing could also be enforced in the PHP parser using an extension.

Other major changes:
* Renamed transclusion content typeof from mw:Object/Template to
  mw:Transclusion
* Renamed extension typeof from mw:Object/Extensions/foo to mw:Extension/foo
  and switched Cite to use lower-case ref/references too

Other minor changes:
* Only apply template encapsulation algorithm on DOM to mw:Transclusion and
  mw:Param objects, and no longer to all mw:Object/* elements. We should
  probably switch to a more explicit encapsulation type instead. Maybe
  something like mw:EncapStart/Transclusion and mw:EncapEnd/Transclusion
  instead of the current mw:Transclusion and mw:Transclusion/End, so that
  stripping those metas out selectively is easy?
* Changed the DOMTraverser logic to let handlers explicitly return the next
  element to handle. Useful when several siblings are handled, as is the case
  for the fragment unwrapper for example, and avoids some cases where deleted
  nodes were still being processed.
* Changed Cite to use mw:Extension/Ref{,erences} as all other extensions.
* Make sure we round-trip gallery when the PHP preprocessor is not used

Five parsoid-specific wt2html tests are failing due to the typeof change.
They will be fine once those tests are adjusted.  For now, adding them
to parser tests blacklist.

TODO:
* Switch the Cite extension to the generic DOMFragment mechanism instead of
  marker tokens.

Change-Id: I64b560a12695256915d2196d0646347381400e80