3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Solr_Importer_Ee { protected $_restClient = null; protected $_solr = null; public function __construct(Solr $solr) { $this->_solr = $solr; $this->_restClient = $solr->getRestClient(); } public function update() { $this->_bootstrap(); $items = $this->getItems('updated_date > '.$GLOBALS['_SOLR']['lastRunTime']); $client = $this->_restClient; $cores = $this->_solr->config('ee_cores'); $client->setCore($cores[0]); $model = new Solr_Model_Ee(); foreach ($items as $item) { $client->add($model->as_document($item)); } } public function reindex() { $this->_bootstrap(); $client = $this->_restClient; $cores = $this->_solr->config('ee_cores'); $liveCore = $cores[0]; $deckCore = $cores[1]; echo "using $deckCore...\n"; $this->_restClient->setCore($deckCore); $this->_restClient->deleteIndex(); echo "Starting data import into index...\n"; @set_time_limit(-1); // Expand upon ExpressionEngines timeouts, set to infinite. $items = $this->getItems(); $model = new Solr_Model_Ee(); foreach ($items as $item) { $client->add($model->as_document($item)); } $this->_restClient->swapCores($deckCore, $liveCore); } /** * Bootstraps EE * @throws Exception * @return */ protected function _bootstrap() { try { $system_path = realpath($this->_solr->getAppDir().$this->_solr->config('ee_app_path')); set_include_path(get_include_path() . PATH_SEPARATOR . $system_path); // Here, if we don't create that controller, it won't work // class Required_Controller extends CI_Controller {} $assign_to_config['enable_query_strings'] = TRUE; $assign_to_config['subclass_prefix'] = 'EE_'; if (realpath($system_path) !== FALSE) { $system_path = realpath($system_path).'/'; } // ensure there's a trailing slash $system_path = rtrim($system_path, '/').'/'; $_SERVER['REQUEST_URI'] = 'solr.gif'; define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); define('EXT', '.php'); define('BASEPATH', str_replace("\\", "/", $system_path.'codeigniter/system/')); define('APPPATH', $system_path.'expressionengine/'); define('FCPATH', str_replace(SELF, '', __FILE__)); define('SYSDIR', trim(strrchr(trim(str_replace("\\", "/", $system_path), '/'), '/'), '/')); define('UTF8_ENABLED', false); define('CI_VERSION', '2.0'); //define('AJAX_REQUEST',false); define('DEBUG',false); require BASEPATH.'core/Common'.EXT; require APPPATH.'config/constants'.EXT; $CFG =& load_class('Config', 'core'); $URI =& load_class('URI', 'core'); $IN =& load_class('Input', 'core'); $OUT =& load_class('Output', 'core'); $LANG =& load_class('Lang', 'core'); $SEC =& load_class('Security', 'core'); $loader = load_class('Loader', 'core'); // Load the base controller class require BASEPATH.'core/Controller'.EXT; function &get_instance() { return CI_Controller::get_instance(); } $EE = @new Solr_Importer_Ee_Bootstrap; $this->EE = $EE; return $this->EE; } catch (Exception $e) { throw new Exception($e->getMessage()); } } public function getItems($filter = null) { $channelList = $this->_solr->config('channels'); $SOLR_entries = array(); $site_mapping = $this->_solr->config('ee_sites'); $configSites = $this->_solr->config('ee_sites'); if ( ! is_array( $configSites ) ) { die('Site and Store IDs need to be defined in config.php'); } // retrieve all channels we have defined in the config. $this->EE->db->where_in('channel_name', array_keys($channelList)); $channels = $this->EE->db->get('channels' )->result(); foreach($channels as $channel) { // Retrieve all custom fields $fields = $this->EE->db->get_where('channel_fields', array( 'group_id' => $channel->field_group ) )->result(); //Grab all the categories for an entry $this->EE->db->select('(select GROUP_CONCAT(cat_name) from exp_category_posts cp join exp_categories c on c.cat_id = cp.cat_id where cp.entry_id = exp_d.entry_id) as cats', false); // Build out which custom fields we need and get entries $channel_title_fields = array('entry_id','title','url_title','status','year','month','day','entry_date'); foreach( $channel_title_fields as $i) $this->EE->db->select('t.' . $i); $this->EE->db->select('unix_timestamp(t.edit_date) as updated_date'); $this->EE->db->join('channel_titles t', 't.entry_id=d.entry_id'); $this->EE->db->join('channel_data pd', 'pd.entry_id=d.entry_id'); //$this->EE->db->select('pl.short_name')->join('publisher_languages pl', 'pl.id=pd.publisher_lang_id'); $field_map = array(); foreach($fields as $field) { $this->EE->db->select("pd.field_id_{$field->field_id}"); $field_map[ 'field_' . $field->field_name] = 'field_id_' . $field->field_id; } $this->EE->db->where('d.channel_id', $channel->channel_id); if ($filter) { $filter = str_replace('updated_date', 'unix_timestamp(t.edit_date)', $filter); $this->EE->db->where($filter); } $this->EE->db->where('t.status', 'open'); $entries = $this->EE->db->get('channel_data exp_d'); // Build items. foreach( $entries->result() as $entry ) { $base_item = $this->makeItem( $channel, $field_map, $entry ); /** * Figure out solr id for content */ $base_item['site_id'] = 1;//hard coding until multi site is needed - $site_mapping[$entry->short_name]; $SOLR_entries[] = $base_item; } } return $SOLR_entries; } function makeItem( $channel, $fieldmap, $entry ) { $data = array(); $tmpdata = array(); $data['entry_id'] = $entry->entry_id; $data['channel_id'] = $channel->channel_id; $data['channel_name'] = $channel->channel_name; $data['channel_title'] = htmlentities($channel->channel_title); $data['title'] = $entry->title; $data['status'] = $entry->status; $data['updated'] = $entry->entry_date; $data['category'] = explode(',',$entry->cats); //$data['fields'] = Solr::config('defaults'); $channelRules = $this->_solr->config('channels'); if ( array_key_exists( $channel->channel_name, $channelRules ) ) { // if this channel has custom fields $fields = $channelRules[$channel->channel_name]; foreach($fields as $field => $rules) { is_array($rules) or $rules = array( 'type' => '', 'value' => $rules); preg_match_all('/@\!(\w+)/', $rules['value'], $matches); // handle tmp fields foreach($matches[1] as $match) { $tmp = array_key_exists($match, $tmpdata) ? $tmpdata[$match] : ''; $rules['value'] = str_replace('@!' . $match, $tmp, $rules['value']); } unset($matches); preg_match_all('/@(\w+)/', $rules['value'], $matches); foreach($matches[1] as $orig_match) { $match = array_key_exists($orig_match, $fieldmap) ? $fieldmap[$orig_match] : $orig_match; $tmp = property_exists($entry, $match) ? $entry->{$match} : (property_exists($channel, $match) ? $channel->{$match}: $match); $rules['value'] = str_replace('@' . $orig_match, $tmp, $rules['value']); } unset($matches); switch ($rules['type']) { case 'expression': $rules['value'] = $this->parse( $rules['value'] ); break; default: $rules['value'] = filter_var( strip_tags($rules['value']), FILTER_SANITIZE_STRING); //$rules['value'] = filter_var($rules['value'], FILTER_SANITIZE_STRING); } switch (@$rules['save']) { case 'tmp': $tmpdata[$field] = $rules['value']; break; default: $data['fields'][$field] = $rules['value']; } } } return $data; } }
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6

preferences:
272 ms | 404 KiB | 406 Q