function import_folder($folder, $parent_id) { $qb = midcom_db_topic::new_query_builder(); $qb->add_constraint('up', '=', (int) $parent_id); $qb->add_constraint('name', '=', $folder->name); $existing = $qb->execute(); if (count($existing) > 0 && $existing[0]->up == $parent_id) { $topic = $existing[0]; echo "Using existing topic {$topic->name} (#{$topic->id}) from #{$topic->up}\n"; } else { $topic = new midcom_db_topic(); $topic->up = $parent_id; $topic->name = $folder->name; if (!$topic->create()) { echo "Failed to create folder {$folder->name}: " . midcom_connection::get_error_string() . "\n"; return false; } echo "Created folder {$topic->name} (#{$topic->id}) under #{$topic->up}\n"; } $topic->extra = $folder->title; $topic->update(); $topic->parameter('midcom', 'component', $folder->component); if ($folder->component == 'net.nehmer.static') { if (!$folder->has_index) { $topic->parameter('net.nehmer.static', 'autoindex', 1); } else { $topic->parameter('net.nehmer.static', 'autoindex', ''); } } foreach ($folder->files as $file) { $this->import_file($file, $topic->id); } foreach ($folder->folders as $subfolder) { $this->import_folder($subfolder, $topic->id); } return true; }
function import_file($title, $revision_path) { if (!is_readable($revision_path)) { return false; } if (!is_object($this->root_topic) || empty($this->root_topic->id)) { return false; } if (!$this->_datamanager) { return false; } if (!$this->_l10n) { return false; } $resolver = net_nemein_wiki_resolver($this->root_topic->id); // Make sure this is clean $this->add_parameters = array(); $content = trim(file_get_contents($revision_path)) . "\n"; $content = $this->moinmoin2markdown($content, $title); $resolved = $resolver->path_to_wikipage($title, true); echo "INFO: Importing '{$revision_path}' into '{$title}'<br/>\n"; if (!empty($resolved['latest_parent'])) { $to_node =& $resolved['latest_parent']; } else { $to_node =& $resolved['folder']; } $created_page = false; switch (true) { case strstr($resolved['remaining_path'], '/'): // One or more namespaces left, find first, create it and recurse $paths = explode('/', $resolved['remaining_path']); $folder_title = array_shift($paths); echo "NOTICE: Creating new wiki topic '{$folder_title}' under #{$to_node[MIDCOM_NAV_ID]}<br/>\n"; $topic = new midcom_db_topic(); $topic->up = $to_node[MIDCOM_NAV_ID]; $topic->extra = $folder_title; $topic->title = $folder_title; $topic->name = midcom_helper_misc::generate_urlname_from_string($folder_title); $topic->component = 'net.nemein.wiki'; if (!$topic->create()) { throw new midcom_error("could not create topic, error: " . midcom_connection::get_error_string()); } $topic = new midcom_db_topic($topic->id); // Set the component $topic->parameter('midcom', 'component', 'net.nemein.wiki'); // See if we have article with same title in immediate parent $qb = net_nemein_wiki_wikipage::new_query_builder(); $qb->add_constraint('title', '=', $folder_title); $qb->add_constraint('topic', '=', $topic->up); $results = $qb->execute(); if (is_array($results) && count($results) == 1) { echo "INFO: Found page with same title in parent, moving to be index of this new topic<br/>\n"; $article =& $results[0]; $article->name = 'index'; $article->topic = $topic->id; if (!$article->update()) { // Could not move article, do something ? echo "FAILURE: Could not move the page, errstr: " . midcom_connection::get_error_string() . "<br/>\n"; } } else { $page = new net_nemein_wiki_wikipage(); $page->topic = $topic->id; $page->name = 'index'; $page->title = $topic->extra; $page->content = $this->_l10n->get('wiki default page content'); $page->author = midcom_connection::get_user(); if (!$page->create()) { // Could not create index $topic->delete(); throw new midcom_error("Could not create index for new topic, errstr: " . midcom_connection::get_error_string()); } } // We have created a new topic, now recurse to create the rest of the path. echo "INFO: New topic created with id #{$topic->id}, now recursing the import to process next levels<br/>\n"; return $this->import_file($title, $revision_path); break; case is_object($resolved['wikipage']): // Page exists, create new revision echo "INFO: Updating wikipage #{$resolved['wikipage']->id}<br/>\n"; $wikipage =& $resolved['wikipage']; break; default: // No more namespaces left, create the page to latest parent echo "INFO: Creating new wikipage '{$resolved['remaining_path']}' in topic #{$to_node[MIDCOM_NAV_ID]} <br/>\n"; $wikipage = new net_nemein_wiki_wikipage(); $wikipage->title = $resolved['remaining_path']; $wikipage->name = midcom_helper_misc::generate_urlname_from_string($resolved['remaining_path']); $wikipage->topic = $to_node[MIDCOM_NAV_ID]; $wikipage->author = midcom_connection::get_user(); if (!$wikipage->create()) { echo "FAILURE: could not create wikipage object, error: " . midcom_connection::get_error_string() . "<br/>\n"; return false; } $wikipage = new net_nemein_wiki_wikipage($wikipage->id); $created_page =& $wikipage; $wikipage->set_parameter('midcom.helper.datamanager2', 'schema_name', 'default'); break; } if (!$this->_datamanager->autoset_storage($wikipage)) { // DM2 initialization failure echo "FAILURE: Could not initialize DM2<br/>\n"; if (is_object($created_page)) { // Clean up the just created page $created_page->delete(); } return false; } $this->_datamanager->types['content']->value = $content; if (!$this->_datamanager->save()) { // DM2 save failure echo "FAILURE: DM2->save() failed, errstr: " . midcom_connection::get_error_string() . "<br/>\n"; if (is_object($created_page)) { // Clean up the just created page $created_page->delete(); } return false; } // Handle $this->add_object_parameters if (!empty($this->add_object_parameters)) { foreach ($this->add_object_parameters as $param) { $wikipage->set_parameter($param['domain'], $param['name'], $param['value']); } } echo "INFO: file imported OK<br/>\n"; return true; }