goldendict-ng/mediawiki.cc

582 lines
18 KiB
C++
Raw Normal View History

2012-02-20 21:47:14 +00:00
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "mediawiki.hh"
#include "wstring_qt.hh"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl>
#include <QtXml>
#include <algorithm>
#include <list>
#include "gddebug.hh"
2012-01-09 13:48:27 +00:00
#include "audiolink.hh"
#include "langcoder.hh"
#include "utils.hh"
#include <QRegularExpression>
2022-07-11 13:01:49 +00:00
#include "globalbroadcaster.h"
namespace MediaWiki {
using namespace Dictionary;
namespace {
class MediaWikiDictionary: public Dictionary::Class
{
string name;
QString url, icon;
QNetworkAccessManager & netMgr;
quint32 langId;
2009-04-21 20:09:02 +00:00
public:
MediaWikiDictionary( string const & id, string const & name_,
QString const & url_,
QString const & icon_,
QNetworkAccessManager & netMgr_ ):
Dictionary::Class( id, vector< string >() ),
name( name_ ),
url( url_ ),
icon( icon_ ),
netMgr( netMgr_ ),
langId( 0 )
{
int n = url.indexOf( "." );
if( n == 2 || ( n > 3 && url[ n-3 ] == '/' ) )
langId = LangCoder::code2toInt( url.mid( n - 2, 2 ).toLatin1().data() );
}
2009-04-21 20:09:02 +00:00
virtual string getName() noexcept
{ return name; }
virtual map< Property, string > getProperties() noexcept
{ return map< Property, string >(); }
virtual unsigned long getArticleCount() noexcept
{ return 0; }
virtual unsigned long getWordCount() noexcept
{ return 0; }
virtual sptr< WordSearchRequest > prefixMatch( wstring const &,
2022-01-09 08:35:07 +00:00
unsigned long maxResults ) ;
virtual sptr< DataRequest > getArticle( wstring const &, vector< wstring > const & alts,
wstring const &, bool );
virtual quint32 getLangFrom() const
{ return langId; }
virtual quint32 getLangTo() const
{ return langId; }
protected:
virtual void loadIcon() noexcept;
};
void MediaWikiDictionary::loadIcon() noexcept
{
if ( dictionaryIconLoaded )
return;
if( !icon.isEmpty() )
{
QFileInfo fInfo( QDir( Config::getConfigDir() ), icon );
if( fInfo.isFile() )
loadIconFromFile( fInfo.absoluteFilePath(), true );
}
if( dictionaryIcon.isNull() )
{
if( url.contains( "tionary" ) )
dictionaryIcon = dictionaryNativeIcon = QIcon( ":/icons/wiktionary.png" );
else
dictionaryIcon = dictionaryNativeIcon = QIcon( ":/icons/icon32_wiki.png" );
}
dictionaryIconLoaded = true;
}
class MediaWikiWordSearchRequest: public MediaWikiWordSearchRequestSlots
{
sptr< QNetworkReply > netReply;
bool isCancelling;
public:
MediaWikiWordSearchRequest( wstring const &,
QString const & url, QNetworkAccessManager & mgr );
~MediaWikiWordSearchRequest();
virtual void cancel();
private:
2009-04-21 20:09:02 +00:00
virtual void downloadFinished();
};
MediaWikiWordSearchRequest::MediaWikiWordSearchRequest( wstring const & str,
QString const & url,
QNetworkAccessManager & mgr ) :
isCancelling( false )
{
GD_DPRINTF( "request begin\n" );
QUrl reqUrl( url + "/api.php?action=query&list=allpages&aplimit=40&format=xml" );
2022-07-11 13:01:49 +00:00
GlobalBroadcaster::instance()->addWhitelist( reqUrl.host() );
Utils::Url::addQueryItem( reqUrl, "apfrom", gd::toQString( str ).replace( '+', "%2B" ) );
netReply = mgr.get( QNetworkRequest( reqUrl ) );
connect( netReply.get(), SIGNAL( finished() ),
this, SLOT( downloadFinished() ) );
2015-02-20 14:18:03 +00:00
#ifndef QT_NO_OPENSSL
connect( netReply.get(), SIGNAL( sslErrors( QList< QSslError > ) ),
netReply.get(), SLOT( ignoreSslErrors() ) );
#endif
}
MediaWikiWordSearchRequest::~MediaWikiWordSearchRequest()
{
GD_DPRINTF( "request end\n" );
}
void MediaWikiWordSearchRequest::cancel()
{
// We either finish it in place, or in the timer handler
isCancelling = true;
if( netReply.get() )
netReply.reset();
finish();
GD_DPRINTF( "cancel the request" );
}
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
2022-04-05 12:53:25 +00:00
for( int x = 0; x < nl.length(); ++x )
matches.push_back( gd::toWString( nl.item( x ).toElement().attribute( "title" ) ) );
}
}
GD_DPRINTF( "done.\n" );
}
else
setErrorString( netReply->errorString() );
finish();
}
class MediaWikiArticleRequest: public MediaWikiDataRequestSlots
{
typedef std::list< std::pair< QNetworkReply *, bool > > NetReplies;
NetReplies netReplies;
QString url;
2009-04-21 20:09:02 +00:00
public:
MediaWikiArticleRequest( wstring const & word, vector< wstring > const & alts,
2012-01-09 13:48:27 +00:00
QString const & url, QNetworkAccessManager & mgr,
Class * dictPtr_ );
virtual void cancel();
private:
2009-04-21 20:09:02 +00:00
void addQuery( QNetworkAccessManager & mgr, wstring const & word );
virtual void requestFinished( QNetworkReply * );
/// This simple set implementation should be much more efficient than tree-
/// and hash-based standard/Qt containers when there are very few elements.
template< typename T >
class SmallSet {
public:
bool insert( T x )
{
if( std::find( elements.begin(), elements.end(), x ) != elements.end() )
return false;
elements.push_back( x );
return true;
}
private:
std::vector< T > elements;
};
/// The page id set allows to filter out duplicate articles in case MediaWiki
/// redirects the main word and words in the alts collection to the same page.
SmallSet< long long > addedPageIds;
Class * dictPtr;
};
void MediaWikiArticleRequest::cancel()
{
finish();
}
MediaWikiArticleRequest::MediaWikiArticleRequest( wstring const & str,
vector< wstring > const & alts,
QString const & url_,
2012-01-09 13:48:27 +00:00
QNetworkAccessManager & mgr,
Class * dictPtr_ ):
url( url_ ), dictPtr( dictPtr_ )
{
connect( &mgr, SIGNAL( finished( QNetworkReply * ) ),
this, SLOT( requestFinished( QNetworkReply * ) ),
Qt::QueuedConnection );
addQuery( mgr, str );
for( unsigned x = 0; x < alts.size(); ++x )
addQuery( mgr, alts[ x ] );
}
void MediaWikiArticleRequest::addQuery( QNetworkAccessManager & mgr,
wstring const & str )
{
gdDebug( "MediaWiki: requesting article %s\n", gd::toQString( str ).toUtf8().data() );
2009-04-21 20:09:02 +00:00
QUrl reqUrl( url + "/api.php?action=parse&prop=text|revid&format=xml&redirects" );
Utils::Url::addQueryItem( reqUrl, "page", gd::toQString( str ).replace( '+', "%2B" ) );
QNetworkRequest req( reqUrl ) ;
//millseconds.
req.setTransferTimeout(3000);
QNetworkReply * netReply = mgr.get(req);
connect( netReply, &QNetworkReply::errorOccurred, this, [=](QNetworkReply::NetworkError e){
qDebug()<< "error:"<<e;
} );
2015-02-20 14:18:03 +00:00
#ifndef QT_NO_OPENSSL
connect( netReply, SIGNAL( sslErrors( QList< QSslError > ) ),
netReply, SLOT( ignoreSslErrors() ) );
2015-02-20 14:18:03 +00:00
#endif
netReplies.push_back( std::make_pair( netReply, false ) );
}
void MediaWikiArticleRequest::requestFinished( QNetworkReply * r )
{
GD_DPRINTF( "Finished.\n" );
if ( isFinished() ) // Was cancelled
return;
// Find this reply
bool found = false;
for( NetReplies::iterator i = netReplies.begin(); i != netReplies.end(); ++i )
{
if ( i->first == r )
{
i->second = true; // Mark as finished
found = true;
break;
}
}
if ( !found )
{
// Well, that's not our reply, don't do anything
return;
}
bool updated = false;
for( ; netReplies.size() && netReplies.front().second; netReplies.pop_front() )
{
QNetworkReply * netReply = netReplies.front().first;
if ( netReply->error() == QNetworkReply::NoError )
{
QDomDocument dd;
QString errorStr;
int errorLine, errorColumn;
if ( !dd.setContent( netReply, 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"
// Don't show the same article more than once:
&& addedPageIds.insert( parseNode.toElement().attribute( "pageid" ).toLongLong() ) )
{
QDomNode textNode = parseNode.namedItem( "text" );
if ( !textNode.isNull() )
{
QString articleString = textNode.toElement().text();
// Replace all ":" in links, remove '#' part in links to other articles
int pos = 0;
QRegularExpression regLinks( "<a\\s+href=\"/([^\"]+)\"" );
QString articleNewString;
QRegularExpressionMatchIterator it = regLinks.globalMatch( articleString );
while( it.hasNext() )
{
QRegularExpressionMatch match = it.next();
2022-02-27 05:17:37 +00:00
articleNewString += articleString.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd();
QString link = match.captured( 1 );
2021-11-19 13:47:22 +00:00
if( link.indexOf( "://" ) >= 0 )
{
// External link
articleNewString += match.captured();
2021-11-19 13:47:22 +00:00
continue;
}
if( link.indexOf( ':' ) >= 0 )
link.replace( ':', "%3A" );
int n = link.indexOf( '#', 1 );
if( n > 0 )
2015-10-31 19:26:50 +00:00
{
QString anchor = link.mid( n + 1 ).replace( '_', "%5F" );
link.truncate( n );
2015-10-31 19:26:50 +00:00
link += QString( "?gdanchor=%1" ).arg( anchor );
}
QString newLink = QString( "<a href=\"/%1\"" ).arg( link );
articleNewString += newLink;
}
if( pos )
{
2022-02-27 05:17:37 +00:00
articleNewString += articleString.mid( pos );
articleString = articleNewString;
articleNewString.clear();
}
2021-11-19 13:47:22 +00:00
QUrl wikiUrl( url );
wikiUrl.setPath( "/" );
// Update any special index.php pages to be absolute
articleString.replace( QRegularExpression( "<a\\shref=\"(/([\\w]*/)*index.php\\?)" ),
QString( "<a href=\"%1\\1" ).arg( wikiUrl.toString() ) );
2021-11-19 13:47:22 +00:00
// audio tag
QRegularExpression reg1( "<audio\\s.+?</audio>",
QRegularExpression::CaseInsensitiveOption
| QRegularExpression::DotMatchesEverythingOption );
QRegularExpression reg2( "<source\\s+src=\"([^\"]+)",
QRegularExpression::CaseInsensitiveOption );
pos = 0;
it = reg1.globalMatch( articleString );
while( it.hasNext() )
{
QRegularExpressionMatch match = it.next();
2022-02-27 05:17:37 +00:00
articleNewString += articleString.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd();
QString tag = match.captured();
QRegularExpressionMatch match2 = reg2.match( tag );
if( match2.hasMatch() )
{
QString ref = match2.captured( 1 );
QString audio_url = "<a href=\"" + ref
+ "\"><img src=\"qrcx://localhost/icons/playsound.png\" border=\"0\" align=\"absmiddle\" alt=\"Play\"/></a>";
articleNewString += audio_url;
}
else
articleNewString += match.captured();
}
if( pos )
{
2022-02-27 05:17:37 +00:00
articleNewString += articleString.mid( pos );
articleString = articleNewString;
articleNewString.clear();
}
2021-11-19 13:47:22 +00:00
// audio url
articleString.replace( QRegularExpression( "<a\\s+href=\"(//upload\\.wikimedia\\.org/wikipedia/[^\"'&]*\\.og[ga](?:\\.mp3|))\"" ),
2021-11-19 13:47:22 +00:00
QString::fromStdString( addAudioLink( string( "\"" ) + wikiUrl.scheme().toStdString() + ":\\1\"",
this->dictPtr->getId() ) + "<a href=\"" + wikiUrl.scheme().toStdString() + ":\\1\"" ) );
// Add url scheme to image source urls
articleString.replace( " src=\"//", " src=\"" + wikiUrl.scheme() + "://" );
//fix src="/foo/bar/Baz.png"
2016-06-13 21:15:12 +00:00
articleString.replace( "src=\"/", "src=\"" + wikiUrl.toString() );
// Remove the /wiki/ prefix from links
articleString.replace( "<a href=\"/wiki/", "<a href=\"" );
// In those strings, change any underscores to spaces
QRegularExpression rxLink( "<a\\s+href=\"[^/:\">#]+" );
it = rxLink.globalMatch( articleString );
while( it.hasNext() )
{
QRegularExpressionMatch match = it.next();
for( int i = match.capturedStart() + 9; i < match.capturedEnd(); i++ )
if( articleString.at( i ) == QChar( '_') )
articleString[ i ] = ' ';
}
2021-11-19 13:47:22 +00:00
//fix file: url
articleString.replace( QRegularExpression( "<a\\s+href=\"([^:/\"]*file%3A[^/\"]+\")",
QRegularExpression::CaseInsensitiveOption ),
2021-11-19 13:47:22 +00:00
QString( "<a href=\"%1/index.php?title=\\1" ).arg( url ));
// Add url scheme to other urls like "//xxx"
articleString.replace( " href=\"//", " href=\"" + wikiUrl.scheme() + "://" );
// Fix urls in "srcset" attribute
pos = 0;
QRegularExpression regSrcset( " srcset\\s*=\\s*\"/[^\"]+\"" );
it = regSrcset.globalMatch( articleString );
while( it.hasNext() )
{
QRegularExpressionMatch match = it.next();
2022-02-27 05:17:37 +00:00
articleNewString += articleString.mid( pos, match.capturedStart() - pos );
pos = match.capturedEnd();
QString srcset = match.captured();
2021-11-19 13:47:22 +00:00
QString newSrcset = srcset.replace( "//", wikiUrl.scheme() + "://" );
articleNewString += newSrcset;
}
if( pos )
{
2022-02-27 05:17:37 +00:00
articleNewString += articleString.mid( pos );
articleString = articleNewString;
articleNewString.clear();
}
2021-11-19 13:47:22 +00:00
QByteArray articleBody = articleString.toUtf8();
articleBody.prepend( dictPtr->isToLanguageRTL() ? "<div class=\"mwiki\" dir=\"rtl\">" :
"<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;
}
}
}
GD_DPRINTF( "done.\n" );
}
else
setErrorString( netReply->errorString() );
disconnect( netReply, 0, 0, 0 );
netReply->deleteLater();
}
if ( netReplies.empty() )
finish();
else
if ( updated )
update();
}
sptr< WordSearchRequest > MediaWikiDictionary::prefixMatch( wstring const & word,
unsigned long maxResults )
2022-01-09 08:35:07 +00:00
{
2012-10-31 13:58:35 +00:00
(void) maxResults;
if ( word.size() > 80 )
{
// Don't make excessively large queries -- they're fruitless anyway
return new WordSearchRequestInstant();
}
else
return new MediaWikiWordSearchRequest( word, url, netMgr );
}
sptr< DataRequest > MediaWikiDictionary::getArticle( wstring const & word,
vector< wstring > const & alts,
wstring const &, bool )
2022-01-09 08:35:07 +00:00
{
if ( word.size() > 80 )
{
// Don't make excessively large queries -- they're fruitless anyway
return new DataRequestInstant( false );
}
else
return new MediaWikiArticleRequest( word, alts, url, netMgr, this );
}
}
vector< sptr< Dictionary::Class > > makeDictionaries(
Dictionary::Initializing &,
Config::MediaWikis const & wikis,
QNetworkAccessManager & mgr )
2022-01-09 08:35:07 +00:00
{
vector< sptr< Dictionary::Class > > result;
2012-12-10 12:49:45 +00:00
for( int 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,
wikis[ x ].icon,
mgr ) );
}
return result;
}
2009-04-21 20:09:02 +00:00
}