Exemplo n.º 1
0
    def get_search_results(self, req, terms, filters):
        if not 'changesethyperest' in filters:
            return
        #estcmd.exeのパス
        estcmd_path = self.env.config.get('searchhyperestraier', 'estcmd_path',
                                          'estcmd')
        #estcmd.exeの引数
        estcmd_arg = self.env.config.get('searchhyperestraier', 'estcmd_arg',
                                         'search -vx -sf -ic Shift_JIS')
        estcmd_encode = self.env.config.get('searchhyperestraier',
                                            'estcmd_encode', 'mbcs')

        #for multi repos
        for option in self.config['searchhyperestraier']:
            #リポジトリのパス
            if not option.endswith('.cs_index_path'):
                continue
            mrepstr = option[:-len(
                '.cs_index_path')]  #'.cs_index_path'の前の文字列がreponame
            if RepositoryManager(self.env).get_repository(
                    mrepstr) is None:  #mrepstrのrepositoryがない
                continue
            repoinfo = RepositoryManager(self.env).get_all_repositories().get(
                mrepstr, {})
            #self.log.debug('type:%s' % repoinfo.get('type'))
            if repoinfo.get(
                    'type') != 'direct-svnfs':  #'direct-svnfs'のリポジトリでない
                continue
            #インデックスのパス
            cs_index_path = self.env.config.get('searchhyperestraier',
                                                mrepstr + '.cs_index_path', '')
            if cs_index_path == '':  #mrepstr+'.cs_index_path'がない
                continue
            if mrepstr != '':  #defaultでない
                mrepstr = '/' + mrepstr

            #cmdline = "%s %s %s %s" % (estcmd_path,estcmd_arg,cs_index_path,unicode(query,'utf-8').encode('CP932'))
            qline = ' '.join(terms)
            cmdline = "%s %s %s %s" % (estcmd_path, estcmd_arg, cs_index_path,
                                       qline)
            self.log.debug('SearchChangesetHyperEstraier:%s' % cmdline)
            cmdline = unicode(cmdline).encode(estcmd_encode)
            np = NaivePopen(cmdline)
            #self.log.debug('Result:%s' % unicode(np.out,'utf-8').encode('mbcs'))
            #self.log.debug('Result:%s' % np.out)
            if np.errorlevel or np.err:
                err = 'Running (%s) failed: %s, %s.' % (cmdline, np.errorlevel,
                                                        np.err)
                raise Exception, err
            if np.out == '':  #何も入ってない
                continue
            dom = parseString(np.out)
            root = dom.documentElement
            #estresult_node = root.getElementsByTagName("document")[0]
            element_array = root.getElementsByTagName("document")
            for element in element_array:
                #self.log.debug('Result:%s' % 'hoge')
                url = ""
                title = ""
                date = 0
                detail = ""
                author = "不明"

                #detailを生成
                elem_array = element.getElementsByTagName("snippet")
                detail = self._get_innerText("", elem_array)

                #その他の属性を生成
                attrelem_array = element.getElementsByTagName("attribute")
                for attrelem in attrelem_array:
                    attr_name = attrelem.getAttribute("name")
                    attr_value = unicode(attrelem.getAttribute("value"))
                    #URLとタイトルを生成
                    if attr_name == "_lreal":
                        attr_value = attr_value.replace(".txt", "")
                        end = len(attr_value)
                        for m in range(1, end):
                            if not attr_value[(end - m):].isdigit():
                                break
                        attr_value = attr_value[
                            (end - m + 1):] + mrepstr  #数字の文字列 + mrepstr
                        url = self.env.href('/changeset/' + attr_value)
                        title = "changeset:" + attr_value
                    #更新日時を生成
                    elif attr_name == "@mdate":
                        date = time.strptime(attr_value, "%Y-%m-%dT%H:%M:%SZ")
                        self.log.debug('date:%s' % attr_value)
                        date = to_datetime(
                            datetime.datetime(date[0], date[1], date[2],
                                              date[3], date[4], date[5], 0,
                                              utc))  # for Trac0.11
                yield (url, title, date, to_unicode(author, 'utf-8'),
                       to_unicode(detail, 'utf-8'))
        return