madeon (madeon) wrote in changelog,
madeon
madeon
changelog

[livejournal] r19349: LJSUP-9106 fix: Added methods need_res(s...

Committer: sbelyaev
LJSUP-9106 fix: Added methods need_res(string{}, string[]), need_res(string[]) to Page class. Refactored construction of head sectcion.
U   trunk/bin/upgrading/s2layers/core1.s2
U   trunk/cgi-bin/LJ/S2/DayPage.pm
U   trunk/cgi-bin/LJ/S2/EntryPage.pm
U   trunk/cgi-bin/LJ/S2/FriendsPage.pm
A   trunk/cgi-bin/LJ/S2/HeadContent.pm
U   trunk/cgi-bin/LJ/S2/MonthPage.pm
U   trunk/cgi-bin/LJ/S2/RecentPage.pm
U   trunk/cgi-bin/LJ/S2/ReplyPage.pm
U   trunk/cgi-bin/LJ/S2/TagsPage.pm
U   trunk/cgi-bin/LJ/S2/YearPage.pm
U   trunk/cgi-bin/LJ/S2.pm
Modified: trunk/bin/upgrading/s2layers/core1.s2
===================================================================
--- trunk/bin/upgrading/s2layers/core1.s2	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/bin/upgrading/s2layers/core1.s2	2011-06-23 07:53:17 UTC (rev 19349)
@@ -535,7 +535,13 @@
     function print_head [fixed]
     "Print server side supplied head content. This is a fixed function, so you can't override it. See
     [method[Page.print_custom_head()]] if you want to supply custom head content.";
+    
+    function builtin need_res(string{} options, string[] resources)
+    "Appends resoures to header content with correct timestamp.";
 
+    function builtin need_res(string[] resources)
+    "Appends resoures to header content with correct timestamp.";
+
     function builtin print_hbox_top [fixed]
     "Prints an horizontal bar of site-specific content at the top of a journal.";
 

Modified: trunk/cgi-bin/LJ/S2/DayPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/DayPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/DayPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -15,16 +15,12 @@
     $p->{'_type'} = "DayPage";
     $p->{'view'} = "day";
     $p->{'entries'} = [];
-
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote);
 
     my $user = $u->{'user'};
     my $journalbase = LJ::journal_base($user, $opts->{'vhost'});
 
-    if ($u->should_block_robots) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
-
     my $get = $opts->{'getargs'};
 
     my $month = $get->{'month'};

Modified: trunk/cgi-bin/LJ/S2/EntryPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/EntryPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/EntryPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -20,7 +20,7 @@
     $p->{'comment_pages'} = undef;
 
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote);
-
+    $p->{head_content}->set_object_type( $p->{_type} );
     # setup viewall options
     my ($viewall, $viewsome) = (0, 0);
     if ($get->{viewall} && LJ::check_priv($remote, 'canview', 'suspended')) {
@@ -48,12 +48,8 @@
     my $style_set = defined $get->{'s2id'} ? "s2id=" . int( $get->{'s2id'} ) : "";
     my $style_arg = ($stylemine ne '' and $style_set ne '') ? ($stylemine . '&' . $style_set) : ($stylemine . $style_set);
 
-    if ($u->should_block_robots || $entry->should_block_robots) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
-    if ($LJ::UNICODE) {
-        $p->{'head_content'} .= '<meta http-equiv="Content-Type" content="text/html; charset='.$opts->{'saycharset'}."\" />\n";
-    }
+    my $block_robots = $entry->should_block_robots;
+    $p->{head_content}->set_options( { entry_block_robots => $block_robots } );
 
     # quickreply js libs
     LJ::need_res(qw(
@@ -323,6 +319,7 @@
     }
 
     # print comment info
+    my $entry_cminfo = ();
     {
         my $canAdmin = ($remote && ($remote->can_manage($u) || $remote->can_sweep($u))) ? 1 : 0;
         my $formauth = LJ::ejs(LJ::eurl(LJ::form_auth(1)));
@@ -364,15 +361,12 @@
         $js .= "var LJ_cmtinfo = " . LJ::js_dumper($cmtinfo) . "\n";
         $js .= '</script>';
         $p->{'LJ_cmtinfo'} = $js if $opts->{'need_cmtinfo'};
-        $p->{'head_content'} .= $js;
+        $entry_cminfo .= $js;
     }
+    
+    $p->{head_content}->set_options( { entry_cmtinfo => $entry_cminfo} );
+    $p->{head_content}->set_options( { entry_metadata => $entry->extract_metadata } );
 
-    my %meta = %{ $entry->extract_metadata };
-
-    $p->{'head_content'} .= "<meta property=\"og:title\" name=\"title\" content=\"" . LJ::ehtml( $meta{'title'} ) . "\" />\n";
-    $p->{'head_content'} .= "<meta property=\"og:description\" name=\"description\" content=\"" . LJ::ehtml( $meta{'description'} ) . "\" />\n";
-    $p->{'head_content'} .= "<meta property=\"og:image\" content=\"" . LJ::ehtml( $meta{'image'} ) . "\" />\n";
-
     LJ::need_res(qw(
                     js/commentmanage.js
                     ));

Modified: trunk/cgi-bin/LJ/S2/FriendsPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/FriendsPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/FriendsPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -24,12 +24,10 @@
     $p->{'friends_title'} = LJ::ehtml($u->{'friendspagetitle'});
     $p->{'filter_active'} = 0;
     $p->{'filter_name'} = "";
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
 
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote);
 
-    # Add a friends-specific XRDS reference
-    $p->{'head_content'} .= qq{<meta http-equiv="X-XRDS-Location" content="}.LJ::ehtml($u->journal_base).qq{/data/yadis/friends" />\n};
-
     my $sth;
     my $user = $u->{'user'};
 
@@ -73,10 +71,6 @@
         if ($remote && $remote->{'userid'} == $u->{'userid'} &&
             $remote->{'opt_imagelinks'} =~ m/^(\d+)\|(\d+)$/);
 
-    ## never have spiders index friends pages (change too much, and some
-    ## people might not want to be indexed)
-    $p->{'head_content'} .= LJ::robot_meta_tags();
-
     my $itemshow = S2::get_property_value($opts->{'ctx'}, "page_friends_items")+0;
     if ($itemshow < 1) { $itemshow = 20; }
     elsif ($itemshow > 50) { $itemshow = 50; }
@@ -477,11 +471,6 @@
     }
 
     $p->{'nav'} = $nav;
-
-    if ($get->{'mode'} eq "framed") {
-        $p->{'head_content'} .= "<base target='_top' />";
-    }
-    
     # warn "[FriendsPage=$user] page prepared. elapsed=" . Time::HiRes::tv_interval( $t0, [Time::HiRes::gettimeofday]) . " sec";
 
     return $p;

Added: trunk/cgi-bin/LJ/S2/HeadContent.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/HeadContent.pm	                        (rev 0)
+++ trunk/cgi-bin/LJ/S2/HeadContent.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -0,0 +1,276 @@
+package LJ::S2::HeadContent;
+use strict;
+use warnings;
+
+use overload q("") => 'as_string';
+
+sub new {
+    my ( $class, $self ) = @_;
+
+    return bless( $self, $class );
+}
+
+sub set_object_type {
+    my ( $self, $type ) = @_;
+
+    $self->{type} = $type;
+}
+
+sub set_options {
+    my ( $self, $options ) = @_;
+
+    for my $key (keys %$options) {
+        $self->{opts}{$key} = $options->{$key};
+    }
+}
+
+sub as_string {
+    my $self = shift;
+
+    my $head_content = $self->_page_head();
+    my %handlers = (
+        'EntryPage'   => \&_entry_page_head,
+        'DayPage'     => \&_day_page_head,
+        'FriendsPage' => \&_friends_page_head,
+        'MonthPage'   => \&_month_page_head,
+        'RecentPage'  => \&_recent_page_head,
+        'ReplyPage'   => \&_reply_page_head,
+        'TagsPage'    => \&_tags_page_head,
+        'YearPage'    => \&_year_page_head,
+    );
+
+    my $handler = $handlers{ $self->{type} };
+    if ($handler){
+        $head_content .= $self->$handler;
+    }
+
+    return $head_content;
+}
+
+sub _page_head {
+    my $self = shift;
+
+    my $u = $self->{u};
+    my $remote = $self->{remote};
+    my $opts = $self->{opts} || {};
+
+    my $head_content = '';
+    my $base_url = $u->{'_journalbase'};
+
+    if ($LJ::UNICODE) {
+        $head_content .= qq(<meta http-equiv="Content-Type" );
+        $head_content .= qq(content="text/html;);
+        $head_content .= qq(charset=$opts->{'saycharset'}" />\n);
+    }
+
+    if ( LJ::are_hooks('s2_head_content_extra') ) {
+        LJ::run_hooks('s2_head_content_extra', \$head_content, $remote, $opts->{r});
+    }
+
+    # Automatic Discovery of RSS/Atom
+    if ( $opts->{'addfeeds'} ) {
+        $head_content .= qq(<link rel="alternate" type="application/rss+xml" );
+        $head_content .= qq(title="RSS" href="$base_url/data/rss" />\n );
+
+        $head_content .= qq(<link rel="alternate" type="application/atom+xml" );
+        $head_content .= qq(title="Atom" href="$base_url/data/atom" />\n);
+
+        $head_content .= qq(<link rel="service.feed" type="application/atom+xml" );
+        $head_content .= qq(title="AtomAPI-enabled feed" );
+        $head_content .= qq(href="$LJ::SITEROOT/interface/atomapi/$u->{'user'}/feed" />\n);
+
+        $head_content .= qq(<link rel="service.post" type="application/atom+xml" );
+        $head_content .= qq(title="Create a new post" );
+        $head_content .= qq(href="$LJ::SITEROOT/interface/atomapi/$u->{'user'}/post" />\n);
+    }
+
+    # OpenID information if the caller asked us to include it here.
+    if ( $opts->{'addopenid'} ) {
+        $head_content .= $u->openid_tags;
+    }
+
+    # Ads and control strip
+    my $ad_base_url = LJ::stat_src_to_url('/ad_base.css');
+    $head_content .= qq(<link rel='stylesheet' );
+    $head_content .= qq(href='$ad_base_url' type='text/css' />\n);
+
+    # FOAF autodiscovery
+    my $foafurl = $u->{external_foaf_url} ?
+                      LJ::eurl($u->{external_foaf_url}) : "$base_url/data/foaf";
+    $head_content .= qq(<link rel="meta" type="application/rdf+xml" );
+    $head_content .= qq(title="FOAF" href="$foafurl" />\n);
+
+    if ( $u->email_visible($remote) ) {
+        my $digest = Digest::SHA1::sha1_hex('mailto:' . $u->email_raw);
+        $head_content .= qq(<meta name="foaf:maker" );
+        $head_content .= qq(content="foaf:mbox_sha1sum $digest" />\n);
+    }
+
+    # Include any head stc or js head content
+    LJ::run_hooks( "need_res_for_journals", $u );
+    my $graphicpreviews_obj = LJ::graphicpreviews_obj();
+    $graphicpreviews_obj->need_res($u);
+    my $extra_js = LJ::statusvis_message_js($u);
+
+    if ( LJ::is_enabled('sharing') ) {
+        LJ::Share->request_resources;
+    }
+
+    $head_content .= LJ::res_includes() . $extra_js;
+    LJ::run_hooks( 'head_content', \$head_content );
+    my $calendar_json = LJ::JSON->to_json( LJ::get_calendar_data_for_month($u) );
+    $head_content .= qq(<script type='text/javascript'>\n);
+    $head_content .= qq(Site = window.Site || {};\n);
+    $head_content .= qq(Site.journal_calendar = $calendar_json;\n);
+    $head_content .= qq(</script>\n);
+
+    my $get = $opts->{'getargs'};
+    my $need_block_robots = $opts->{entry_block_robots};
+    $need_block_robots ||= $u->should_block_robots;
+    $need_block_robots ||= $get->{'skip'};
+
+    ## never have spiders index friends pages (change too much, and some
+    ## people might not want to be indexed)
+    if ( $need_block_robots || $self->{type} eq 'FriendsPage' ) {
+        $head_content .= LJ::robot_meta_tags();
+    }
+
+    return $head_content;
+}
+
+sub _entry_page_head {
+    my ($self) = @_;
+
+    my $u = $self->{u};
+    my $remote = $self->{remote};
+    my $opts = $self->{opts} || {};
+
+    my $head_content = '';
+
+    $head_content .= $opts->{entry_cmtinfo};
+
+    if ( $opts->{entry_metadata} ) {
+        my %meta = %{ $opts->{entry_metadata} };
+        $head_content .= qq(<meta property="og:title" name="title" );
+        $head_content .= qq(content=");
+        $head_content .= LJ::ehtml( $meta{'title'} );
+        $head_content .= qq(" />\n);
+
+        $head_content .= qq(<meta property="og:description" );
+        $head_content .= qq(name="description" content=");
+        $head_content .= LJ::ehtml( $meta{'description'} );
+        $head_content .= qq(" />\n);
+
+        $head_content .= qq(<meta property="og:image" );
+        $head_content .= qq(content=");
+        $head_content .= LJ::ehtml( $meta{'image'} );
+        $head_content .= qq( " />\n);
+    }
+    return $head_content;
+}
+
+sub _day_page_head {
+    my ($self) = @_;
+
+    my $head_content = '';
+
+    return $head_content;
+}
+
+sub _friends_page_head {
+    my ($self) = @_;
+
+    my $u = $self->{u};
+    my $opts = $self->{opts} || {};
+
+    my $head_content = '';
+
+    # Add a friends-specific XRDS reference
+    $head_content .= qq(<meta http-equiv="X-XRDS-Location" content=");
+    $head_content .= LJ::ehtml( $u->journal_base );
+    $head_content .= qq(/data/yadis/friends" />\n);
+
+    my $get = $opts->{'getargs'};
+
+    if ( $get->{'mode'} eq "framed" ) {
+        $head_content .= "<base target='_top' />";
+    }
+
+    return $head_content;
+}
+
+sub _month_page_head {
+    my ($self) = @_;
+
+    my $head_content = '';
+
+    return $head_content;
+}
+
+sub _recent_page_head {
+    my ($self) = @_;
+
+    my $u = $self->{u};
+    my $remote = $self->{remote};
+    my $opts = $self->{opts} || {};
+
+    my $head_content = '';
+
+    # Link to the friends page as a "group",
+    # for use with OpenID "Group Membership Protocol"
+
+    my $is_comm = $u->is_community;
+    my $friendstitle = $LJ::SITENAMESHORT." ".($is_comm ? "members" : "friends");
+    my $rel = "group ".($is_comm ? "members" : "friends made");
+    my $friendsurl = $u->journal_base."/friends"; # We want the canonical form here, not the vhost form
+    $head_content .= qq(<link rel="$rel" );
+    $head_content .= qq(title=");
+    $head_content .= LJ::ehtml($friendstitle);
+    $head_content .= qq(" );
+    $head_content .= qq(fhref=") .LJ::ehtml($friendsurl). qq(" />\n);
+
+    if ( my $icbm = $u->prop("icbm") ) {
+        $head_content .= qq(<meta name="ICBM" content="$icbm" />\n);
+    }
+    return $head_content;
+}
+
+sub _reply_page_head {
+    my ($self) = @_;
+
+    my $remote = $self->{remote};
+    my $opts = $self->{opts} || {};
+
+    my $head_content = $LJ::COMMON_CODE{'chalresp_js'};
+
+    if ( $opts->{reply_block_robots} ) {
+        $head_content .= LJ::robot_meta_tags();
+    }
+
+    return $head_content;
+}
+
+sub _tags_page_head {
+    my ($self) = @_;
+
+    my $u = $self->{u};
+    my $head_content = '';
+
+    $head_content = $u->openid_tags;
+
+    if ($u->should_block_robots) {
+        $head_content .= LJ::robot_meta_tags();
+    }
+
+    return $head_content;
+}
+
+sub _year_page_head {
+    my ($self) = @_;
+
+    my $head_content = '';
+
+    return $head_content;
+}
+
+1;

Modified: trunk/cgi-bin/LJ/S2/MonthPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/MonthPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/MonthPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -17,6 +17,7 @@
     $p->{'_type'} = "MonthPage";
     $p->{'view'} = "month";
     $p->{'days'} = [];
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
 
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote);
 
@@ -27,9 +28,6 @@
     my $user = $u->{'user'};
     my $journalbase = LJ::journal_base($user, $opts->{'vhost'});
 
-    if ($u->should_block_robots) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
 
     my ($year, $month);
     if ($opts->{'pathextra'} =~ m!^/(\d\d\d\d)/(\d\d)\b!) {

Modified: trunk/cgi-bin/LJ/S2/RecentPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/RecentPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/RecentPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -20,16 +20,8 @@
     $p->{'_type'} = "RecentPage";
     $p->{'view'} = "recent";
     $p->{'entries'} = [];
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
 
-    # Link to the friends page as a "group", for use with OpenID "Group Membership Protocol"
-    {
-        my $is_comm = $u->is_community;
-        my $friendstitle = $LJ::SITENAMESHORT." ".($is_comm ? "members" : "friends");
-        my $rel = "group ".($is_comm ? "members" : "friends made");
-        my $friendsurl = $u->journal_base."/friends"; # We want the canonical form here, not the vhost form
-        $p->{head_content} .= '<link rel="'.$rel.'" title="'.LJ::ehtml($friendstitle).'" href="'.LJ::ehtml($friendsurl)."\" />\n";
-    }
-    
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote); 
 
     my $user = $u->{'user'};
@@ -60,15 +52,7 @@
         $opts->{'badargs'} = 1;
         return 1;
     }
-
-    if ($u->should_block_robots || $get->{'skip'}) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
-
-    if (my $icbm = $u->prop("icbm")) {
-        $p->{'head_content'} .= qq{<meta name="ICBM" content="$icbm" />\n};
-    }
-
+    
     my $itemshow = S2::get_property_value($opts->{'ctx'}, "page_recent_items")+0;
     if ($itemshow < 1) { $itemshow = 20; }
     elsif ($itemshow > 50 && !$LJ::S2_TRUSTED{ $u->{'userid'} } ) { $itemshow = 50; }

Modified: trunk/cgi-bin/LJ/S2/ReplyPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/ReplyPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/ReplyPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -17,6 +17,7 @@
     my $p = Page($u, $opts);
     $p->{'_type'} = "ReplyPage";
     $p->{'view'} = "reply";
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
 
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote); 
 
@@ -45,14 +46,12 @@
 
     return if $opts->{'handler_return'};
     return if $opts->{'redir'};
-    my $ditemid = $entry->ditemid;
-    $p->{'head_content'} .= $LJ::COMMON_CODE{'chalresp_js'};
+    my $ditemid = $entry->ditemid;    
 
     LJ::Talk::resources_for_talkform();    
 
-    if ($u->should_block_robots || $entry->should_block_robots) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
+    my $block_robots = ($u->should_block_robots || $entry->should_block_robots);
+    $p->{head_content}->set_options( {reply_block_robots => $block_robots } );
 
     $p->{'entry'} = $s2entry;
     LJ::run_hook('notify_event_displayed', $entry);

Modified: trunk/cgi-bin/LJ/S2/TagsPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/TagsPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/TagsPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -11,7 +11,7 @@
     $p->{'_type'} = "TagsPage";
     $p->{'view'} = "tags";
     $p->{'tags'} = [];
-
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote); 
 
     my $user = $u->{'user'};
@@ -22,12 +22,6 @@
         return 1;
     }
 
-    $p->{'head_content'} .= $u->openid_tags;
-
-    if ($u->should_block_robots) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
-
     # get tags for the page to display
     my @taglist;
     my $tags = LJ::Tags::get_usertags($u, { remote => $remote });

Modified: trunk/cgi-bin/LJ/S2/YearPage.pm
===================================================================
--- trunk/cgi-bin/LJ/S2/YearPage.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2/YearPage.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -14,18 +14,10 @@
     my $p = Page($u, $opts);
     $p->{'_type'} = "YearPage";
     $p->{'view'} = "archive";
-
+    $p->{'head_content'}->set_object_type( $p->{'_type'} );
     $p->{'view_my_games'} = $remote && $remote->equals($u) && !LJ::SUP->is_remote_sup() && LJ::UserApps->user_games_count($remote); 
 
     my $user = $u->{'user'};
-
-    if ($u->should_block_robots) {
-        $p->{'head_content'} .= LJ::robot_meta_tags();
-    }
-    if ($LJ::UNICODE) {
-        $p->{'head_content'} .= '<meta http-equiv="Content-Type" content="text/html; charset='.$opts->{'saycharset'}."\" />\n";
-    }
-
     my $get = $opts->{'getargs'};
 
     my $count = LJ::S2::get_journal_day_counts($p);

Modified: trunk/cgi-bin/LJ/S2.pm
===================================================================
--- trunk/cgi-bin/LJ/S2.pm	2011-06-23 07:50:36 UTC (rev 19348)
+++ trunk/cgi-bin/LJ/S2.pm	2011-06-23 07:53:17 UTC (rev 19349)
@@ -19,6 +19,7 @@
 use LJ::S2::EntryPage;
 use LJ::S2::ReplyPage;
 use LJ::S2::TagsPage;
+use LJ::S2::HeadContent;
 
 use HTMLCleaner;
 
@@ -142,23 +143,6 @@
     # like print_stylesheet() won't run, which don't have an method invocant
     return $page if $page && ref $page ne 'HASH';
 
-    # Include any head stc or js head content
-    LJ::run_hooks("need_res_for_journals", $u);
-    my $graphicpreviews_obj = LJ::graphicpreviews_obj();
-    $graphicpreviews_obj->need_res($u);
-    my $extra_js = LJ::statusvis_message_js($u);
-
-    if ( LJ::is_enabled('sharing') ) {
-        LJ::Share->request_resources;
-    }
-
-    $page->{head_content} .= LJ::res_includes() . $extra_js;
-    LJ::run_hooks('head_content', \$page->{head_content});
-    my $calendar_json = LJ::JSON->to_json( LJ::get_calendar_data_for_month($u) );
-    $page->{head_content} .= "<script type='text/javascript'>\n" . 
-        "Site = window.Site || {};\nSite.journal_calendar = $calendar_json;\n".
-        "</script>\n";
-
     s2_run($r, $ctx, $opts, $entry, $page);
     
     if (ref $opts->{'errors'} eq "ARRAY" && @{$opts->{'errors'}}) {
@@ -2059,7 +2043,10 @@
         'views_order' => [ 'recent', 'archive', 'friends', 'userinfo' ],
         'global_title' =>  LJ::ehtml($u->{'journaltitle'} || $u->{'name'}),
         'global_subtitle' => LJ::ehtml($u->{'journalsubtitle'}),
-        'head_content' => '',
+        'head_content' => LJ::S2::HeadContent::->new({ u      => $u,
+                                                       remote => $remote,
+                                                       type   => 'Page',
+                                                       opts   => $opts, }),
         'data_link' => {},
         'data_links_order' => [],
         'showspam' => $get->{mode} eq 'showspam' && LJ::is_enabled('spam_button')
@@ -2067,29 +2054,7 @@
         'page_id' => 'journal-' . $u->username,
     };
 
-    if ($LJ::UNICODE && $opts && $opts->{'saycharset'}) {
-        $p->{'head_content'} .= '<meta http-equiv="Content-Type" content="text/html; charset=' . $opts->{'saycharset'} . "\" />\n";
-    }
 
-    if (LJ::are_hooks('s2_head_content_extra')) {
-        LJ::run_hooks('s2_head_content_extra', \$p->{head_content}, $remote, $opts->{r});
-    }
-
-    # Automatic Discovery of RSS/Atom
-    if ($opts && $opts->{'addfeeds'}) {
-        $p->{'head_content'} .= qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$p->{'base_url'}/data/rss" />\n};
-        $p->{'head_content'} .= qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$p->{'base_url'}/data/atom" />\n};
-        $p->{'head_content'} .= qq{<link rel="service.feed" type="application/atom+xml" title="AtomAPI-enabled feed" href="$LJ::SITEROOT/interface/atomapi/$u->{'user'}/feed" />\n};
-        $p->{'head_content'} .= qq{<link rel="service.post" type="application/atom+xml" title="Create a new post" href="$LJ::SITEROOT/interface/atomapi/$u->{'user'}/post" />\n};
-    }
-
-    # OpenID information if the caller asked us to include it here.
-    $p->{'head_content'} .= $u->openid_tags if $opts && $opts->{'addopenid'};
-
-    # Ads and control strip
-    my $ad_base_url = LJ::stat_src_to_url('/ad_base.css');
-    $p->{'head_content'} .= qq{<link rel='stylesheet' href='$ad_base_url' type='text/css' />\n};
-
     my $show_control_strip = LJ::run_hook('show_control_strip', {
         user => $u->{user},
     });
@@ -2101,18 +2066,8 @@
     }
     LJ::journal_js_inject();
 
-    # FOAF autodiscovery
-    my $foafurl = $u->{external_foaf_url} ? LJ::eurl($u->{external_foaf_url}) : "$p->{base_url}/data/foaf";
-    $p->{head_content} .= qq{<link rel="meta" type="application/rdf+xml" title="FOAF" href="$foafurl" />\n};
-
-    if ($u->email_visible($remote)) {
-        my $digest = Digest::SHA1::sha1_hex('mailto:' . $u->email_raw);
-        $p->{head_content} .= qq{<meta name="foaf:maker" content="foaf:mbox_sha1sum '$digest'" />\n};
-    }
-
     # Identity (type I) accounts only have friends views
     $p->{'views_order'} = [ 'friends', 'userinfo' ] if $u->{'journaltype'} eq 'I';
-
     return $p;
 }
 
@@ -4177,7 +4132,7 @@
 }
 
 #my %approved_widget_classes = map { $_ => $_ } qw (TopEntries TopUsers FaceBookILike PublicStats OnLivejournal MySuperWidget);
-    
+
 sub Page__widget
 {
     my ($ctx, $this, $opts) = @_;
@@ -4827,4 +4782,49 @@
     return $lang;
 }
 
+# there are two ways this function can be called:
+# $this->need_res( [ "a.css", "b.css" ] );
+# $this->need_res( { "condition" => "IE" }, [ "a.css", "b.css" ] );
+sub Page__need_res {
+    my ($ctx, $this, $arguments, $resources) = @_;
+    
+    if ( ref $arguments eq 'ARRAY' && !$resources ) {
+        $resources = $arguments;
+        $arguments = {};
+    } else {
+        my $condition = delete $arguments->{'condition'};
+        if ( $condition && $condition !~ /^[\w\s]+$/ ) {
+            undef $condition;
+        }
+
+        my $args = delete $arguments->{'args'};
+        if ( $args && $args !~ /^media="\w+"$/ ) {
+            undef $args;
+        }
+
+        $arguments = {};
+        $arguments->{'condition'} = $condition if defined $condition;
+        $arguments->{'args'} = $args if defined $args;
+    }
+
+    my @valid_resources = grep { _is_secure_resource($_) } @$resources;
+
+    if ( $arguments && %$arguments ) {
+        LJ::need_res( $arguments, @valid_resources );
+    } else {
+        LJ::need_res(@valid_resources);
+    }
+}
+
+sub _is_secure_resource {
+    my $resource = shift;
+
+    return $resource =~ /^
+                            (?:\w+\/)*   # path
+                            [\w+\-]+     # filename
+                            \.
+                            (?:js|css)   # extension
+                        $/x;
+}
+
 1; 

Tags: livejournal, madeon, pm, s2
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments