private string CleanUriForEmail(XUri uri) { uri = uri.AsPublicUri(); string schemehostport = uri.SchemeHostPort; string pathQueryFragment = uri.PathQueryFragment; return(schemehostport + pathQueryFragment.Replace(":", "%3A")); }
public void XmlAsUriWithDreamContext() { DreamHostInfo hostInfo = DreamTestHelper.CreateRandomPortHost(); MockServiceInfo mock = MockService.CreateMockService(hostInfo); mock.Service.CatchAllCallback = delegate(DreamContext context, DreamMessage request, Result <DreamMessage> response) { XUri uri = mock.AtLocalMachine.Uri; XDoc doc = new XDoc("test").Elem("uri", uri); Assert.AreEqual(uri.AsPublicUri().ToString(), doc["uri"].AsText); Assert.AreEqual(uri, doc["uri"].AsUri); response.Return(DreamMessage.Ok(doc)); }; DreamMessage result = mock.AtLocalMachine.PostAsync().Wait(); Assert.IsTrue(result.IsSuccessful, "failure in service"); Assert.AreEqual(mock.AtLocalHost.Uri.WithoutQuery(), result.ToDocument()["uri"].AsUri); }
//--- Methods --- public void Append(XDoc manifestDoc) { XUri apiUri = DekiContext.Current.ApiUri; _log.DebugFormat("building import plan against api uri: {0}", apiUri.AsPublicUri()); bool preserveExistingLocalChanges = manifestDoc["@preserve-local"].AsBool ?? false; var security = manifestDoc["security"]; foreach (XDoc importXml in manifestDoc.Elements) { try { if (importXml.HasName("page")) { if (importXml["path"].IsEmpty) { throw new PageIdInvalidArgumentException(); } // Generate the request needed to import the page Title title = Title.FromRelativePath(_relToTitle, importXml["path"].Contents); XUri uri = apiUri.At("pages", title.AsApiParam(), "contents").With("reltopath", _relToTitle.AsPrefixedDbPath()); _uris[uri] = null; uri = uri.With("language", importXml["language"].AsText) .With("title", importXml["title"].AsText) .With("edittime", DateTime.MaxValue.ToString("yyyyMMddHHmmss")) .With("redirects", "0"); var importEtag = importXml["etag"].AsText; var importDateModified = (importXml["date.modified"].AsDate ?? DateTime.MinValue).ToSafeUniversalTime(); if (importDateModified != DateTime.MinValue) { uri = uri.With("importtime", importDateModified.ToString("yyyyMMddHHmmss")); } // unless we're forcing overwrite... if (!_forceoverwrite) { // ...check for existing page var page = PageBL.GetPageByTitle(title); if (page != null && page.ID != 0 && !page.IsRedirect) { // check for previous import var lastImport = PropertyBL.Instance.GetPageProperty((uint)page.ID, LAST_IMPORT); if (lastImport != null) { try { var content = lastImport.Content; var previousImportXml = XDocFactory.From(content.ToStream(), content.MimeType); var previousImportEtag = previousImportXml["etag"].AsText; var previousImportDateModified = (previousImportXml["date.modified"].AsDate ?? DateTime.MinValue).ToSafeUniversalTime(); if (preserveExistingLocalChanges) // if we are supposed to preserve local changes { if (importEtag == previousImportEtag || // and page either hasn't changed previousImportEtag != page.Etag // or is locally modified ) // then skip importing the page { _log.DebugFormat("skipping previously imported page '{0}', because page either hasn't changed or is locally modified", title); continue; } } else if (previousImportDateModified > importDateModified) { // if the previous import is newer, skip import _log.DebugFormat("skipping previously imported page '{0}', because previous import is newer", title); continue; } } catch (Exception e) { // if there was a parsing problem, skip import as a precaution _log.Warn(string.Format("a parsing problem occured with the import info property and import of page {0} is being skipped", page.ID), e); continue; } } else if (preserveExistingLocalChanges) { // there was no previous import property, so the local content is by definition locally changed, ignore import _log.DebugFormat("skipping page '{0}', because of local content and preserve-local package preference", title); continue; } } } AddRequest("POST", uri, importXml["@dataid"].AsText, MimeType.XML.ToString()); // if we have security xml for the import, create a request command for it if (!security.IsEmpty) { AddBodyRequest("POST", apiUri.At("pages", title.AsApiParam(), "security"), security, MimeType.XML.ToString()); } } else if (importXml.HasName("tags")) { // Ensure that the tags have a corresponding import page Title title = Title.FromRelativePath(_relToTitle, importXml["path"].Contents); XUri pageUri = apiUri.At("pages", title.AsApiParam(), "contents").With("reltopath", _relToTitle.AsPrefixedDbPath()); if (!_uris.ContainsKey(pageUri)) { PageBE page = PageBL.GetPageByTitle(title); if ((null == page) && (0 >= page.ID)) { throw new PageIdInvalidArgumentException(); } else { _uris[pageUri] = null; } } // Generate the request to import the tags XUri tagsUri = apiUri.At("pages", title.AsApiParam(), "tags"); _uris[tagsUri] = null; tagsUri = tagsUri.With("redirects", "0"); AddRequest("PUT", tagsUri, importXml["@dataid"].AsText, MimeType.XML.ToString()); } else if (importXml.HasName("file")) { if (importXml["filename"].IsEmpty) { throw new AttachmentMissingFilenameInvalidArgumentException(); } // Ensure that the file has a corresponding import page or that the page already exists in the wiki Title title = Title.FromRelativePath(_relToTitle, importXml["path"].Contents); XUri pageUri = apiUri.At("pages", title.AsApiParam(), "contents").With("reltopath", _relToTitle.AsPrefixedDbPath()); if (!_uris.ContainsKey(pageUri)) { PageBE page = PageBL.GetPageByTitle(title); if ((null == page) && (0 >= page.ID)) { throw new PageIdInvalidArgumentException(); } _uris[pageUri] = null; } // Generate the request to import the file XUri fileUri = apiUri.At("pages", title.AsApiParam(), "files", Title.AsApiParam(importXml["filename"].Contents)); _uris[fileUri] = null; fileUri = fileUri.With("redirects", "0"); AddRequest("PUT", fileUri, importXml["@dataid"].AsText, importXml["contents/@type"].AsText); } else if (importXml.HasName("property")) { if (importXml["name"].IsEmpty) { throw new SiteImportUndefinedNameInvalidArgumentException("name"); } // Ensure that that the property has a corresponding import page/file Title title = Title.FromRelativePath(_relToTitle, importXml["path"].Contents); XUri uri = apiUri.At("pages", title.AsApiParam()); if (importXml["filename"].IsEmpty) { if (!_uris.ContainsKey(uri.At("contents").With("reltopath", _relToTitle.AsPrefixedDbPath()))) { throw new PageIdInvalidArgumentException(); } } else { uri = uri.At("files", Title.AsApiParam(importXml["filename"].AsText)); if (!_uris.ContainsKey(uri)) { throw new AttachmentFilenameInvalidArgumentException(); } } // Generate the request to import the property uri = uri.At("properties", Title.AsApiParam(importXml["name"].Contents).Substring(1)); _uris[uri] = null; uri = uri.With("abort", "never") .With("redirects", "0"); AddRequest("PUT", uri, importXml["@dataid"].AsText, importXml["contents/@type"].AsText); } else { throw new DreamResponseException(DreamMessage.NotImplemented(importXml.Name)); } } catch (ResourcedMindTouchException e) { ErrorImport(e.Message, (int)e.Status, importXml); } catch (DreamAbortException e) { ErrorImport(e.Message, (int)e.Response.Status, importXml); } catch (Exception e) { ErrorImport(e.Message, (int)DreamStatus.InternalError, importXml); } } }