r46815 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46814‎ | r46815 | r46816 >
Date:20:11, 4 February 2009
Author:catrope
Status:deferred
Tags:
Comment:
* API: (bug 17007) Add action=import
* Add intoken=import to prop=info
* Store message key and arguments in WikiErrorMsg
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/WikiError.php (modified) (history)
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiImport.php (added) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQueryInfo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiImport.php
@@ -0,0 +1,173 @@
 2+<?php
 3+
 4+/*
 5+ * Created on Feb 4, 2009
 6+ *
 7+ * API for MediaWiki 1.8+
 8+ *
 9+ * Copyright (C) 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
 10+ *
 11+ * This program is free software; you can redistribute it and/or modify
 12+ * it under the terms of the GNU General Public License as published by
 13+ * the Free Software Foundation; either version 2 of the License, or
 14+ * (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License along
 22+ * with this program; if not, write to the Free Software Foundation, Inc.,
 23+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 24+ * http://www.gnu.org/copyleft/gpl.html
 25+ */
 26+
 27+if (!defined('MEDIAWIKI')) {
 28+ // Eclipse helper - will be ignored in production
 29+ require_once ('ApiBase.php');
 30+}
 31+
 32+/**
 33+ * API module that imports an XML file like Special:Import does
 34+ *
 35+ * @ingroup API
 36+ */
 37+class ApiImport extends ApiBase {
 38+
 39+ public function __construct($main, $action) {
 40+ parent :: __construct($main, $action);
 41+ }
 42+
 43+ public function execute() {
 44+ global $wgUser;
 45+ $this->getMain()->requestWriteMode();
 46+ if(!$wgUser->isAllowed('import'))
 47+ $this->dieUsageMsg(array('cantimport'));
 48+ $params = $this->extractRequestParams();
 49+ if(!isset($params['token']))
 50+ $this->dieUsageMsg(array('missingparam', 'token'));
 51+ if(!$wgUser->matchEditToken($params['token']))
 52+ $this->dieUsageMsg(array('sessionfailure'));
 53+
 54+ $source = null;
 55+ $isUpload = false;
 56+ if(isset($params['interwikisource']))
 57+ {
 58+ if(!isset($params['interwikipage']))
 59+ $this->dieUsageMsg(array('missingparam', 'interwikipage'));
 60+ $source = ImportStreamSource::newFromInterwiki(
 61+ $params['interwikisource'],
 62+ $params['interwikipage'],
 63+ $params['fullhistory']);
 64+ }
 65+ else
 66+ {
 67+ $isUpload = true;
 68+ if(!$wgUser->isAllowed('importupload'))
 69+ $this->dieUsageMsg(array('cantimport-upload'));
 70+ $source = ImportStreamSource::newFromUpload('xml');
 71+ }
 72+ if($source instanceof WikiErrorMsg)
 73+ $this->dieUsageMsg(array_merge(
 74+ array($source->getMessageKey()),
 75+ $source->getMessageArgs()));
 76+ else if(WikiError::isError($source))
 77+ // This shouldn't happen
 78+ $this->dieUsageMsg(array('import-unknownerror', $source->getMessage()));
 79+
 80+ $importer = new WikiImporter($source);
 81+ if(isset($params['namespace']))
 82+ $importer->setTargetNamespace($params['namespace']);
 83+ $reporter = new ApiImportReporter($importer, $isUpload,
 84+ $params['interwikisource'],
 85+ $params['summary']);
 86+
 87+ $result = $importer->doImport();
 88+ if($result instanceof WikiXmlError)
 89+ $this->dieUsageMsg(array('import-xml-error',
 90+ $result->mLine,
 91+ $result->mColumn,
 92+ $result->mByte . $result->mContext,
 93+ xml_error_string($result->mXmlError)));
 94+ else if(WikiError::isError($result))
 95+ // This shouldn't happen
 96+ $this->dieUsageMsg(array('import-unknownerror', $result->getMessage()));
 97+ $resultData = $reporter->getData();
 98+ $this->getResult()->setIndexedTagName($resultData, 'page');
 99+ $this->getResult()->addValue(null, $this->getModuleName(), $resultData);
 100+ }
 101+
 102+ public function mustBePosted() { return true; }
 103+
 104+ public function getAllowedParams() {
 105+ global $wgImportSources;
 106+ return array (
 107+ 'token' => null,
 108+ 'summary' => null,
 109+ 'xml' => null,
 110+ 'interwikisource' => array(
 111+ ApiBase :: PARAM_TYPE => $wgImportSources
 112+ ),
 113+ 'interwikipage' => null,
 114+ 'fullhistory' => false,
 115+ 'namespace' => array(
 116+ ApiBase :: PARAM_TYPE => 'namespace'
 117+ )
 118+ );
 119+ }
 120+
 121+ public function getParamDescription() {
 122+ return array (
 123+ 'token' => 'Import token obtained through prop=info',
 124+ 'summary' => 'Import summary',
 125+ 'xml' => 'Uploaded XML file',
 126+ 'interwikisource' => 'For interwiki imports: wiki to import from',
 127+ 'interwikipage' => 'For interwiki imports: page to import',
 128+ 'fullhistory' => 'For interwiki imports: import the full history, not just the current version',
 129+ 'namespace' => 'For interwiki imports: import to this namespace',
 130+ );
 131+ }
 132+
 133+ public function getDescription() {
 134+ return array (
 135+ 'Import a page from another wiki, or an XML file'
 136+ );
 137+ }
 138+
 139+ protected function getExamples() {
 140+ return array(
 141+ 'Import [[meta:Help:Parserfunctions]] to namespace 100 with full history:',
 142+ ' api.php?action=import&interwikisource=meta&interwikipage=Help:ParserFunctions&namespace=100&fullhistory&token=123ABC',
 143+ );
 144+ }
 145+
 146+ public function getVersion() {
 147+ return __CLASS__ . ': $Id$';
 148+ }
 149+}
 150+
 151+/**
 152+ * Import reporter for the API
 153+ * @ingroup API
 154+ */
 155+class ApiImportReporter extends ImportReporter {
 156+ private $mResultArr = array();
 157+
 158+ function reportPage($title, $origTitle, $revisionCount, $successCount)
 159+ {
 160+ // Add a result entry
 161+ $r = array();
 162+ ApiQueryBase::addTitleInfo($r, $title);
 163+ $r['revisions'] = $successCount;
 164+ $this->mResultArr[] = $r;
 165+
 166+ // Piggyback on the parent to do the logging
 167+ parent::reportPage($title, $origTitle, $revisionCount, $successCount);
 168+ }
 169+
 170+ function getData()
 171+ {
 172+ return $this->mResultArr;
 173+ }
 174+}
Property changes on: trunk/phase3/includes/api/ApiImport.php
___________________________________________________________________
Added: svn:eol-style
1175 + native
Added: svn:keywords
2176 + Id
Index: trunk/phase3/includes/api/ApiMain.php
@@ -80,6 +80,7 @@
8181 'emailuser' => 'ApiEmailUser',
8282 'watch' => 'ApiWatch',
8383 'patrol' => 'ApiPatrol',
 84+ 'import' => 'ApiImport',
8485 );
8586
8687 /**
Index: trunk/phase3/includes/api/ApiBase.php
@@ -727,7 +727,16 @@
728728 'protect-invalidaction' => array('code' => 'protect-invalidaction', 'info' => "Invalid protection type ``\$1''"),
729729 'protect-invalidlevel' => array('code' => 'protect-invalidlevel', 'info' => "Invalid protection level ``\$1''"),
730730 'toofewexpiries' => array('code' => 'toofewexpiries', 'info' => "\$1 expiry timestamps were provided where \$2 were needed"),
731 -
 731+ 'cantimport' => array('code' => 'cantimport', 'info' => "You don't have permission to import pages"),
 732+ 'cantimport-upload' => array('code' => 'cantimport-upload', 'info' => "You don't have permission to import uploaded pages"),
 733+ 'importnofile' => array('code' => 'nofile', 'info' => "You didn't upload a file"),
 734+ 'importuploaderrorsize' => array('code' => 'filetoobig', 'info' => 'The file you uploaded is bigger than the maximum upload size'),
 735+ 'importuploaderrorpartial' => array('code' => 'partialupload', 'info' => 'The file was only partially uploaded'),
 736+ 'importuploaderrortemp' => array('code' => 'notempdir', 'info' => 'The temporary upload directory is missing'),
 737+ 'importcantopen' => array('code' => 'cantopenfile', 'info' => "Couldn't open the uploaded file"),
 738+ 'import-noarticle' => array('code' => 'badinterwiki', 'info' => 'Invalid interwiki title specified'),
 739+ 'importbadinterwiki' => array('code' => 'badinterwiki', 'info' => 'Invalid interwiki title specified'),
 740+ 'import-unknownerror' => array('code' => 'import-unknownerror', 'info' => "Unknown error on import: ``\$1''"),
732741
733742 // ApiEditPage messages
734743 'noimageredirect-anon' => array('code' => 'noimageredirect-anon', 'info' => "Anonymous users can't create image redirects"),
Index: trunk/phase3/includes/api/ApiQueryInfo.php
@@ -70,6 +70,7 @@
7171 'block' => array( 'ApiQueryInfo', 'getBlockToken' ),
7272 'unblock' => array( 'ApiQueryInfo', 'getUnblockToken' ),
7373 'email' => array( 'ApiQueryInfo', 'getEmailToken' ),
 74+ 'import' => array( 'ApiQueryInfo', 'getImportToken' ),
7475 );
7576 wfRunHooks('APIQueryInfoTokens', array(&$this->tokenFunctions));
7677 return $this->tokenFunctions;
@@ -167,7 +168,21 @@
168169 $cachedEmailToken = $wgUser->editToken();
169170 return $cachedEmailToken;
170171 }
 172+
 173+ public static function getImportToken($pageid, $title)
 174+ {
 175+ global $wgUser;
 176+ if(!$wgUser->isAllowed('import'))
 177+ return false;
171178
 179+ static $cachedImportToken = null;
 180+ if(!is_null($cachedImportToken))
 181+ return $cachedImportToken;
 182+
 183+ $cachedImportToken = $wgUser->editToken();
 184+ return $cachedImportToken;
 185+ }
 186+
172187 public function execute() {
173188
174189 global $wgUser;
Index: trunk/phase3/includes/AutoLoader.php
@@ -234,6 +234,8 @@
235235 'ApiFormatXml' => 'includes/api/ApiFormatXml.php',
236236 'ApiFormatYaml' => 'includes/api/ApiFormatYaml.php',
237237 'ApiHelp' => 'includes/api/ApiHelp.php',
 238+ 'ApiImport' => 'includes/api/ApiImport.php',
 239+ 'ApiImportReporter' => 'includes/api/ApiImport.php',
238240 'ApiLogin' => 'includes/api/ApiLogin.php',
239241 'ApiLogout' => 'includes/api/ApiLogout.php',
240242 'ApiMain' => 'includes/api/ApiMain.php',
Index: trunk/phase3/includes/WikiError.php
@@ -75,7 +75,17 @@
7676 $args = func_get_args();
7777 array_shift( $args );
7878 $this->mMessage = wfMsgReal( $message, $args, true );
 79+ $this->mMsgKey = $message;
 80+ $this->mMsgArgs = $args;
7981 }
 82+
 83+ function getMessageKey() {
 84+ return $this->mMsgKey;
 85+ }
 86+
 87+ function getMessageArgs() {
 88+ return $this->mMsgArgs;
 89+ }
8090 }
8191
8292 /**
Index: trunk/phase3/RELEASE-NOTES
@@ -160,6 +160,7 @@
161161 * (bug 17007) Added export and exportnowrap parameters to action=query
162162 * (bug 17326) BREAKING CHANGE: Changed output format for iiprop=metadata
163163 * (bug 17355) Added auwitheditsonly parameter to list=allusers
 164+* (bug 17007) Added action=import
164165
165166 === Languages updated in 1.15 ===
166167

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r46730* API: (bug 17007) Add export functionality to the API...catrope20:07, 2 February 2009

Status & tagging log