2014-04-11 Example of code to import xml into wiki using api import

edit

I spent a lot of time and can not figure out the right code to import wiki text into wiki. (Previously I exported this wiki text from wiki.) I try to use Ajax POST request with xhr.setRequestHeader('Content-Type', 'multipart/form-data; ... and get errors like the following

  • "<error code="badupload_xml" info="File upload param xml is not a file upload; be sure to use multipart/form-data for your POST and include a filename in the Content-Disposition header."/>".
  • <?xml version="1.0"?><api><error code="help" info="" xml:space="preserve">

Please, could somebody give ANY working PHP (and/or Ajax and/or HTML) code that uses mediawiki api "import function". Thanks in advance. Roman (t3531350@yahoo.com)

Answer to the question (working code):

<div>
    <fieldset>
	<legend>Загрузить XML-данные</legend>
        <form enctype="multipart/form-data" method="post" action="/api.php" action="submit" id="mw-import-upload-form">
            <p></p>
			
            <input type="hidden" name="action" value="import" />
            <input type="hidden" name="source" value="upload" />
            <table id="mw-import-table-upload">
		<tr>
                    <td class="mw-label">
                        <label for="xmlimport">Имя файла:</label>
                    </td>
                    <td class="mw-input">
                        <input id="xmlimport" type="file" value="" name="xml" />
                    </td>
                </tr>
                <tr>
                    <td class="mw-label">
                        <label for="mw-import-comment">Примечание:</label>
                    </td>
                    <td class="mw-input">
                        <input name="log-comment" size="50" value="" id="mw-import-comment" type="text" />
                    </td>
                </tr>
                <tr>
                    <td class="mw-label">
                        <label for="mw-interwiki-rootpage-upload">Корневая страница (необязательно):</label>
                    </td>
                    <td class="mw-input">
                        <input name="rootpage" size="50" value="" id="mw-interwiki-rootpage-upload" type="text" />
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td class="mw-submit">
                        <input type="submit" onclick = "readBlob()" value="Загрузить файл" />
                    </td>
                </tr>
            </table>
            <input type="hidden" value="51cdbc...ee63bc1d52d+\" name="token" />
        </form>
    </fieldset>
</div>


2013-09-19

edit
  • Is this really only for Version 1.15?
  • How would an import via API work in 1.20 or up?
  • How to handle the following error?
<?xml version="1.0"?><api><error code="nofile" info="You didn't upload a file" /></api>

workaround

edit

in import.php:

	static function newFromUpload( $fieldname = "xmlimport" ) {
		// instead of: $upload =& $_FILES[$fieldname];
		$upload= reset($_FILES);

somehow the $_FILES array does not have it's index by name any more at this point. $_FILES["xml"] is not found. With a dummy api.php it can be shown that $_FILES has the correct setting in the POST initially:

<?php
  // var_dump($_FILES); 
  echo("FILES=".print_r($_FILES)."\n");
	// echo("FILES - keys=".print_r(array_keys($_FILES))."\n");
?>

Can Import Json Output From External Wiki API Url?

edit

Can we import a json file? A json url? A live external wikipage? An XML file? A plain wikitext file? etc

Return to "Import" page.