* API: (bug 17007) Add export functionality to the API
authorRoan Kattouw <catrope@users.mediawiki.org>
Mon, 2 Feb 2009 20:07:33 +0000 (20:07 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Mon, 2 Feb 2009 20:07:33 +0000 (20:07 +0000)
* 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
includes/AutoLoader.php
includes/api/ApiFormatRaw.php [new file with mode: 0644]
includes/api/ApiQuery.php

index 716f471..afc44af 100644 (file)
@@ -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 ===
 
index 6bf9018..4aeb433 100644 (file)
@@ -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 (file)
index 0000000..098d55f
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+/*
+ * Created on Feb 2, 2009
+ *
+ * API for MediaWiki 1.8+
+ *
+ * Copyright (C) 2008 Roan Kattouw <Firstname>.<Lastname>@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$';
+       }
+}
index db2c867..56f84ef 100644 (file)
@@ -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.'
                );
        }