25 #include <QtCore/QTimer>
26 #include <QtCore/QFile>
30 #include <Soprano/QueryResultIterator>
31 #include <Soprano/PluginManager>
32 #include <Soprano/Serializer>
33 #include <Soprano/Util/SimpleStatementIterator>
34 #include <Soprano/Vocabulary/NAO>
38 using namespace Nepomuk2;
39 using namespace Nepomuk2::Backup;
40 using namespace Nepomuk2::Vocabulary;
41 using namespace Soprano::Vocabulary;
44 const QString& outputFile,
QObject* parent)
47 , m_inputFile(inputFile)
48 , m_outputFile(outputFile)
49 , m_filter(Filter_None)
56 QTimer::singleShot( 0,
this, SLOT(doJob()) );
64 void StatementGenerator::doJob()
66 Soprano::PluginManager* pg = Soprano::PluginManager::instance();
67 const Soprano::Serializer* serializer = pg->discoverSerializerForSerialization( Soprano::SerializationNQuads );
69 QFile input( m_inputFile );
70 kDebug() <<
"INPUT: " << m_inputFile;
71 if( !input.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
73 setErrorText( QString::fromLatin1(
"Could not open file %1").arg( m_inputFile ) );
78 QFile output( m_outputFile );
79 if( !output.open( QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ) ) {
81 setErrorText( QString::fromLatin1(
"Could not open file %1").arg( m_outputFile ) );
86 QTextStream inputStream( &input );
87 QTextStream outputStream( &output );
90 while( !inputStream.atEnd() ) {
91 const QUrl uri( inputStream.readLine() );
92 kDebug() <<
"Processing" << uri;
93 QList<Soprano::Statement> stList;
96 stList << fetchNonDiscardableStatements(uri);
97 if( stList.count() ) {
98 QStringList properties;
99 properties <<
"nao:lastModified" <<
"nao:created" <<
"rdf:type" <<
"nie:url";
101 stList << fetchProperties(uri, properties);
105 if( hasType(uri, NFO::FileDataObject()) ) {
106 QList<QString> fileProperties;
107 fileProperties <<
"rdf:type" <<
"nao:lastModified" <<
"nao:created"
108 <<
"nie:url" <<
"nao:numericRating" <<
"nao:hasTag" <<
"nao:description";
110 stList << fetchProperties(uri, fileProperties);
113 bool hasPrefLabel =
false;
114 Soprano::Statement identifierSt;
116 QList<Soprano::Statement> tagStatements = fetchAllStatements(uri);
117 foreach(
const Soprano::Statement& st, tagStatements) {
120 if( st.object().isResource() && st.object().uri().scheme() == QLatin1String(
"nepomuk") )
123 if( st.predicate().uri() == NAO::prefLabel() )
125 else if( st.predicate().uri() == NAO::identifier() )
131 if( !hasPrefLabel ) {
132 identifierSt.setPredicate( NAO::prefLabel() );
133 stList << identifierSt;
138 if( !stList.isEmpty() ) {
140 Soprano::Util::SimpleStatementIterator iter( stList );
141 serializer->serialize( iter, outputStream, Soprano::SerializationNQuads );
143 m_statementCount += stList.size();
146 if( m_resourceCount ) {
148 emitPercent( count, m_resourceCount );
159 bool isValid(
const Soprano::Statement& st) {
163 if( !st.subject().isResource() )
166 if( !st.context().isResource() )
169 Soprano::Node
object = st.object();
170 if( !
object.isResource() && !
object.isLiteral() )
173 if(
object.isLiteral() ) {
174 Soprano::LiteralValue lv =
object.literal();
175 if( lv.dataTypeUri().isEmpty() ) {
177 if( lv.isPlain() && !lv.toString().isEmpty() )
182 if( lv.isString() && lv.toString().isEmpty() )
190 bool StatementGenerator::hasType(
const QUrl& uri,
const QUrl& type)
192 QString query = QString::fromLatin1(
"ask where { %1 a %2 . }")
193 .arg( Soprano::Node::resourceToN3(uri),
194 Soprano::Node::resourceToN3(type) );
196 return m_model->executeQuery( query, Soprano::Query::QueryLanguageSparql ).boolValue();
199 QList<Soprano::Statement> StatementGenerator::fetchProperties(
const QUrl& uri, QStringList properties)
201 QList<Soprano::Statement> stList;
202 QString query = QString::fromLatin1(
"select distinct ?p ?o ?g where { graph ?g { %1 ?p ?o. } "
203 " FILTER(?p in (%2)) . }")
204 .arg( Soprano::Node::resourceToN3(uri),
205 properties.join(
",") );
207 Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
209 Soprano::Statement st( uri, it[0], it[1], it[2] );
217 QList< Soprano::Statement > StatementGenerator::fetchNonDiscardableStatements(
const QUrl& uri)
219 QString query = QString::fromLatin1(
"select distinct ?p ?o ?g where { graph ?g { %1 ?p ?o. } "
220 " FILTER(!(?p in (nao:lastModified, nao:created, rdf:type, nie:url))) ."
221 " ?g a nrl:InstanceBase ."
222 " FILTER NOT EXISTS { ?g a nrl:DiscardableInstanceBase . } }")
223 .arg( Soprano::Node::resourceToN3(uri) );
225 QList<Soprano::Statement> stList;
226 Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
228 Soprano::Statement st( uri, it[0], it[1], it[2] );
236 QList< Soprano::Statement > StatementGenerator::fetchAllStatements(
const QUrl& uri)
238 QString query = QString::fromLatin1(
"select distinct ?p ?o ?g where { graph ?g { %1 ?p ?o. } }")
239 .arg( Soprano::Node::resourceToN3(uri) );
241 QList<Soprano::Statement> stList;
242 Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
244 Soprano::Statement st( uri, it[0], it[1], it[2] );
StatementGenerator(Soprano::Model *model, const QString &inputFile, const QString &outputFile, QObject *parent)
void setFilter(Filter filter)