Develop and Download Open Source Software

Browse CVS Repository

Diff of /xoonips/AL/commonal.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.45 by aga, Wed Jan 19 10:07:53 2005 UTC revision 1.46 by youi, Fri Jan 21 01:36:11 2005 UTC
# Line 2  Line 2 
2   *   *
3   * $Revision$   * $Revision$
4   * $Log$   * $Log$
5     * Revision 1.46  2005/01/21 01:36:11  youi
6     * pubmed補完機能を追加した.
7     *
8   * Revision 1.45  2005/01/19 10:07:53  aga   * Revision 1.45  2005/01/19 10:07:53  aga
9   * ・ゲストユーザ向けの修正.   * ・ゲストユーザ向けの修正.
10   *   *
# Line 216  Line 219 
219  #include <assert.h>  #include <assert.h>
220  #include <sql.h>  #include <sql.h>
221  #include <sqlext.h>  #include <sqlext.h>
222    #include <libxml/xmlreader.h>
223    
224  using namespace std;  using namespace std;
225    
226  #include "common.h"  #include "common.h"
# Line 229  using namespace std; Line 234  using namespace std;
234  #include "item.h"  #include "item.h"
235  #include "index.h"  #include "index.h"
236  #include "changelog.h"  #include "changelog.h"
237    #include "pubmed.h"
238    
239  static string dbprefix; //!< XOOPSデータベーステーブルのPREFIX  static string dbprefix; //!< XOOPSデータベーステーブルのPREFIX
240    
# Line 236  static SQLHANDLE henv = NULL; Line 242  static SQLHANDLE henv = NULL;
242  static SQLHANDLE hdbc = NULL;  static SQLHANDLE hdbc = NULL;
243  static SQLHANDLE hstmt = NULL;  static SQLHANDLE hstmt = NULL;
244    
245    static void processEfetch(xmlTextReaderPtr reader, pubmed_t* p);
246    static int streamPubmedFile(const char *filename, pubmed_t* p);
247    
248  static result_t insertIndexInternal( sessionid_t sid, index_t *index, indexid_t *xid );  static result_t insertIndexInternal( sessionid_t sid, index_t *index, indexid_t *xid );
249    
250  static string odbcDiagString( SQLSMALLINT HandleType, SQLHANDLE hstmt, SQLRETURN sqlcode );  static string odbcDiagString( SQLSMALLINT HandleType, SQLHANDLE hstmt, SQLRETURN sqlcode );
# Line 3106  result_t insertItem( sessionid_t sid, co Line 3115  result_t insertItem( sessionid_t sid, co
3115              strncpy2( (char*)doi, item -> getDOI(), XNP_ITEM_DOI_LEN );              strncpy2( (char*)doi, item -> getDOI(), XNP_ITEM_DOI_LEN );
3116              item_type_id = item -> getItemTypeID( );              item_type_id = item -> getItemTypeID( );
3117              uid = item -> getContributorUID( );              uid = item -> getContributorUID( );
3118              creation_date = item -> getCreationDate( );              creation_date = time( NULL );
3119              last_update_date = item -> getLastUpdateDate( );              last_update_date = time( NULL );
3120                            
3121              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){
3122                  SQLINTEGER count = 0;                  SQLINTEGER count = 0;
# Line 3439  result_t updateItem( sessionid_t sid, co Line 3448  result_t updateItem( sessionid_t sid, co
3448          sql += ", description=?";          sql += ", description=?";
3449          sql += ", doi=?";          sql += ", doi=?";
3450          sql += ", uid=?";          sql += ", uid=?";
         sql += ", creation_date=?";  
3451          sql += ", last_update_date=?";          sql += ", last_update_date=?";
3452          sql += " WHERE item_id=?";          sql += " WHERE item_id=?";
3453          sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);          sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);
# Line 3450  result_t updateItem( sessionid_t sid, co Line 3458  result_t updateItem( sessionid_t sid, co
3458              SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_ITEM_DESCRIPTION_LEN, 0, description, 0, &cbDescription );              SQLBindParameter(hstmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_ITEM_DESCRIPTION_LEN, 0, description, 0, &cbDescription );
3459              SQLBindParameter(hstmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_ITEM_DOI_LEN, 0, doi, 0, &cbDOI );              SQLBindParameter(hstmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_ITEM_DOI_LEN, 0, doi, 0, &cbDOI );
3460              SQLBindParameter(hstmt, 6, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 0, 0, &uid, 0, &cbUid );              SQLBindParameter(hstmt, 6, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 0, 0, &uid, 0, &cbUid );
3461              SQLBindParameter(hstmt, 7, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &creation_date, 0, &cbCreation_date );              SQLBindParameter(hstmt, 7, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &last_update_date, 0, &cbLast_update_date );
3462              SQLBindParameter(hstmt, 8, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &last_update_date, 0, &cbLast_update_date );              SQLBindParameter(hstmt, 8, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &item_id, 0, &cbItem_id );
             SQLBindParameter(hstmt, 9, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &item_id, 0, &cbItem_id );  
3463                            
3464              strncpy2( (char*)title, item -> getTitle(), XNP_ITEM_TITLE_LEN );              strncpy2( (char*)title, item -> getTitle(), XNP_ITEM_TITLE_LEN );
3465              strncpy2( (char*)keywords, item -> getKeywords(), XNP_ITEM_KEYWORDS_LEN );              strncpy2( (char*)keywords, item -> getKeywords(), XNP_ITEM_KEYWORDS_LEN );
# Line 3460  result_t updateItem( sessionid_t sid, co Line 3467  result_t updateItem( sessionid_t sid, co
3467              strncpy2( (char*)doi, item -> getDOI(), XNP_ITEM_DOI_LEN );              strncpy2( (char*)doi, item -> getDOI(), XNP_ITEM_DOI_LEN );
3468              item_type_id = item -> getItemTypeID( );              item_type_id = item -> getItemTypeID( );
3469              uid = item -> getContributorUID( );              uid = item -> getContributorUID( );
3470              creation_date = item -> getCreationDate( );              last_update_date = time( NULL );
             last_update_date = item -> getLastUpdateDate( );  
3471              item_id = item -> getItemID( );              item_id = item -> getItemID( );
3472                            
3473              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){
# Line 3624  result_t deleteItem( sessionid_t sid, it Line 3630  result_t deleteItem( sessionid_t sid, it
3630  /**  /**
3631   *   *
3632   * インデックスに登録されたアイテムのIDを取得します.   * インデックスに登録されたアイテムのIDを取得します.
3633     * 権限が無くて読めないものは結果に含みません.
3634     * 未承認で読めないものも結果に含みません.
3635     *
3636   * @param sid セッションID   * @param sid セッションID
3637   * @param binderid 対象インデックスのID   * @param binderid 対象インデックスのID
3638   * @param cri 結果の範囲指定,ソート条件指定   * @param cri 結果の範囲指定,ソート条件指定
# Line 3646  result_t getItemIDByIndexID( sessionid_t Line 3655  result_t getItemIDByIndexID( sessionid_t
3655      string sql;      string sql;
3656      SQLRETURN sqlcode;      SQLRETURN sqlcode;
3657      SQLINTEGER count = 0;      SQLINTEGER count = 0;
3658        userid_t uid;
3659            
3660      sql = "SELECT t1.item_id FROM " + dbprefix + "_xnpaccount_item_basic as t1, "      if( ( ret = sessionID2UID( sid, &uid ) ) != RES_OK ) return ret;
3661          + dbprefix + "_xnpaccount_index_item_link as t2 ";      sql = "SELECT DISTINCT tlink.item_id AS item_id FROM " + dbprefix + "_xnpaccount_index_item_link AS tlink";
3662      sql += " WHERE t1.item_id = t2.item_id";      sql+= " LEFT JOIN " + dbprefix + "_xnpaccount_index AS tx ON tlink.index_id = tx.index_id";
3663      sql += " AND t2.index_id=" + unsignedIntToString( xid );      sql+=       " AND certify_state=" + unsignedIntToString( index::CERTIFIED );
3664        sql+= " LEFT JOIN " + dbprefix + "_xnpaccount_item_basic AS ti ON tlink.item_id = ti.item_id";
3665        sql+= " LEFT JOIN " + dbprefix + "_xnpaccount_groups_users_link as tgulink ON tx.gid=tgulink.gid AND (ti.uid=";
3666        sql+= unsignedIntToString( uid ) + " OR ";
3667        sql+= string( isModerator( sid, uid ) ? "1" : "0" ) + ") LEFT JOIN ";
3668        sql+= dbprefix + "_xnpaccount_config AS tc ON tc.name=\"" XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_KEY "\" LEFT JOIN ";
3669        sql+= dbprefix + "_xnpaccount_session AS tsess ON tsess.uid=" + unsignedIntToString( uid );
3670        sql+= " WHERE ( tc.value=\"" XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_ALL "\"";
3671        sql+=       " AND tx.open_level=" + unsignedIntToString( index::OL_PUBLIC ) + " AND tsess.uid IS NULL";
3672        sql+=    " OR tc.value!=\"" XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_ALL "\"";
3673        sql+=       " AND tx.open_level=" + unsignedIntToString( index::OL_PUBLIC ) + " AND tsess.uid IS NOT NULL";
3674        sql+=    " OR tgulink.uid=" + unsignedIntToString( uid );
3675        sql+=    " OR tx.open_level=" + unsignedIntToString( index::OL_PRIVATE );
3676        sql+=       " AND tx.uid=" + unsignedIntToString( uid );
3677        sql+=    " OR " + string( isModerator( sid, uid ) ? "1" : "0" );
3678        sql+=    " OR tx.uid IS NULL ";
3679        sql+=    " AND tx.open_level=" + unsignedIntToString( index::OL_PUBLIC );
3680        sql+=    ")";
3681      sql += criteria2str( cri );      sql += criteria2str( cri );
3682        fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3683      if( countResultRows( sql.c_str(), &count ) == RES_OK ){      if( countResultRows( sql.c_str(), &count ) == RES_OK ){
3684          dst = new itemid_t[ count ];          dst = new itemid_t[ count ];
3685          *iids = dst;          *iids = dst;
# Line 3876  result_t registerItem( sessionid_t sid, Line 3904  result_t registerItem( sessionid_t sid,
3904      unsigned int tmp;      unsigned int tmp;
3905      char* certify_item_val;      char* certify_item_val;
3906      string sql;      string sql;
3907        result_t ret = RES_ERROR;
3908            
3909      if( !getIndexPermission( sid, xid, index::OP_REGISTER ) ) return RES_NO_WRITE_ACCESS_RIGHT;      if( !getIndexPermission( sid, xid, index::OP_REGISTER ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3910            
# Line 3885  result_t registerItem( sessionid_t sid, Line 3914  result_t registerItem( sessionid_t sid,
3914          + unsignedIntToString(iid) + ","          + unsignedIntToString(iid) + ","
3915          + unsignedIntToString( getInitialCertifyStateFromConfig( ) ) + ")";          + unsignedIntToString( getInitialCertifyStateFromConfig( ) ) + ")";
3916      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3917      return querySimple( "registerItem", sql );      ret = querySimple( "registerItem", sql );
3918        if( ret == RES_OK ){
3919            // update last update date
3920            sql = "UPDATE " + dbprefix + "_xnpaccount_item_basic SET last_update_date="
3921                + unsignedIntToString( time( NULL ) ) + " WHERE item_id="
3922                + unsignedIntToString( xid );
3923            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3924            ret = querySimple( "registerItem", sql );
3925        }
3926        return ret;
3927  }  }
3928    
3929  /**  /**
# Line 3910  result_t unregisterItem( sessionid_t sid Line 3948  result_t unregisterItem( sessionid_t sid
3948      const index_t* index;      const index_t* index;
3949      unsigned int tmp;      unsigned int tmp;
3950      string sql;      string sql;
3951        result_t ret = RES_ERROR;
3952            
3953      if( !getIndexPermission( sid, xid, index::OP_UNREGISTER ) ) return RES_NO_WRITE_ACCESS_RIGHT;      if( !getIndexPermission( sid, xid, index::OP_UNREGISTER ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3954            
# Line 3918  result_t unregisterItem( sessionid_t sid Line 3957  result_t unregisterItem( sessionid_t sid
3957      sql += " WHERE index_id=" + unsignedIntToString(xid);      sql += " WHERE index_id=" + unsignedIntToString(xid);
3958      sql += " AND item_id=" + unsignedIntToString(iid);      sql += " AND item_id=" + unsignedIntToString(iid);
3959      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3960      return querySimple( "unregisterItem", sql );      ret = querySimple( "unregisterItem", sql );
3961        if( ret == RES_OK ){
3962            // update last update date
3963            sql = "UPDATE " + dbprefix + "_xnpaccount_item_basic SET last_update_date="
3964                + unsignedIntToString( time( NULL ) ) + " WHERE item_id="
3965                + unsignedIntToString( xid );
3966            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
3967            ret = querySimple( "unregisterItem", sql );
3968        }
3969        return ret;
3970  }  }
3971    
3972  /**  /**
# Line 3944  result_t registerBinderItem( sessionid_t Line 3992  result_t registerBinderItem( sessionid_t
3992      unsigned int tmp;      unsigned int tmp;
3993      char* certify_item_val;      char* certify_item_val;
3994      string sql;      string sql;
3995        result_t ret = RES_ERROR;
3996            
3997      if( !getItemPermission( sid, binderid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;      if( !getItemPermission( sid, binderid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;
3998            
# Line 3952  result_t registerBinderItem( sessionid_t Line 4001  result_t registerBinderItem( sessionid_t
4001          + unsignedIntToString(binderid) + ","          + unsignedIntToString(binderid) + ","
4002          + unsignedIntToString(iid) + ")";          + unsignedIntToString(iid) + ")";
4003      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
4004      return querySimple( "registerBinderItem", sql );      ret = querySimple( "registerBinderItem", sql );
4005        if( ret == RES_OK ){
4006            // update binder's last update date
4007            sql = "UPDATE " + dbprefix + "_xnpaccount_item_basic SET last_update_date="
4008                + unsignedIntToString( time( NULL ) ) + " WHERE item_id="
4009                + unsignedIntToString( binderid );
4010            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
4011            ret = querySimple( "registerBinderItem", sql );
4012        }
4013        return ret;
4014  }  }
4015    
4016  /**  /**
# Line 3977  result_t unregisterBinderItem( sessionid Line 4035  result_t unregisterBinderItem( sessionid
4035      const index_t* index;      const index_t* index;
4036      unsigned int tmp;      unsigned int tmp;
4037      string sql;      string sql;
4038        result_t ret = RES_OK;
4039            
4040      if( !getItemPermission( sid, binderid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;      if( !getItemPermission( sid, binderid, item::OP_MODIFY ) ) return RES_NO_WRITE_ACCESS_RIGHT;
4041            
# Line 3985  result_t unregisterBinderItem( sessionid Line 4044  result_t unregisterBinderItem( sessionid
4044      sql += " WHERE binder_id=" + unsignedIntToString(binderid);      sql += " WHERE binder_id=" + unsignedIntToString(binderid);
4045      sql += " AND item_id=" + unsignedIntToString(iid);      sql += " AND item_id=" + unsignedIntToString(iid);
4046      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );      //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
4047      return querySimple( "unregisterBinderItem", sql );      ret = querySimple( "unregisterBinderItem", sql );
4048        if( ret == RES_OK ){
4049            // update binder's last update date
4050            sql = "UPDATE " + dbprefix + "_xnpaccount_item_basic SET last_update_date="
4051                + unsignedIntToString( time( NULL ) ) + " WHERE item_id="
4052                + unsignedIntToString( binderid );
4053            //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
4054            ret = querySimple( "unregisterBinderItem", sql );
4055        }
4056        return ret;
4057  }  }
4058    
4059  /**  /**
# Line 5574  result_t insertChangeLog( sessionid_t si Line 5642  result_t insertChangeLog( sessionid_t si
5642      string sql;      string sql;
5643      SQLHANDLE hstmt = NULL;      SQLHANDLE hstmt = NULL;
5644      SQLRETURN sqlcode;      SQLRETURN sqlcode;
5645        time_t now;
5646            
5647        time( &now );
5648      // insert change log      // insert change log
5649      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {      if( ( sqlcode = SQLAllocHandle( SQL_HANDLE_STMT, hdbc, &hstmt ) ) == SQL_SUCCESS ) {
5650          SQLINTEGER cbLog = SQL_NTS;          SQLINTEGER cbLog = SQL_NTS;
5651          string sql;          string sql;
5652          sql = "INSERT INTO " + dbprefix + "_xnpaccount_changelog (item_id, log_date, log) VALUES (";          sql = "INSERT INTO " + dbprefix + "_xnpaccount_changelog (item_id, log_date, log) VALUES (";
5653          sql += unsignedIntToString( itemid ) + ", ";          sql += unsignedIntToString( itemid ) + ", ";
5654          sql += unsignedIntToString( time( NULL ) ) + ", ?)";          sql += unsignedIntToString( now ) + ", ?)";
5655          //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );          //fprintf( stderr, "\nsql at %d=%s\n", __LINE__, sql.c_str() );
5656          sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);          sqlcode = SQLPrepare(hstmt, (SQLCHAR*)sql.c_str(), SQL_NTS);
5657          if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){          if( sqlcode == SQL_SUCCESS || sqlcode == SQL_SUCCESS_WITH_INFO ){
5658              SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_CHANGELOG_LOG_LEN, 0,              SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_LONGVARCHAR, XNP_CHANGELOG_LOG_LEN, 0,
5659                               (SQLCHAR*)( log ), strlen( log ), &cbLog );                               (SQLCHAR*)( log ), strlen( log ), &cbLog );
5660              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){              if( ( sqlcode = SQLExecute( hstmt ) ) == SQL_SUCCESS ){
5661                    // update last update date
5662                    sql = "UPDATE " + dbprefix + "_xnpaccount_item_basic SET last_update_date="
5663                        + unsignedIntToString( now ) + " WHERE item_id="
5664                        + unsignedIntToString( itemid );
5665                    querySimple( "insertChangeLog", sql );
5666                  ret = RES_OK;                  ret = RES_OK;
5667              }else{              }else{
5668                  string s( "SQLExecute in insertChangeLog " );                  string s( "SQLExecute in insertChangeLog " );
# Line 5606  result_t insertChangeLog( sessionid_t si Line 5681  result_t insertChangeLog( sessionid_t si
5681      return ret;      return ret;
5682  }  }
5683    
5684    /**
5685     *
5686     *
5687     * @param pmid PUBMEDID
5688     * @param pubmed 結果を書き込むpubmed_tのポインタ引数
5689     *
5690     *
5691     *
5692     */
5693    result_t pubmedComplete( pubmedid_t pmid, const pubmed_t** pubmed )
5694    {
5695        result_t ret = RES_ERROR;
5696        
5697        string url( PUBMED_EFETCH_URL_BASE );
5698        url += unsignedIntToString( pmid );
5699        *pubmed = new pubmed_t[1];
5700        if( streamPubmedFile( url.c_str(), ( pubmed_t* )*pubmed ) == 0 ) ret = RES_OK;
5701        return ret;
5702    }
5703    
5704  void freeIndex( const index_t* ptr ){ delete[] ptr; }  void freeIndex( const index_t* ptr ){ delete[] ptr; }
5705  void freeIndexID( const indexid_t* ptr ){ delete[] ptr; }  void freeIndexID( const indexid_t* ptr ){ delete[] ptr; }
5706  void freeAccount( const account_t* ptr ){ delete[] ( account_t* )ptr; }  void freeAccount( const account_t* ptr ){ delete[] ( account_t* )ptr; }
# Line 5617  void freeItem( const item_t* ptr ){ dele Line 5712  void freeItem( const item_t* ptr ){ dele
5712  void freeItemID( const itemid_t* ptr ){ delete[] ( itemid_t* )ptr; }  void freeItemID( const itemid_t* ptr ){ delete[] ( itemid_t* )ptr; }
5713  void freeString( char* str ){ delete[] str; }  void freeString( char* str ){ delete[] str; }
5714  void freeChangeLog( const changelog_t* ptr ){ delete[] ( changelog_t* )ptr; }  void freeChangeLog( const changelog_t* ptr ){ delete[] ( changelog_t* )ptr; }
5715    void freePubmed( const pubmed_t* ptr ){ delete[] ( pubmed_t* )ptr; }
5716    
5717  static string errstr;  static string errstr;
5718  const char* getLastErrorString()  const char* getLastErrorString()
# Line 5632  void setLastErrorString( const char* str Line 5728  void setLastErrorString( const char* str
5728      syslog( LOG_DEBUG, "%s", str );      syslog( LOG_DEBUG, "%s", str );
5729      closelog( );      closelog( );
5730  #endif  #endif
5731      //fprintf( stderr, "\nsetLastErrorString( '%s' );\n", str );      fprintf( stderr, "\nsetLastErrorString( '%s' );\n", str );
5732    }
5733    
5734    
5735    //現在スキャン中のノードのテキストを取り出す
5736    //失敗時はNULL
5737    //使用後はxmlFreeで解放の必要あり
5738    static xmlChar* getChildText(xmlTextReaderPtr reader)
5739    {
5740        if( xmlTextReaderRead(reader) == 1 ){
5741            if( xmlTextReaderNodeType( reader ) == XML_READER_TYPE_TEXT ){
5742                return xmlTextReaderValue( reader );
5743            }
5744        }
5745        return NULL;
5746    }
5747    
5748    static int streamPubmedFile(const char *filename, pubmed_t* p)
5749    {
5750        xmlTextReaderPtr reader;
5751        int ret;
5752        
5753        reader = xmlNewTextReaderFilename(filename);
5754        if( reader != NULL ){
5755            ret = xmlTextReaderRead(reader);
5756            while( ret == 1 ){
5757                processEfetch(reader, p);
5758                ret = xmlTextReaderRead(reader);
5759            }
5760            xmlFreeTextReader(reader);
5761            if( ret != 0 ){
5762                //printf("%s : failed to parse\n", filename);
5763            }
5764        }else{
5765            //printf("Unable to open %s\n", filename);
5766        }
5767        xmlErrorPtr err = xmlGetLastError();
5768        if( err != NULL ){
5769            setLastErrorString( err -> message );
5770        }
5771        return ret;
5772  }  }
5773    
5774    static void processEfetch(xmlTextReaderPtr reader, pubmed_t* p )
5775    {
5776        xmlChar* name = NULL;
5777        xmlChar* value = NULL;
5778        
5779        name = xmlTextReaderName(reader);
5780        if( name == NULL)
5781            name = xmlStrdup(BAD_CAST "--");
5782        value = xmlTextReaderValue(reader);
5783        
5784        if( xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT ){
5785            if( strcmp( "PMID", ( char* )name ) == 0 ){
5786                if( xmlTextReaderRead(reader) == 1 ){
5787                    if( xmlTextReaderNodeType( reader ) == XML_READER_TYPE_TEXT ){
5788                        xmlChar* value = xmlTextReaderValue( reader );
5789                        //printf( "PMID=%s\n", value );
5790                        p -> setID( atoi( ( char* )value ) );
5791                        xmlFree( value );
5792                    }
5793                }
5794            }else if( strcmp( "ArticleTitle", ( char* )name ) == 0 ){
5795                xmlChar* val = getChildText( reader );
5796                p -> setTitle( ( char* )val );
5797                if( val != NULL ) xmlFree( val );
5798            }else if( strcmp( "ForeName", ( char* )name ) == 0 ){
5799                xmlChar* val = getChildText( reader );
5800                int len = strlen( p -> getAuthor( ) ) + strlen( ( char* )val )
5801                    + 2;// ' ', '\0'
5802                char* buf = new char[ len ];
5803                snprintf( buf, len, "%s %s", p -> getAuthor( ), val );
5804                p -> setAuthor( ( char* )buf );
5805                if( val != NULL ) xmlFree( val );
5806                delete[] buf;
5807            }else if( strcmp( "LastName", ( char* )name ) == 0 ){
5808                xmlChar* val = getChildText( reader );
5809                if( strlen( p -> getAuthor( ) ) > 0 ){
5810                    int len = strlen( p -> getAuthor( ) ) + strlen( ( char* )val )
5811                        + 3;// '\n', ' ', '\0'
5812                    char* buf = new char[ len ];
5813                    snprintf( buf, len, "%s\n%s", p -> getAuthor( ), val );
5814                    p -> setAuthor( ( char* )buf );
5815                    delete[] buf;
5816                }else{
5817                    p -> setAuthor( ( char* )val );
5818                }
5819                if( val != NULL ) xmlFree( val );
5820            }else if( strcmp( "MedlineTA", ( char* )name ) == 0 ){
5821                xmlChar* val = getChildText( reader );
5822                p -> setJournal( ( char* )val );
5823                if( val != NULL ) xmlFree( val );
5824            }else if( strcmp( "Year", ( char* )name ) == 0 ){//TODO
5825                xmlChar* val = getChildText( reader );
5826                p -> setPubYear( atoi( ( char* )val ) );
5827                if( val != NULL ) xmlFree( val );
5828            }else if( strcmp( "Volume", ( char* )name ) == 0 ){
5829                xmlChar* val = getChildText( reader );
5830                p -> setVolume( atoi( ( char* )val ) );
5831                if( val != NULL ) xmlFree( val );
5832            }else if( strcmp( "Issue", ( char* )name ) == 0 ){
5833                xmlChar* val = getChildText( reader );
5834                p -> setNumber( atoi( ( char* )val ) );
5835                if( val != NULL ) xmlFree( val );
5836            }else if( strcmp( "MedlinePgn", ( char* )name ) == 0 ){
5837                xmlChar* val = getChildText( reader );
5838                p -> setPage( ( char* )val );
5839                if( val != NULL ) xmlFree( val );
5840            }else if( strcmp( "AbstractText", ( char* )name ) == 0 ){
5841                xmlChar* val = getChildText( reader );
5842                p -> setAbstract( ( char* )val );
5843                if( val != NULL ) xmlFree( val );
5844            }
5845        }
5846        xmlFree(name);
5847        
5848        if( value != NULL) xmlFree(value);
5849    }
5850    
5851    /*
5852    void main( int argc, char* argv[] )
5853    {
5854        pubmed_t p;
5855        
5856        printf( "result in %d\n", streamFile( argv[1], &p ) );
5857        printf( "'%d'\n"
5858                "'%s'\n"
5859                "'%s'\n"
5860                "'%s'\n"
5861                "'%d'\n"
5862                "'%d'\n"
5863                "'%d'\n"
5864                "'%s'\n"
5865                "'%s'\n",
5866                p.getID( ),
5867                p.getTitle( ),
5868                p.getAuthor( ),
5869                p.getJournal( ),
5870                p.getPubYear( ),
5871                p.getVolume( ),
5872                p.getNumber( ),
5873                p.getPage( ),
5874                p.getAbstract( ) );
5875    }
5876    */

Legend:
Removed from v.1.45  
changed lines
  Added in v.1.46

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26