From 81d5d8adc2572e8ef765464cb2a72c7274b60fd5 Mon Sep 17 00:00:00 2001 From: Kevin Israel Date: Fri, 8 Jul 2016 16:22:27 -0400 Subject: [PATCH] ApiQuery: Don't mess with PHP output buffering Specifically, it is not necessary to use output buffering functions to capture XML generated by the export code because it is already possible to set the "output sink" object to be used. * Created a DumpStringOutput class, which appends all output to a string property rather than printing output immediately. * Used that class, instead of ob_start() and ob_get_clean(), in ApiQuery and ExportTest. Change-Id: I238f5d5ec7fd442c845b25cb59ef81ac3285099f --- autoload.php | 1 + includes/api/ApiQuery.php | 8 ++--- includes/export/DumpStringOutput.php | 45 +++++++++++++++++++++++++++ tests/phpunit/includes/ExportTest.php | 5 +-- 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 includes/export/DumpStringOutput.php diff --git a/autoload.php b/autoload.php index 8e214ce210..57da6a038e 100644 --- a/autoload.php +++ b/autoload.php @@ -389,6 +389,7 @@ $wgAutoloadLocalClasses = [ 'DumpPipeOutput' => __DIR__ . '/includes/export/DumpPipeOutput.php', 'DumpRenderer' => __DIR__ . '/maintenance/renderDump.php', 'DumpRev' => __DIR__ . '/maintenance/storage/dumpRev.php', + 'DumpStringOutput' => __DIR__ . '/includes/export/DumpStringOutput.php', 'DuplicateJob' => __DIR__ . '/includes/jobqueue/jobs/DuplicateJob.php', 'EditAction' => __DIR__ . '/includes/actions/EditAction.php', 'EditCLI' => __DIR__ . '/maintenance/edit.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index ed4d373a7c..1e912561bc 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -443,16 +443,14 @@ class ApiQuery extends ApiBase { } $exporter = new WikiExporter( $this->getDB() ); - // WikiExporter writes to stdout, so catch its - // output with an ob - ob_start(); + $sink = new DumpStringOutput; + $exporter->setOutputSink( $sink ); $exporter->openStream(); foreach ( $exportTitles as $title ) { $exporter->pageByTitle( $title ); } $exporter->closeStream(); - $exportxml = ob_get_contents(); - ob_end_clean(); + $exportxml = $sink->getOutput(); // Don't check the size of exported stuff // It's not continuable, so it would cause more diff --git a/includes/export/DumpStringOutput.php b/includes/export/DumpStringOutput.php new file mode 100644 index 0000000000..5168225179 --- /dev/null +++ b/includes/export/DumpStringOutput.php @@ -0,0 +1,45 @@ +output .= $string; + } + + /** + * Get the string containing the output. + * + * @return string + */ + public function getOutput() { + return $this->output; + } +} diff --git a/tests/phpunit/includes/ExportTest.php b/tests/phpunit/includes/ExportTest.php index a2bb97a2bc..c07708158b 100644 --- a/tests/phpunit/includes/ExportTest.php +++ b/tests/phpunit/includes/ExportTest.php @@ -30,11 +30,12 @@ class ExportTest extends MediaWikiLangTestCase { $title = Title::newFromText( $pageTitle ); - ob_start(); + $sink = new DumpStringOutput; + $exporter->setOutputSink( $sink ); $exporter->openStream(); $exporter->pageByTitle( $title ); $exporter->closeStream(); - $xmlString = ob_get_clean(); + $xmlString = $sink->getOutput(); // This throws error if invalid xml output $xmlObject = simplexml_load_string( $xmlString ); -- 2.20.1