/** * DM2 creation callback, binds to the current content topic. */ public function &dm2_create_callback(&$controller) { $this->_page = new net_nemein_wiki_wikipage(); $this->_page->topic = $this->_topic->id; $this->_page->title = $this->_wikiword; $this->_page->author = midcom_connection::get_user(); // We can clear the session now $this->_request_data['session']->remove('wikiword'); if (!$this->_page->create()) { debug_print_r('We operated on this object:', $this->_page); throw new midcom_error('Failed to create a new page. Last Midgard error was: ' . midcom_connection::get_error_string()); } $this->_page = new net_nemein_wiki_wikipage($this->_page->id); return $this->_page; }
public static function initialize_index_article($topic) { $page = new net_nemein_wiki_wikipage(); $page->topic = $topic->id; $page->name = 'index'; $page->title = $topic->extra; $page->content = midcom::get('i18n')->get_string('wiki default page content', 'net.nemein.wiki'); $page->author = midcom_connection::get_user(); if ($page->create()) { return $page; } return false; }
public function testCRUD() { midcom::get('auth')->request_sudo('net.nemein.wiki'); $page = new net_nemein_wiki_wikipage(); $topic = $this->get_component_node('net.nemein.wiki'); $page->topic = $topic->id; $page->title = 'TEST TITLE'; $stat = $page->create(); $this->assertTrue($stat, midcom_connection::get_error_string()); $this->register_object($page); $page->refresh(); $this->assertEquals('test-title', $page->name); $page->title = 'Test Title 2'; $stat = $page->update(); $this->assertTrue($stat); $page->refresh(); $this->assertEquals('Test Title 2', $page->title); $stat = $page->delete(); $this->assertTrue($stat); midcom::get('auth')->drop_sudo(); }
/** * @param mixed $handler_id The ID of the handler. * @param Array $args The argument list. * @param Array &$data The local request data. */ public function _handler_emailimport($handler_id, array $args, array &$data) { //Content-Type midcom::get()->skip_page_style = true; midcom::get('cache')->content->content_type('text/plain'); //Make sure we have the components we use and the Mail_mimeDecode package if (!class_exists('org_openpsa_mail_decoder')) { throw new midcom_error('org.openpsa.mail decoder could not be loaded.'); } if (!class_exists('Mail_mimeDecode')) { throw new midcom_error('Cannot decode attachments, aborting.'); } //Make sure the message_source is POSTed if (!array_key_exists('message_source', $_POST) || empty($_POST['message_source'])) { throw new midcom_error('_POST[\'message_source\'] not present or empty.'); } $decoder = new org_openpsa_mail_decoder(); $decoder->mime_decode($_POST['message_source']); //Parse email addresses $regex = '/<?([a-zA-Z0-9_.-]+?@[a-zA-Z0-9_.-]+)>?[ ,]?/'; $emails = array(); if (preg_match_all($regex, $decoder->headers['To'], $matches_to)) { foreach ($matches_to[1] as $email) { //Each address only once $emails[$email] = $email; } } if (preg_match_all($regex, $decoder->headers['Cc'], $matches_cc)) { foreach ($matches_cc[1] as $email) { //Each address only once $emails[$email] = $email; } } $from = false; if (preg_match_all($regex, $decoder->headers['From'], $matches_from)) { foreach ($matches_from[1] as $email) { //Each address only once $emails[$email] = $email; //It's unlikely that we'd get multiple matches in From, but we use the latest $from = $email; } } midcom::get('auth')->request_sudo(); //TODO: Create wikinote $wikipage = new net_nemein_wiki_wikipage(); $wikipage->topic = $this->_topic->id; //PONDER: add from, to & subject into the body ?? $wikipage->content = $decoder->body; $title_format = $this->_config->get('emailimport_title_format'); $wikipage->title = sprintf($title_format, $decoder->subject, $from, strftime('%x', time())); //Check for duplicate title(s) $qb = net_nemein_wiki_wikipage::new_query_builder(); $qb->add_constraint('topic', '=', $wikipage->topic); $qb->add_constraint('title', 'LIKE', $wikipage->title . '%'); $results = $qb->execute_unchecked(); if (($found = count($results)) > 0) { foreach ($results as $foundpage) { if ($foundpage->content == $wikipage->content) { //Content exact duplicate, abort import debug_print_r("duplicate content with page '{$wikipage->title}", $wikipage->content); throw new midcom_error('Duplicate content with an existing page with similar title.'); } } //In theory this should be recursive but we'll leave it at this for now $wikipage->title .= ' ' . ($found + 1); } //Figure out author $author = $this->emailimport_find_person($from); $wikipage->author = $author->id; if (!$wikipage->author) { //Default to first user in the db $qb = midcom_db_person::new_query_builder(); $qb->add_constraint('username', '<>', ''); $results = $qb->execute_unchecked(); if (empty($results)) { //No users found throw new midcom_error('Cannot set any author for the wikipage'); } $wikipage->author = $results[0]->id; } if (!$wikipage->create()) { throw new midcom_error('wikipage->create returned failure, errstr: ' . midcom_connection::get_error_string()); } //Mark as email $wikipage->parameter('net.nemein.wiki:emailimport', 'is_email', time()); $embeds_added = false; $attachments_added = false; foreach ($decoder->attachments as $att) { debug_add("processing attachment {$att['name']}"); $attobj = $wikipage->create_attachment($att['name'], $att['name'], $att['mimetype']); if (!$attobj) { //Could not create attachment debug_add("Could not create attachment '{$att['name']}', errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); continue; } $fp = @$attobj->open('w'); if (!$fp) { //Could not open for writing, clean up and continue debug_add("Could not open attachment {$attobj->guid} for writing, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); $attobj->delete(); continue; } if (!fwrite($fp, $att['content'], strlen($att['content']))) { //Could not write, clean up and continue debug_add("Error when writing attachment {$attobj->guid}, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_ERROR); fclose($fp); $attobj->delete(); continue; } fclose($fp); if (isset($att['part']) && isset($att['part']->headers) && isset($att['part']->headers['content-id'])) { //Attachment is embed, add tag to end of note if (!$embeds_added) { $wikipage->content .= "\n\n"; $embeds_added = true; } $wikipage->content .= "![{$attobj->title}](" . midcom_connection::get_url('self') . "midcom-serveattachmentguid-{$attobj->guid}/{$attobj->name})\n\n"; } else { //Add normal attachments as links to end of note if (!$attachments_added) { //We hope the client handles these so that embeds come first and attachments then so we can avoid double pass over this array $wikipage->content .= "\n\n"; $attachments_added = true; } $wikipage->content .= "[{$attobj->title}](" . midcom_connection::get_url('self') . "midcom-serveattachmentguid-{$attobj->guid}/{$attobj->name}), "; } } if ($embeds_added || $attachments_added) { $wikipage->update(); } // Store recipients of the email to the page for possible later use reset($emails); foreach ($emails as $email) { $wikipage->parameter('net.nemein.wiki:emailimport_recipients', $email, time()); debug_add("Processing email address {$email} for related-to links"); $person = $this->emailimport_find_person($email); if (!$person) { $group = $this->emailimport_find_group($email); if (!$group) { debug_add("Could not find person or group for email {$email}, cannot link, storing email for future reference", MIDCOM_LOG_WARN); $wikipage->parameter('net.nemein.wiki:emailimport_notlinked', $email, time()); continue; } continue; } } midcom::get('auth')->drop_sudo(); }
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; }