On Mon, Jun 28, 2010 at 8:19 PM, Samuel Lampa samuel.lampa.l@rilnet.comwrote:
Is it possible (and if so, how to) totally replace the output from a Special page (including the sending headers etc)?
That is, can I use a SpecialPage as a SPARQL Endpoint and have it output pure RDF/XML (or similar) if a SPARQL query is sent to it via POST, while if not, it should just display the SpecialPage including a form for submitting a SPARQL query.
It sure is! In your execute() method, you'll want to call $wgOut->disable(). This'll turn off the regular page display output at the end of request processing, and give you a chance to take over and do whatever you like, eg sending appropriate HTTP headers and output directly.
The traditional example I always like to use is Special:Export in the core code. If you bump down a bit past the initialization code and input validation, you'll see this code that's called just before we're actually doing the XML output:
if ( $this->doExport ) { $wgOut->disable(); // Cancel output buffering and gzipping if set // This should provide safer streaming for pages with history wfResetOutputBuffers(); $wgRequest->response()->header( "Content-type: application/xml; charset=utf-8" );
You should always be sure to set the content-type correctly; and if outputting XML or HTML, be sure to include a charset header.
The wfResetOutputBuffers() bit isn't required in most cases, but if you're outputting a *lot* of data it can help to let you stream output directly to the downloader. (By default, buffering is used to apply gzip encoding and send a Content-Size header. This is usually good, but means that all your output has to be built and stored in memory before it actually gets sent to the client... potentially a problem if you're streaming out fifty thousand revisions of [[Hurricane Katrina]]!)
When you're doing your raw output, you can go ahead and do 'print' or 'echo' directly if you like -- you're now in control. ;)
As a general note -- always be careful of your output to make sure you're applying proper escaping when generating HTML or XML; raw output can be a security risk if input isn't sanitized properly.
-- brion vibber (brion @ pobox.com)