/** * DM2 creation callback, binds to the current content topic. */ private function _create_article($title) { $author = midcom::get('auth')->user->get_storage(); $article = new midcom_db_article(); $article->topic = $this->_content_topic->id; $article->title = $title; //Figure out author $article->author = $author->id; if (!$article->create()) { debug_print_r('We operated on this object:', $article); return null; } // Generate URL name if ($article->name == '') { $article->name = midcom_helper_misc::generate_urlname_from_string($article->title); $tries = 0; $maxtries = 999; while (!$article->update() && $tries < $maxtries) { $article->name = midcom_helper_misc::generate_urlname_from_string($article->title); if ($tries > 0) { // Append an integer if articles with same name exist $article->name .= sprintf("-%03d", $tries); } $tries++; } } $article->parameter('midcom.helper.datamanager2', 'schema_name', $this->_config->get('api_metaweblog_schema')); return $article; }
/** * DM2 creation callback, binds to the current content topic. */ private function _create_article($title) { $this->_article = new midcom_db_article(); $author = $this->_find_email_person($this->_request_data['from']); if (!$author) { debug_add("Author '{$this->_request_data['from']}' not found", MIDCOM_LOG_WARN); if ($this->_config->get('api_email_abort_authornotfound') !== false) { throw new midcom_error("Author '{$this->_request_data['from']}' not found"); } $this->_article->author = midcom_connection::get_user(); } else { // TODO: This code needs a bit of rethinking $author_user = midcom::get('auth')->get_user($author->guid); if (!$this->_content_topic->can_do('midgard:create', $author_user)) { throw new midcom_error('Author doesn\'t have posting privileges'); } $this->_article->author = $author->id; } //Default to first user in DB if author is not set if (!$this->_article->author) { $qb = midcom_db_person::new_query_builder(); $qb->add_constraint('username', '<>', ''); $qb->set_limit(1); $results = $qb->execute(); unset($qb); if (empty($results)) { //No users found throw new midcom_error('Cannot set any author for the article'); } $this->_article->author = $results[0]->id; } $resolver = new midcom_helper_reflector_nameresolver($this->_article); $this->_article->topic = $this->_content_topic->id; $this->_article->title = $title; $this->_article->allow_name_catenate = true; $this->_article->name = $resolver->generate_unique_name('title'); if (empty($this->_article->name)) { debug_add('Could not generate unique name for the new article from title, using timestamp', MIDCOM_LOG_INFO); $this->_article->name = time(); $resolver = new midcom_helper_reflector_nameresolver($this->_article); if (!$resolver->name_is_unique()) { throw new midcom_error('Failed to create unique name for the new article, aborting.'); } } if (!$this->_article->create()) { debug_print_r('Failed to create article:', $this->_article); throw new midcom_error('Failed to create a new article. Last Midgard error was: ' . midcom_connection::get_error_string()); } $this->_article->parameter('midcom.helper.datamanager2', 'schema_name', $this->_config->get('api_email_schema')); return true; }
/** * DM2 creation callback, binds to the current content topic. */ public function &dm2_create_callback(&$controller) { $this->_article = new midcom_db_article(); $this->_article->topic = $this->_content_topic->id; if (!$this->_article->create()) { debug_print_r('We operated on this object:', $this->_article); throw new midcom_error('Failed to create a new article. Last Midgard error was: ' . midcom_connection::get_error_string()); } // Callback possibility if ($this->_config->get('callback_function')) { if ($this->_config->get('callback_snippet')) { // mgd_include_snippet($this->_config->get('callback_snippet')); $eval = midcom_helper_misc::get_snippet_content($this->_config->get('callback_snippet')); if ($eval) { eval($eval); } } $callback = $this->_config->get('callback_function'); $callback($this->_article, $this->_content_topic); } return $this->_article; }
/** * DM2 creation callback, binds to the current content topic. */ public function &dm2_create_callback(&$controller) { $this->_article = new midcom_db_article(); $this->_article->topic = $this->_content_topic->id; if (array_key_exists('name', $this->_defaults) && $this->_defaults['name'] == 'index') { // Store this to article directly in case name field is not editable in schema $this->_article->name = 'index'; } if (!$this->_article->create()) { debug_print_r('We operated on this object:', $this->_article); throw new midcom_error('Failed to create a new article. Last Midgard error was: ' . midcom_connection::get_error_string()); } // Callback possibility if ($this->_config->get('callback_function')) { if ($this->_config->get('callback_snippet')) { midcom_helper_misc::include_snippet_php($this->_config->get('callback_snippet')); } $callback = $this->_config->get('callback_function'); $callback($this->_article, $this->_content_topic); } return $this->_article; }
private function read_structure($structure, $parent_id = 0) { if ($parent_id == 0) { $topic = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC); } else { $object_qb = midcom_db_topic::new_query_builder(); $object_qb->add_constraint('up', '=', $parent_id); $object_qb->add_constraint('name', '=', $structure['name']); if ($object_qb->count() == 0) { // New style $topic = new midcom_db_topic(); $topic->up = $parent_id; $topic->name = $structure['name']; $topic->extra = $structure['title']; $topic->title = $structure['title']; $topic->component = $structure['component']; if (!$topic->create()) { return false; } if (!empty($structure['create_index'])) { // Create index article for n.n.static $article = new midcom_db_article(); $article->name = 'index'; $article->title = $structure['title']; $article->topic = $topic->id; $article->create(); } } else { $topics = $object_qb->execute(); $topic = $topics[0]; } } // Update this folder properly $updated = false; if ($topic->extra != $structure['title']) { $topic->extra = $structure['title']; $topic->title = $structure['title']; $updated = true; } if ($topic->component != $structure['component']) { $topic->component = $structure['component']; $updated = true; } if ($topic->style != $structure['style']) { $topic->style = $structure['style']; $updated = true; } if ($topic->styleInherit != $structure['style_inherit']) { $topic->styleInherit = $structure['style_inherit']; $updated = true; } if ($updated) { $topic->update(); } // Remove parameters that are not in the topic $existing_params = $topic->list_parameters(); foreach ($existing_params as $domain => $params) { foreach ($params as $name => $value) { if (!isset($structure['parameters'][$domain]) || !isset($structure['parameters'][$domain][$name])) { $topic->set_parameter($domain, $name, ''); } } } // Set all new parameters foreach ($structure['parameters'] as $domain => $params) { foreach ($params as $name => $value) { $topic->set_parameter($domain, $name, $value); } } // FIXME: Implement ACLs // Handle subdirectories $foldernames = array(); foreach ($structure['nodes'] as $child_structure) { $this->read_structure($child_structure, $topic->id); $foldernames[] = $child_structure['name']; } if ($this->delete_missing) { // Then delete files and folders that are in DB but not in the importing folder $this->delete_missing_folders($foldernames, $topic->id); } }
function import_file($file, $parent_id) { $qb = midcom_db_article::new_query_builder(); $qb->add_constraint('topic', '=', $parent_id); $qb->add_constraint('name', '=', $file->name); $existing = $qb->execute(); if (count($existing) > 0 && $existing[0]->topic == $parent_id) { $article = $existing[0]; echo "Using existing article {$article->name} (#{$article->id}) from #{$article->topic}\n"; } else { $article = new midcom_db_article(); $article->topic = $parent_id; $article->name = $file->name; if (!$article->create()) { echo "Failed to create article {$article->name}: " . midcom_connection::get_error_string() . "\n"; return false; } echo "Created article {$article->name} (#{$article->id}) under #{$article->topic}\n"; } $article->title = $file->title; $article->content = $file->content; return $article->update(); }