Hi, I'm experimenting with the api, I'm interested in create a bot to upload images. This is my code (I'm using the Snoopy.php class):
# get token $getToken = array( 'action' => 'query', 'prop' => 'info', 'intoken' => 'edit', 'titles' => 'Wiki1', 'format' => 'php');
echo "<br/>"; if(!$snoopy->submit($api_url, $getToken)) echo "TOKEN DENIED"; else echo "GOT TOKEN"; echo "<br/>";
$array_ = unserialize($snoopy->results);
$myFile = fopen("/var/www/dib.jpg","r"); $returned = fread($myFile, 500); while($devuelto != false) { $fileContent .= $returned; $returned = fread($myFile, 500); }
#upload $uploadVars['format'] = 'php'; $uploadVars['action'] = 'upload'; $uploadVars['filename'] = 'wikip.png'; $uploadVars['url'] = 'http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png '; $uploadVars['file'] = $fileContent; $uploadVars['token'] = $array_["query"]["pages"][key($array_["query"]["pages"])]["edittoken"];
$snoopy->set_submit_multipart(); echo "<br/>"; if(!$snoopy->submit($api_url, $uploadVars)) echo "UPLOAD DENIED"; else echo "UPLOAD OK"; echo "<br/>";
$array_ = unserialize($snoopy->results); echo "<br/>-----------<br/>"; print_r($array_); echo "<br/>.....................<br/>"; echo $snoopy->results; echo "<br/>-----------";
The execution of this code produce the output bellow: GOT TOKEN
UPLOAD OK
----------- ..................... -----------
It's supposed I should receive something like this in the last step:
<api> <upload result="Success" filename="Test.txt"> <imageinfo timestamp="2000-01-01T00:00:00Z" user="127.0.0.1" anon="" size="1000" width="0" height="0" url="http://localhost/images/3/35/Test.txt" descriptionurl="http://localhost/index.php/File:Test.txt" comment="" sha1="b8f32ebbf9512d8641d7e72c86614c2cee3e8108" metadata="" mime="text/plain" bitdepth="0" /> </upload> </api>
In fact, nothing really happen, no file is uploaded (the file exists).
Some useful information: PHP 5.3.3 Ubuntu10.10 mediawiki1.16.0
2011/2/9 Juan Rodríguez jmrgjuan@gmail.com:
In fact, nothing really happen, no file is uploaded (the file exists).
What error message is returned?
In fact, nothing really happen, no file is uploaded (the file exists).
What error message is returned? _______________________________________________
There is no response to the last request (the response should appear
between the last dotted lines), nothing happens indeed. Could anyone advise me about which of the bot would be good for easy uploading?
There is a bug in my first message: $uploadVars['url'] = 'http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png '; //$uploadVars['file'] = $fileContent;
You must not use both params. I proved but it isn't still working. I have another problem: I installed LAMP and there is no support to curl by default, so I might have to compile from source... I still think that code must work with minimal modifications. I suspect the problem is the wrapper: According with wikimedia doc, it must be a multipart/form-data request, and I'm not sure if I'm doing the last step correctly.
2011/2/10 Juan Rodríguez jmrgjuan@gmail.com:
There is a bug in my first message: $uploadVars['url'] = 'http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png'; //$uploadVars['file'] = $fileContent;
You must not use both params. I proved but it isn't still working. I have another problem: I installed LAMP and there is no support to curl by default, so I might have to compile from source... I still think that code must work with minimal modifications. I suspect the problem is the wrapper: According with wikimedia doc, it must be a multipart/form-data request, and I'm not sure if I'm doing the last step correctly.
If you're using upload-by-url you need to have $wgAllowCopyUploads=true and you're user needs to have the proper userright (can't remember which one, look it up in the docs). You don't need multipart/form-data for upload-by-url, only for regular file uploads. Also, for regular file uploads, your file field needs to be of the file-type, that is it requires the filename attributes and perhaps some others.
Bryan
*If you're using upload-by-url you need to have $wgAllowCopyUploads=true and you're user needs to have the proper userright (can't remember which one, look it up in the docs). You don't need multipart/form-data for upload-by-url, only for regular file uploads. Also, for regular file uploads, your file field needs to be of the file-type, that is it requires the filename attributes and perhaps some others.*
I have this in my LocalSettings:
$wgEnableUploads = true; $wgFileExtensions = array('png', 'gif', 'jpg', 'jpeg'); $wgAllowCopyUploads = true; $wgGroupPermissions['user']['upload_by_url'] = true; $wgGroupPermissions['user']['upload'] = true;
I have proved to upload from url with and without multipart/form-data and also from file. The problems persists. I just wanna create a simple php code to upload to my wiki, how can this be such an annoying task? :-(
2011/2/10 Juan Rodríguez jmrgjuan@gmail.com:
If you're using upload-by-url you need to have $wgAllowCopyUploads=true and you're user needs to have the proper userright (can't remember which one, look it up in the docs). You don't need multipart/form-data for upload-by-url, only for regular file uploads. Also, for regular file uploads, your file field needs to be of the file-type, that is it requires the filename attributes and perhaps some others.
I have this in my LocalSettings: $wgEnableUploads = true; $wgFileExtensions = array('png', 'gif', 'jpg', 'jpeg'); $wgAllowCopyUploads = true; $wgGroupPermissions['user']['upload_by_url'] = true; $wgGroupPermissions['user']['upload'] = true;
I have proved to upload from url with and without multipart/form-data and also from file. The problems persists. I just wanna create a simple php code to upload to my wiki, how can this be such an annoying task? :-(
If you keep on receiving a blank page, this usually means that a PHP error occurred. Check your apache error log.
Bryan
If you keep on receiving a blank page, this usually means that a PHP
error occurred. Check your apache error log.
OK, this is interesting. I found this in the apache log:
[Thu Feb 10 22:58:47 2011] [error] [client ::1] PHP Warning: Missing argument 3 for UploadFromUrl::initialize(), called in /var/www/mediawiki2/includes/api/ApiUpload.php on line 93 and defined in /var/www/mediawiki2/includes/upload/UploadFromUrl.php on line 35 [Thu Feb 10 22:58:47 2011] [error] [client ::1] PHP Fatal error: Call to undefined function curl_init() in /var/www/mediawiki2/includes/upload/UploadFromUrl.php on line 102
The first message show me a bug in the code, because the file UploadFromUrl.php defines the function: *public function initialize( $name, $url, $na, $nb = false )* but never use the two last parameter inside the code: global $wgTmpDirectory; $localFile = tempnam( $wgTmpDirectory, 'WEBUPLOAD' ); $this->initializePathInfo( $name, $localFile, 0, true ); $this->mUrl = trim( $url );
Hopefully, it's very easy to fix :-)
The second message is about curl, as I suspected, it's not working. Could I add to a precompiled installation of php?
2011/2/10 Juan Rodríguez jmrgjuan@gmail.com:
The second message is about curl, as I suspected, it's not working. Could I add to a precompiled installation of php?
Did you enable the curl extension in php.ini?
Bryan
2011/2/10 Bryan Tong Minh bryan.tongminh@gmail.com
2011/2/10 Juan Rodríguez jmrgjuan@gmail.com:
The second message is about curl, as I suspected, it's not working. Could
I
add to a precompiled installation of php?
Did you enable the curl extension in php.ini?
No I didn't, but I think it's not necessary because when I execute
phpinfo() I see: *Additional .ini files parsed* /etc/php5/apache2/conf.d/curl.ini, /etc/php5/apache2/conf.d/gd.ini...
Ok, so curl is ok now, the problem is upload from a local file, I proved using the path of the file as a sting and the file content as you see in the code, but any of both worked. I get the message:
*Array ( [error] => Array ( [code] => missingparam [info] => One of the parameters sessionkey, file, url is required ) ) *
What's wrong then? (I have no errors in error.log now).
The second message is about curl, as I suspected, it's not working. Could I add to a precompiled installation of php?
I got it! --> I fixed the params bug: just putting: *public function initialize( $name, $url/*, $na, $nb = false*/ ) {* --> I added curl support by doing: sudo apt-get install *curl* libcurl3 libcurl3-dev php5-*curl * sudo /etc/init.d/apache2 restart
Now the code work fine when uploading from url. I cannot still upload from file, but think I'll fix soon. Thank you a lot for your help!
Juan Rodríguez escribió:
If you keep on receiving a blank page, this usually means that a PHP
error occurred. Check your apache error log.
OK, this is interesting. I found this in the apache log:
[Thu Feb 10 22:58:47 2011] [error] [client ::1] PHP Warning: Missing argument 3 for UploadFromUrl::initialize(), called in /var/www/mediawiki2/includes/api/ApiUpload.php on line 93 and defined in /var/www/mediawiki2/includes/upload/UploadFromUrl.php on line 35 [Thu Feb 10 22:58:47 2011] [error] [client ::1] PHP Fatal error: Call to undefined function curl_init() in /var/www/mediawiki2/includes/upload/UploadFromUrl.php on line 102
The first message show me a bug in the code, because the file UploadFromUrl.php defines the function: /public function initialize( $name, $url, $na, $nb = false )/ but never use the two last parameter inside the code: global $wgTmpDirectory; $localFile = tempnam( $wgTmpDirectory, 'WEBUPLOAD' ); $this->initializePathInfo( $name, $localFile, 0, true ); $this->mUrl = trim( $url );
Hopefully, it's very easy to fix :-)
The second message is about curl, as I suspected, it's not working. Could I add to a precompiled installation of php?
Open php.ini and search for extension_dir option. Change it to: extension_dir = "ext" (it may be there commented, or already right)
Then continue down to the list of extension= Uncomment extension=curl.dll
That should install curl in a normal php binary. Hopefully it won't be different in LAMP. You may need to restart apache after changing php.ini
Platonides wrote:
Open php.ini and search for extension_dir option. Change it to: extension_dir = "ext" (it may be there commented, or already right)
Then continue down to the list of extension= Uncomment extension=curl.dll
That should install curl in a normal php binary. Hopefully it won't be different in LAMP. You may need to restart apache after changing php.ini
Sorry, I read WAMP....
2011/2/10 Juan Rodríguez jmrgjuan@gmail.com:
I installed LAMP and there is no support to curl by default, so I might have to compile from source...
Unless LAMP does not use the standard PHP windows binaries, it does have curl support. See the comments in the documentation: http://nl.php.net/manual/en/curl.installation.php
Bryan
Snoopy is horribly out of date and no longer maintainted, as far as I know. I would reccommend using a framework that works with API. There's a list at http://enwp.org/WP:MAKEBOT.
-X!
On Feb 9, 2011, at 11:48 AM, Juan Rodríguez wrote:
Hi, I'm experimenting with the api, I'm interested in create a bot to upload images. This is my code (I'm using the Snoopy.php class):
# get token $getToken = array( 'action' => 'query', 'prop' => 'info', 'intoken' => 'edit', 'titles' => 'Wiki1', 'format' => 'php');
echo "<br/>"; if(!$snoopy->submit($api_url, $getToken)) echo "TOKEN DENIED"; else echo "GOT TOKEN"; echo "<br/>"; $array_ = unserialize($snoopy->results);
$myFile = fopen("/var/www/dib.jpg","r"); $returned = fread($myFile, 500); while($devuelto != false) { $fileContent .= $returned; $returned = fread($myFile, 500); }
#upload $uploadVars['format'] = 'php'; $uploadVars['action'] = 'upload'; $uploadVars['filename'] = 'wikip.png'; $uploadVars['url'] = 'http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png'; $uploadVars['file'] = $fileContent; $uploadVars['token'] = $array_["query"]["pages"][key($array_["query"]["pages"])]["edittoken"];
$snoopy->set_submit_multipart(); echo "<br/>"; if(!$snoopy->submit($api_url, $uploadVars)) echo "UPLOAD DENIED"; else echo "UPLOAD OK"; echo "<br/>";
$array_ = unserialize($snoopy->results); echo "<br/>-----------<br/>"; print_r($array_); echo "<br/>.....................<br/>"; echo $snoopy->results; echo "<br/>-----------";
The execution of this code produce the output bellow: GOT TOKEN
UPLOAD OK
.....................
It's supposed I should receive something like this in the last step:
<api> <upload result="Success" filename="Test.txt">
<imageinfo timestamp="2000-01-01T00:00:00Z" user="127.0.0.1" anon="" size="1000" width="0" height="0" url="http://localhost/images/3/35/Test.txt" descriptionurl="http://localhost/index.php/File:Test.txt" comment="" sha1="b8f32ebbf9512d8641d7e72c86614c2cee3e8108" metadata="" mime="text/plain" bitdepth="0" />
</upload> </api>
In fact, nothing really happen, no file is uploaded (the file exists).
Some useful information: PHP 5.3.3 Ubuntu10.10 mediawiki1.16.0
Mediawiki-api mailing list Mediawiki-api@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
mediawiki-api@lists.wikimedia.org