| 1 | ################ LaTeXML Startup Utilities ##################### | ||
| 2 | package LaTeXML::Plugin::LtxMojo::Startup; | ||
| 3 | use LaTeXML::Util::ObjectDB; | ||
| 4 | use LaTeXML::Util::Pathname; | ||
| 5 | use Data::Dumper; | ||
| 6 | $Data::Dumper::Terse = 1; # don't output names where feasible | ||
| 7 | $Data::Dumper::Indent = 3; # turn off all pretty print | ||
| 8 | |||
| 9 | our $DB_FILE = '.LaTeXML_Startup.cache'; | ||
| 10 | #TODO: Eliminate redundant options | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | ($class,%opts) = @_; | ||
| 14 | $opts{cache}=1 unless defined $opts{cache}; | ||
| 15 | my $dbfile; | ||
| 16 | if ($opts{cache}) { | ||
| 17 | my $dbfile = $opts{dbfile}||$DB_FILE; | ||
| 18 | if(defined $dbfile && !-f $dbfile){ | ||
| 19 | if(my $dbdir = pathname_directory($dbfile)){ | ||
| 20 | pathname_mkdir($dbdir); }} | ||
| 21 | } | ||
| 22 | my $DB = LaTeXML::Util::ObjectDB->new(dbfile=>$dbfile); | ||
| 23 | bless {converters=>{},db=>$DB}, $class;} | ||
| 24 | |||
| 25 | ########################################### | ||
| 26 | #### User Management ##### | ||
| 27 | ########################################### | ||
| 28 | |||
| 29 | sub users { | ||
| 30 | my ($self) = @_; | ||
| 31 | [ sort map {s/^user://; $_;} grep {/^user:/} grep defined, $self->{db}->getKeys ]; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub exists_user { | ||
| 35 | my ($self,$user) = @_; | ||
| 36 | return defined $self->{db}->lookup("user:$user"); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub summary_user { | ||
| 40 | my ($self,$user) = @_; | ||
| 41 | my $entry = $self->{db}->lookup("user:$user"); | ||
| 42 | my $summary = '<table class="summary"><tr><th>Property</th><th>Value</th></tr>'; | ||
| 43 | foreach ($entry->getKeys){ | ||
| 44 | next if $_ =~ /^pass|key$/; #No pass|key display | ||
| 45 | $summary .= "<tr><td>$_</td><td>".Dumper($entry->getValue($_))."</td></tr>\n"; | ||
| 46 | } | ||
| 47 | $summary.="</table>"; | ||
| 48 | $summary; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub verify_user { | ||
| 52 | my ($self,$user,$pass) = @_; | ||
| 53 | my $user_entry = $self->{db}->lookup("user:$user"); | ||
| 54 | if (defined $user_entry && ($pass eq $user_entry->getValue('pass'))) { | ||
| 55 | return $user_entry->getValue('role')||1; | ||
| 56 | } | ||
| 57 | return 0; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub modify_user { | ||
| 61 | my ($self,$user,$pass,$role,$default) = @_; | ||
| 62 | return "Error: Username can't be empty!\n" unless $user; | ||
| 63 | my $user_entry = $self->{db}->lookup("user:$user"); | ||
| 64 | $role = 'standard' unless $role; | ||
| 65 | $default = 'standard' unless $default; | ||
| 66 | if (!$user_entry) { #New: | ||
| 67 | return "Error: Username and Password can't be empty!\n" unless $user && $pass; | ||
| 68 | $self->{db}->register("user:$user",pass=>$pass,role=>$role,default=>$default); | ||
| 69 | return "Successfully added user $user with role:$role, default profile:$default!\n"; | ||
| 70 | } else { #Modify: | ||
| 71 | $pass = $user_entry->getValue('pass') unless $pass; | ||
| 72 | $role = $user_entry->getValue('role') unless $role; | ||
| 73 | $default = $user_entry->getValue('default') unless $default; | ||
| 74 | |||
| 75 | $user_entry->setValues(pass=>$pass,role=>$role,default=>$default); | ||
| 76 | return "Successfully modified user $user!\n"; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | sub delete_user { | ||
| 81 | my ($self,$user) = @_; | ||
| 82 | $self->{db}->unregister("user:$user"); | ||
| 83 | return "Successfully deleted user $user!\n"; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub lookup_user_property { | ||
| 87 | my ($self,$user,$prop) = @_; | ||
| 88 | my $user_entry = $self->{db}->lookup("user:$user"); | ||
| 89 | return undef unless defined $user_entry; | ||
| 90 | $user_entry->getValue($prop); | ||
| 91 | } | ||
| 92 | |||
| 93 | ########################################### | ||
| 94 | #### Profile Management ##### | ||
| 95 | ########################################### | ||
| 96 | |||
| 97 | |||
| 98 | sub modify_profile { | ||
| 99 | my($self,$profile,$opts) = @_; | ||
| 100 | $self->{db}->register("profile:$profile",%$opts); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub get_profile { | ||
| 104 | my ($self,$profile) = @_; | ||
| 105 | my $p_entry = $self->{db}->lookup("profile:$profile"); | ||
| 106 | return $p_entry->as_hashref if defined $p_entry; | ||
| 107 | undef; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub profiles { | ||
| 111 | my ($self) = @_; | ||
| 112 | [ sort map {s/^profile://; $_;} grep {/^profile:/} grep defined, $self->{db}->getKeys ]; | ||
| 113 | } | ||
| 114 | |||
| 115 | our $profile_templates = { | ||
| 116 | # Arrayrefs: | ||
| 117 | css => 'arrayref|text', | ||
| 118 | paths => 'arrayref|text', | ||
| 119 | preload => 'arrayref|text', | ||
| 120 | debugs => 'arrayref|text', | ||
| 121 | # Hashrefs: | ||
| 122 | math_formats => 'arrayref|text', | ||
| 123 | authlist => 'hashref|user:text,pass:pass', | ||
| 124 | # Bools: | ||
| 125 | noparse => 'bool', | ||
| 126 | defaultcss => 'bool', | ||
| 127 | local => 'bool', | ||
| 128 | comments => 'bool', | ||
| 129 | icon => 'bool', | ||
| 130 | embed => 'bool', | ||
| 131 | post => 'bool', | ||
| 132 | parallelmath => 'bool', | ||
| 133 | showversion => 'bool', | ||
| 134 | strict => 'bool', | ||
| 135 | includestyles => 'bool', | ||
| 136 | 'force_ids' => 'bool', | ||
| 137 | # Text: | ||
| 138 | stylesheet => 'text', | ||
| 139 | inputencoding => 'text', | ||
| 140 | base => 'text', | ||
| 141 | preamble => 'text', | ||
| 142 | documentid => 'text', | ||
| 143 | timeout => 'number',#Do we want this? | ||
| 144 | # Choice: | ||
| 145 | format => 'select|xhtml,html5,html,xml,tex,box', #TODO | ||
| 146 | verbosity => 'select|-5,-1,0,1,5', | ||
| 147 | type => 'select|auto,bib' | ||
| 148 | }; | ||
| 149 | |||
| 150 | sub summary_profile { | ||
| 151 | my ($self,$profile) = @_; | ||
| 152 | my $entry = $self->{db}->lookup("profile:$profile"); | ||
| 153 | my $summary = '<div class="left-div"><table class="form-table">'; | ||
| 154 | my @keys = sort $entry->getKeys; | ||
| 155 | my $split_when = int(scalar(@keys)/2)+1; | ||
| 156 | foreach my $e(@keys) { | ||
| 157 | my $t = $profile_templates->{$e}; | ||
| 158 | $summary.= _inner_handle($e,$t,$entry); | ||
| 159 | $split_when--; | ||
| 160 | unless ($split_when) { $summary.='</table></div><div class="right-div"><table class="form-table">';} | ||
| 161 | } | ||
| 162 | $summary.='</table></div><div class="left-div"><table class="form-table"><tr><td></td><td class="input"><input type="submit" value="Save" /></td></tr>'; | ||
| 163 | $summary.='</table></div>'; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub _inner_handle { | ||
| 167 | my ($e,$t,$entry,$flag) = @_; | ||
| 168 | my ($type, $spec) = split(/\|/,$t); | ||
| 169 | my $summary = q{}; | ||
| 170 | if ($type eq 'bool') { $summary .= "<tr><td>$e</td><td class=\"input\"><select name=\"$e\">"; | ||
| 171 | if ($entry->getValue($e)||$flag) { | ||
| 172 | $summary .= "<option value=\"1\">yes</option><option value=\"0\">no</option>"; | ||
| 173 | } else { | ||
| 174 | $summary .= "<option value=\"0\">no</option><option value=\"1\">yes</option>"; | ||
| 175 | } | ||
| 176 | $summary.="</td></tr>\n"; } | ||
| 177 | elsif ($type eq 'text') { $summary .= "<tr><td>$e</td><td class=\"input\"><input name=\"$e\"></td></tr>\n"; } | ||
| 178 | elsif ($type eq 'pass') { $summary .= "<tr><td>$e</td><td class=\"input\"><input type=\"password\" name=\"$e\"></td></tr>\n"; } | ||
| 179 | elsif ($type eq 'number') { $summary .= "<tr><td>$e</td><td class=\"input\"><input name=\"$e\"></td></tr>\n"; } | ||
| 180 | elsif ($type eq 'select') { | ||
| 181 | $summary .= "<tr><td>$e</td><td class=\"input\"><select name=\"$e\">"; | ||
| 182 | my $sel = $entry->getValue($e); | ||
| 183 | foreach (split(',',$spec)) { | ||
| 184 | if ($sel ne $_ ) { | ||
| 185 | $summary .= "<option value=\"$_\">$_</option>"; | ||
| 186 | } else { | ||
| 187 | $summary .= "<option value=\"$_\" selected>$_</option>"; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | $summary.="</td></tr>\n"; } | ||
| 191 | elsif ($type eq 'arrayref') { | ||
| 192 | my $current = $entry->getValue($e); | ||
| 193 | $summary .= "<tr><td>$e"; | ||
| 194 | $summary .= "</td><td class=\"input\"><input name=\"$e\" value=\"$_\"></td></tr><tr><td>\n" foreach @$current; | ||
| 195 | $summary.= "</td><td></td></tr>"; | ||
| 196 | } | ||
| 197 | elsif ($type eq 'hashref') { | ||
| 198 | my $current = $entry->getValue($e); | ||
| 199 | $summary .= "<tr><td style=\"border-bottom:2px dashed gray;\"> </td><td style=\"border-bottom:2px dashed gray;\"> <td/></tr>"; | ||
| 200 | $summary .= "<tr><td><b>Block $e</b><td></td></tr>"; | ||
| 201 | foreach (split(/,/,$spec)) { | ||
| 202 | my ($in_e,$in_t) = split(/:/,$_); | ||
| 203 | $summary .= _inner_handle($in_e,$in_t,$entry,$current->{$in_e}); | ||
| 204 | } | ||
| 205 | $summary .= "<tr><td style=\"border-top:2px dashed gray;\"> </td><td style=\"border-top:2px dashed gray;\"> <td/></tr>"; | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | $summary; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub template_profile { | ||
| 213 | my ($self,$profile,$value) = @_; | ||
| 214 | # Add template for adding a new profile | ||
| 215 | } | ||
| 216 | ########################################### | ||
| 217 | #### Statistics ##### | ||
| 218 | ########################################### | ||
| 219 | |||
| 220 | |||
| 221 | sub status { | ||
| 222 | my ($self) = @_; | ||
| 223 | #TODO: Provide info on the status of the DB, table of booted converters with their profiles, etc. | ||
| 224 | $self->{db}->status; | ||
| 225 | } | ||
| 226 | |||
| 227 | #TODO: Also provide an interface for examining and changing the options of existing converters | ||
| 228 | |||
| 229 | ########################################### | ||
| 230 | #### ObjectDB helpers ##### | ||
| 231 | ########################################### | ||
| 232 | package LaTeXML::Util::ObjectDB::Entry; | ||
| 233 | sub as_hashref { | ||
| 234 | my($self)=@_; | ||
| 235 | my $hashref = {}; | ||
| 236 | foreach (keys %$self) { | ||
| 237 | $hashref->{$_} = decodeValue($self->{$_}); | ||
| 238 | } | ||
| 239 | return $hashref; | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | |||
| 244 | __END__ |