<commit_before><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

/** notification manager for solr module */
class Solr_Notification extends MIDAS_Notification
  {
  public $moduleName = 'solr';
  public $_models = array('Item', 'ItemRevision', 'Metadata');
  public $_moduleComponents = array('Solr');

  /** init notification process */
  public function init()
    {
    $this->addCallBack('CALLBACK_CORE_ITEM_SAVED', 'indexItem');
    $this->addCallBack('CALLBACK_CORE_ITEM_DELETED', 'itemDeleted');
    $this->addCallBack('CALLBACK_CORE_ITEM_SEARCH_DEFAULT_BEHAVIOR_OVERRIDE', 'itemSearch');

    $this->addCallBack('CALLBACK_CORE_GET_DASHBOARD', 'getAdminDashboard');

    $this->addTask('TASK_CORE_RESET_ITEM_INDEXES', 'resetItemIndexes', 'Recompute lucene indexes');
    }

  /** Add a tab to the manage community page for size quota */
  public function indexItem($args)
    {
    $item = $args['item'];
    try
      {
      $index = $this->ModuleComponent->Solr->getSolrIndex();
      }
    catch(Exception $e)
      {
      $this->getLogger()->warn($e->getMessage.' - Could not index item metadata ('.$item->getKey().')');
      return;
      }

    try
      {
      $response = $index->search('id: item_'.$item->getKey(), 0, 99999);
      foreach($response->response->docs as $doc)
        {
        $index->deleteById($doc->id);
        }
      if($response->response->numFound > 0)
        {
        $index->commit();
        }
      $doc = new Apache_Solr_Document();
      $doc->addField('id', 'item_'.$item->getKey());
      $doc->addField('key', $item->getKey());
      $doc->addField('name', $item->getName(), 3); //boost factor of 3 for name
      $doc->addField('description', $item->getDescription(), 2); //boost factor of 2 for description

      $revision = $this->Item->getLastRevision($item);

      if($revision != false)
        {
        $metadata = $this->ItemRevision->getMetadata($revision);
        $metadataString = '';

        foreach($metadata as $m)
          {
          $fieldName = $this->Metadata->mapTypeToName($m->getMetadatatype());
          $fieldName .= '-'.$m->getElement();
          if($m->getQualifier())
            {
            $fieldName .= '.'.$m->getQualifier();
            }
          $doc->addField($fieldName, $m->getValue());
          if(!is_numeric($m->getValue()))
            {
            $metadataString .= ' '.$m->getValue();
            }
          }
        $doc->addField('metadata', $metadataString);

        $bitstreams = $revision->getBitstreams();
        $md5String = '';
        foreach($bitstreams as $bitstream)
          {
          $md5String = $bitstream->getChecksum().' ';
          }
        $doc->addField('md5', trim($md5String));
        }
      $index->addDocument($doc);
      $index->commit();
      }
    catch(Exception $e)
      {
      $this->getLogger()->warn('Error saving item to Solr index: '.$e->getMessage());
      }
    }

  /**
   * Override the default item search behavior
   */
  public function itemSearch($args)
    {
    $query = $args['query'];
    $limit = $args['limit'];
    $user = $args['user'];

    $solrQuery = 'name: '.$query.<commit_msg>ENH: refs #718. Add item id to log message if solr indexing of item fails<commit_after><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

/** notification manager for solr module */
class Solr_Notification extends MIDAS_Notification
  {
  public $moduleName = 'solr';
  public $_models = array('Item', 'ItemRevision', 'Metadata');
  public $_moduleComponents = array('Solr');

  /** init notification process */
  public function init()
    {
    $this->addCallBack('CALLBACK_CORE_ITEM_SAVED', 'indexItem');
    $this->addCallBack('CALLBACK_CORE_ITEM_DELETED', 'itemDeleted');
    $this->addCallBack('CALLBACK_CORE_ITEM_SEARCH_DEFAULT_BEHAVIOR_OVERRIDE', 'itemSearch');

    $this->addCallBack('CALLBACK_CORE_GET_DASHBOARD', 'getAdminDashboard');

    $this->addTask('TASK_CORE_RESET_ITEM_INDEXES', 'resetItemIndexes', 'Recompute lucene indexes');
    }

  /** Add a tab to the manage community page for size quota */
  public function indexItem($args)
    {
    $item = $args['item'];
    try
      {
      $index = $this->ModuleComponent->Solr->getSolrIndex();
      }
    catch(Exception $e)
      {
      $this->getLogger()->warn($e->getMessage.' - Could not index item metadata ('.$item->getKey().')');
      return;
      }

    try
      {
      $response = $index->search('id: item_'.$item->getKey(), 0, 99999);
      foreach($response->response->docs as $doc)
        {
        $index->deleteById($doc->id);
        }
      if($response->response->numFound > 0)
        {
        $index->commit();
        }
      $doc = new Apache_Solr_Document();
      $doc->addField('id', 'item_'.$item->getKey());
      $doc->addField('key', $item->getKey());
      $doc->addField('name', $item->getName(), 3); //boost factor of 3 for name
      $doc->addField('description', $item->getDescription(), 2); //boost factor of 2 for description

      $revision = $this->Item->getLastRevision($item);

      if($revision != false)
        {
        $metadata = $this->ItemRevision->getMetadata($revision);
        $metadataString = '';

        foreach($metadata as $m)
          {
          $fieldName = $this->Metadata->mapTypeToName($m->getMetadatatype());
          $fieldName .= '-'.$m->getElement();
          if($m->getQualifier())
            {
            $fieldName .= '.'.$m->getQualifier();
            }
          $doc->addField($fieldName, $m->getValue());
          if(!is_numeric($m->getValue()))
            {
            $metadataString .= ' '.$m->getValue();
            }
          }
        $doc->addField('metadata', $metadataString);

        $bitstreams = $revision->getBitstreams();
        $md5String = '';
        foreach($bitstreams as $bitstream)
          {
          $md5String = $bitstream->getChecksum().' ';
          }
        $doc->addField('md5', trim($md5String));
        }
      $index->addDocument($doc);
      $index->commit();
      }
    catch(Exception $e)
      {
      $this->getLogger()->warn('Error saving item ('.$item->getKey().') to Solr index: '.$e->getMessage());
      }
    }

  /**
   * Override the default item search behavior
   */
  public function itemSearch($args)
    {
    $query = $args['query'];
    $limit = $args['limit'];
    $user = $args['user'];

    $solrQuery = 'name: '.$query.
{'fraction_non_alphanumeric': 0.18690654672663667, 'fraction_numerical': 0.006871564217891054, 'mean_word_length': 2.8934824902723735, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '217824', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2266, 'n_words': 546}