summaryrefslogtreecommitdiffstats
path: root/src/webqueryciteseerx.cpp
blob: 6ac7da4a983e9cd4db838d2355374627ba8ccafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/***************************************************************************
 *   Copyright (C) 2008 by Jacob Kanev <j_kanev@arcor.de>,                 *
 *   Thomas Fischer <fischer@unix-ag.uni-kl.de>                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <ntqfile.h>
#include <ntqregexp.h>
#include <ntqbuffer.h>
#include <ntqspinbox.h>

#include <klocale.h>
#include <klineedit.h>
#include <kmessagebox.h>
#include <kurl.h>
#include <kdebug.h>

#include <fileimporterbibtex.h>
#include <encoderxml.h>
#include <settings.h>
#include "webqueryciteseerx.h"

using BibTeX::Value;
using BibTeX::Entry;
using BibTeX::EntryField;

namespace KBibTeX
{

    //_______________________________________________________________________________________________________________
    // Construct widget

    WebQueryCiteSeerXWidget::WebQueryCiteSeerXWidget( TQWidget *parent, const char *name )
            : WebQueryWidget( parent, name )
    {
        init();

        Settings *settings = Settings::self();
        TQString value = settings->getWebQueryDefault( "CiteSeerX" );
        value = value == TQString::null ? "" : value;
        lineEditQuery->setText( value );
        slotTextChanged( value, true );
    }


    //_______________________________________________________________________________________________________________
    // Construct

    WebQueryCiteSeerX::WebQueryCiteSeerX( TQWidget* parent )
            : WebQuery( parent ), m_citeSeerXServer( "citeseerx.ist.psu.edu" )
    {
        m_widget = new WebQueryCiteSeerXWidget( parent );
    }


    //_______________________________________________________________________________________________________________
    // Destroy

    WebQueryCiteSeerX::~WebQueryCiteSeerX()
    {
        delete m_widget;
    }


    //_______________________________________________________________________________________________________________
    // GUI string

    TQString WebQueryCiteSeerX::title()
    {
        return i18n( "CiteSeerX" );
    }


    //_______________________________________________________________________________________________________________
    // GUI info

    TQString WebQueryCiteSeerX::disclaimer()
    {
        return i18n( "About CiteSeerX" );
    }


    //_______________________________________________________________________________________________________________
    // URL for disclaimer

    TQString WebQueryCiteSeerX::disclaimerURL()
    {
        return "http://citeseerx.ist.psu.edu/about/site";
    }


    //_______________________________________________________________________________________________________________
    // return pointer to widget

    WebQueryWidget *WebQueryCiteSeerX::widget()
    {
        return m_widget;
    }


    //_______________________________________________________________________________________________________________
    // user has pressed "Cancel"

    void WebQueryCiteSeerX::cancelQuery()
    {
        m_queryQueue.clear();
    }


    //_______________________________________________________________________________________________________________
    // main function -- collects all queries for one search

    void WebQueryCiteSeerX::query()
    {
        // store CiteSeerX as future default
        WebQuery::query();
        Settings *settings = Settings::self();
        settings->setWebQueryDefault( "CiteSeerX", m_widget->lineEditQuery->text() );

        // read number of desired results from GUI
        m_queryQueue.clear();
        m_desiredHits = m_widget->spinBoxMaxHits->value();
        // one for each entry, and one for each page of 10 links
        setNumStages( m_desiredHits + ( m_desiredHits / 10 + 1 ) );

        // prepare search term
        TQString searchTerm = m_widget->lineEditQuery->text().stripWhiteSpace().replace( '$', "" );
        TQStringList queryWords = TQStringList::split( TQRegExp( "\\s+" ), searchTerm );

        if ( searchTerm.isEmpty() || queryWords.size() == 0 )
        {
            setEndSearch( WebQuery::statusInvalidQuery );
            return;
        }

        // build query from search term
        TQString query;

        for ( uint i = 0; i < queryWords.size(); ++i )
        {
            if ( i ) query += " AND ";

            query += queryWords[i];
        }

        query = query.replace( "%", "%25" ).replace( "+", "%2B" ).replace( " ", "%20" ).replace( "#", "%23" ).replace( "&", "%26" ).replace( "?", "%3F" );

        // schedule jobs
        DataRequest dr;
        dr.url = KURL( TQString( "http://citeseerx.ist.psu.edu/search?q=" ).append( query ).append( "&submit=Search&sort=rel" ) );
        dr.parser = &WebQueryCiteSeerX::parseSummaryPage;
        m_queryQueue.push_back( dr );

        // start job queue
        nextJob();
    }


    //_______________________________________________________________________________________________________________
    // process results from current job

    void WebQueryCiteSeerX::parseSummaryPage( const TQString& data )
    {
        // regexp. for finding paper entries (example: href="/viewdoc/summary;jsessionid=12345ABCD?doi=10.1.1.108.9937")
        TQRegExp paperXpr( "href=\"(/viewdoc/summary[^?]*\\?doi=[^\"]+)\"" );

        // count paper results and schedule single paper URLs

        for ( int p = paperXpr.search( data ); p >= 0; p = paperXpr.search( data, p + paperXpr.matchedLength() ) )
        {
            if ( ++m_receivedHits > m_desiredHits )
                break;

            DataRequest dr;

            dr.url = KURL( TQString( "http://" ) + m_citeSeerXServer + paperXpr.cap( 1 ) );

            dr.parser = &WebQueryCiteSeerX::parsePaperPage;

            m_queryQueue.push_back( dr );
        }

        // if we haven't reached the desired number of hits, schedule the next summary page
        TQRegExp nextSummaryXpr( "<a href=\"([^\"]+)\">Next 10" );

        if ( m_receivedHits < m_desiredHits )
            if ( nextSummaryXpr.search( data ) >= 0 )
            {
                DataRequest dr;
                dr.url = KURL( TQString( "http://" ) + m_citeSeerXServer + nextSummaryXpr.cap( 1 ).replace( "&amp;", "&" ) );
                dr.parser = &WebQueryCiteSeerX::parseSummaryPage;
                m_queryQueue.push_back( dr );
            }
    }


    //_______________________________________________________________________________________________________________
    // process the result of one single paper link

    void WebQueryCiteSeerX::parsePaperPage( const TQString& data )
    {
        // find type and id: @XXX{ YYY
        TQRegExp typeIdXpr( "@(.*)\\{(.*)," );
        typeIdXpr.setMinimal( true );
        typeIdXpr.search( data );
        TQString typeStr = typeIdXpr.cap( 1 );
        TQString id = typeIdXpr.cap( 2 );

        // create entry
        Entry *entry = new BibTeX::Entry( typeIdXpr.cap( 1 ), typeIdXpr.cap( 2 ) );

        // find abstract: <..>Abstract:</..> <..> XXX </..>
        parseForSingleExpression( "<[^<]+>Abstract:</[^<]+>\\s*<[^<]+>([^<]+)</[^<]+>", data, entry, BibTeX::EntryField::ftAbstract );

        // find title: title = {XXX}
        parseForSingleExpression( "title = \\{([^}]+)\\}", data, entry, BibTeX::EntryField::ftTitle );

        // find author: author = {XXX}
        parseForSingleExpression( "author = \\{([^}]+)\\}", data, entry, BibTeX::EntryField::ftAuthor );

        // find year: year = {XXX}
        parseForSingleExpression( "year = \\{([^}]+)\\}", data, entry, BibTeX::EntryField::ftYear );

        // find journal: journal = {XXX}
        parseForSingleExpression( "journal = \\{([^}]+)\\}", data, entry, BibTeX::EntryField::ftJournal );

        // find pages: pages = {XXX}
        parseForSingleExpression( "pages = \\{([^}]+)\\}", data, entry, BibTeX::EntryField::ftPages );

        // publish what we've found
        emit foundEntry( entry, false );
    }


    //_______________________________________________________________________________________________________________
    // find single bibtex field in html page and add to entry

    void WebQueryCiteSeerX::parseForSingleExpression( TQString description, const TQString &data, Entry *entry, BibTeX::EntryField::FieldType type )
    {
        // search, and add to entry if found
        TQRegExp xpr( description );

        if ( xpr.search( data ) + 1 )
        {
            EntryField *field = new EntryField( type );
            field->setValue( new Value( xpr.cap( 1 ), false ) );
            entry->addField( field );
        }
    }


    //_______________________________________________________________________________________________________________
    // read data from the job and start the current parser

    void WebQueryCiteSeerX::getData( KIO::Job *job )
    {
        // advance GUI progress bar
        enterNextStage();

        if ( job && !job->error() && !m_aborted )
        {

            // read data
            TQBuffer data;
            data.open( IO_WriteOnly );
            data.writeBlock( dynamic_cast<KIO::StoredTransferJob*>( job )->data() );
            data.close();
            data.open( IO_ReadOnly );
            TQTextStream ts( &data );
            TQString result = ts.read();
            data.close();

            // hand the read data over to the parser
            ( this->*m_currentParser )( result );
        }

        // proceed
        nextJob();
    }


    //_______________________________________________________________________________________________________________
    // call the next job

    void WebQueryCiteSeerX::nextJob()
    {
        // no more requests: finished
        if ( !m_queryQueue.size() )
        {
            setEndSearch( WebQuery::statusSuccess );
            m_receivedHits = 0;
        }
        // else: take the next request from queue and start it
        else if ( !m_aborted )
        {
            m_currentParser = m_queryQueue.front().parser;
            KIO::Job *job = KIO::storedGet( m_queryQueue.front().url, FALSE, FALSE );
            m_queryQueue.pop_front();
            connect( job, SIGNAL( result( KIO::Job * ) ), this, SLOT( getData( KIO::Job * ) ) );
        }
    }

}

#include "webqueryciteseerx.moc"