2010-06-03 17:41:04 -07:00
using System ;
using System.Linq ;
using System.Text.RegularExpressions ;
using System.Xml.Linq ;
using Orchard.Commands ;
using Orchard.ContentManagement ;
using Orchard.ContentManagement.Aspects ;
using Orchard.Core.Common.Models ;
using Orchard.Core.Navigation.Models ;
2010-07-13 02:52:02 -07:00
using Orchard.Core.Routable.Models ;
2010-06-03 17:41:04 -07:00
using Orchard.Security ;
using Orchard.Blogs.Services ;
using Orchard.Core.Navigation.Services ;
namespace Orchard.Blogs.Commands {
public class BlogCommands : DefaultOrchardCommandHandler {
private readonly IContentManager _contentManager ;
private readonly IMembershipService _membershipService ;
private readonly IBlogService _blogService ;
private readonly IMenuService _menuService ;
public BlogCommands (
IContentManager contentManager ,
IMembershipService membershipService ,
IBlogService blogService ,
IMenuService menuService ) {
_contentManager = contentManager ;
_membershipService = membershipService ;
_blogService = blogService ;
_menuService = menuService ;
}
[OrchardSwitch]
public string FeedUrl { get ; set ; }
[OrchardSwitch]
public string Slug { get ; set ; }
2010-10-14 18:19:52 -07:00
[OrchardSwitch]
public string Owner { get ; set ; }
2010-06-03 17:41:04 -07:00
[OrchardSwitch]
public string Title { get ; set ; }
[OrchardSwitch]
public string MenuText { get ; set ; }
[CommandName("blog create")]
2010-10-14 18:19:52 -07:00
[CommandHelp("blog create /Slug:<slug> /Title:<title> /Owner:<username> [/MenuText:<menu text>] \ r \ n \ t " + " Creates a new Blog ")]
[OrchardSwitches("Slug,Title,Owner,MenuText")]
2010-06-03 17:41:04 -07:00
public string Create ( ) {
2010-10-14 18:19:52 -07:00
var owner = _membershipService . GetUser ( Owner ) ;
if ( owner = = null ) {
Context . Output . WriteLine ( ) ;
return T ( "Invalid username: {0}" , Owner ) . Text ;
}
2010-06-03 17:41:04 -07:00
if ( ! IsSlugValid ( Slug ) ) {
return "Invalid Slug provided. Blog creation failed." ;
}
2010-06-24 16:34:10 -07:00
var blog = _contentManager . New ( "Blog" ) ;
2010-10-14 18:19:52 -07:00
blog . As < ICommonPart > ( ) . Owner = owner ;
2010-07-23 01:01:49 -07:00
blog . As < RoutePart > ( ) . Slug = Slug ;
2010-07-27 18:47:19 -07:00
blog . As < RoutePart > ( ) . Path = Slug ;
2010-07-23 01:01:49 -07:00
blog . As < RoutePart > ( ) . Title = Title ;
2010-06-03 17:41:04 -07:00
if ( ! String . IsNullOrWhiteSpace ( MenuText ) ) {
blog . As < MenuPart > ( ) . OnMainMenu = true ;
blog . As < MenuPart > ( ) . MenuPosition = _menuService . Get ( ) . Select ( menuPart = > menuPart . MenuPosition ) . Max ( ) + 1 + ".0" ;
blog . As < MenuPart > ( ) . MenuText = MenuText ;
}
_contentManager . Create ( blog ) ;
return "Blog created successfully" ;
}
[CommandName("blog import")]
2010-10-14 18:19:52 -07:00
[CommandHelp("blog import /Slug:<slug> /FeedUrl:<feed url> /Owner:<username>\r\n\t" + "Import all items from <feed url> into the blog at the specified <slug>")]
[OrchardSwitches("FeedUrl,Slug,Owner")]
2010-06-03 17:41:04 -07:00
public string Import ( ) {
2010-10-14 18:19:52 -07:00
var owner = _membershipService . GetUser ( Owner ) ;
if ( owner = = null ) {
Context . Output . WriteLine ( ) ;
return T ( "Invalid username: {0}" , Owner ) . Text ;
}
2010-06-03 17:41:04 -07:00
XDocument doc ;
try {
Context . Output . WriteLine ( "Loading feed..." ) ;
doc = XDocument . Load ( FeedUrl ) ;
Context . Output . WriteLine ( "Found {0} items" , doc . Descendants ( "item" ) . Count ( ) ) ;
}
catch ( Exception ex ) {
Context . Output . WriteLine ( T ( "An error occured while loading the file: " + ex . Message ) ) ;
return "Import terminated." ;
}
var blog = _blogService . Get ( Slug ) ;
if ( blog = = null ) {
return "Blog not found at specified slug: " + Slug ;
}
foreach ( var item in doc . Descendants ( "item" ) ) {
string postName = item . Element ( "title" ) . Value ;
Context . Output . WriteLine ( "Adding post: {0}..." , postName . Substring ( 0 , Math . Min ( postName . Length , 40 ) ) ) ;
2010-06-24 16:34:10 -07:00
var post = _contentManager . New ( "BlogPost" ) ;
2010-10-14 18:19:52 -07:00
post . As < ICommonPart > ( ) . Owner = owner ;
2010-07-22 12:52:16 -07:00
post . As < ICommonPart > ( ) . Container = blog ;
2010-07-27 18:47:19 -07:00
var slug = Slugify ( postName ) ;
post . As < RoutePart > ( ) . Slug = slug ;
2010-08-24 11:41:41 -07:00
post . As < RoutePart > ( ) . Path = post . As < RoutePart > ( ) . GetPathWithSlug ( slug ) ;
2010-07-23 01:01:49 -07:00
post . As < RoutePart > ( ) . Title = postName ;
2010-07-22 12:52:16 -07:00
post . As < BodyPart > ( ) . Text = item . Element ( "description" ) . Value ;
2010-06-03 17:41:04 -07:00
_contentManager . Create ( post ) ;
}
return "Import feed completed." ;
}
private static string Slugify ( string slug ) {
var dissallowed = new Regex ( @"[/:?#\[\]@!$&'()*+,;=\s]+" ) ;
slug = dissallowed . Replace ( slug , "-" ) ;
slug = slug . Trim ( '-' ) ;
if ( slug . Length > 1000 )
slug = slug . Substring ( 0 , 1000 ) ;
return slug . ToLowerInvariant ( ) ;
}
private static bool IsSlugValid ( string slug ) {
// see http://tools.ietf.org/html/rfc3987 for prohibited chars
return slug = = null | | String . IsNullOrEmpty ( slug . Trim ( ) ) | | Regex . IsMatch ( slug , @"^[^/:?#\[\]@!$&'()*+,;=\s]+$" ) ;
}
}
}