abd27771890299ce00c99b26e3cff6ade133d6ee
5 * Created on Feb 4, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that imports an XML file like Special:Import does
32 class ApiImport
extends ApiBase
{
34 public function __construct( $main, $action ) {
35 parent
::__construct( $main, $action );
38 public function execute() {
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
43 if ( isset( $params['interwikisource'] ) ) {
44 if ( !$user->isAllowed( 'import' ) ) {
45 $this->dieUsageMsg( 'cantimport' );
47 if ( !isset( $params['interwikipage'] ) ) {
48 $this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
50 $source = ImportStreamSource
::newFromInterwiki(
51 $params['interwikisource'],
52 $params['interwikipage'],
53 $params['fullhistory'],
58 if ( !$user->isAllowed( 'importupload' ) ) {
59 $this->dieUsageMsg( 'cantimport-upload' );
61 $source = ImportStreamSource
::newFromUpload( 'xml' );
63 if ( !$source->isOK() ) {
64 $this->dieUsageMsg( $source->getErrorsArray() );
67 $importer = new WikiImporter( $source->value
);
68 if ( isset( $params['namespace'] ) ) {
69 $importer->setTargetNamespace( $params['namespace'] );
71 $reporter = new ApiImportReporter(
74 $params['interwikisource'],
79 $importer->doImport();
80 } catch ( MWException
$e ) {
81 $this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
84 $resultData = $reporter->getData();
85 $result = $this->getResult();
86 $result->setIndexedTagName( $resultData, 'page' );
87 $result->addValue( null, $this->getModuleName(), $resultData );
90 public function mustBePosted() {
94 public function isWriteMode() {
98 public function getAllowedParams() {
99 global $wgImportSources;
102 ApiBase
::PARAM_TYPE
=> 'string',
103 ApiBase
::PARAM_REQUIRED
=> true
107 'interwikisource' => array(
108 ApiBase
::PARAM_TYPE
=> $wgImportSources
110 'interwikipage' => null,
111 'fullhistory' => false,
112 'templates' => false,
113 'namespace' => array(
114 ApiBase
::PARAM_TYPE
=> 'namespace'
119 public function getParamDescription() {
121 'token' => 'Import token obtained through prop=info',
122 'summary' => 'Import summary',
123 'xml' => 'Uploaded XML file',
124 'interwikisource' => 'For interwiki imports: wiki to import from',
125 'interwikipage' => 'For interwiki imports: page to import',
126 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
127 'templates' => 'For interwiki imports: import all included templates as well',
128 'namespace' => 'For interwiki imports: import to this namespace',
132 public function getResultProperties() {
134 ApiBase
::PROP_LIST
=> true,
138 'revisions' => 'integer'
143 public function getDescription() {
145 'Import a page from another wiki, or an XML file.' ,
146 'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
147 'sending a file for the "xml" parameter.'
151 public function getPossibleErrors() {
152 return array_merge( parent
::getPossibleErrors(), array(
153 array( 'cantimport' ),
154 array( 'missingparam', 'interwikipage' ),
155 array( 'cantimport-upload' ),
156 array( 'import-unknownerror', 'source' ),
157 array( 'import-unknownerror', 'result' ),
161 public function needsToken() {
165 public function getTokenSalt() {
169 public function getExamples() {
171 'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory=&token=123ABC'
172 => 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
176 public function getHelpUrls() {
177 return 'https://www.mediawiki.org/wiki/API:Import';
180 public function getVersion() {
181 return __CLASS__
. ': $Id$';
186 * Import reporter for the API
189 class ApiImportReporter
extends ImportReporter
{
190 private $mResultArr = array();
193 * @param $title Title
194 * @param $origTitle Title
195 * @param $revisionCount int
196 * @param $successCount int
200 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
201 // Add a result entry
204 if ( $title === null ) {
205 # Invalid or non-importable title
206 $r['title'] = $pageInfo['title'];
209 ApiQueryBase
::addTitleInfo( $r, $title );
210 $r['revisions'] = intval( $successCount );
213 $this->mResultArr
[] = $r;
215 // Piggyback on the parent to do the logging
216 parent
::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
220 return $this->mResultArr
;