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-03-29 05:04 am

[dw-free] Updates to FAQ backend

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

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

Allow disabling of FAQ summaries.

Patch by [personal profile] pauamma.

Files modified:
  • htdocs/admin/faq/faqedit.bml
  • htdocs/admin/faq/readcat.bml
  • htdocs/support/faqbrowse.bml
--------------------------------------------------------------------------------
diff -r ab4f6e106dfb -r bccae67619a0 htdocs/admin/faq/faqedit.bml
--- a/htdocs/admin/faq/faqedit.bml	Sun Mar 29 04:55:54 2009 +0000
+++ b/htdocs/admin/faq/faqedit.bml	Sun Mar 29 05:04:47 2009 +0000
@@ -25,7 +25,7 @@ body<=
     LJ::remote_has_priv( $remote, "faqedit", \%ac_edit );
     my $faqd = LJ::Lang::get_dom( "faq" );
     my $rlang = LJ::Lang::get_root_lang( $faqd );
-    my ( $faqcat, $sortorder, $question, $summary, $answer );
+    my ( $faqcat, $sortorder, $question, $summary, $answer, $has_summary );
 
     if ( $id != 0 ) {
         my $faq = LJ::Faq->load( $id, lang => $rlang->{lncode} )
@@ -36,6 +36,7 @@ body<=
         $question = $faq->question_raw;
         $summary = $faq->summary_raw;
         $answer = $faq->answer_raw;
+        $has_summary = $faq->has_summary;
         unless ( $ac_edit{'*'} || $ac_edit{$faqcat} ) {
             if ( %ac_edit ) {
                 return "<b>Error: </b> You do not have access to edit a FAQ question in the \"$faqcat\" category.";
@@ -62,12 +63,13 @@ body<=
             my $do_trans = sub {
                 my $id = shift;
                 return unless $faqd;
-                LJ::Lang::set_text($dbh, $faqd->{dmid}, $rlang->{lncode},
-                                   "$id.1question", $FORM{q}, $opts_question);
-                LJ::Lang::set_text($dbh, $faqd->{dmid}, $rlang->{lncode},
-                                   "$id.3summary", $FORM{s}, $opts_summary);
-                LJ::Lang::set_text($dbh, $faqd->{dmid}, $rlang->{lncode},
-                                   "$id.2answer", $FORM{a}, $opts_answer);
+                LJ::Lang::set_text( $dbh, $faqd->{dmid}, $rlang->{lncode},
+                                    "$id.1question", $FORM{q}, $opts_question );
+                LJ::Lang::set_text( $dbh, $faqd->{dmid}, $rlang->{lncode},
+                                    "$id.3summary", $FORM{s}, $opts_summary )
+                    if LJ::is_enabled( 'faq_summaries' );
+                LJ::Lang::set_text( $dbh, $faqd->{dmid}, $rlang->{lncode},
+                                    "$id.2answer", $FORM{a}, $opts_answer );
             };
 
             if ( $id == 0 ) {
@@ -89,6 +91,7 @@ body<=
                 $do_trans->( $id ) if $id;
             } elsif ( $FORM{q} =~ /\S/ ) {
                 my $sortorder = $FORM{sortorder}+0 || 50;
+                # FIXME: don't overwrite existing summary if missing in form
                 $dbh->do( qq{ UPDATE faq SET question=?, summary=?, answer=?,
                                              faqcat=?, lastmoduserid=?,
                                              lastmodtime=NOW(), sortorder=?
@@ -148,19 +151,27 @@ body<=
         $ret .= "</p>";
     }
 
-    $ret .= "<?h1 Summary h1?> <?p (should be a shortish paragraph, urls are automatically linked, same markup as journal entries, no lj-cut) p?><p>";
+    if ( LJ::is_enabled( 'faq_summaries' ) || $has_summary ) {
+        # If FAQ has summary and summaries are disabled, leave field in, but
+        # make it read-only to let FAQ editors copy from it.
+        my $readonly = !LJ::is_enabled( 'faq_summaries' );
+        $ret .= "<?h1 Summary h1?> <?p (should be a shortish paragraph, urls are automatically linked, same markup as journal entries, no lj-cut) p?><p>";
 
-    $ret .= LJ::html_textarea( { name => 's', value => $summary,
-                                 rows => '10', cols => '70', wrap => 'soft' } );
-    $ret .= "</p>";
+        $ret .= LJ::html_textarea( { name => 's', value => $summary,
+                                     rows => '10', cols => '70',
+                                     wrap => 'soft', disabled => $readonly } );
+        $ret .= "</p>";
 
-    if ( $faqd && $id != 0 ) {
-        $ret .= "<p><b>Select modification level for summary:</b> ";
-        $ret .= LJ::html_select( { name => "sev_summary", selected => 0 },
-                                 0 => "Typo/etc (no notify)",
-                                 1 => "Minor (notify translators)",
-                                 2 => "Major (require translation updates)" );
-        $ret .= "</p>";
+        if ( $faqd && $id != 0 && !$readonly ) {
+            # If absent, severity will default to 0 (unchanged), which is
+            # the right thing for a readonly field.
+            $ret .= "<p><b>Select modification level for summary:</b> ";
+            $ret .= LJ::html_select( { name => "sev_summary", selected => 0 },
+                                     0 => "Typo/etc (no notify)",
+                                     1 => "Minor (notify translators)",
+                                     2 => "Major (require translation updates)" );
+            $ret .= "</p>";
+        }
     }
 
     $ret .= "<?h1 Answer h1?> <?p (long as you want, urls are automatically linked, same markup as journal entries, no lj-cut) p?><p>";
diff -r ab4f6e106dfb -r bccae67619a0 htdocs/admin/faq/readcat.bml
--- a/htdocs/admin/faq/readcat.bml	Sun Mar 29 04:55:54 2009 +0000
+++ b/htdocs/admin/faq/readcat.bml	Sun Mar 29 05:04:47 2009 +0000
@@ -49,7 +49,8 @@ body<=
         BML::note_mod_time( $faq->lastmodtime ); 
         $q =~ s/^\s+//; $q =~ s/\s+$//;
         $q =~ s!\n!<br />!g;
-        my $s = $faq->has_summary ? $faq->summary_html : '';
+        my $s = ( LJ::is_enabled( 'faq_summaries') && $faq->has_summary )
+            ? $faq->summary_html : '';
         $s =~ s/^\s+//;
         $s =~ s/\s+$//;
         $s =~ s/\n( +)/"\n" . "&nbsp;&nbsp;"x length( $1 )/eg;
diff -r ab4f6e106dfb -r bccae67619a0 htdocs/support/faqbrowse.bml
--- a/htdocs/support/faqbrowse.bml	Sun Mar 29 04:55:54 2009 +0000
+++ b/htdocs/support/faqbrowse.bml	Sun Mar 29 05:04:47 2009 +0000
@@ -28,6 +28,7 @@ body<=
 
     # get faqid and redirect to faq.bml if none
     my $faqidarg = $GET{'faqid'} + 0;
+    # FIXME: disallow both faqid and faqcat (or ignore one)
     my $faqcatarg = $GET{'faqcat'};
     
     unless ($faqidarg || $faqcatarg) {
@@ -111,8 +112,22 @@ body<=
         my $summary = $f->summary_raw;
         my $answer = $f->answer_raw;
 
-        # Old FAQs don't have summaries, so show the answer instead
-        $summary = $answer unless $f->has_summary;
+        # What to display?
+        my $display_summary;
+        my $display_answer;
+        if ( $mode eq 'answer' ) { # answer, summary if present
+            $display_answer = 1;
+            $display_summary = $f->has_summary;
+        } else { # summary if there's one, answer if there's no summary
+            $display_summary = $f->has_summary;
+            $display_answer = !$display_summary;
+        }
+
+        # If summaries are disabled, pretend the FAQ doesn't have one.
+        unless ( LJ::is_enabled( 'faq_summaries' ) ) {
+            $display_answer ||= $display_summary;
+            $display_summary = 0;
+        }
 
         # escape question
         my $question = $f->question_html;
@@ -120,9 +135,10 @@ body<=
         $question =~ s/\n/<br \/>/g;
         
         # Clean this as if it were an entry, but don't allow lj-cuts
-        LJ::CleanHTML::clean_event(\$summary, {'ljcut_disable' => 1});
+        LJ::CleanHTML::clean_event(\$summary, {'ljcut_disable' => 1})
+            if $display_summary;
         LJ::CleanHTML::clean_event(\$answer,  {'ljcut_disable' => 1})
-            if $f->has_summary;
+            if $display_answer;
 
         # Highlight search terms
         my $term = sub {
@@ -135,6 +151,8 @@ body<=
             $question =~ s/(\Q$qterm\E)/$term->($1)/ige;
 
             # don't highlight terms in URLs or HTML tags
+            # FIXME: if the search term is present in a tag, should still
+            # highlight occurences outside tags.
             $summary =~ s!((?:https?://[^>]+)?\Q$qterm\E)!$term->($1)!ige
                 unless $summary =~ m!<[^>]*\Q$qterm\E[^>]*>!;
 
@@ -142,11 +160,15 @@ body<=
                 unless $answer =~ m!<[^>]*\Q$qterm\E[^>]*>!;
         }
 
-        if (LJ::are_hooks("faq.$faqid.transform")) {
-           # run hook to transform the text of this FAQ before it's finally rendered
-           LJ::run_hook("faq.$faqid.transform", $remote,
-                        question => \$question, summary => \$summary, answer => \$answer);
-        }
+        # run hook to transform the text of this FAQ before it's rendered
+        # FIXME: hook is responsible for clean-up if it changes $display_*
+        # from false to true. Should that be checked/enforced here instead?
+        # FIXME: do we even need that hook? It looks like LJ only ever used
+        # it to add stuff to LJ Talk FAQs, for Gizmo sponsorship.
+        LJ::run_hook( "faq.$faqid.transform", $remote, question => \$question,
+                      summary => \$summary, display_summary => \$display_summary,
+                      answer => \$answer, display_answer => \$display_answer)
+            if LJ::are_hooks( "faq.$faqid.transform" );
 
         # display output
         if ($faqcatarg) {
@@ -157,32 +179,32 @@ body<=
         }
         $$body .= "<div style='margin-left: 20px;'>";
 
-        $$body .= "<div name='summary' id='summary'>$summary</div>";
-        $$body .= "<br />";
+        if ( $display_summary ) {
+            $$body .= "<div name='summary' id='summary'>$summary</div>";
+            $$body .= "<br />";
 
-        if ($mode eq 'summary' && $f->has_summary) {
-            my $q = "&amp;q=" . LJ::eurl($qterm) if $qterm;
+            unless ( $display_answer ) {
+                my $q = "&amp;q=" . LJ::eurl($qterm) if $qterm;
 
-            $$body .= "<div id='rm' name='rm'>";
-            my $oc = $faqcatarg ? '' : "onclick='return showAnswer();'";
-            $$body .= "<b>(&nbsp;<a $oc href='faqbrowse.bml?faqid=$faqid$q&amp;view=full'>" .
-                "$ML{'.more'}</a>&nbsp;)</b>";
-            $$body .= "</div>";
+                $$body .= "<div id='rm' name='rm'>";
+                my $oc = $faqcatarg ? '' : "onclick='return showAnswer();'";
+                $$body .= "<b>(&nbsp;<a $oc href='faqbrowse.bml?faqid=$faqid$q&amp;view=full'>" .
+                    "$ML{'.more'}</a>&nbsp;)</b>";
+                $$body .= "</div>";
+            }
         }
 
-        if (!$faqcatarg && $f->has_summary) {
-            # If we're just viewing one faq and it has a summary
-            if ($mode eq 'answer') {
-                $$body .= "<div id='answer' name='answer'>$answer</div>";
-            } else {
-                $$body .= "<div id='answer' name='answer' style='display: none'>$answer</div>";
-            }
+        if ( $display_answer ) {
+            $$body .= "<div id='answer' name='answer'>$answer</div>";
+        } else {
+            $$body .= "<div id='answer' name='answer' style='display: none'>$answer</div>";
         }
 
         $$body .= "</div>";
 
         my $lastmodwho = LJ::get_username($f->lastmoduserid);
         if ($lastmodwho) {
+            # FIXME: lastmodtime and lastmodwho should be vars in .lastupdated
             $$body .= "<p align=\"right\"><b>$ML{'.lastupdated'}</b><br />"
                 . $f->lastmodtime . " ($lastmodwho)</p>";
         }
--------------------------------------------------------------------------------

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