API:EPrints/Session

From EPrints Documentation
Revision as of 16:50, 10 August 2009 by Tdb01r (talk | contribs)
Jump to: navigation, search

NAME

EPrints::Session - Single connection to the EPrints system

Contents

DESCRIPTION

This module is not really a session. The name is out of date, buthard to change. EPrints::Session represents a connection to the EPrints system. Itconnects to a single EPrints repository, and the database used bythat repository. Thus it has an associated EPrints::Database andEPrints::Repository object. Each "session" has a "current language". If you are running in a multilingual mode, this is used by the HTML rendering functions tochoose what language to return text in. The "session" object also knows about the current apache connection,if there is one, including the CGI parameters. If the connection requires a username and password then it can also give access to the EPrints::DataObj::User object representing the user who iscausing this request. The session object also provides many methods for creating XHTML results which can be returned via the web interface.

new

 $session = EPrints::Session->new( $mode, [$repository_id], [$noise], [$nocheckdb] )

Create a connection to an EPrints repository which provides access to the database and to the repository configuration. This method can be called in two modes. Setting $mode to 0 means thisis a connection via a CGI web page. $repository_id is ignored, insteadthe value is taken from the "PerlSetVar EPrints_ArchiveID" option inthe apache configuration for the current directory. If this is being called from a command line script, then $mode shouldbe 1, and $repository_id should be the ID of the repository we want toconnect to. $mode :mode = 0 - We are online (CGI script)mode = 1 - We are offline (bin script) $repository_id is repository_idmode = 2 - We are online, but don't create a CGI query (so we

don't consume the data).

$noise is the level of debugging output.0 - silent1 - quietish2 - noisy3 - debug all SQL statements4 - debug database connection

Under normal conditions use "0" for online and "1" for offline.

$nocheckdb - if this is set to 1 then a connection is made to thedatabase without checking that the tables exist.

get_request

 $request = $session->get_request;

Return the Apache request object (from mod_perl) or undefined if this isn't a CGI script.

get_query

 $query = $session->get_query;

Return the CGI.pm object describing the current HTTP query, or undefined if this isn't a CGI script.

terminate

 $session->terminate

Perform any cleaning up necessary, for example SQL cache tables whichare no longer needed.

Language Related Methods

get_session_language

 $langid = EPrints::Session::get_session_language( $repository, $request )

Given an repository object and a Apache (mod_perl) request object, thismethod decides what language the session should be. First it looks at the HTTP cookie "eprints_lang", failing that itlooks at the prefered language of the request from the HTTP header,failing that it looks at the default language for the repository. The language ID it returns is the highest on the list that the giveneprint repository actually supports.

change_lang

 $session->change_lang( $newlangid )

Change the current language of the session. $newlangid should be avalid country code for the current repository. An invalid code will cause eprints to terminate with an error.

html_phrase

 $xhtml_phrase = $session->html_phrase( $phraseid, %inserts )

Return an XHTML DOM object describing a phrase from the phrase files. $phraseid is the id of the phrase to return. If the same ID appearsin both the repository-specific phrases file and the system phrases filethen the repository-specific one is used. If the phrase contains <ep:pin> elements, then each one should havean entry in %inserts where the key is the "ref" of the pin and thevalue is an XHTML DOM object describing what the pin should be replaced with.

phrase

 $utf8_text = $session->phrase( $phraseid, %inserts )

Performs the same function as html_phrase, but returns plain text. All HTML elements will be removed, <br> and <p> will be converted into breaks in the text. <img> tags will be replaced with their "alt" values.

get_lang

 $language = $session->get_lang

Return the EPrints::Language object for this sessions current language.

get_langid

 $langid = $session->get_langid

Return the ID code of the current language of this session.

best_language

 $value = EPrints::Session::best_language( $repository, $lang, %values )

$repository is the current repository. $lang is the prefered language. %values contains keys which are language ids, and values which istext or phrases in those languages, all translations of the same thing. This function returns one of the values from %values based on the following logic: If possible, return the value for $lang. Otherwise, if possible return the value for the default language ofthis repository. Otherwise, if possible return the value for "en" (English). Otherwise just return any one value. This means that the view sees the best possible phrase.

get_view_name

 $viewname = $session->get_view_name( $dataset, $viewid )

Return a UTF8 encoded string containing the human readable nameof the /view/ section with the ID $viewid.

Accessor Methods

get_database

 $db = $session->get_database

Return the current EPrints::Database connection object.

get_storage

 $store = $session->get_storage

Return the storage control object.

get_repository

 $repository = $session->get_repository

Return the EPrints::Repository object associated with the Session.

get_url

 $url = $session->get_url( [ @OPTS ] [, $page] )

Utility method to get various URLs. See EPrints::URL. With no arguments returns the same as get_uri().

 # Return the current static path
 $session->get_url( path => "static" );
 # Return the current cgi path
 $session->get_url( path => "cgi" );
 # Return a full URL to the current cgi path
 $session->get_url( host => 1, path => "cgi" );
 # Return a full URL to the static path under HTTP
 $session->get_url( scheme => "http", host => 1, path => "static" );
 # Return a full URL to the image 'foo.png'
 $session->get_url( host => 1, path => "images", "foo.png" );

get_uri

 $uri = $session->get_uri

Returns the URL of the current script. Or "undef".

get_full_url

 $uri = $session->get_full_url

Returns the URL of the current script plus the CGI params.

get_noise

 $noise_level = $session->get_noise

Return the noise level for the current session. See the explainationunder EPrints::Session->new()

get_online

 $boolean = $session->get_online

Return true if this script is running via CGI, return false if we'reon the command line.

get_secure

 $secure = $session->get_secure

Returns true if we're using HTTPS/SSL (checks get_online first).

DOM Related Methods

These methods help build XML. Usually, but not always XHTML.

make_element

 $dom = $session->make_element( $element_name, %attribs )

Return a DOM element with name ename and the specified attributes. eg. $session->make_element( "img", src => "/foo.gif", alt => "my pic" ) Will return the DOM object describing: <img src="/foo.gif" alt="my pic" /> Note that in the call we use "=>" not "=".

make_indent

 $dom = $session->make_indent( $width )

Return a DOM object describing a C.R. and then $width spaces. Thisis used to make nice looking XML for things like the OAI interface.

make_comment

 $dom = $session->make_comment( $text )

Return a DOM object describing a comment containing $text. eg. <!-- this is a comment -->

make_text

 $DOM = $session->make_text( $text )

Return a DOM object containing the given text. $text should beUTF-8 encoded. Characters will be treated as _text_ including < > etc. eg. $session->make_text( "This is <b> an example" ); Would return a DOM object representing the XML: "This is &lt;b&gt; an example"

make_javascript

 $DOM = $session->make_javascript( $code, %attribs )

Return a new DOM "script" element containing $code in javascript. %attribs willbe added to the script element, similar to make_element(). E.g.

 <script type="text/javascript">
 // <![CDATA[
 alert("Hello, World!");
 // ]]>
 </script>

make_doc_fragment

 $fragment = $session->make_doc_fragment

Return a new XML document fragment. This is an item which can haveXML elements added to it, but does not actually get rendered itself. If appended to an element then it disappears and its children jointhe element at that point.

XHTML Related Methods

These methods help build XHTML.

render_ruler

 $ruler = $session->render_ruler

Return an HR.in ruler.xml

render_nbsp

 $nbsp = $session->render_nbsp

Return an XHTML &nbsp; character.

render_data_element

 $xhtml = $session->render_data_element( $indent, $elementname, $value, [%opts] )

This is used to help render neat XML data. It returns a fragment containing an element of name $elementname containing the value$value, the element is indented by $indent spaces. The %opts describe any extra attributes for the element eg.$session->render_data_element( 4, "foo", "bar", class=>"fred" ) would return a XML DOM object describing:

   <foo class="fred">bar</foo>

render_link

 $xhtml = $session->render_link( $uri, [$target] )

Returns an HTML link to the given uri, with the optional $target ifit needs to point to a different frame or window.

render_row

 $table_row = $session->render_row( $key, @values );

Return the key and values in a DOM encoded HTML table row. eg.

<tr><th>$key:</th><td>$value[0]</td><td>...</td></tr>

render_language_name

 $xhtml = $session->render_language_name( $langid ) Return a DOM object containing the description of the specified languagein the current default language, or failing that from languages.xml

render_type_name

 $xhtml = $session->render_type_name( $type_set, $type ) 

Return a DOM object containing the description of the specified typein the type set. eg. "eprint", "article"

get_type_name

 $string = $session->get_type_name( $type_set, $type ) 

As above, but return a utf-8 string. Used in <option> elements, forexample.

render_name

 $xhtml_name = $session->render_name( $name, [$familylast] )

$name is a ref. to a hash containing family, given etc. Returns an XML DOM fragment with the name rendered in the mannerof the repository. Usually "John Smith". If $familylast is set then the family and given parts are reversed, eg."Smith, John"

render_option_list

 $xhtml_select = $session->render_option_list( %params )

This method renders an XHTML <select>. The options are complicatedand may change, so it's better not to use it.

render_single_option

 $option = $session->render_single_option( $key, $desc, $selected )

Used by render_option_list.

render_hidden_field

 $xhtml_hidden = $session->render_hidden_field( $name, $value )

Return the XHTML DOM describing an <input> element of type "hidden"and name and value as specified. eg. <input type="hidden" name="foo" value="bar" />

render_upload_field

 $xhtml_uploda = $session->render_upload_field( $name )

Render into XHTML DOM a file upload form button with the given name. eg.<input type="file" name="foo" />

render_action_buttons

 $dom = $session->render_action_buttons( %buttons )

Returns a DOM object describing the set of buttons. The keys of %buttons are the ids of the action that button will cause,the values are UTF-8 text that should appear on the button. Two optional additional keys may be used: _order => [ "action1", "action2" ] will force the buttons to appear in a set order. _class => "my_css_class" will add a class attribute to the <div> containing the buttons to allow additional styling.

render_internal_buttons

 $dom = $session->render_internal_buttons( %buttons )

As for render_action_buttons, but creates buttons for actions whichwill modify the state of the current form, not continue with whateverprocess the form is part of. eg. the "More Spaces" button and the up and down arrows on multipletype fields.

render_form

 $dom = $session->render_form( $method, $dest )

Return a DOM object describing an HTML form element. $method should be "get" or "post" $dest is the target of the form. By default the current page. eg. $session->render_form( "GET", "http://example.com/cgi/foo" ); returns a DOM object representing: <form method="get" action="http://example.com/cgi/foo" accept-charset="utf-8" /> If $method is "post" then an addition attribute is set:enctype="multipart/form-data" This just controls how the data is passed from the browser to theCGI library. You don't need to worry about it.

render_subjects

 $ul = $session->render_subjects( $subject_list, [$baseid], [$currentid], [$linkmode], [$sizes] )

Return as XHTML DOM a nested set of <ul> and <li> tags describingpart of a subject tree. $subject_list is a array ref of subject ids to render. $baseid is top top level node to render the tree from. If only a singlesubject is in subject_list, all subjects up to $baseid will still berendered. Default is the ROOT element. If $currentid is set then the subject with that ID is rendered in<strong> $linkmode can 0, 1, 2 or 3. 0. Don't link the subjects. 1. Links subjects to the URL which edits them in edit_subjects. 2. Links subjects to "subjectid.html" (where subjectid is the id of the subject) 3. Links the subjects to "subjectid/". $sizes must be set. Only subjects with a size of more than one are linked. 4. Links the subjects to "../subjectid/". $sizes must be set. Only subjects with a size of more than one are linked. $sizes may be a ref. to hash mapping the subjectid's to the numberof items in that subject which will be rendered in brackets next toeach subject.

render_error

 $session->render_error( $error_text, $back_to, $back_to_text )

Renders an error page with the given error text. A link, with thetext $back_to_text, is offered, the destination of this is $back_to,which should take the user somewhere sensible.

render_input_form

 $dom = $session->render_input_form( %params )

Return a DOM object representing an entire input form. %params contains the following options: dataset: The EPrints::Dataset to which the form relates, if any. fields: a reference to an array of EPrint::MetaField objects,which describe the fields to be added to the form. values: a set of default values. A reference to a hash wherethe keys are ID's of fields, and the values are the defaultvalues for those fields. show_help: if true, show the fieldhelp phrase for each input field. show_name: if true, show the fieldname phrase for each input field. buttons: a description of the buttons to appear at the bottomof the form. See render_action_buttons for details. top_buttons: a description of the buttons to appear at the topof the form (optional). default_action: the id of the action to be performed by default, ie. if the user pushes "return" in a text field. dest: The URL of the target for this form. If not defined thenthe current URI is used. type: if this form relates to a user or an eprint, the type ofeprint/user can effect what fields are flagged as required. Thisparam contains the ID of the eprint/user if any, and if relevant. staff: if true, this form is being presented to repository staff (admin, or editor). This may change which fields are required. hidden_fields: reference to a hash. The keys of which are CGI keysand the values are the values they are set to. This causes hiddenform elements to be set, so additional information can be passed. object: The DataObj which this form is editing, if any. comment: not yet used.

Methods relating to the current XHTML page

write_static_page

 $session->write_static_page( $filebase, $parts, [$page_id], [$wrote_files] )

Write an .html file plus a set of files describing the parts of thepage for use with the dynamic template option. File base is the name of the page without the .html suffix. parts is a reference to a hash containing DOM trees. If $wrote_files is defined then any filenames written are logged in it as keys.

prepare_page

 $session->prepare_page( $parts, %options )

Create an XHTML page for this session. $parts is a hash of XHTML elements to insert into the pins in thetemplate. Usually: title, page. Maybe pagetop and head. If template is set then an alternate template file is used. This function only builds the page it does not output it any way, seethe methods below for that. Options include: page_id=>"id to put in body tag"template=>"The template to use instead of default."

send_page

 $session->send_page( %httpopts )

Send a web page out by HTTP. Only relevant if this is a CGI script.build_page must have been called first. See send_http_header for an explanation of %httpopts Dispose of the XML once it's sent out.

page_to_file

 $session->page_to_file( $filename, [$wrote_files] )

Write out the current webpage to the given filename. build_page must have been called first. Dispose of the XML once it's sent out. If $wrote_files is set then keys are created in it for each filecreated.

set_page

 $session->set_page( $newhtml )

Erase the current page for this session, if any, and replace it withthe XML DOM structure described by $newhtml. This page is what is output by page_to_file or send_page. $newhtml is a normal DOM Element, not a document object.

clone_for_me

 $copy_of_node = $session->clone_for_me( $node, [$deep] )

XML DOM items can only be added to the document which they belong to. A EPrints::Session has it's own XML DOM DOcument. This method copies an XML node from _any_ document. The copy belongsto this sessions document. If $deep is set then the children, (and their children etc.), are copied too.

redirect

 $session->redirect( $url, [%opts] )

Redirects the browser to $url.

not_found

 $session->not_found( [ $message ] )

Send a 404 Not Found header. If $message is undef sets message to'Not Found' but does NOT print an error message, otherwisedefaults to the normal 404 Not Found type response.

send_http_header

 $session->send_http_header( %opts )

Send the HTTP header. Only makes sense if this is running as a CGI script. Opts supported are: content_type. Default value is "text/html; charset=UTF-8". This setsthe http content type header. lang. If this is set then a cookie setting the language preferenceis set in the http header.

Input Methods

These handle input from the user, browser and apache.

param

 $value or @values = $session->param( $name )

Passes through to CGI.pm param method. $value = $session->param( $name ): returns the value of CGI parameter$name. $value = $session->param( $name ): returns the value of CGI parameter$name. @values = $session->param: returns an array of the names of all theCGI parameters in the current request.

have_parameters

 $bool = $session->have_parameters

Return true if the current script had any parameters (post or get)

current_user

 $user = $session->current_user

Return the current EPrints::DataObj::User for this session. Return undef if there isn't one.

seen_form

 $boolean = $session->seen_form

Return true if the current request contains the values from aform generated by EPrints. This is identified by a hidden field placed into forms named_seen with value "true".

internal_button_pressed

 $boolean = $session->internal_button_pressed( $buttonid )

Return true if a button has been pressed in a form which is intendedto reload the current page with some change. Examples include the "more spaces" button on multiple fields, the "lookup" button on succeeds, etc.

get_action_button

 $action_id = $session->get_action_button

Return the ID of the eprint action button which has been pressed ina form, if there was one. The name of the button is "_action_" followed by the id. This also handles the .x and .y inserted in image submit. This is designed to get back the name of an action button createdby render_action_buttons.

get_internal_button

 $button_id = $session->get_internal_button

Return the id of the internal button which has been pushed, or undef if one wasn't.

client

 $client = $session->client

Return a string representing the kind of browser that made the current request. Options are GECKO, LYNX, MSIE4, MSIE5, MSIE6, ?. GECKO covers mozilla and firefox. ? is what's returned if none of the others were matched. These divisions are intended for modifying the way pages are renderednot logging what browser was used. Hence merging mozilla and firefox.

get_http_status

 $status = $session->get_http_status

Return the status of the current HTTP request.

Methods related to Plugins

plugin

 $plugin = $session->plugin( $pluginid )

Return the plugin with the given pluginid, in this repository or, failingthat, from the system level plugins.

plugin_list

 @plugin_ids  = $session->plugin_list( %restrictions )

Return either a list of all the plugins available to this repository orreturn a list of available plugins which can accept the given restrictions. Restictions:

vary depending on the type of the plugin.

get_plugins

 @plugins = $session->get_plugins( [ $params, ] %restrictions )

Returns a list of plugin objects that conform to %restrictions (may be empty). If $params is given uses that hash reference to initialise the plugins. Always passes this session to the plugin constructor method.

Other Methods

microtime

 $time = EPrints::Session::microtime();

This function is currently buggy so just returns the time in seconds. Return the time of day in seconds, but to a precision of microseconds. Accuracy depends on the operating system etc.

mail_administrator

 $foo = $session->mail_administrator( $subjectid, $messageid, %inserts )

Sends a mail to the repository administrator with the given subject andmessage body. $subjectid is the name of a phrase in the phrase file to usefor the subject. $messageid is the name of a phrase in the phrase file to use as thebasis for the mail body. %inserts is a hash. The keys are the pins in the messageid phrase andthe values the utf8 strings to replace the pins with.

destroy

 $session->DESTROY

Destructor. Don't call directly.