Index: openacs-4/packages/acs-content-repository/tcl/filter-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-content-repository/tcl/filter-procs.tcl,v diff -u -r1.16.2.6 -r1.16.2.7 --- openacs-4/packages/acs-content-repository/tcl/filter-procs.tcl 12 Feb 2005 07:26:26 -0000 1.16.2.6 +++ openacs-4/packages/acs-content-repository/tcl/filter-procs.tcl 12 Feb 2005 14:38:10 -0000 1.16.2.7 @@ -5,11 +5,13 @@ namespace eval content { -variable item_id -variable item_url -variable template_url -variable revision_id + variable item_id + variable item_url + variable template_url + variable revision_id +} + # Find the directory in the file system where templates are stored. # There are a variety of ways in which this can be set. The proc # looks for that directory in the following places in this order: @@ -22,166 +24,167 @@ # If the value resulting from the search does not start with a '/' # it is taken to be relative to [acs_root_dir] -ad_proc -public get_template_root {} { +ad_proc -public content::get_template_root {} { - # Look for package-defined root - set package_id [ad_conn package_id] - set template_root \ - [ad_parameter -package_id $package_id TemplateRoot dummy ""] + # Look for package-defined root + set package_id [ad_conn package_id] + set template_root \ + [ad_parameter -package_id $package_id TemplateRoot dummy ""] - if { [empty_string_p $template_root] } { - # Look for template root defined in the CR - set package_id [apm_package_id_from_key "acs-content-repository"] + if { [empty_string_p $template_root] } { + # Look for template root defined in the CR + set package_id [apm_package_id_from_key "acs-content-repository"] - set template_root [ad_parameter -package_id $package_id \ - TemplateRoot dummy "templates"] - } + set template_root [ad_parameter -package_id $package_id \ + TemplateRoot dummy "templates"] + } - if { [string index $template_root 0] != "/" } { - # Relative path, prepend server_root - set template_root "[acs_root_dir]/$template_root" - } + if { [string index $template_root 0] != "/" } { + # Relative path, prepend server_root + set template_root "[acs_root_dir]/$template_root" + } - return [ns_normalizepath $template_root] + return [ns_normalizepath $template_root] } # return true if the request has content associated with it -ad_proc -public has_content {} { +ad_proc -public content::has_content {} { - variable item_id + variable item_id - return [info exists item_id] -} + return [info exists item_id] +} -ad_proc -public get_item_id {} { +ad_proc -public content::get_item_id {} { - variable item_id + variable item_id - return $item_id -} + return $item_id +} -ad_proc -public get_content { { content_type {} } } { - - variable item_id - variable revision_id +ad_proc -public content::get_content { { content_type {} } } { - if { [template::util::is_nil item_id] } { - ns_log warning "content::get_content: No active item in content::get_content" - return - } + variable item_id + variable revision_id - if { [template::util::is_nil revision_id] } { - # Try to get the live revision - ns_log notice "content::get_content: trying to get live revision" - set revision_id [db_string get_revision ""] - if { [template::util::is_nil revision_id] } { - ns_log notice "content::get_content: No live revision for item $item_id" - return - } - } + if { [template::util::is_nil item_id] } { + ns_log warning "content::get_content: No active item in content::get_content" + return + } - # Get the mime type, decide if we want the text - set mime_type [db_string get_mime_type ""] - - if { [template::util::is_nil mime_type] } { - ns_log notice "content::get_content: No such revision: $revision_id" - return - } + if { [template::util::is_nil revision_id] } { + # Try to get the live revision + ns_log notice "content::get_content: trying to get live revision" + set revision_id [db_string get_revision ""] + if { [template::util::is_nil revision_id] } { + ns_log notice "content::get_content: No live revision for item $item_id" + return + } + } - if { [string equal -length 4 "text" $mime_type] } { - set text_sql [db_map content_as_text] - } else { - set text_sql "" - } - - # Get the content type - if { [empty_string_p $content_type] } { - set content_type [db_string get_content_type ""] - } + # Get the mime type, decide if we want the text + set mime_type [db_string get_mime_type ""] - # Get the table name - set table_name [db_string get_table_name ""] + if { [template::util::is_nil mime_type] } { + ns_log notice "content::get_content: No such revision: $revision_id" + return + } - upvar content content + if { [string equal -length 4 "text" $mime_type] } { + set text_sql [db_map content_as_text] + } else { + set text_sql "" + } - # Get (all) the content (note this is really dependent on file type) - if {![db_0or1row get_content "" -column_array content]} { - ns_log notice "content::get_content: No data found for item $item_id, revision $revision_id" - return 0 - } + # Get the content type + if { [empty_string_p $content_type] } { + set content_type [db_string get_content_type ""] + } + # Get the table name + set table_name [db_string get_table_name ""] + + upvar content content + + # Get (all) the content (note this is really dependent on file type) + if {![db_0or1row get_content "" -column_array content]} { + ns_log notice "content::get_content: No data found for item $item_id, revision $revision_id" + return 0 + } + } -ad_proc -public get_template_url {} { - variable template_url +ad_proc -public content::get_template_url {} { - return $template_url + variable template_url + + return $template_url } # Set a data source in the calling frame with folder URL and label # Useful for generating a context bar -ad_proc -public get_folder_labels { { varname "folders" } } { - - variable item_id +ad_proc -public content::get_folder_labels { { varname "folders" } } { - # this repeats the query used to look up the item in the first place - # but there does not seem to be a clear way around this + variable item_id - # build the folder URL out as we iterate over the query - set query [db_map get_url] - db_multirow -extend {url} $varname ignore_get_url $query { - append url "$name/" - } + # this repeats the query used to look up the item in the first place + # but there does not seem to be a clear way around this + + # build the folder URL out as we iterate over the query + set query [db_map get_url] + db_multirow -extend {url} $varname ignore_get_url $query { + append url "$name/" + } } -ad_proc -public get_content_value { revision_id } { +ad_proc -public content::get_content_value { revision_id } { - db_transaction { - db_exec_plsql gcv_get_revision_id { - begin + db_transaction { + db_exec_plsql gcv_get_revision_id { + begin content_revision.to_temporary_clob(:revision_id); - end; - } + end; + } - # Query for values from a previous revision - set content [db_string gcv_get_previous_content ""] + # Query for values from a previous revision + set content [db_string gcv_get_previous_content ""] - } + } - return $content + return $content } -ad_proc -public init { urlvar rootvar {content_root ""} {template_root ""} {context "public"} {rev_id ""} {content_type ""} } { +ad_proc -public content::init { + {-resolve_index "f"} + {-revision "live"} + urlvar + rootvar + {content_root ""} + {template_root ""} + {context "public"} + {rev_id ""} + {content_type ""} +} { - upvar $urlvar url $rootvar root_path + upvar $urlvar url $rootvar root_path + variable root_folder_id + variable item_id + variable revision_id - variable item_id - variable revision_id - - # if a .tcl file exists at this url, then don't do any queries - if { [file exists [ns_url2file "$url.tcl"]] } { - return 0 - } + set root_folder_id $content_root + # if a .tcl file exists at this url, then don't do any queries + if { [file exists [ns_url2file "$url.tcl"]] } { + return 0 + } - # cache this query persistently for 1 hour set item_info(item_id) [::content::item::get_id -item_path $url \ -root_folder_id $content_root \ -resolve_index "f"] - # Make sure we are not dealing with an upgraded file and there exists a file with the title - - if { [string equal "" $item_info(item_id)] } { - set splitted_url [split $url "/"] - set item_url_title [lindex $splitted_url end] - set item_url_folder [lindex $splitted_url end-1] - set item_info(item_id) [db_string test "select i.item_id from cr_revisions r, cr_items i where r.item_id = i.item_id and r.title = :item_url_title and i.parent_id = (select item_id from cr_items where name = :item_url_folder) order by revision_id desc limit 1" -default ""] - } - - set item_info(content_type) [::content::item::get_content_type \ -item_id $item_info(item_id)] @@ -191,64 +194,71 @@ -root_folder_id $content_root \ -resolve_index "f"] set item_info(content_type) [::content::item::get_content_type \ - -item_id $item_info(item_id)] + -item_id $item_id] if { [string equal "" $item_info(item_id)] } { ns_log notice "content::init: no content found for url $url" return 0 } } - variable item_url - set item_url $url + variable item_url + set item_url $url - set item_id $item_info(item_id) - if { [empty_string_p $content_type] } { - set content_type $item_info(content_type) - } + set item_id $item_info(item_id) + if { [empty_string_p $content_type] } { + set content_type $item_info(content_type) + } - # Make sure that a live revision exists - if { [empty_string_p $rev_id] } { - set live_revision [db_string get_live_revision ""] - - if { [template::util::is_nil live_revision] } { - ns_log notice "content::init: no live revision found for content item $item_id" - return 0 + # TODO accept latest revision as well. DaveB + # Make sure that a live revision exists + if { [empty_string_p $rev_id] } { + if {[string equal "best" $revision]} { + # lastest_revision unless live_revision is set, then live_revision + set revision_id [::item::get_best_revision $item_id] + } else { + # default live_revision + set revision_id [::item::get_live_revision $item_id] } - set revision_id $live_revision - } else { - set revision_id $rev_id - } - variable template_path + if { [string equal "" $revision_id] } { + ns_log notice "content::init: no live revision found for content item $item_id" + return 0 + } - # Get the template - set template_found_p [db_0or1row get_template_url "" -column_array info] + } else { + set revision_id $rev_id + } - if { !$template_found_p || [string equal $info(template_url) {}] } { - ns_log notice "content::init: No template found to render content item $item_id in context '$context'" - return 0 - } + variable template_path - set url $info(template_url) - set root_path [get_template_root] + # Get the template + set template_found_p [db_0or1row get_template_url "" -column_array info] - # Added so that published templates are regenerated if they are missing. - # This is useful for default templates. - # (OpenACS - DanW, dcwickstrom@earthlink.net) + if { !$template_found_p || [string equal $info(template_url) {}] } { + ns_log notice "content::init: No template found to render content item $item_id in context '$context'" + return 0 + } - set file ${root_path}/${url}.adp - if { ![file exists $file] } { + set url $info(template_url) + set root_path [get_template_root] - file mkdir [file dirname $file] - set text [content::get_content_value $info(template_id)] - template::util::write_file $file $text - } + # Added so that published templates are regenerated if they are missing. + # This is useful for default templates. + # (OpenACS - DanW, dcwickstrom@earthlink.net) - set file ${root_path}/${url}.tcl - if { ![file exists $file] } { + set file ${root_path}/${url}.adp + if { ![file exists $file] } { - file mkdir [file dirname $file] - set text "\# Put the current revision's attributes in a onerow datasource named \"content\". + file mkdir [file dirname $file] + set text [content::get_content_value $info(template_id)] + template::util::write_file $file $text + } + + set file ${root_path}/${url}.tcl + if { ![file exists $file] } { + + file mkdir [file dirname $file] + set text "\# Put the current revision's attributes in a onerow datasource named \"content\". \# The detected content type is \"$content_type\". content::get_content $content_type @@ -280,17 +290,13 @@ # render the template and write it to the file system -ad_proc -public deploy { url_stub } { - - set output_path [ns_info pageroot]$url_stub +ad_proc -public content::deploy { url_stub } { - init url_stub root_path + set output_path [ns_info pageroot]$url_stub - set output [template::adp_parse $file_stub] + init url_stub root_path - template::util::write_file $output_path $output -} + set output [template::adp_parse $file_stub] -# end of content namespace - + template::util::write_file $output_path $output }