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();
|
|
|
|
|
|
|
|
|
|
$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
|
|
|
|
2010-12-05 14:22:49 +00:00
|
|
|
$importer = new WikiImporter( $source->value );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $params['namespace'] ) ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$importer->setTargetNamespace( $params['namespace'] );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2012-08-15 08:07:26 +00:00
|
|
|
if ( isset( $params['rootpage'] ) ) {
|
|
|
|
|
$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();
|
|
|
|
|
} catch ( MWException $e ) {
|
|
|
|
|
$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();
|
|
|
|
|
$result->setIndexedTagName( $resultData, 'page' );
|
|
|
|
|
$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(
|
2012-07-18 17:24:38 +00:00
|
|
|
'token' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
|
|
|
|
),
|
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
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2009-02-04 20:11:27 +00:00
|
|
|
'token' => 'Import token obtained through prop=info',
|
|
|
|
|
'summary' => 'Import summary',
|
|
|
|
|
'xml' => 'Uploaded XML file',
|
|
|
|
|
'interwikisource' => 'For interwiki imports: wiki to import from',
|
|
|
|
|
'interwikipage' => 'For interwiki imports: page to import',
|
|
|
|
|
'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
|
2009-02-06 00:21:06 +00:00
|
|
|
'templates' => 'For interwiki imports: import all included templates as well',
|
2009-02-04 20:11:27 +00:00
|
|
|
'namespace' => 'For interwiki imports: import to this namespace',
|
2012-08-15 08:07:26 +00:00
|
|
|
'rootpage' => 'Import as subpage of this page',
|
2009-02-04 20:11:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDescription() {
|
2011-04-06 22:16:38 +00:00
|
|
|
return array(
|
2013-02-03 18:47:42 +00:00
|
|
|
'Import a page from another wiki, or an XML file.',
|
2011-04-06 22:16:38 +00:00
|
|
|
'Note that the HTTP POST must be done as a file upload (i.e. using multipart/form-data) when',
|
2014-03-09 20:22:47 +00:00
|
|
|
'sending a file for the "xml" parameter.'
|
2011-04-06 22:16:38 +00:00
|
|
|
);
|
2009-02-04 20:11:27 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 23:53:43 +00:00
|
|
|
public function getTokenSalt() {
|
2010-02-16 21:59:16 +00:00
|
|
|
return '';
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2009-02-04 20:11:27 +00:00
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2009-02-04 20:11:27 +00:00
|
|
|
return array(
|
2013-11-16 19:09:17 +00:00
|
|
|
'api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&' .
|
|
|
|
|
'namespace=100&fullhistory=&token=123ABC'
|
2011-12-27 16:22:35 +00:00
|
|
|
=> 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history',
|
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'];
|
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
|
|
|
$r['invalid'] = '';
|
|
|
|
|
} 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
|
|
|
}
|