接管特殊页面中的输出

This page is a translated version of the page Taking over output in your special page and the translation is 17% complete.

Normally your Special page will queue up some HTML and/or wikitext output via $wgOut, which will take care of actual HTML output and skinning at the end of the request.

Sometimes though you want to output things directly, such as to export a custom XML type, data feed, or binary download. It's not too hard to do, but it's not super-obvious either... Here's some example bits snipped from Special:Export:

In your SpecialPage class's execute() method:

  // Disable the regular OutputPage stuff -- we're taking over output!
  $wgOut->disable();
  
  // Cancel output buffering and gzipping if set
  // You might need this if you're creating HUGE output, otherwise skip it
  //wfResetOutputBuffers();
  
  // Set your content type... this can XML or binary or whatever you need.
  header( "Content-type: application/xml; charset=utf-8" );
  
  // If you want to force browsers to download instead of showing XML inline you can do something like this:
  // Provide a sane filename suggestion
  $filename = urlencode( $wgSitename . '-' . wfTimestampNow() . '.xml' );
  header( "Content-disposition: attachment;filename={$filename}" );
  
  // Now you can output data directly with 'print', 'echo', etc.
  print "<xml><hello to='world'/></xml>";