mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-12-16 03:23 am

[dw-free] Convert /misc/whereami to TT

[commit: http://hg.dwscoalition.org/dw-free/rev/515eb2dc34a6]

http://bugs.dwscoalition.org/show_bug.cgi?id=2236

Convert /misc/whereami to use Template Toolkit. Includes some fixes/changes
to the DW::Template infrastructure.

Patch by [staff profile] mark.

Files modified:
  • cgi-bin/DW/Controller/Misc.pm
  • cgi-bin/DW/Request/Apache2.pm
  • cgi-bin/DW/Template/Apache2.pm
  • cgi-bin/LJ/Lang.pm
  • htdocs/misc/render_sitescheme.bml
  • htdocs/misc/whereami.bml
  • htdocs/misc/whereami.bml.text
  • views/error.tt
  • views/misc/whereami.tt
  • views/misc/whereami.tt.text
--------------------------------------------------------------------------------
diff -r 1b0b57545725 -r 515eb2dc34a6 cgi-bin/DW/Controller/Misc.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Controller/Misc.pm	Wed Dec 16 03:23:02 2009 +0000
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+#
+# DW::Controller::Misc
+#
+# This controller is for miscellaneous, tiny pages that don't have much in the
+# way of actions.  Things that aren't hard to do and can be done in 10-20 lines.
+# If the page you want to create is bigger, please consider creating its own
+# file to house it.
+#
+# Authors:
+#      Mark Smith <mark@dreamwidth.org>
+#
+# Copyright (c) 2009 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself. For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+
+package DW::Controller::Misc;
+
+use strict;
+use warnings;
+use DW::Routing::Apache2;
+use DW::Template::Apache2;
+
+DW::Routing::Apache2->register_string( '/misc/whereami', \&whereami_handler, app => 1 );
+
+# redirects the user to the login page to handle that eventuality
+sub needlogin {
+    my $r = DW::Request->get;
+
+    my $uri = $r->uri;
+    if ( my $qs = $r->query_string ) {
+        $uri .= '?' . $qs;
+    }
+    $uri = LJ::eurl( $uri );
+
+    $r->header_out( Location => "$LJ::SITEROOT/?returnto=$uri" );
+    return $r->REDIRECT;
+}
+
+# returns an error page using a language string
+sub error_ml {
+    return DW::Template::Apache2->render_template(
+        DW::Request->get->r, 'error.tt', { message => LJ::Lang::ml( $_[0] ) }
+    );
+}
+
+# handles the /misc/whereami page
+sub whereami_handler {
+    my $r = DW::Request->get;
+
+    my $remote = LJ::get_remote()
+        or return needlogin();
+    my $u = LJ::get_authas_user( $r->get_args->{authas} || $remote->user )
+        or return error_ml( 'error.invalidauth' );
+
+    my $vars = {
+        user         => $u,
+        cluster_name => $LJ::CLUSTER_NAME{$u->clusterid} || LJ::Lang::ml( '.cluster.unknown' ),
+        authas_html  => LJ::make_authas_select( $remote, { authas => $u->user } ),
+    };
+
+    return DW::Template::Apache2->render_template(
+        $r->r, 'misc/whereami.tt', $vars, {}
+    );
+}
+
+1;
diff -r 1b0b57545725 -r 515eb2dc34a6 cgi-bin/DW/Request/Apache2.pm
--- a/cgi-bin/DW/Request/Apache2.pm	Tue Dec 15 05:49:51 2009 +0000
+++ b/cgi-bin/DW/Request/Apache2.pm	Wed Dec 16 03:23:02 2009 +0000
@@ -212,6 +212,12 @@ sub read {
     return $ret;
 }
 
+# return the internal Apache2 request object
+sub r {
+    my DW::Request::Apache2 $self = $_[0];
+    return $self->{r};
+}
+
 # constants
 sub OK {
     my DW::Request::Apache2 $self = $_[0];
diff -r 1b0b57545725 -r 515eb2dc34a6 cgi-bin/DW/Template/Apache2.pm
--- a/cgi-bin/DW/Template/Apache2.pm	Tue Dec 15 05:49:51 2009 +0000
+++ b/cgi-bin/DW/Template/Apache2.pm	Wed Dec 16 03:23:02 2009 +0000
@@ -56,7 +56,7 @@ my $view_engine = Template->new({
         roots => $roots_constants,
     },
     FILTERS => {
-        ml => [ \&ml, 0 ],
+        ml => [ \&ml, 1 ],
     },
     CACHE_SIZE => $LJ::TEMPLATE_CACHE_SIZE, # this can be undef, and that means cache everything.
     STAT_TTL => $LJ::IS_DEV_SERVER ? 1 : 3600,
@@ -82,7 +82,8 @@ sub template_string {
     $r->note('ml_scope',"/$filename") unless $r->note('ml_scope');
 
     my $out;
-    $view_engine->process( $filename, $opts, \$out ) or die $view_engine->error();
+    $view_engine->process( $filename, $opts, \$out )
+        or die Template->error();
 
     return $out;
 }
@@ -248,12 +249,21 @@ sub ml_scope {
 =cut
 
 sub ml {
-    my ( $code, $vars ) = @_;
-    my $r = DW::Request->get;
-    $code = $r->note( 'ml_scope' ) . $code if rindex($code, '.', 0) == 0;
-    my $lang = decide_language();
-    return $code if $lang eq 'debug';
-    LJ::Lang::get_text( $lang, $code, undef, $vars );
+    # save the last argument as the hashref, hopefully
+    my $args = $_[-1];
+    $args = {} unless $args && ref $args eq 'HASH';
+
+    # we have to return a sub here since we are a dynamic filter
+    return sub {
+        my ( $code ) = @_;
+
+        $code = DW::Request->get->note( 'ml_scope' ) . $code
+            if rindex( $code, '.', 0 ) == 0;
+
+        my $lang = decide_language();
+        return $code if $lang eq 'debug';
+        return LJ::Lang::get_text( $lang, $code, undef, $args );
+    };
 }
 
 sub decide_language {
diff -r 1b0b57545725 -r 515eb2dc34a6 cgi-bin/LJ/Lang.pm
--- a/cgi-bin/LJ/Lang.pm	Tue Dec 15 05:49:51 2009 +0000
+++ b/cgi-bin/LJ/Lang.pm	Wed Dec 16 03:23:02 2009 +0000
@@ -547,6 +547,10 @@ sub get_text
             my $file;
             ($file, $localcode) = ("$LJ::HTDOCS$1", $2);
             @files = ("$file.text.local", "$file.text");
+        } elsif ( $code =~ m!^(/.+\.tt)(\..+)! ) {
+            my $file;
+            ( $file, $localcode ) = ( "$LJ::HOME/views$1", $2 );
+            @files = ( "$file.text.local", "$file.text" );
         } else {
             $localcode = $code;
             @files = ("$LJ::HOME/bin/upgrading/$LJ::DEFAULT_LANG.dat",
diff -r 1b0b57545725 -r 515eb2dc34a6 htdocs/misc/render_sitescheme.bml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/misc/render_sitescheme.bml	Wed Dec 16 03:23:02 2009 +0000
@@ -0,0 +1,42 @@
+<?_c
+#
+# misc/render_sitescheme.bml
+#
+# Internal page to run bml code from a pnote through the BML parser
+# without having to duplicate a fair bit of code.
+#
+# Authors:
+#      Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2009 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself. For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+_c?>
+<?page
+body<=
+<?_code
+{
+    use strict;
+    use DW::Request;
+    use vars qw/ $title $windowtitle $headextra /;
+
+    my $r = DW::Request->get;
+    my $code = $r->pnote( 'render_sitescheme_code' );
+    my $extras = $r->pnote( 'render_sitescheme_extra' );
+    return BML::redirect($LJ::SITEROOT) unless $code;
+
+    $headextra = $extras->{headextra} || "";
+    $title = $extras->{title} || "";
+    $windowtitle = $extras->{windowtitle} || "";
+
+    return BML::ebml($code);
+}
+_code?>
+<=body
+headextra=><?_code return BML::ebml( $headextra ); _code?>
+title=><?_code return BML::ebml( $title ); _code?>
+windowtitle=><?_code return BML::ebml( $windowtitle ); _code?>
+page?>
diff -r 1b0b57545725 -r 515eb2dc34a6 htdocs/misc/whereami.bml
--- a/htdocs/misc/whereami.bml	Tue Dec 15 05:49:51 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-<?_c
-#
-# misc/whereami.bml
-#
-# User facing "nifty" page to let users see what cluster they're on.
-#
-# Authors:
-#      Mark Smith <mark@dreamwidth.org>
-#
-# Copyright (c) 2009 by Dreamwidth Studios, LLC.
-#
-# This program is free software; you may redistribute it and/or modify it under
-# the same terms as Perl itself. For a copy of the license, please reference
-# 'perldoc perlartistic' or 'perldoc perlgpl'.
-#
-_c?><?page
-body<=
-<?_code
-{
-    use strict;
-
-    # for pages that require authentication
-    my $remote = LJ::get_remote()
-        or return '<?needlogin?>';
-
-    # allow the remote user to authenticate as another account (community, etc)
-    my $authas = $GET{authas} || $remote->user;
-    my $u = LJ::get_authas_user( $authas );
-    return LJ::bad_input( $ML{'error.invalidauth'} )
-        unless $u;
-
-    # okay, now print out the useful information
-    my $cname = $LJ::CLUSTER_NAME{$u->clusterid} || $ML{'.cluster.unknown'};
-
-    my $ret = '<?p <?_ml .intro _ml?> p?>';
-    $ret .= "<form method='get' id='userpic_authas' action='$LJ::SITEROOT/misc/whereami'>\n";
-    $ret .= LJ::make_authas_select($remote, { authas => $GET{authas} }) . "\n";
-    $ret .= "</form><br /><?p ";
-    $ret .= BML::ml( '.cluster', { cluster => $cname, user => $u->ljuser_display } );
-    $ret .= ' p?>';
-    return $ret;
-}
-_code?>
-<=body
-title=><?_ml .title _ml?>
-page?>
diff -r 1b0b57545725 -r 515eb2dc34a6 htdocs/misc/whereami.bml.text
--- a/htdocs/misc/whereami.bml.text	Tue Dec 15 05:49:51 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-;; -*- coding: utf-8 -*-
-
-.cluster=[[user]] is located on the <strong>[[cluster]]</strong>.
-
-.cluster.unknown=Unnamed Cluster
-
-.intro=This site is made up of clusters of servers that store user data.  Each of these clusters can contain many tens or hundreds of thousands of different users and communities.
-
-.title=Cluster Locator
diff -r 1b0b57545725 -r 515eb2dc34a6 views/error.tt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/error.tt	Wed Dec 16 03:23:02 2009 +0000
@@ -0,0 +1,3 @@
+<h1>[% 'Error' | ml %]</h1>
+
+<p><strong>[% message %]</strong></p>
diff -r 1b0b57545725 -r 515eb2dc34a6 views/misc/whereami.tt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/misc/whereami.tt	Wed Dec 16 03:23:02 2009 +0000
@@ -0,0 +1,9 @@
+<h1>[% '.title' | ml %]</h1>
+
+<p>[% '.intro' | ml %]</p>
+
+<form action='[% roots.site %]/misc/whereami'>
+    [% authas_html %]
+</form><br />
+
+<p>[% '.cluster' | ml(user = user.ljuser_display, cluster = cluster_name) %]</p>
diff -r 1b0b57545725 -r 515eb2dc34a6 views/misc/whereami.tt.text
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/misc/whereami.tt.text	Wed Dec 16 03:23:02 2009 +0000
@@ -0,0 +1,9 @@
+;; -*- coding: utf-8 -*-
+
+.cluster=[[user]] is located on the <strong>[[cluster]]</strong>.
+
+.cluster.unknown=Unnamed Cluster
+
+.intro=This site is made up of clusters of servers that store user data.  Each of these clusters can contain many tens or hundreds of thousands of different users and communities.
+
+.title=Cluster Locator
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org