Changeset 1186

Show
Ignore:
Timestamp:
06/26/10 20:41:41 (3 years ago)
Author:
d0nut
Message:

solr crawler finished, solr status page finished, need some kind of automatism

Location:
solr
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • solr/acptemplates/solr.tpl

    r1185 r1186  
    2020.statBarLabel { 
    2121        margin-left: 410px; 
    22 }                        
     22} 
    2323/*]]>*/ 
    2424</style> 
    2525{/capture} 
    2626{include file='header'} 
    27          
     27 
    2828<div class="mainHeadline"> 
    29         <img src="{@RELATIVE_WCF_DIR}icon/statsL.png" alt="" /> 
     29        <img src="{@RELATIVE_WCF_DIR}icon/solrL.png" alt="" /> 
    3030        <div class="headlineContainer"> 
    31                 <h2>{lang}wcf.acp.stats{/lang}</h2> 
     31                <h2>{lang}wcf.acp.solr{/lang}</h2> 
    3232        </div> 
    3333</div> 
     
    3838 
    3939{if $results|isset && !$results|count} 
    40         <p class="error">{lang}wcf.acp.stats.noResults{/lang}</p> 
     40        <p class="error">{lang}wcf.acp.solr.noResults{/lang}</p> 
    4141{/if} 
    4242{* 
    43 <form  method="post" action="index.php?form=Stats"> 
     43<form  method="post" action="index.php?form=Solr"> 
    4444        <div class="border content"> 
    4545                <div class="container-1"> 
    4646                        <fieldset> 
    47                                 <legend>{lang}wcf.acp.stats.config{/lang}</legend> 
    48                                  
     47                                <legend>{lang}wcf.acp.solr.config{/lang}</legend> 
     48 
    4949                                <div class="formElement{if $errorField == 'username'} formError{/if}"> 
    5050                                        <div class="formFieldLabel"> 
     
    6060                                        </div> 
    6161                                </div> 
    62                                  
     62 
    6363                                {if $additionalFields|isset}{@$additionalFields}{/if} 
    6464                        </fieldset> 
    6565                </div> 
    6666        </div> 
    67                  
     67 
    6868        <div class="formSubmit"> 
    6969                <input type="submit" accesskey="s" value="{lang}wcf.global.button.submit{/lang}" /> 
     
    7878                <div class="container-1"> 
    7979                        <fieldset> 
    80                                 <legend>{lang}wcf.acp.stats.results{/lang}</legend> 
    81          
     80                                <legend>{lang}wcf.acp.solr.results{/lang}</legend> 
     81 
    8282                                {foreach from=$results item=result key=$type} 
    8383                                        <div class="formElement"> 
    8484                                                <p class="formFieldLabel">{$type}</p> 
    85                                                 <div class="formField"><div class="statBar"><div style="width: {$result.percent|round}%;"></div></div><p class="statBarLabel">{#$result.current}/{#$result.total}</p></div> 
     85                                                <div class="formField"> 
     86                                                        <div class="statBar"><div style="width: {$result.percent|round}%;"></div></div> 
     87                                                        <p class="statBarLabel"> 
     88                                                                {$result.percent|round}% {#$result.current}/{#$result.total} 
     89                                                        </p> 
     90                                                </div> 
    8691                                        </div> 
    87                                  
     92 
    8893                                {/foreach} 
    8994                        </fieldset> 
  • solr/files/lib/acp/form/SolrForm.class.php

    r1185 r1186  
    2626         */ 
    2727        public $activeMenuItem = 'wcf.acp.menu.link.solr.index'; 
    28          
    29         /** 
    30          * Permission 
    31          * 
    32          * @var string 
    33          */ 
    34         public $neededPermissions = 'admin.contest.canAddClass'; 
    3528 
    3629 
     
    4437                parent::readFormParameters(); 
    4538 
    46                 if (isset($_POST['topic'])) $this->topic = StringUtil::trim($_POST['topic']); 
     39                if (isset($_POST['type'])) $this->type = StringUtil::trim($_POST['type']); 
    4740        } 
    4841 
     
    5548                $this->bridge = new SolrBridge(); 
    5649                $this->status = $this->bridge->getIndexStatus(); 
    57         } 
    58  
    59         /** 
    60          * @see Form::save() 
    61          */ 
    62         public function save() { 
    63                 parent::save(); 
    64  
    65                 // save 
    66                 $this->contestClass = ContestClassEditor::create($this->topic, $this->text,  
    67                         $this->parentClassID, $this->position, WCF::getLanguage()->getLanguageID()); 
    68                 $this->saved(); 
    69  
    70                 // reset values 
    71                 $this->topic = $this->text = $this->parentClassID = ''; 
    72                 $this->languageID = $this->position = 0; 
    73  
    74                 // show success message 
    75                 WCF::getTPL()->assign('success', true); 
     50                 
     51                # demo call for crawling 
     52                # $this->bridge->doCrawl(array('lexicon'), 50); 
    7653        } 
    7754 
  • solr/files/lib/data/solr/SolrBridge.php

    r1185 r1186  
    77/** 
    88 * 
    9  */      
     9 */ 
    1010class SolrBridge { 
    11          
    12         /** 
    13          *  
     11 
     12        /** 
     13         * 
    1414         * @var array<Apache_Solr_Document> 
    1515         */ 
    1616        protected $documents = array(); 
    17          
     17 
    1818        /** 
    1919         * 
     
    2121         */ 
    2222        protected $solr = null; 
    23          
     23 
     24        protected static $typeids = null; 
     25 
    2426        /** 
    2527         * 
    2628         */ 
    2729        public function __construct() { 
    28          
     30 
    2931                // load search type objects 
    3032                SearchEngine::getSearchTypes(); 
    31                  
     33 
    3234                $this->solr = new SolrService(); 
    3335        } 
    34          
     36 
    3537        /** 
    3638         * 
    3739         */ 
    3840        protected function commit() { 
    39                 $this->solr->addDocuments( $this->documents ); 
    40                 $this->solr->commit(); 
    41                  
     41                #$this->solr->addDocuments( $this->documents ); 
     42                #$this->solr->commit(); 
     43 
    4244                // mark items as done 
    4345                $sql = "INSERT IGNORE INTO 
    44                                         wcf".WCF_N."_solr_index 
    45                                         (typeID, messageID)"; 
     46                                wcf".WCF_N."_solr_index 
     47                                (typeID, messageID) 
     48                        VALUES "; 
    4649                foreach($this->documents as $doc) { 
    47                         $sql .= ""; //TODO: mark as done 
    48                 } 
     50                        $typeID = $this->getTypeID($doc->messageType); 
     51                        $sql .= "($typeID,".$doc->messageID."),"; 
     52                } 
     53                $sql = rtrim($sql, ','); 
    4954                $result = WCF::getDB()->sendQuery($sql); 
    50                  
     55 
    5156                // reset array 
    5257                $this->documents = array(); 
    53                  
    54                 $this->solr->optimize(); 
    55         } 
    56          
     58 
     59                // optimize solr index 
     60                #$this->solr->optimize(); 
     61        } 
     62 
    5763        /** 
    5864         * 
     
    6773                        if (!$doc->isAccessible()) continue; 
    6874                        if (!empty($sql)) $sql .= "\nUNION\n"; 
    69                          
     75 
    7076                        // get field names 
    7177                        $messageIDFieldName = $doc->getIDFieldName(); 
    7278                        $messageIDFieldName = strpos($messageIDFieldName, '.') !== false ? $messageIDFieldName : "messageTable.".$messageIDFieldName; 
    73                          
    74                         $sql .= "(       
     79 
     80                        $sql .= "( 
    7581                                SELECT          ".$func."(".$messageIDFieldName.") AS messageID, 
    7682                                                '".$type."' AS messageType 
     
    7985                        )"; 
    8086                } 
    81                  
     87 
    8288                // send search query 
    8389                $types = array(); 
     
    8692                        $types[$row['messageType']] = $row['messageID']; 
    8793                } 
    88                  
     94 
    8995                return $types; 
    9096        } 
    91          
    92         /** 
    93          * 
    94          */ 
    95         public function loadDocuments($type, $min, $max) { 
     97 
     98        private function cleanText($message) { 
     99                require_once(WCF_DIR.'lib/data/message/bbcode/MessageParser.class.php'); 
     100 
     101                // add cache resources 
     102                WCF::getCache()->addResource('bbcodes', WCF_DIR.'cache/cache.bbcodes.php', WCF_DIR.'lib/system/cache/CacheBuilderBBCodes.class.php'); 
     103                WCF::getCache()->addResource('smileys', WCF_DIR.'cache/cache.smileys.php', WCF_DIR.'lib/system/cache/CacheBuilderSmileys.class.php'); 
     104 
     105                $parser = MessageParser::getInstance(); 
     106                $parser->setOutputType('text/plain'); 
     107                $message = StringUtil::stripHTML($message); 
     108                return $parser->parse($message, false, false, true, false); 
     109        } 
     110 
     111        /** 
     112         * 
     113         * @return      integer number of added documents 
     114         */ 
     115        public function loadDocuments($type, $min, $max, $limit) { 
    96116                // get search type object 
    97117                $doc = SearchEngine::$searchTypeObjects[$type]; 
    98118                if (!$doc->isAccessible()) continue; 
    99                  
     119 
    100120                // get field names 
    101121                $messageIDFieldName = $doc->getIDFieldName(); 
     
    106126                $usernameFieldName = $doc->getUsernameFieldName(); 
    107127                $timeFieldName = $doc->getTimeFieldName(); 
    108                  
    109                 $sql = "SELECT           
     128 
     129                $sql = "SELECT 
    110130                                        '".$type."' AS messageType, 
    111131                                        ".$messageIDFieldName." AS messageID, 
     
    118138                                        ".$doc->getJoins()." 
    119139                        WHERE           ".$messageIDFieldName." BETWEEN $min AND $max 
    120                                         ".(!empty($conditions[$type]) ? " ".(!empty($q) ? "AND" : "")." (".$conditions[$type].")" : "")." 
    121                         GROUP BY        messageID"; 
     140                        GROUP BY        messageID 
     141                        ORDER BY        messageID ASC"; 
    122142 
    123143                $result = WCF::getDB()->sendQuery($sql, $limit); 
     144                $i = 0; 
    124145                while ($row = WCF::getDB()->fetchArray($result)) { 
     146                        $row['message'] = $this->cleanText($row['message']); 
    125147                        $this->addDocument($row); 
    126                 } 
    127         } 
    128          
     148                        $i++; 
     149                } 
     150                return $i; 
     151        } 
     152 
    129153        /** 
    130154         * 
     
    142166                        } 
    143167                } 
    144                  
     168 
    145169                $this->documents[] = $part; 
    146170        } 
    147          
    148         /** 
    149          * 
    150          */ 
    151         public function doCrawl($types, $limit) { 
    152                 foreach($this->getIndexStatus($types) as $typeName => $status) { 
    153                  
     171 
     172        /** 
     173         * 
     174         */ 
     175        public function doCrawl($types = null, $limit = null) { 
     176 
     177                // get types 
     178                $types = is_array($types) ? $types : $this->getSearchTypes(); 
     179 
     180                $i = 0; 
     181 
     182                foreach($this->getIndexStatus($types, 'MAX') as $type => $status) { 
     183 
    154184                        // nothing to do? 
    155                         if($row['total'] == $row['current']) { 
     185                        if($status['total'] == $status['current']) { 
    156186                                continue; 
    157187                        } 
    158                          
     188                // get search type object 
     189                $doc = SearchEngine::$searchTypeObjects[$type]; 
     190                if (!$doc->isAccessible()) continue; 
     191 
    159192                        if (!isset(SearchEngine::$searchTypeObjects[$type])) { 
    160193                                throw new SystemException('unknown search type '.$type, 101001); 
    161194                        } 
     195                        $j = $this->loadDocuments($type, $status['current'] + 1, $status['total'], $limit); 
     196                        if($j) { 
     197                                $i += $j; 
     198 
     199                                // write to solr 
     200                                $this->commit(); 
     201                        } 
     202                } 
     203 
     204                return $i; 
     205        } 
     206 
     207        private function getTypeID($type) { 
     208 
     209                if(self::$typeids === null) { 
     210                        self::$typeids = array(); 
     211 
     212                        $sql = 'SELECT          * 
     213                                FROM            wcf'.WCF_N.'_searchable_message_type'; 
     214                        $result = WCF::getDB()->sendQuery($sql); 
     215                        while ($row = WCF::getDB()->fetchArray($result)) { 
     216                                self::$typeids[$row['typeName']] = $row['typeID']; 
     217                        } 
     218                } 
     219                return self::$typeids[$type]; 
     220        } 
     221         
     222        private function getSearchTypes() { 
     223                $types = SearchEngine::getSearchTypes(); 
     224                $return = array(); 
     225                foreach($types as $type) { 
     226                        $doc = SearchEngine::$searchTypeObjects[$type]; 
     227                        if (!$doc->isAccessible()) continue; 
    162228                         
    163                         $this->loadDocuments($row['current'] + 1, min($row['total'], $row['current'] + 1 + $limit)); 
    164  
    165                         // write to solr 
    166                         $this->commit(); 
    167                 } 
    168         } 
    169          
    170         /** 
    171          *  
    172          */ 
    173         public function getIndexStatus($types = null) { 
    174                  
     229                        $return[] = $type; 
     230                } 
     231                return $return; 
     232        } 
     233 
     234        /** 
     235         * 
     236         */ 
     237        public function getIndexStatus($types = null, $func = 'COUNT') { 
     238 
    175239                // read available types 
    176240                $status = array(); 
    177                  
     241 
    178242                // get types 
    179                 $types = is_array($types) ? $types : SearchEngine::getSearchTypes(); 
    180                  
     243                $types = is_array($types) ? $types : $this->getSearchTypes(); 
     244 
    181245                // set counters to zero 
    182246                foreach ($types as $type) { 
     
    187251                        ); 
    188252                } 
    189                  
     253 
    190254                // read current status 
    191255                $sql = 'SELECT          typeName, 
    192256                                        c 
    193257                        FROM ( 
    194                                 SELECT          typeID,  
    195                                                 COUNT(typeID) AS c 
    196                                 FROM            wcf'.WCF_N.'_solr_index  
     258                                SELECT          typeID, 
     259                                                '.$func.'(messageID) AS c 
     260                                FROM            wcf'.WCF_N.'_solr_index 
    197261                                GROUP BY        typeID 
    198262                        ) x 
    199                         INNER JOIN      wcf'.WCF_N.'_searchable_message_type USING(typeID)'; 
    200                 while ($row = WCF::getDB()->fetchArray($sql)) { 
     263                        INNER JOIN      wcf'.WCF_N.'_searchable_message_type USING(typeID) 
     264                        WHERE           typeName IN ("'.implode('","', $types).'")'; 
     265                $result = WCF::getDB()->sendQuery($sql); 
     266                while ($row = WCF::getDB()->fetchArray($result)) { 
    201267                        $typeName = $row['typeName']; 
    202268                        $status[$typeName]['current'] = $row['c']; 
     
    204270 
    205271                // read totals 
    206                 foreach ($this->getTotals($types, 'COUNT') as $typeName => $count) { 
    207                         $percent = $count ? 100 / $count * $status[$typeName]['current'] : 0; 
     272                foreach ($this->getTotals($types, $func) as $typeName => $count) { 
    208273                        $status[$typeName]['total'] = $count; 
    209                 } 
    210                  
     274                        $status[$typeName]['percent'] = $count ? 100 / $count * $status[$typeName]['current'] : 0; 
     275                } 
     276 
    211277                return $status; 
    212278        }