From 5e5684ec7735381cac31dc8dfe8c867e449ada09 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 2 Feb 2009 20:07:33 +0000 Subject: [PATCH] * API: (bug 17007) Add export functionality to the API * Accessed through the export and exportnowrap parameters added to action=query * To facilitate &exportnowrap, add ApiFormatRaw, a formatter that just spits out its input without any formatting (not accessible through &format= of course) * Fix up the action=query description message to reflect the deprecation of query.php --- RELEASE-NOTES | 1 + includes/AutoLoader.php | 1 + includes/api/ApiFormatRaw.php | 58 +++++++++++++++++++++++++++++++++++ includes/api/ApiQuery.php | 37 ++++++++++++++++++++-- 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 includes/api/ApiFormatRaw.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 716f4711de..afc44af5c3 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -155,6 +155,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 17224) Added siprop=rightsinfo to meta=siteinfo * (bug 17239) Added prop=displaytitle to action=parse * (bug 17317) Added watch parameter to action=protect +* (bug 17007) Added export and exportnowrap parameters to action=query === Languages updated in 1.15 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 6bf9018bb0..4aeb4337de 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -228,6 +228,7 @@ $wgAutoloadLocalClasses = array( 'ApiFormatFeedWrapper' => 'includes/api/ApiFormatBase.php', 'ApiFormatJson' => 'includes/api/ApiFormatJson.php', 'ApiFormatPhp' => 'includes/api/ApiFormatPhp.php', + 'ApiFormatRaw' => 'includes/api/ApiFormatRaw.php', 'ApiFormatTxt' => 'includes/api/ApiFormatTxt.php', 'ApiFormatWddx' => 'includes/api/ApiFormatWddx.php', 'ApiFormatXml' => 'includes/api/ApiFormatXml.php', diff --git a/includes/api/ApiFormatRaw.php b/includes/api/ApiFormatRaw.php new file mode 100644 index 0000000000..098d55f268 --- /dev/null +++ b/includes/api/ApiFormatRaw.php @@ -0,0 +1,58 @@ +.@home.nl + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if (!defined('MEDIAWIKI')) { + // Eclipse helper - will be ignored in production + require_once ('ApiFormatBase.php'); +} + +/** + * Formatter that spits out anything you like with any desired MIME type + * @ingroup API + */ +class ApiFormatRaw extends ApiFormatBase { + + public function __construct($main, $format) { + parent :: __construct($main, $format); + } + + public function getMimeType() { + $data = $this->getResultData(); + if(!isset($data['mime'])) + ApiBase::dieDebug(__METHOD__, "No MIME type set for raw formatter"); + return $data['mime']; + } + + public function execute() { + $data = $this->getResultData(); + if(!isset($data['text'])) + ApiBase::dieDebug(__METHOD__, "No text given for raw formatter"); + $this->printText($data['text']); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +} diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index db2c86732a..56f84efc0f 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -160,6 +160,14 @@ class ApiQuery extends ApiBase { function getModules() { return array_merge($this->mQueryPropModules, $this->mQueryListModules, $this->mQueryMetaModules); } + + public function getCustomPrinter() { + // If &exportnowrap is set, use the raw formatter + if ($this->getParameter('exportnowrap')) + return new ApiFormatRaw($this->getMain()); + else + return null; + } /** * Query execution happens in the following steps: @@ -347,6 +355,27 @@ class ApiQuery extends ApiBase { $result->setIndexedTagName($pages, 'page'); $result->addValue('query', 'pages', $pages); + + if ($this->params['export']) { + $exporter = new WikiExporter($this->getDB()); + // WikiExporter writes to stdout, so catch its + // output with an ob + ob_start(); + $exporter->openStream(); + foreach ($pageSet->getGoodTitles() as $title) + if ($title->userCanRead()) + $exporter->pageByTitle($title); + $exporter->closeStream(); + $exportxml = ob_get_contents(); + ob_end_clean(); + if ($this->params['exportnowrap']) { + $result->reset(); + // Raw formatter will handle this + $result->addValue(null, 'text', $exportxml); + $result->addValue(null, 'mime', 'text/xml'); + } else + $result->addValue('query', 'export', $exportxml); + } } } @@ -415,6 +444,8 @@ class ApiQuery extends ApiBase { ), 'redirects' => false, 'indexpageids' => false, + 'export' => false, + 'exportnowrap' => false, ); } @@ -491,14 +522,16 @@ class ApiQuery extends ApiBase { 'meta' => 'Which meta data to get about the site', 'generator' => 'Use the output of a list as the input for other prop/list/meta items', 'redirects' => 'Automatically resolve redirects', - 'indexpageids' => 'Include an additional pageids section listing all returned page IDs.' + 'indexpageids' => 'Include an additional pageids section listing all returned page IDs.', + 'export' => 'Export the current revisions of all given or generated pages', + 'exportnowrap' => 'Return the export XML without wrapping it in an XML result', ); } public function getDescription() { return array ( 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,', - 'and is loosely based on the Query API interface currently available on all MediaWiki servers.', + 'and is loosely based on the old query.php interface.', 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.' ); } -- 2.20.1