2010-12-09 12:31:50 +00:00
|
|
|
/* This file is (c) 2008-2011 Konstantin Isakov <ikm@goldendict.org>
|
2009-03-26 19:00:08 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "mediawiki.hh"
|
2009-04-18 17:20:12 +00:00
|
|
|
#include "wstring_qt.hh"
|
2009-03-26 19:00:08 +00:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QtXml>
|
2009-05-05 08:56:46 +00:00
|
|
|
#include <list>
|
2011-06-19 18:50:11 +00:00
|
|
|
#include "dprintf.hh"
|
2012-01-09 13:48:27 +00:00
|
|
|
#include "audiolink.hh"
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
namespace MediaWiki {
|
|
|
|
|
|
|
|
using namespace Dictionary;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MediaWikiDictionary: public Dictionary::Class
|
|
|
|
{
|
|
|
|
string name;
|
|
|
|
QString url;
|
|
|
|
QNetworkAccessManager & netMgr;
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
MediaWikiDictionary( string const & id, string const & name_,
|
|
|
|
QString const & url_,
|
|
|
|
QNetworkAccessManager & netMgr_ ):
|
|
|
|
Dictionary::Class( id, vector< string >() ),
|
|
|
|
name( name_ ),
|
|
|
|
url( url_ ),
|
|
|
|
netMgr( netMgr_ )
|
|
|
|
{
|
|
|
|
}
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual string getName() throw()
|
|
|
|
{ return name; }
|
|
|
|
|
|
|
|
virtual map< Property, string > getProperties() throw()
|
|
|
|
{ return map< Property, string >(); }
|
|
|
|
|
|
|
|
virtual unsigned long getArticleCount() throw()
|
|
|
|
{ return 0; }
|
|
|
|
|
|
|
|
virtual unsigned long getWordCount() throw()
|
|
|
|
{ return 0; }
|
|
|
|
|
2009-04-21 20:09:02 +00:00
|
|
|
virtual QIcon getIcon() throw()
|
|
|
|
{ return QIcon(":/icons/icon32_wiki.png"); }
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual sptr< WordSearchRequest > prefixMatch( wstring const &,
|
|
|
|
unsigned long maxResults ) throw( std::exception );
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
virtual sptr< DataRequest > getArticle( wstring const &, vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
2009-03-26 19:00:08 +00:00
|
|
|
throw( std::exception );
|
|
|
|
};
|
|
|
|
|
|
|
|
class MediaWikiWordSearchRequest: public MediaWikiWordSearchRequestSlots
|
|
|
|
{
|
|
|
|
sptr< QNetworkReply > netReply;
|
|
|
|
bool livedLongEnough; // Indicates that the request has lived long enough
|
|
|
|
// to be destroyed prematurely. Used to prevent excessive
|
|
|
|
// network loads when typing search terms rapidly.
|
|
|
|
bool isCancelling;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
MediaWikiWordSearchRequest( wstring const &,
|
|
|
|
QString const & url, QNetworkAccessManager & mgr );
|
|
|
|
|
|
|
|
~MediaWikiWordSearchRequest();
|
|
|
|
|
|
|
|
virtual void cancel();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual void timerEvent( QTimerEvent * );
|
|
|
|
|
|
|
|
private:
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
virtual void downloadFinished();
|
|
|
|
};
|
|
|
|
|
|
|
|
MediaWikiWordSearchRequest::MediaWikiWordSearchRequest( wstring const & str,
|
|
|
|
QString const & url,
|
|
|
|
QNetworkAccessManager & mgr ):
|
|
|
|
livedLongEnough( false ), isCancelling( false )
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "request begin\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
QUrl reqUrl( url + "/api.php?action=query&list=allpages&aplimit=40&format=xml" );
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
reqUrl.addQueryItem( "apfrom", gd::toQString( str ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
netReply = mgr.get( QNetworkRequest( reqUrl ) );
|
|
|
|
|
|
|
|
connect( netReply.get(), SIGNAL( finished() ),
|
|
|
|
this, SLOT( downloadFinished() ) );
|
|
|
|
|
|
|
|
// We start a timer to postpone early destruction, so a rapid type won't make
|
|
|
|
// unnecessary network load
|
|
|
|
startTimer( 200 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaWikiWordSearchRequest::timerEvent( QTimerEvent * ev )
|
|
|
|
{
|
|
|
|
killTimer( ev->timerId() );
|
|
|
|
livedLongEnough = true;
|
|
|
|
|
|
|
|
if ( isCancelling )
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaWikiWordSearchRequest::~MediaWikiWordSearchRequest()
|
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "request end\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaWikiWordSearchRequest::cancel()
|
|
|
|
{
|
|
|
|
// We either finish it in place, or in the timer handler
|
|
|
|
isCancelling = true;
|
|
|
|
|
|
|
|
if ( netReply.get() )
|
|
|
|
netReply.reset();
|
|
|
|
|
|
|
|
if ( livedLongEnough )
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
else
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF("not long enough\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaWikiWordSearchRequest::downloadFinished()
|
|
|
|
{
|
|
|
|
if ( isCancelling || isFinished() ) // Was cancelled
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( netReply->error() == QNetworkReply::NoError )
|
|
|
|
{
|
|
|
|
QDomDocument dd;
|
|
|
|
|
|
|
|
QString errorStr;
|
|
|
|
int errorLine, errorColumn;
|
|
|
|
|
|
|
|
if ( !dd.setContent( netReply.get(), false, &errorStr, &errorLine, &errorColumn ) )
|
|
|
|
{
|
|
|
|
setErrorString( QString( tr( "XML parse error: %1 at %2,%3" ).
|
|
|
|
arg( errorStr ).arg( errorLine ).arg( errorColumn ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDomNode pages = dd.namedItem( "api" ).namedItem( "query" ).namedItem( "allpages" );
|
|
|
|
|
|
|
|
if ( !pages.isNull() )
|
|
|
|
{
|
|
|
|
QDomNodeList nl = pages.toElement().elementsByTagName( "p" );
|
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
for( unsigned x = 0; x < nl.length(); ++x )
|
2009-04-18 17:20:12 +00:00
|
|
|
matches.push_back( gd::toWString( nl.item( x ).toElement().attribute( "title" ) ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "done.\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
setErrorString( netReply->errorString() );
|
|
|
|
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MediaWikiArticleRequest: public MediaWikiDataRequestSlots
|
|
|
|
{
|
2009-05-05 08:56:46 +00:00
|
|
|
typedef std::list< std::pair< sptr< QNetworkReply >, bool > > NetReplies;
|
|
|
|
NetReplies netReplies;
|
2009-03-26 19:00:08 +00:00
|
|
|
QString url;
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
public:
|
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
MediaWikiArticleRequest( wstring const & word, vector< wstring > const & alts,
|
2012-01-09 13:48:27 +00:00
|
|
|
QString const & url, QNetworkAccessManager & mgr,
|
2012-01-11 12:14:16 +00:00
|
|
|
string const & wikidictID );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
virtual void cancel();
|
|
|
|
|
|
|
|
private:
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
void addQuery( QNetworkAccessManager & mgr, wstring const & word );
|
|
|
|
|
|
|
|
virtual void requestFinished( QNetworkReply * );
|
2012-01-09 13:48:27 +00:00
|
|
|
string dictID;
|
2009-03-26 19:00:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void MediaWikiArticleRequest::cancel()
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaWikiArticleRequest::MediaWikiArticleRequest( wstring const & str,
|
2009-05-05 08:56:46 +00:00
|
|
|
vector< wstring > const & alts,
|
2009-03-26 19:00:08 +00:00
|
|
|
QString const & url_,
|
2012-01-09 13:48:27 +00:00
|
|
|
QNetworkAccessManager & mgr,
|
2012-01-11 12:14:16 +00:00
|
|
|
string const & wikidictID ):
|
|
|
|
url( url_ ), dictID( wikidictID )
|
2009-05-05 08:56:46 +00:00
|
|
|
{
|
|
|
|
connect( &mgr, SIGNAL( finished( QNetworkReply * ) ),
|
2009-10-10 17:37:48 +00:00
|
|
|
this, SLOT( requestFinished( QNetworkReply * ) ),
|
|
|
|
Qt::QueuedConnection );
|
2009-05-05 08:56:46 +00:00
|
|
|
|
|
|
|
addQuery( mgr, str );
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < alts.size(); ++x )
|
|
|
|
addQuery( mgr, alts[ x ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaWikiArticleRequest::addQuery( QNetworkAccessManager & mgr,
|
|
|
|
wstring const & str )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "Requesting article %ls\n", str.c_str() );
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
QUrl reqUrl( url + "/api.php?action=parse&prop=text|revid&format=xml&redirects" );
|
|
|
|
|
2009-04-18 17:20:12 +00:00
|
|
|
reqUrl.addQueryItem( "page", gd::toQString( str ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
sptr< QNetworkReply > netReply = mgr.get( QNetworkRequest( reqUrl ) );
|
|
|
|
|
|
|
|
netReplies.push_back( std::make_pair( netReply, false ) );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "Finished.\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
if ( isFinished() ) // Was cancelled
|
|
|
|
return;
|
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
// Find this reply
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for( NetReplies::iterator i = netReplies.begin(); i != netReplies.end(); ++i )
|
|
|
|
{
|
|
|
|
if ( i->first.get() == r )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2009-05-05 08:56:46 +00:00
|
|
|
i->second = true; // Mark as finished
|
|
|
|
found = true;
|
|
|
|
break;
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-05-05 08:56:46 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
if ( !found )
|
|
|
|
{
|
|
|
|
// Well, that's not our reply, don't do anything
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool updated = false;
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
for( ; netReplies.size() && netReplies.front().second; netReplies.pop_front() )
|
|
|
|
{
|
|
|
|
sptr< QNetworkReply > netReply = netReplies.front().first;
|
|
|
|
|
|
|
|
if ( netReply->error() == QNetworkReply::NoError )
|
|
|
|
{
|
|
|
|
QDomDocument dd;
|
|
|
|
|
|
|
|
QString errorStr;
|
|
|
|
int errorLine, errorColumn;
|
|
|
|
|
|
|
|
if ( !dd.setContent( netReply.get(), false, &errorStr, &errorLine, &errorColumn ) )
|
|
|
|
{
|
|
|
|
setErrorString( QString( tr( "XML parse error: %1 at %2,%3" ).
|
|
|
|
arg( errorStr ).arg( errorLine ).arg( errorColumn ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QDomNode parseNode = dd.namedItem( "api" ).namedItem( "parse" );
|
|
|
|
|
|
|
|
if ( !parseNode.isNull() && parseNode.toElement().attribute( "revid" ) != "0" )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2009-05-05 08:56:46 +00:00
|
|
|
QDomNode textNode = parseNode.namedItem( "text" );
|
|
|
|
|
|
|
|
if ( !textNode.isNull() )
|
2009-03-26 19:00:08 +00:00
|
|
|
{
|
2009-05-05 08:56:46 +00:00
|
|
|
QString articleString = textNode.toElement().text();
|
|
|
|
|
|
|
|
QUrl wikiUrl( url );
|
|
|
|
wikiUrl.setPath( "" );
|
|
|
|
|
|
|
|
// Update any special index.php pages to be absolute
|
|
|
|
articleString.replace( QRegExp( "<a\\shref=\"(/(\\w*/)*index.php\\?)" ),
|
|
|
|
QString( "<a href=\"%1\\1" ).arg( wikiUrl.toString() ) );
|
|
|
|
// Replace the href="/foo/bar/Baz" to just href="Baz".
|
2009-05-18 18:46:18 +00:00
|
|
|
articleString.replace( QRegExp( "<a\\shref=\"/([\\w\\.]*/)*" ), "<a href=\"" );
|
2009-05-05 08:56:46 +00:00
|
|
|
|
2011-10-22 11:52:52 +00:00
|
|
|
// Add "http:" to image source urls
|
|
|
|
articleString.replace( " src=\"//", " src=\"http://" );
|
2012-01-09 13:48:27 +00:00
|
|
|
//fix audio
|
|
|
|
articleString.replace( QRegExp("<button\\s+[^>]*(upload\\.wikimedia\\.org/wikipedia/commons/[^\"'&]*\\.ogg)[^>]*>\\s*<[^<]*</button>"),
|
|
|
|
QString::fromStdString(addAudioLink("\"http://\\1\"",this->dictID)+
|
|
|
|
"<a href=\"http://\\1\"><img src=\"qrcx://localhost/icons/playsound.png\" border=\"0\" alt=\"Play\"></a>"));
|
2009-05-05 08:56:46 +00:00
|
|
|
// In those strings, change any underscores to spaces
|
|
|
|
for( ; ; )
|
|
|
|
{
|
|
|
|
QString before = articleString;
|
|
|
|
articleString.replace( QRegExp( "<a href=\"([^/:\">#]*)_" ), "<a href=\"\\1 " );
|
|
|
|
|
|
|
|
if ( articleString == before )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray articleBody = articleString.toUtf8();
|
|
|
|
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "Article body after: %s\n", articleBody.data() );
|
2009-05-05 08:56:46 +00:00
|
|
|
|
|
|
|
articleBody.prepend( "<div class=\"mwiki\">" );
|
|
|
|
articleBody.append( "</div>" );
|
|
|
|
|
|
|
|
Mutex::Lock _( dataMutex );
|
|
|
|
|
|
|
|
size_t prevSize = data.size();
|
|
|
|
|
|
|
|
data.resize( prevSize + articleBody.size() );
|
|
|
|
|
|
|
|
memcpy( &data.front() + prevSize, articleBody.data(), articleBody.size() );
|
|
|
|
|
|
|
|
hasAnyData = true;
|
|
|
|
|
|
|
|
updated = true;
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-19 18:50:11 +00:00
|
|
|
DPRINTF( "done.\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-05-05 08:56:46 +00:00
|
|
|
else
|
|
|
|
setErrorString( netReply->errorString() );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-05 08:56:46 +00:00
|
|
|
if ( netReplies.empty() )
|
|
|
|
finish();
|
|
|
|
else
|
|
|
|
if ( updated )
|
|
|
|
update();
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr< WordSearchRequest > MediaWikiDictionary::prefixMatch( wstring const & word,
|
|
|
|
unsigned long maxResults )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
2010-03-30 16:52:44 +00:00
|
|
|
if ( word.size() > 80 )
|
|
|
|
{
|
|
|
|
// Don't make excessively large queries -- they're fruitless anyway
|
|
|
|
|
|
|
|
return new WordSearchRequestInstant();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return new MediaWikiWordSearchRequest( word, url, netMgr );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
sptr< DataRequest > MediaWikiDictionary::getArticle( wstring const & word,
|
|
|
|
vector< wstring > const & alts,
|
|
|
|
wstring const & )
|
2009-03-26 19:00:08 +00:00
|
|
|
throw( std::exception )
|
|
|
|
{
|
2010-03-30 16:52:44 +00:00
|
|
|
if ( word.size() > 80 )
|
|
|
|
{
|
|
|
|
// Don't make excessively large queries -- they're fruitless anyway
|
|
|
|
|
|
|
|
return new DataRequestInstant( false );
|
|
|
|
}
|
|
|
|
else
|
2012-01-09 13:48:27 +00:00
|
|
|
return new MediaWikiArticleRequest( word, alts, url, netMgr,this->getId() );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector< sptr< Dictionary::Class > > makeDictionaries(
|
|
|
|
Dictionary::Initializing &,
|
|
|
|
Config::MediaWikis const & wikis,
|
|
|
|
QNetworkAccessManager & mgr )
|
|
|
|
throw( std::exception )
|
|
|
|
{
|
|
|
|
vector< sptr< Dictionary::Class > > result;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < wikis.size(); ++x )
|
|
|
|
{
|
|
|
|
if ( wikis[ x ].enabled )
|
|
|
|
result.push_back( new MediaWikiDictionary( wikis[ x ].id.toStdString(),
|
|
|
|
wikis[ x ].name.toUtf8().data(),
|
|
|
|
wikis[ x ].url,
|
|
|
|
mgr ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2009-04-21 20:09:02 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|