2009-02-04 20:11:27 +00:00
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2009-02-04 20:11:27 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Feb 4, 2009
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
|
2009-02-04 20:11:27 +00:00
|
|
|
*
|
|
|
|
|
* 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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-04 20:11:27 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2009-02-04 20:11:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API module that imports an XML file like Special:Import does
|
|
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiImport extends ApiBase {
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2011-10-26 23:27:01 +00:00
|
|
|
$user = $this->getUser();
|
2009-02-04 20:11:27 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
2015-05-14 19:32:30 +00:00
|
|
|
$this->requireMaxOneParameter( $params, 'namespace', 'rootpage' );
|
|
|
|
|
|
2009-02-04 20:11:27 +00:00
|
|
|
$isUpload = false;
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $params['interwikisource'] ) ) {
|
2011-10-26 23:27:01 +00:00
|
|
|
if ( !$user->isAllowed( 'import' ) ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'cantimport' );
|
2011-03-27 01:14:57 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !isset( $params['interwikipage'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->dieUsageMsg( array( 'missingparam', 'interwikipage' ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2009-02-04 20:11:27 +00:00
|
|
|
$source = ImportStreamSource::newFromInterwiki(
|
2010-02-23 18:05:46 +00:00
|
|
|
$params['interwikisource'],
|
|
|
|
|
$params['interwikipage'],
|
|
|
|
|
$params['fullhistory'],
|
|
|
|
|
$params['templates']
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2009-02-04 20:11:27 +00:00
|
|
|
$isUpload = true;
|
2011-10-26 23:27:01 +00:00
|
|
|
if ( !$user->isAllowed( 'importupload' ) ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'cantimport-upload' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
$source = ImportStreamSource::newFromUpload( 'xml' );
|
2009-02-04 20:11:27 +00:00
|
|
|
}
|
2010-12-05 14:22:49 +00:00
|
|
|
if ( !$source->isOK() ) {
|
2013-06-13 17:56:29 +00:00
|
|
|
$this->dieStatus( $source );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2009-02-04 20:11:27 +00:00
|
|
|
|
2014-08-23 07:40:00 +00:00
|
|
|
$importer = new WikiImporter( $source->value, $this->getConfig() );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $params['namespace'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$importer->setTargetNamespace( $params['namespace'] );
|
2015-01-27 09:01:04 +00:00
|
|
|
} elseif ( isset( $params['rootpage'] ) ) {
|
2012-08-15 08:07:26 +00:00
|
|
|
$statusRootPage = $importer->setTargetRootPage( $params['rootpage'] );
|
2013-04-19 18:03:05 +00:00
|
|
|
if ( !$statusRootPage->isGood() ) {
|
2013-06-13 17:56:29 +00:00
|
|
|
$this->dieStatus( $statusRootPage );
|
2012-08-15 08:07:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
$reporter = new ApiImportReporter(
|
|
|
|
|
$importer,
|
|
|
|
|
$isUpload,
|
|
|
|
|
$params['interwikisource'],
|
|
|
|
|
$params['summary']
|
|
|
|
|
);
|
2009-02-04 20:11:27 +00:00
|
|
|
|
2010-11-24 11:45:01 +00:00
|
|
|
try {
|
|
|
|
|
$importer->doImport();
|
2015-01-09 23:44:47 +00:00
|
|
|
} catch ( Exception $e ) {
|
2010-11-24 11:45:01 +00:00
|
|
|
$this->dieUsageMsg( array( 'import-unknownerror', $e->getMessage() ) );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-01-23 22:26:40 +00:00
|
|
|
|
2009-02-04 20:11:27 +00:00
|
|
|
$resultData = $reporter->getData();
|
2011-06-30 01:06:17 +00:00
|
|
|
$result = $this->getResult();
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
ApiResult::setIndexedTagName( $resultData, 'page' );
|
2011-06-30 01:06:17 +00:00
|
|
|
$result->addValue( null, $this->getModuleName(), $resultData );
|
2009-02-04 20:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-14 21:12:11 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2009-02-04 20:11:27 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-04 20:11:27 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2009-02-04 20:11:27 +00:00
|
|
|
'summary' => null,
|
2013-02-26 21:45:37 +00:00
|
|
|
'xml' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'upload',
|
|
|
|
|
),
|
2009-02-04 20:11:27 +00:00
|
|
|
'interwikisource' => array(
|
2014-01-24 02:51:11 +00:00
|
|
|
ApiBase::PARAM_TYPE => $this->getConfig()->get( 'ImportSources' ),
|
2009-02-04 20:11:27 +00:00
|
|
|
),
|
|
|
|
|
'interwikipage' => null,
|
|
|
|
|
'fullhistory' => false,
|
2009-02-06 00:21:06 +00:00
|
|
|
'templates' => false,
|
2009-02-04 20:11:27 +00:00
|
|
|
'namespace' => array(
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'namespace'
|
2012-08-15 08:07:26 +00:00
|
|
|
),
|
|
|
|
|
'rootpage' => null,
|
2009-02-04 20:11:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'csrf';
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2009-02-04 20:11:27 +00:00
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2009-02-04 20:11:27 +00:00
|
|
|
return array(
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
|
2013-11-16 19:09:17 +00:00
|
|
|
'namespace=100&fullhistory=&token=123ABC'
|
2014-09-18 17:38:23 +00:00
|
|
|
=> 'apihelp-import-example-import',
|
2009-02-04 20:11:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/API:Import';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2009-02-04 20:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Import reporter for the API
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiImportReporter extends ImportReporter {
|
|
|
|
|
private $mResultArr = array();
|
|
|
|
|
|
2011-05-20 22:03:10 +00:00
|
|
|
/**
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param Title $title
|
|
|
|
|
* @param Title $origTitle
|
|
|
|
|
* @param int $revisionCount
|
|
|
|
|
* @param int $successCount
|
|
|
|
|
* @param array $pageInfo
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-05-20 22:03:10 +00:00
|
|
|
*/
|
2010-11-10 00:54:19 +00:00
|
|
|
function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
|
2009-02-04 20:11:27 +00:00
|
|
|
// Add a result entry
|
|
|
|
|
$r = array();
|
2012-04-29 18:14:40 +00:00
|
|
|
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
if ( $title === null ) {
|
2012-04-29 18:14:40 +00:00
|
|
|
# Invalid or non-importable title
|
|
|
|
|
$r['title'] = $pageInfo['title'];
|
2015-01-16 19:00:07 +00:00
|
|
|
$r['invalid'] = true;
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
} else {
|
2012-04-29 18:14:40 +00:00
|
|
|
ApiQueryBase::addTitleInfo( $r, $title );
|
|
|
|
|
$r['revisions'] = intval( $successCount );
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-04 20:11:27 +00:00
|
|
|
$this->mResultArr[] = $r;
|
|
|
|
|
|
|
|
|
|
// Piggyback on the parent to do the logging
|
2010-11-10 00:54:19 +00:00
|
|
|
parent::reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo );
|
2009-02-04 20:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
function getData() {
|
2009-02-04 20:11:27 +00:00
|
|
|
return $this->mResultArr;
|
|
|
|
|
}
|
2010-05-12 13:28:13 +00:00
|
|
|
}
|