r62268 - in /trunk/libmediawiki-api-perl: Changes META.yml debian/changelog debian/control debian/copyright lib/MediaWiki/API.pm

ansgar-guest at users.alioth.debian.org ansgar-guest at users.alioth.debian.org
Wed Sep 1 12:38:29 UTC 2010


Author: ansgar-guest
Date: Wed Sep  1 12:38:19 2010
New Revision: 62268

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=62268
Log:
* New upstream release
* debian/copyright: Refer to /usr/share/common-licenses/GPL-1; refer to
  "Debian systems" instead of "Debian GNU/Linux systems".
* Bump Standards-Version to 3.9.1.

Modified:
    trunk/libmediawiki-api-perl/Changes
    trunk/libmediawiki-api-perl/META.yml
    trunk/libmediawiki-api-perl/debian/changelog
    trunk/libmediawiki-api-perl/debian/control
    trunk/libmediawiki-api-perl/debian/copyright
    trunk/libmediawiki-api-perl/lib/MediaWiki/API.pm

Modified: trunk/libmediawiki-api-perl/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libmediawiki-api-perl/Changes?rev=62268&op=diff
==============================================================================
--- trunk/libmediawiki-api-perl/Changes (original)
+++ trunk/libmediawiki-api-perl/Changes Wed Sep  1 12:38:19 2010
@@ -1,4 +1,11 @@
 Revision history for MediaWiki-API
+
+0.35    2010-08-29
+        The module now is a little bit more intelligent about utf-8 strings. If you have the "use utf8" pragma set in
+        your code and pass utf-8 strings in the query, you no longer need to set the skip_encoding config option. The
+        option is only needed if you are passing utf-8 strings but the utf-8 flag is not set on the strings. Note that
+        this may not work correctly if the version of URI module is less than 1.36, and in that case you may well need
+        to specify skip_encoding still (The module does contains a workaround however for older URI versions).
 
 0.34    2010-07-25
         if skip_encoding encoding was used in conjuctions with use_http_get the querystring was not correctly encoded

Modified: trunk/libmediawiki-api-perl/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libmediawiki-api-perl/META.yml?rev=62268&op=diff
==============================================================================
--- trunk/libmediawiki-api-perl/META.yml (original)
+++ trunk/libmediawiki-api-perl/META.yml Wed Sep  1 12:38:19 2010
@@ -1,6 +1,6 @@
 --- #YAML:1.0
 name:               MediaWiki-API
-version:            0.34
+version:            0.35
 abstract:           Provides a Perl interface to the MediaWiki API (http://www.mediawiki.org/wiki/API)
 author:
     - Jools Wills <buzz at exotica.org.uk>

Modified: trunk/libmediawiki-api-perl/debian/changelog
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libmediawiki-api-perl/debian/changelog?rev=62268&op=diff
==============================================================================
--- trunk/libmediawiki-api-perl/debian/changelog (original)
+++ trunk/libmediawiki-api-perl/debian/changelog Wed Sep  1 12:38:19 2010
@@ -1,3 +1,12 @@
+libmediawiki-api-perl (0.35-1) unstable; urgency=low
+
+  * New upstream release
+  * debian/copyright: Refer to /usr/share/common-licenses/GPL-1; refer to
+    "Debian systems" instead of "Debian GNU/Linux systems".
+  * Bump Standards-Version to 3.9.1.
+
+ -- Ansgar Burchardt <ansgar at 43-1.org>  Wed, 01 Sep 2010 21:38:06 +0900
+
 libmediawiki-api-perl (0.34-1) unstable; urgency=low
 
   * New upstream release.

Modified: trunk/libmediawiki-api-perl/debian/control
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libmediawiki-api-perl/debian/control?rev=62268&op=diff
==============================================================================
--- trunk/libmediawiki-api-perl/debian/control (original)
+++ trunk/libmediawiki-api-perl/debian/control Wed Sep  1 12:38:19 2010
@@ -7,7 +7,7 @@
 Maintainer: Debian Perl Group <pkg-perl-maintainers at lists.alioth.debian.org>
 Uploaders: Ryan Niebur <ryan at debian.org>, Jonathan Yu <jawnsy at cpan.org>,
  Ansgar Burchardt <ansgar at 43-1.org>
-Standards-Version: 3.9.0
+Standards-Version: 3.9.1
 Homepage: http://search.cpan.org/dist/MediaWiki-API/
 Vcs-Svn: svn://svn.debian.org/pkg-perl/trunk/libmediawiki-api-perl/
 Vcs-Browser: http://svn.debian.org/viewsvn/pkg-perl/trunk/libmediawiki-api-perl/

Modified: trunk/libmediawiki-api-perl/debian/copyright
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libmediawiki-api-perl/debian/copyright?rev=62268&op=diff
==============================================================================
--- trunk/libmediawiki-api-perl/debian/copyright (original)
+++ trunk/libmediawiki-api-perl/debian/copyright Wed Sep  1 12:38:19 2010
@@ -16,8 +16,8 @@
  This program is free software; you can redistribute it and/or modify
  it under the terms of the Artistic License, which comes with Perl.
  .
- On Debian GNU/Linux systems, the complete text of the Artistic License
- can be found in `/usr/share/common-licenses/Artistic'
+ On Debian systems, the complete text of the Artistic License
+ can be found in `/usr/share/common-licenses/Artistic'.
 
 License: GPL-1+
  This program is free software; you can redistribute it and/or modify
@@ -25,8 +25,8 @@
  the Free Software Foundation; either version 1, or (at your option)
  any later version.
  .
- On Debian GNU/Linux systems, the complete text of the GNU General
- Public License can be found in `/usr/share/common-licenses/GPL'
+ On Debian systems, the complete text of version 1 of the GNU General
+ Public License can be found in `/usr/share/common-licenses/GPL-1'.
 
 License: GPL-3+
  This program is free software; you can redistribute it and/or modify
@@ -34,6 +34,5 @@
  the Free Software Foundation; either version 3, or (at your option)
  any later version.
  .
- On Debian GNU/Linux systems, the complete text of the GNU General
- Public License can be found in `/usr/share/common-licenses/GPL-3'
-
+ On Debian systems, the complete text of version 3 of the GNU General
+ Public License can be found in `/usr/share/common-licenses/GPL-3'.

Modified: trunk/libmediawiki-api-perl/lib/MediaWiki/API.pm
URL: http://svn.debian.org/wsvn/pkg-perl/trunk/libmediawiki-api-perl/lib/MediaWiki/API.pm?rev=62268&op=diff
==============================================================================
--- trunk/libmediawiki-api-perl/lib/MediaWiki/API.pm (original)
+++ trunk/libmediawiki-api-perl/lib/MediaWiki/API.pm Wed Sep  1 12:38:19 2010
@@ -7,11 +7,13 @@
 
 use LWP::UserAgent;
 use URI::Escape;
+use Encode;
 use JSON;
-use Encode;
 use Carp;
 
+# just for debugging the module
 # use Data::Dumper;
+# use Devel::Peek;
 
 use constant {
   ERR_NO_ERROR => 0,
@@ -40,11 +42,11 @@
 
 =head1 VERSION
 
-Version 0.34
-
-=cut
-
-our $VERSION  = "0.34";
+Version 0.35
+
+=cut
+
+our $VERSION  = "0.35";
 
 =head1 SYNOPSIS
 
@@ -185,7 +187,7 @@
 
   $self->{ua} = $ua;
 
-  my $json = JSON->new->utf8()->max_depth(10) ;
+  my $json = JSON->new->utf8(1);
   $self->{json} = $json;
 
   # initialise error code values
@@ -253,7 +255,7 @@
 
 =head2 MediaWiki::API->api( $query_hashref, $options_hashref )
 
-Call the MediaWiki API interface. Parameters are passed as a hashref which are described on the MediaWiki API page (http://www.mediawiki.org/wiki/API). returns a hashref with the results of the call or undef on failure with the error code and details stored in MediaWiki::API->{error}->{code} and MediaWiki::API->{error}->{details}. MediaWiki::API uses the LWP::UserAgent module to send the http requests to the MediaWiki API. After any API call, the response object returned by LWP::UserAgent is available in $mw->{response};
+Call the MediaWiki API interface. Parameters are passed as a hashref which are described on the MediaWiki API page (http://www.mediawiki.org/wiki/API). returns a hashref with the results of the call or undef on failure with the error code and details stored in MediaWiki::API->{error}->{code} and MediaWiki::API->{error}->{details}. MediaWiki::API uses the LWP::UserAgent module to send the http requests to the MediaWiki API. After any API call, the response object returned by LWP::UserAgent is available in $mw->{response}. This function will NOT modify the input query_hashref in any way.
 
   binmode STDOUT, ':utf8';
 
@@ -276,15 +278,32 @@
     print "$_->{'*'}\n";
   }
 
-Parameters are encoded from perl strings to UTF-8 to be passed to Mediawiki automatically, which is normally what you would want. In case for any reason your parameters are already in UTF-8 you can skip the encoding by passing an option skip_encoding => 1 in the $options_hash. For example:
-
-  # $data already contains utf-8 encoded wikitext
-  my $ref = $mw->api( { action => 'parse', text => $data }, { skip_encoding => 1 } );
+MediaWiki's API uses UTF-8 and any 8 bit character string parameters are encoded automatically by the API call. If your parameters are already in UTF-8 this will be detected and the encoding will be skipped. If your parameters for some reason contain UTF-8 data but no UTF-8 flag is set (i.e. you did not use the "use utf8;" pragma) you should prevent re-encoding by passing an option skip_encoding => 1 in the $options_hash. For example:
+
+ my $mw = MediaWiki::API->new();
+ $mw->{config}->{api_url} = 'http://fr.wiktionary.org/w/api.php';
+
+ my $query = {action => 'query',
+   list => 'categorymembers',
+   cmlimit => 'max'};
+
+ $query->{cmtitle} ="Cat\x{e9}gorie:moyen_fran\x{e7}ais"; # latin1 string
+ $mw->list ( $query ); # ok 
+
+ $query->{cmtitle} = "Cat". pack("U", 0xe9)."gorie:moyen_fran".pack("U",0xe7)."ais"; # unicode string
+ $mw->list ( $query ); # ok
+
+ $query->{cmtitle} ="Cat\x{c3}\x{a9}gorie:moyen_fran\x{c3}\x{a7}ais";  # unicode data without utf-8 flag
+ # $mw->list ( $query ); # NOT OK
+ $mw->list ( $query, {skip_encoding => 1} ); # ok
 
 =cut
 
 sub api {
   my ($self, $query, $options) = @_;
+
+  return $self->_error(ERR_CONFIG,"You need to give the URL to the mediawiki API php.")
+    unless $self->{config}->{api_url};
 
   my $get_actions = {
     'query' => 1,
@@ -293,19 +312,19 @@
     'paraminfo' => 1
   };
 
-  unless ( $options->{skip_encoding} ) {
-    $self->_encode_hashref_utf8($query);
-  }
-
   my $retries = $self->{config}->{retries};
   my $maxlagretries = $self->{config}->{max_lag_retries};
 
+  $self->_encode_hashref_utf8($query, $options->{skip_encoding});
   $query->{maxlag} = $self->{config}->{max_lag} if defined $self->{config}->{max_lag}; 
-
-  return $self->_error(ERR_CONFIG,"You need to give the URL to the mediawiki API php.")
-    unless $self->{config}->{api_url};
-
   $query->{format}='json';
+
+  # if the config is set to use GET we need to contruct a querystring. some actions are "POST" only -
+  # edit, move, action = rollback, action = undelete, action = 
+  my $querystring = '';
+  if ( $self->{config}->{use_http_get} && defined $get_actions->{$query->{action}} ){
+    $querystring = _make_querystring( $query );
+  }
 
   my $ref;
   while (1) {
@@ -318,12 +337,10 @@
         sleep $self->{config}->{retry_delay};
       }
 
-      # if the config is set to use GET we need to contruct the querystring. some actions are "POST" only -
-      # edit, move, action = rollback, action = undelete, action = 
       my $response;
-      if ( $self->{config}->{use_http_get} && defined $get_actions->{$query->{action}} ) {
-        my $qs = _make_querystring( $query, $options->{skip_encoding} );
-        $response = $self->{ua}->get( $self->{config}->{api_url} . $qs );
+      # if we are using the get method ($querystring is set above)
+      if ( $querystring ) {
+        $response = $self->{ua}->get( $self->{config}->{api_url} . $querystring );
       } else {
         $response = $self->{ua}->post( $self->{config}->{api_url}, $query );
       }
@@ -415,7 +432,7 @@
 
 =head2 MediaWiki::API->edit( $query_hashref, $options_hashref )
 
-A helper function for doing edits using the MediaWiki API. Parameters are passed as a hashref which are described on the MediaWiki API editing page (http://www.mediawiki.org/wiki/API:Changing_wiki_content). Note that you need $wgEnableWriteAPI = true in your LocalSettings.php to use these features.
+A helper function for doing edits using the MediaWiki API. Parameters are passed as a hashref which are described on the MediaWiki API editing page (http://www.mediawiki.org/wiki/API:Changing_wiki_content). Note that you need $wgEnableWriteAPI = true in your LocalSettings.php to use these features. This function will modify the input hashref.
 
 Currently only
 
@@ -539,7 +556,7 @@
 
 =head2 MediaWiki::API->list( $query_hashref, $options_hashref )
 
-A helper function for getting lists using the MediaWiki API. Parameters are passed as a hashref which are described on the MediaWiki API editing page (http://www.mediawiki.org/wiki/API:Query_-_Lists).
+A helper function for getting lists using the MediaWiki API. Parameters are passed as a hashref which are described on the MediaWiki API editing page (http://www.mediawiki.org/wiki/API:Query_-_Lists). This function modifies the input query_hashref.
 
 This function will return a reference to an array of hashes or undef on failure. It handles getting lists of data from the MediaWiki api, continuing the request with another connection if needed. The options_hashref currently has three parameters:
 
@@ -724,26 +741,37 @@
   return $response->decoded_content;
 }
 
-# encodes a hash (passed by reference) to utf-8
+# returns a copy of a hash (passed by reference) encoded to utf-8
 # used to encode parameters before being passed to the api
 sub _encode_hashref_utf8 {
-  my ($self, $ref) = @_;
+  my $uriver = $URI::VERSION;
+  my ($self, $ref, $skipenc) = @_;
   for my $key ( keys %{$ref} ) {
-    $ref->{$key} = encode_utf8( $ref->{$key} ) if defined $ref->{$key};
-  }
-}
-
-# creates a querystring from a hashref
+    # skip to next item if no value defined
+    next unless defined $ref->{$key};
+    # if we don't want to skip encoding and the item doesn't already have the utf8 flag set, or we are using
+    # an older version of URI.pm that doesn't handle the encoding correctly then we need to encode to utf8
+    if ( ! $skipenc && ( ! utf8::is_utf8($ref->{$key}) || $URI::VERSION < 1.36) ) {
+      $ref->{$key} = Encode::encode_utf8($ref->{$key});
+    }
+    # turn on the utf8 flag so the URI module knows what to do with it (and so we don't re-encode when we don't need to)
+    # if we are using a new enough version of URI that will handle the encoding correctly.
+    # so what you get is :
+    # URI <  1.36 - utf8 encoded string without utf8 flag (works)
+    # URI >= 1.36 - utf8 encoded string with utf8 flag (works)
+    Encode::_utf8_on($ref->{$key}) if $URI::VERSION >= 1.36;
+  }
+
+  return $ref;
+}
+
+# creates a querystring from a utf-8 hashref
 sub _make_querystring {
-  my ($ref, $skipenc) = @_;
+  my ($ref) = @_;
   my @qs = ();
   my $keyval;
   for my $key ( keys %{$ref} ) {
-    if ( $skipenc ) {
-      $keyval = uri_escape_utf8($key) . '=' . uri_escape_utf8($ref->{$key});
-    } else {
-      $keyval = uri_escape($key) . '=' . uri_escape($ref->{$key});
-    }
+    $keyval = uri_escape_utf8($key) . '=' . uri_escape_utf8($ref->{$key});
     push(@qs, $keyval);
   }
   return '?' . join('&', at qs);




More information about the Pkg-perl-cvs-commits mailing list