Newer
Older
Digital_Repository / OARiNZ / DIY / deb_package / eprints-3.0 / bin / epadmin
nstanger on 7 Jun 2007 40 KB - Added debian package source.
  1. #!/usr/bin/perl -w -I/opt/eprints3/perl_lib
  2.  
  3. ######################################################################
  4. #
  5. # This file is part of GNU EPrints 2.
  6. #
  7. # Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
  8. #
  9. # EPrints 2 is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # EPrints 2 is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with EPrints 2; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. #
  23. ######################################################################
  24.  
  25. =pod
  26.  
  27. =head1 NAME
  28.  
  29. B<epadmin> - EPrints repository admin tool
  30.  
  31. =head1 SYNOPSIS
  32.  
  33. =over 8
  34.  
  35. =item B<epadmin> create
  36.  
  37. START HERE! This option will walk you through the tasks needed to create your repository.
  38.  
  39. =item B<epadmin> test I<repository_id>
  40.  
  41. A null operation which just checks your configuration files are OK and that you can connect to the database.
  42.  
  43. =item B<epadmin> config_core I<repository_id>
  44.  
  45. Set hostname, contact email and repository name.
  46.  
  47. =item B<epadmin> config_db I<repository_id>
  48.  
  49. Set database connection properties and, optionally, to create database and database user.
  50.  
  51. =item B<epadmin> create_db I<repository_id>
  52.  
  53. Create a database and database user with the current settings.
  54.  
  55. =item B<epadmin> create_tables I<repository_id>
  56.  
  57. Create the database tables.
  58.  
  59. =item B<epadmin> create_user I<repository_id>
  60.  
  61. Create a new user. You need to do this to create your first admin account.
  62.  
  63. =item B<epadmin> erase_fulltext_index I<repository_id>
  64.  
  65. This erases all the .words and .indexcodes cache files from your repository, forcing the indexer to rerun the tools used to extract full text from your documents.
  66.  
  67. This is useful if you only setup the fulltext indexing after your repository is already live, or if you discover there has been a problem.
  68.  
  69. =item B<epadmin> recommit I<repository_id> I<dataset_id>
  70.  
  71. Recommit all the records in the given dataset. What this does is cause the automatic values to be re-calculated.
  72.  
  73. =item B<epadmin> reindex I<repository_id> I<dataset_id>
  74.  
  75. Schedule the dataset for reindexing. The indexer will do the actual indexing and it may take some time. This only schedules the reindexing.
  76.  
  77. =item B<epadmin> rehash I<repository_id> [I<document_id>]
  78.  
  79. Recalculate the hashes of the files in this document and write it to a probity log file. If a document id is given then just generate the hash for that document.
  80.  
  81. =item B<epadmin> reload I<repository_id>
  82.  
  83. Cause the web server to reload the repository configuration.
  84.  
  85. =item B<epadmin> redo_thumbnails I<repository_id>
  86.  
  87. Regenerate all the thumbnail and image-preview files.
  88.  
  89. =item B<epadmin> erase_data I<repository_id>
  90.  
  91. Erases and recreates the database. Removes all documents and files. Does not touch the configuration files.
  92.  
  93. =item B<epadmin> erase_eprints I<repository_id>
  94.  
  95. Erases all the documents and eprints (including their files). Recreates the eprint and document tables. Leaves configuration files and the users and subjects tables alone.
  96.  
  97. =item B<epadmin> upgrade I<repository_id>
  98.  
  99. After upgrading EPrints, use this to update the database tables. It will advise any other tasks that are required.
  100.  
  101. =item B<epadmin> --help
  102.  
  103. =back
  104.  
  105. =head1 OPTIONS
  106.  
  107. =over 8
  108.  
  109. =item B<--help>
  110.  
  111. Print a brief help message and exit.
  112.  
  113. =item B<--man>
  114.  
  115. Print the full manual page and then exit.
  116.  
  117. =item B<--quiet>
  118.  
  119. This option does not do anything.
  120.  
  121. =item B<--verbose>
  122.  
  123. Explain in detail what is going on. May be repeated for greater effect.
  124.  
  125. =item B<--version>
  126.  
  127. Output version information and exit.
  128.  
  129. =back
  130.  
  131. =head1 AUTHOR
  132.  
  133. This is part of this EPrints 3 system. EPrints 3 is developed by Christopher Gutteridge.
  134.  
  135. =head1 VERSION
  136.  
  137. EPrints Version: 3.0
  138.  
  139. =head1 CONTACT
  140.  
  141. For more information goto B<http://www.eprints.org/> which give information on mailing lists and the like.
  142.  
  143. Chris Gutteridge may be contacted at B<support@eprints.org>
  144.  
  145. Should you need a real world address for some reason, EPrints can be contacted in the real world at
  146.  
  147. EPrints c/o Christopher Gutteridge
  148. Department of Electronics and Computer Science
  149. University of Southampton
  150. SO17 1BJ
  151. United Kingdom
  152.  
  153. =head1 COPYRIGHT
  154.  
  155. This file is part of GNU EPrints 2.
  156.  
  157. Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
  158.  
  159. EPrints 2 is free software; you can redistribute it and/or modify
  160. it under the terms of the GNU General Public License as published by
  161. the Free Software Foundation; either version 2 of the License, or
  162. (at your option) any later version.
  163.  
  164. EPrints 2 is distributed in the hope that it will be useful,
  165. but WITHOUT ANY WARRANTY; without even the implied warranty of
  166. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  167. GNU General Public License for more details.
  168.  
  169. You should have received a copy of the GNU General Public License
  170. along with EPrints 2; if not, write to the Free Software
  171. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  172.  
  173.  
  174. =cut
  175.  
  176. #cjg Does not use noise levels
  177.  
  178.  
  179. use EPrints;
  180.  
  181. use Sys::Hostname;
  182. use DBI;
  183. use Unicode::String qw(utf8 latin1);
  184. use Data::Dumper;
  185.  
  186. use strict;
  187. use Getopt::Long;
  188. use Pod::Usage;
  189.  
  190. my $verbose = 0;
  191. my $quiet = 0;
  192. my $help = 0;
  193. my $man = 0;
  194. my $version = 0;
  195.  
  196. GetOptions(
  197. 'help|?' => \$help,
  198. 'man' => \$man,
  199. 'version' => \$version,
  200. 'verbose+' => \$verbose,
  201. 'silent' => \$quiet,
  202. 'quiet' => \$quiet
  203. ) || pod2usage( 2 );
  204. EPrints::Utils::cmd_version( "epadmin" ) if $version;
  205. pod2usage( 1 ) if $help;
  206. pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
  207. pod2usage( 2 ) if( scalar @ARGV == 0 );
  208.  
  209. # Set STDOUT to auto flush (without needing a \n)
  210. $|=1;
  211.  
  212. my $noise = 1;
  213. $noise = 0 if( $quiet );
  214. $noise = 1+$verbose if( $verbose );
  215.  
  216. my $REGEXP_HOSTNAME_MIDDLE = '[a-z0-9-]+(\.[a-z0-9-]+)*';
  217. my $REGEXP_HOSTNAME = '^'.$REGEXP_HOSTNAME_MIDDLE.'$';
  218. my $REGEXP_EMAIL = '^[^@]+@'.$REGEXP_HOSTNAME_MIDDLE.'$';
  219. my $REGEXP_HOSTNAME_FULL = '^[a-z0-9-]+(\.[a-z0-9-]+)+$';
  220. my $REGEXP_VARNAME = '^[a-zA-Z][_A-Za-z0-9]*$';
  221. my $REGEXP_NUMBER = '^[0-9]+$';
  222. my $REGEXP_YESNO = '^(yes|no)$';
  223. my $REGEXP_ANY = '^.*$';
  224.  
  225. my $action = $ARGV[0];
  226. if( $action eq "create" ) { create(); }
  227. else
  228. {
  229. my $repoid = $ARGV[1];
  230. pod2usage(1) unless defined $repoid;
  231. if( $action eq "config_core" ) { config_core( $repoid ); }
  232. elsif( $action eq "config_db" ) { config_db( $repoid ); }
  233. elsif( $action eq "create_db" ) { create_db( $repoid ); }
  234. elsif( $action eq "create_user" ) { create_user( $repoid ); }
  235. elsif( $action eq "create_tables" ) { create_tables( $repoid ); }
  236. elsif( $action eq "erase_data" ) { erase_data( $repoid ); }
  237. elsif( $action eq "erase_eprints" ) { erase_eprints( $repoid ); }
  238. elsif( $action eq "erase_fulltext_index" ) { erase_fulltext_index( $repoid ); }
  239. elsif( $action eq "reload" ) { reload( $repoid ); }
  240. elsif( $action eq "redo_thumbnails" ) { redo_thumbnails( $repoid ); }
  241. elsif( $action eq "upgrade" ) { upgrade( $repoid ); }
  242. elsif( $action eq "test" ) { test( $repoid ); }
  243. elsif( $action eq "recommit" )
  244. {
  245. my $datasetid = $ARGV[2];
  246. pod2usage(1) unless defined $datasetid;
  247. recommit( $repoid, $datasetid );
  248. }
  249. elsif( $action eq "reindex" )
  250. {
  251. my $datasetid = $ARGV[2];
  252. pod2usage(1) unless defined $datasetid;
  253. reindex( $repoid, $datasetid );
  254. }
  255. elsif( $action eq "rehash" ) { rehash( $repoid ); }
  256. else { pod2usage( 1 ); }
  257. }
  258.  
  259.  
  260. exit;
  261.  
  262.  
  263.  
  264. sub create
  265. {
  266. pod2usage( 2 ) if( scalar @ARGV != 1 );
  267.  
  268. print <<END;
  269.  
  270. Create an EPrint Repository
  271.  
  272. Please select an ID for the repository, which will be used to create a directory
  273. and identify the repository. Lower case letters and numbers, may not start with
  274. a number. examples: "lemurprints" or "test3"
  275.  
  276. END
  277. if( scalar EPrints::Config::get_repository_ids() )
  278. {
  279. print "Existing repositories:\n";
  280. print join( ", ", EPrints::Config::get_repository_ids() )."\n\n";
  281. }
  282.  
  283. my $repoid = EPrints::Utils::get_input( $REGEXP_VARNAME, 'Archive ID' );
  284.  
  285. my $repodir = EPrints::Config::get( "base_path" )."/archives/".$repoid;
  286.  
  287. my $loaded_config = EPrints::Config::get_repository_config( $repoid );
  288. my $exists = ( defined $loaded_config );
  289.  
  290. if( $exists )
  291. {
  292. print "A repository with that ID already exist.\n";
  293. exit;
  294. }
  295.  
  296. unless( -e $repodir )
  297. {
  298. print "We need to create $repodir, doing it now...\n";
  299. unless( EPrints::Platform::mkdir( $repodir ) )
  300. {
  301. print "Problem creating directory\n\n";
  302. exit;
  303. }
  304. }
  305. unless( -d $repodir )
  306. {
  307. print "$repodir MUST be a directory.\n\n";
  308. }
  309.  
  310. my $username = EPrints::Config::get( "user" );
  311. print "Getting uid and gid information for $username\n";
  312. my(undef,undef,$uid,$gid) = EPrints::Platform::getpwnam( $username );
  313. print "UID: $uid\n";
  314. print "GID: $gid\n";
  315. print "\nCreating initial files:\n";
  316. &install(
  317. EPrints::Config::get( "base_path" )."/lib/defaultcfg",
  318. $EPrints::SystemSettings::conf->{"file_perms"},
  319. $uid,
  320. $gid,
  321. $repodir."/cfg",
  322. $repoid );
  323.  
  324. foreach( "var", "html", "documents", "documents/disk0" )
  325. {
  326. my $dir = $repodir."/".$_;
  327. EPrints::Platform::mkdir( $dir );
  328. EPrints::Platform::chown( $uid, $gid, $dir );
  329. }
  330. print <<END;
  331.  
  332. Ok. I've created the initial config files and directory structure.
  333. I've also created a "disk0" directory under documents/ if you want
  334. your full texts to be stored on a different partition then remove
  335. the disk0, and create a symbolic link to the directory you wish to
  336. store the full texts in. Additional links may be placed here to be
  337. used when the first is full.
  338.  
  339. END
  340. print "\n";
  341. my $config_core = EPrints::Utils::get_input( $REGEXP_YESNO, "Configure vital settings?", "yes" );
  342. if( $config_core eq "yes" )
  343. {
  344. config_core( $repoid );
  345. }
  346. else
  347. {
  348. print "OK, but you'll need to edit 10_core.pl by hand in that case.\n";
  349. }
  350.  
  351. print "\n";
  352. my $config_db = EPrints::Utils::get_input( $REGEXP_YESNO, "Configure database?", "yes" );
  353. if( $config_db eq "yes" )
  354. {
  355. config_db( $repoid );
  356. }
  357. else
  358. {
  359. print "OK, but you'll need to edit database.pl by hand in that case, and make sure the database exists.\n";
  360. }
  361.  
  362.  
  363. print "\n";
  364. my $create_user = EPrints::Utils::get_input( $REGEXP_YESNO, "Create an initial user?", "yes" );
  365. if( $create_user eq "yes" )
  366. {
  367. create_user( $repoid );
  368. }
  369. else
  370. {
  371. print "OK, but you will not be able to log into the website. You can always run 'epadmin create_user $repoid' later.\n"
  372. }
  373. # cjg: Register with website!
  374.  
  375. my $ok;
  376. $ok = EPrints::Utils::get_input( $REGEXP_YESNO, "Do you want to build the static web pages?", "yes" );
  377. if( $ok eq "yes" )
  378. {
  379. run_script( $repoid, "generate_static", "--verbose", $repoid );
  380. }
  381.  
  382. $ok = EPrints::Utils::get_input( $REGEXP_YESNO, "Do you want to import the LOC subjects?", "yes" );
  383. if( $ok eq "yes" )
  384. {
  385. run_script( $repoid, "import_subjects", "--verbose", "--force", $repoid );
  386. }
  387.  
  388. $ok = EPrints::Utils::get_input( $REGEXP_YESNO, "Do you want to update the apache config files? (you still need to add the 'Include' line)", "yes" );
  389. if( $ok eq "yes" )
  390. {
  391. run_script( $repoid, "generate_apacheconf", "--verbose" );
  392. }
  393.  
  394. my $base_path = EPrints::Config::get( "base_path" );
  395. print <<END;
  396.  
  397. --------------------------------------------------------------------------
  398. That seemed to more or less work...
  399. --------------------------------------------------------------------------
  400.  
  401. Now make any required changes to the cfg files.
  402.  
  403. Note that changing the metadata configuration may require the database
  404. tables to be regenerated. epadmin erase_data will regenerate the
  405. eprints and documents tables only. erase_data will regenerate everything.
  406. (nb. these also do erase the contents of the tables, and any uploaded
  407. files).
  408.  
  409. Make sure that your main apache config file contains the line:
  410.  
  411. Include $base_path/cfg/apache.conf
  412.  
  413. Then stop and start your webserver:
  414. Often:
  415. /etc/rc.d/init.d/httpd stop
  416. /etc/rc.d/init.d/httpd start
  417. (or maybe /usr/local/apache/bin/apachectl stop & start)
  418.  
  419. And then try connecting to your repository.
  420. --------------------------------------------------------------------------
  421.  
  422. Don't forget to register your repository at http://roar.eprints.org/
  423.  
  424. END
  425.  
  426. exit;
  427.  
  428. }
  429.  
  430. # don't be fooled. This isn't the same as the install() in
  431. # eprints-install
  432. sub install
  433. {
  434. my($dir, $perms, $user, $group, $dest, $repoid) = @_;
  435.  
  436. print "Installing: $dest\n";
  437. opendir(INDIR, $dir) or die("Unable to install directory: $dir");
  438. my @dirs = ();
  439. my @files = ();
  440. EPrints::Platform::mkdir( $dest );
  441. EPrints::Platform::chown($user, $group, $dest);
  442. while(my $item = readdir(INDIR))
  443. {
  444. next if $item=~m/^\./;
  445. if (-d "$dir/$item") { push(@dirs, $item); }
  446. else { push(@files, $item); }
  447. }
  448. closedir(INDIR);
  449. foreach my $filename (@files)
  450. {
  451. if( $filename eq "Config.pm" )
  452. {
  453. open(my $i_fh, "<", "$dir/$filename");
  454. open(my $o_fh, ">", "$dest/$filename")
  455. or die "Can't write to $dest/$filename";
  456. while(defined($_ = <$i_fh>))
  457. {
  458. if( /^package/ )
  459. {
  460. # Change the package name to the local repository ($/ = line seperator)
  461. $_ = "package EPrints::Config::$repoid;$/";
  462. }
  463. print $o_fh $_;
  464. }
  465. close($i_fh);
  466. close($o_fh);
  467. }
  468. else
  469. {
  470. EPrints::Utils::copy("$dir/$filename", "$dest/$filename");
  471. }
  472. EPrints::Platform::chmod($perms, "$dest/$filename");
  473. EPrints::Platform::chown($user, $group, "$dest/$filename");
  474. }
  475. foreach(@dirs)
  476. {
  477. install("$dir/".$_, $perms, $user, $group, "$dest/$_", $repoid );
  478. }
  479. }
  480.  
  481. sub run_script
  482. {
  483. my( $repoid, $script, @opts ) = @_;
  484.  
  485. my $repo = EPrints::Repository->new( $repoid );
  486. my $bin = $repo->get_conf( "bin_path" );
  487.  
  488. system( "$bin/$script", @opts );
  489. }
  490.  
  491. sub config_core
  492. {
  493. my( $repoid ) = @_;
  494.  
  495. print "Core configuration for $repoid\n\n";
  496.  
  497. my %config = ();
  498.  
  499. $config{port} = 80;
  500. $config{host} = undef;
  501. $config{urlpath} = '/';
  502. $config{archiveroot} = "archives/".$repoid;
  503. $config{configmodule} = "cfg/Config.pm";
  504. $config{aliases} = [];
  505. $config{securehost} = undef;
  506. $config{securepath} = undef;
  507.  
  508. $config{adminemail} = undef;
  509.  
  510. $config{archive_name} = "Test Repository";
  511.  
  512. print <<END;
  513.  
  514. Please enter the fully qualified hostname of the repository.
  515.  
  516. For a production system we recommend against using the real hostname of the
  517. machine.
  518.  
  519. Example: $repoid.footle.ac.uk
  520.  
  521. END
  522.  
  523. $config{host} = EPrints::Utils::get_input( $REGEXP_HOSTNAME_FULL, 'Hostname', $config{host} );
  524.  
  525. print <<END;
  526.  
  527. Please enter the port of the webserver. This is probably 80, but you may wish
  528. to run apache on a different port if you are experimenting.
  529.  
  530. END
  531.  
  532. $config{port} = EPrints::Utils::get_input( $REGEXP_NUMBER, 'Webserver Port', $config{port} );
  533.  
  534.  
  535. # calculate example aliases
  536. my $realhostname = hostname();
  537. if( $realhostname !~ m/\./ )
  538. {
  539. # No dots in the actual hostname! Lets try and got the
  540. # domain from resolv.conf
  541. my $domain = "";
  542. if( open( RESOLV, "/etc/resolv.conf" ) && 0)
  543. {
  544. while( <RESOLV> )
  545. {
  546. if( m/^search\s+([^\s]+)/ )
  547. {
  548. $domain = $1;
  549. last;
  550. }
  551. }
  552. close RESOLV;
  553. }
  554. $domain = "mydomain.com" if( $domain eq "" );
  555. $realhostname.=".".$domain;
  556. }
  557. my @example_aliases = ();
  558. push @example_aliases,$realhostname;
  559. $realhostname=~m/^(([^\.]*)\.[^\.]*)(\.|$)?/;
  560. push @example_aliases,$1 if( $3 eq ".");
  561. push @example_aliases,$2;
  562. $config{host}=~m/^(([^\.]*)\.[^\.]*)(\.|$)?/;
  563. push @example_aliases,$1 if( $3 eq "." );
  564. push @example_aliases,$2;
  565. print <<END;
  566. Please enter all the aliases which could reach the repository, and indicate if
  567. you would like EPrints to write a Redirect Rule to redirect requests to this
  568. alias to the correct URL.
  569. END
  570. if( scalar @{$config{aliases}}==0 )
  571. {
  572. print "Some suggestions:\n";
  573. foreach( @example_aliases )
  574. {
  575. print $_."\n";
  576. }
  577. }
  578. print <<END;
  579. Enter a single hash (#) when you're done.
  580.  
  581. END
  582.  
  583. my @aliases = @{$config{aliases}};
  584. $config{aliases} = [];
  585.  
  586. for(;;)
  587. {
  588. my $default = shift @aliases;
  589. my $alias = EPrints::Utils::get_input( '^('.$REGEXP_HOSTNAME_MIDDLE.'|#)$', 'Alias (enter # when done)',
  590. (defined $default ? $default->{name} : '#' ) );
  591. last if( $alias eq "#" );
  592. my $aliasrecord = {};
  593. $aliasrecord->{name} = $alias;
  594. $aliasrecord->{redirect} =
  595. EPrints::Utils::get_input(
  596. $REGEXP_YESNO,
  597. "Redirect $alias to $config{host}",
  598. (defined $default && !$default->{redirect} ? 'no' : 'yes' ) );
  599. push @{$config{aliases}},$aliasrecord;
  600. print "\n";
  601. }
  602.  
  603. #print <<END;
  604. #
  605. #Language Configuration
  606. #
  607. #Please enter the primary language and other supported languages for the
  608. #repository. Supporting other languages represents a serious commitment to
  609. #translate all the phrases and templates etc. into each of these other
  610. #languages.
  611. #
  612. #Available languages: (please use the ID to refer to them)
  613. #END
  614. #my @langs = EPrints::Config::get_supported_languages();
  615. #foreach( @langs )
  616. #{
  617. # my $title = utf8("");
  618. # $title->utf8( "".EPrints::Config::lang_title( $_ ) );
  619. # print $_." - ".($title->latin1)."\n";
  620. #}
  621. #print <<END;
  622. #
  623. #If you plan to add support for another language, you will need to edit the
  624. #languages.xml file to indicate that this language is supported, and create
  625. #the relevant phrase and template files.
  626. #
  627. #END
  628.  
  629. print "\n";
  630. print "\n";
  631.  
  632. $config{adminemail} = EPrints::Utils::get_input( $REGEXP_EMAIL, 'Administrator Email', $config{adminemail} );
  633. print <<END;
  634.  
  635. Enter the name of the repository in the default language. If you wish to enter
  636. other titles for other languages or enter non ascii characters then you may
  637. enter something as a placeholder and edit the XML config file which this
  638. script generates.
  639.  
  640. END
  641. $config{archive_name} = EPrints::Utils::get_input( '^.+$', 'Archive Name', $config{archive_name} );
  642.  
  643. # Write files?
  644.  
  645. print "\n";
  646. my $config_core = EPrints::Utils::get_input( $REGEXP_YESNO, "Write these core settings?", "yes" );
  647. if( $config_core eq "no" )
  648. {
  649. print "\nOK. Not writing after all.\n";
  650. return;
  651. }
  652.  
  653. # Write files!
  654.  
  655. my $repodir = EPrints::Config::get( "base_path" )."/archives/".$repoid;
  656.  
  657. my $aemailfile = "$repodir/cfg/cfg.d/adminemail.pl";
  658. open( AEMAIL, ">$aemailfile" ) || die "Could not write to $aemailfile: $!";
  659. print AEMAIL Data::Dumper->Dump(
  660. [
  661. $config{adminemail},
  662. ],
  663. [qw/
  664. $c->{adminemail}
  665. /]
  666. );
  667. close AEMAIL;
  668. print "Wrote $aemailfile\n";
  669.  
  670. my $corefile = "$repodir/cfg/cfg.d/10_core.pl";
  671. open( CORE, ">$corefile" ) || die "Could not write to $corefile: $!";
  672. print CORE Data::Dumper->Dump(
  673. [
  674. $config{host},
  675. $config{port},
  676. $config{aliases},
  677. $config{securehost},
  678. $config{secureport},
  679. ],
  680. [qw/
  681. $c->{host}
  682. $c->{port}
  683. $c->{aliases}
  684. $c->{securehost}
  685. $c->{securepath}
  686. /]
  687. );
  688. close CORE;
  689. print "Wrote $corefile\n";
  690.  
  691. my $anamefile = "$repodir/cfg/lang/en/phrases/archive_name.xml";
  692. open( ANAME, ">$anamefile" ) || die "Could not write to $anamefile: $!";
  693. print ANAME <<END;
  694. <?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
  695. <!DOCTYPE phrases SYSTEM "entities.dtd">
  696.  
  697. <epp:phrases xmlns="http://www.w3.org/1999/xhtml"
  698. xmlns:epp="http://eprints.org/ep3/phrase">
  699.  
  700. <epp:phrase id="archive_name">$config{archive_name}</epp:phrase>
  701.  
  702. </epp:phrases>
  703. END
  704. close( ANAME );
  705. print "Wrote $anamefile\n";
  706. }
  707.  
  708. sub config_db
  709. {
  710. my( $repoid ) = @_;
  711.  
  712. my %config = ();
  713. $config{dbname} = $repoid;
  714. $config{dbhost} = "localhost";
  715. $config{dbport} = undef;
  716. $config{dbsock} = undef;
  717. $config{dbuser} = $repoid;
  718. $config{dbpass} = undef;
  719.  
  720. print "\nConfiguring Database for: $repoid\n";
  721.  
  722. $config{dbname} = EPrints::Utils::get_input( $REGEXP_VARNAME, 'Database Name', $config{dbname} );
  723. $config{dbhost} = EPrints::Utils::get_input( $REGEXP_HOSTNAME, 'MySQL Host', $config{dbhost} );
  724.  
  725. print "\nYou probably don't need to set socket and port (unless you do!?).\n";
  726. $config{dbport} = "#" if( !defined $config{dbport} );
  727. $config{dbport} = EPrints::Utils::get_input( '^[0-9]+|#$', 'MySQL Port (# for no setting)', $config{dbport} );
  728. $config{dbport} = undef if( $config{dbport} eq "#" );
  729.  
  730. $config{dbsock} = "#" if( !defined $config{dbsock} );
  731. # can't remember what is a legal mysql socket... cjg
  732. $config{dbsock} = EPrints::Utils::get_input( '^.*$', 'MySQL Socket (# for no setting)', $config{dbsock} );
  733. $config{dbsock} = undef if( $config{dbsock} eq "#" );
  734.  
  735. $config{dbuser} = EPrints::Utils::get_input( $REGEXP_VARNAME, 'Database User', $config{dbuser} );
  736. $config{dbpass} = EPrints::Utils::get_input_hidden( $REGEXP_VARNAME, 'Database Password', $config{dbpass} );
  737.  
  738. print "\n";
  739. my $config_db = EPrints::Utils::get_input( $REGEXP_YESNO, "Write these database settings?", "yes" );
  740. if( $config_db eq "no" )
  741. {
  742. print "\nOK. Not writing after all.\n";
  743. return;
  744. }
  745.  
  746. my $repodir = EPrints::Config::get( "base_path" )."/archives/".$repoid;
  747. my $dbfile = "$repodir/cfg/cfg.d/database.pl";
  748. open( DBCONF, ">$dbfile" ) || die "Could not write to $dbfile: $!";
  749. print DBCONF Data::Dumper->Dump(
  750. [
  751. $config{dbname},
  752. $config{dbhost},
  753. $config{dbport},
  754. $config{dbsock},
  755. $config{dbuser},
  756. $config{dbpass},
  757. ],
  758. [qw/
  759. $c->{dbname}
  760. $c->{dbhost}
  761. $c->{dbport}
  762. $c->{dbsock}
  763. $c->{dbuser}
  764. $c->{dbpass}
  765. /]
  766. );
  767. close DBCONF;
  768. print "Wrote $dbfile\n";
  769.  
  770. print <<END;
  771.  
  772. EPrints can create the database, and grant the correct permissions.
  773. END
  774. my $makedb = EPrints::Utils::get_input( $REGEXP_YESNO, "Create database \"$config{dbname}\"", "yes" );
  775. if( $makedb eq "yes" )
  776. {
  777. create_db( $repoid );
  778. }
  779. else
  780. {
  781. print "\nWell, OK. But you'll need to do it yourself then.\n";
  782. }
  783.  
  784. }
  785.  
  786. my $mysql_root_password;
  787. # subroutine so that it can cache if we do several operations
  788. sub get_mysql_root_password
  789. {
  790. return $mysql_root_password if( defined $mysql_root_password );
  791.  
  792. #cjg hide password from display?
  793. print <<END;
  794.  
  795. Ok, I'll need to connect to the mysql database as root. What is the root
  796. password?
  797. END
  798. $mysql_root_password = EPrints::Utils::get_input_hidden( '^.*$', "MySQL Root Password" );
  799.  
  800. return $mysql_root_password;
  801. }
  802.  
  803. sub root_dbh
  804. {
  805. my( $repoid, $dbname ) = @_;
  806.  
  807. my $config = EPrints::Config::load_repository_config_module( $repoid );
  808.  
  809. $dbname = $config->{dbname} unless defined $dbname;
  810.  
  811. my $mysqlrootpassword = get_mysql_root_password();
  812. print "Connecting to the database...\n";
  813. my $dbh = DBI->connect(
  814. EPrints::Database::build_connection_string(
  815. dbname=>$dbname,
  816. dbsock=>$config->{dbsock},
  817. dbport=>$config->{dbport},
  818. dbhost=>$config->{dbhost} ),
  819. "root",
  820. $mysqlrootpassword );
  821. return $dbh;
  822. }
  823.  
  824. sub create_db
  825. {
  826. my( $repoid ) = @_;
  827.  
  828. my $dbh = root_dbh( $repoid, "mysql" );
  829.  
  830. if( !defined $dbh )
  831. {
  832. print <<END;
  833.  
  834. Hmmm. Problem connecting to database as root. We'll skip this but
  835. you should create it by hand.
  836.  
  837. END
  838. exit 1;
  839. }
  840.  
  841. my $mversion = EPrints::Database::mysql_version_from_dbh( $dbh );
  842. my $oldpasswords = 0;
  843. if( $mversion >= 40100 )
  844. {
  845. print "MySQL version id $mversion >= 40100\n";
  846. print "MYSQL OLD PASSWORDS will be used for compatibility with DBI::mysql\n";
  847. $oldpasswords = 1;
  848. }
  849.  
  850. my $config = EPrints::Config::load_repository_config_module( $repoid );
  851.  
  852. my $sth = $dbh->prepare( "show databases" );
  853. $sth->execute;
  854. my $dbexists = 0;
  855. my @row;
  856. while( @row = $sth->fetchrow_array )
  857. {
  858. $dbexists = 1 if $row[0] eq $config->{dbname};
  859. }
  860. if( $dbexists )
  861. {
  862. print "Hmm. A database called ".$config->{dbname}." already exists, oh well.\n\n";
  863. return;
  864. }
  865. else
  866. {
  867. my $SQL = "CREATE DATABASE ".$config->{dbname};
  868. #print "DOING: $SQL\n";
  869. $dbh->do( $SQL );
  870. }
  871. print "Setting MySQL privs\n";
  872. #cjg @localhost ??? what about remote mysql's?
  873.  
  874. my $SQL;
  875. $SQL = 'GRANT ALL ON '.$config->{dbname}.'.* TO '.$config->{dbuser}.'@localhost';
  876. #print "DOING: $SQL\n";
  877. $dbh->do( $SQL );
  878. my $pass = '"'.$config->{dbpass}.'"';
  879. if( $oldpasswords )
  880. {
  881. $pass = "OLD_PASSWORD($pass)";
  882. } else {
  883. $pass = "PASSWORD($pass)";
  884. }
  885. $SQL = 'SET PASSWORD FOR '.$config->{dbuser}.'@localhost = '.$pass;
  886. #print "DOING: $SQL\n";
  887. $dbh->do( $SQL );
  888.  
  889. print "Disconnecting from database.\n\n";
  890. $dbh->disconnect;
  891.  
  892. my $mktables = EPrints::Utils::get_input( $REGEXP_YESNO, "Create database tables?", "yes" );
  893. if( $mktables eq "yes" )
  894. {
  895. create_tables( $repoid );
  896. }
  897.  
  898. }
  899.  
  900. sub create_user
  901. {
  902. my( $repoid ) = @_;
  903.  
  904. my $session = EPrints::Session->new( 1 , $repoid, $noise );
  905. exit unless( defined $session );
  906.  
  907. my $user_info = {};
  908. print "Creating a new user in $repoid\n\n";
  909. $user_info->{username} = EPrints::Utils::get_input( $REGEXP_VARNAME, 'Enter a username', 'admin' );
  910. while( defined EPrints::DataObj::User::user_with_username( $session, $user_info->{username} ) )
  911. {
  912. print STDERR "User with username '".$user_info->{username}."' already exists.\n";
  913. $user_info->{username} = EPrints::Utils::get_input( $REGEXP_VARNAME, 'Enter a username', 'admin' );
  914. }
  915. my @utypes = $session->get_repository->get_types( "user" );
  916. $user_info->{usertype} = EPrints::Utils::get_input( '^('.join( '|', @utypes ).')$', 'Select a user type ('.join( "|",@utypes).')', 'admin' );
  917. my $rawpassword = EPrints::Utils::get_input_hidden( $REGEXP_VARNAME, 'Enter Password' );
  918. $user_info->{password} = EPrints::Utils::crypt_password( $rawpassword, $session );
  919. $user_info->{email} = EPrints::Utils::get_input( $REGEXP_EMAIL, 'Email' );
  920. my $user_ds = $session->get_repository->get_dataset( "user" );
  921. my $new_user = $user_ds->create_object( $session, $user_info );
  922.  
  923. print "\n";
  924.  
  925. if( defined $new_user )
  926. {
  927. if( $noise >= 1 )
  928. {
  929. print "Successfully created new user:\n";
  930. print " ID: ".$new_user->get_value( "userid" )."\n";
  931. }
  932. if( $noise >= 2 )
  933. {
  934. print " Username: ".$new_user->get_value( "username" )."\n";
  935. print " Type: ".$new_user->get_value( "usertype" )."\n";
  936. }
  937. }
  938. else
  939. {
  940. my $db_error = $session->get_database->error;
  941. print STDERR "Error creating user: $db_error\n";
  942. }
  943. $session->terminate;
  944. }
  945.  
  946.  
  947. sub redo_thumbnails
  948. {
  949. my( $repoid ) = @_;
  950.  
  951. my $session = new EPrints::Session( 1 , $repoid , $noise );
  952. exit( 1 ) unless( defined $session );
  953. my $doc_ds = $session->get_repository->get_dataset( "document" );
  954. $doc_ds->map(
  955. $session,
  956. sub
  957. {
  958. my( $session , $dataset , $doc ) = @_;
  959.  
  960. if( $noise >= 2 )
  961. {
  962. print "Attempting to build thumbnails for document #".$doc->get_id."\n";
  963. }
  964.  
  965. $doc->remove_thumbnails; #ouch!
  966. $doc->make_thumbnails;
  967. } );
  968.  
  969. $session->terminate;
  970. }
  971.  
  972.  
  973.  
  974. sub reload
  975. {
  976. my( $repoid ) = @_;
  977.  
  978. my $session = new EPrints::Session( 1 , $repoid , $noise );
  979. exit( 1 ) unless( defined $session );
  980. my $file = $session->get_repository->get_conf( "variables_path" )."/last_changed.timestamp";
  981. unless( open( CHANGEDFILE, ">$file" ) )
  982. {
  983. EPrints::abort( "Cannot write to file $file" );
  984. }
  985. print CHANGEDFILE "This file last poked at: ".EPrints::Time::human_time()."\n";
  986. close CHANGEDFILE;
  987. if( $noise > 0 )
  988. {
  989. print <<END;
  990. The repository config will be reloaded, but you should still restart apache as
  991. soon as possible.
  992.  
  993. END
  994. }
  995. $session->terminate;
  996. }
  997.  
  998.  
  999. sub create_tables
  1000. {
  1001. my( $repoid ) = @_;
  1002.  
  1003. my $session = new EPrints::Session( 1 , $repoid , $noise, 1 );
  1004. exit( 1 ) unless( defined $session );
  1005.  
  1006. if( $session->get_database->has_table( "eprint" ) )
  1007. {
  1008. print "WARNING: Database is NOT empty. Contains an \"eprint\" table.\n";
  1009. print "You might consider running 'epadmin erase_data $repoid' instead.\n";
  1010. $session->terminate;
  1011. exit 1;
  1012. }
  1013.  
  1014. foreach my $dsid ( &EPrints::DataSet::get_sql_dataset_ids )
  1015. {
  1016. my $ds = $session->get_repository->get_dataset( $dsid );
  1017. my $indexes = $ds->count_indexes;
  1018. if( $indexes > 32 )
  1019. {
  1020. print STDERR "WARNING: Main table of dataset \"$dsid\" requires $indexes indexes.\n";
  1021. $session->terminate;
  1022. EPrints::abort( "MySQL has a maximum of 32 indexes per table and the '$dsid'\ntable requires $indexes. Add an sql_index=>0 parameter to some of the\nfields in this dataset. See the Documentation for more information\non the 'sql_index' metadata parameter. --force will override this\ncheck but the SQL will probably fail anyway." );
  1023. }
  1024. }
  1025. if( $noise>=1 ) { print "Creating database tables...\n"; }
  1026. if( $session->get_database->create_archive_tables )
  1027. {
  1028. if( $noise>=1 ) { print "Done creating database tables.\n\n"; }
  1029. }
  1030. else
  1031. {
  1032. my $error = $session->get_database->error;
  1033. print STDERR "DB Error: $error\n";
  1034. $session->terminate;
  1035. exit 1;
  1036. }
  1037. }
  1038.  
  1039. sub erase_data
  1040. {
  1041. my( $repoid ) = @_;
  1042.  
  1043. print <<END;
  1044. You are about to erase from $repoid:
  1045. - all database tables
  1046. - all eprint files
  1047. - the generated html pages
  1048. but NOT the configuration files.
  1049.  
  1050. END
  1051. my $sure = EPrints::Utils::get_input_confirm( "Are you sure you want this to happen" );
  1052. unless( $sure )
  1053. {
  1054. print "Aborting then.\n";
  1055. exit( 1 );
  1056. }
  1057.  
  1058. erase_eprint_files( $repoid );
  1059. drop_and_recreate_db( $repoid );
  1060. }
  1061.  
  1062. sub erase_eprints
  1063. {
  1064. my( $repoid ) = @_;
  1065.  
  1066. print <<END;
  1067. You are about to erase from $repoid:
  1068. - all eprints and documents data
  1069. - all eprint files
  1070. - all change history
  1071. - the document requests
  1072. - the access logs
  1073. - the generated html pages
  1074. but NOT the configuration files, user data or subject data.
  1075.  
  1076. END
  1077. my $sure = EPrints::Utils::get_input_confirm( "Are you sure you want this to happen" );
  1078. unless( $sure )
  1079. {
  1080. print "Aborting then.\n";
  1081. exit( 1 );
  1082. }
  1083.  
  1084. erase_eprint_files( $repoid );
  1085. foreach( "eprint", "history","access","request","document" )
  1086. {
  1087. reset_dataset( $repoid, $_ );
  1088. }
  1089. my $ok;
  1090. $ok = EPrints::Utils::get_input( $REGEXP_YESNO, "Do you want to build the static web pages?", "yes" );
  1091. if( $ok eq "yes" )
  1092. {
  1093. run_script( $repoid, "generate_static", "--verbose", $repoid );
  1094. }
  1095. }
  1096.  
  1097. sub reset_dataset
  1098. {
  1099. my( $repoid, $datasetid ) = @_;
  1100.  
  1101. my $session = new EPrints::Session( 1 , $repoid , $noise, 1 );
  1102. exit( 1 ) unless( defined $session );
  1103.  
  1104. my $db = $session->get_database;
  1105. my @tables = $db->get_tables;
  1106. print "Erasing dataset $datasetid\n" if( $noise >= 1 );
  1107. foreach my $table ( @tables )
  1108. {
  1109. next unless( $table =~ m/^$datasetid/ );
  1110. print "Erasing table $table\n" if( $noise >= 2 );
  1111. my $sql = "DROP TABLE ".$table;
  1112. $db->do( $sql );
  1113. }
  1114.  
  1115. print "Creating dataset $datasetid\n";
  1116. $db->create_dataset_tables(
  1117. $session->get_repository->get_dataset( $datasetid ) );
  1118.  
  1119. if( $datasetid ne "subject" )
  1120. {
  1121. print "Resetting counter ${datasetid}id\n";
  1122. $db->counter_reset( $datasetid."id" );
  1123. }
  1124.  
  1125. $session->terminate;
  1126. }
  1127.  
  1128. # not an option directly!
  1129. sub drop_and_recreate_db
  1130. {
  1131. my( $repoid ) = @_;
  1132.  
  1133. my $repo = EPrints::Repository->new( $repoid );
  1134. my $database = $repo->get_conf( "dbname" );
  1135. if( $noise>=1 ) { print "Connecting to mysql...\n"; }
  1136. my $dbh = DBI->connect(
  1137. EPrints::Database::build_connection_string(
  1138. dbname=>"mysql",
  1139. dbsock=>$repo->get_conf( "dbsock" ),
  1140. dbport=>$repo->get_conf( "dbport" ),
  1141. dbhost=>$repo->get_conf( "dbhost" ) ),
  1142. "root",
  1143. get_mysql_root_password() );
  1144.  
  1145. if( !defined $dbh )
  1146. {
  1147. print STDERR "\n\nFailed to connect to database. Aborting.\n\n";
  1148. exit( 1 );
  1149. }
  1150.  
  1151. if( $noise>=1 ) { print "Dropping database \"$database\"\n"; }
  1152. $dbh->do( "drop database $database" );
  1153. if( $noise>=1 ) { print "Re-creating database \"$database\"\n"; }
  1154. $dbh->do( "create database $database" );
  1155. $dbh->disconnect;
  1156.  
  1157. if( $noise>=1 ) { print "Done recreating database\n\n"; }
  1158.  
  1159. my $mktables = EPrints::Utils::get_input( $REGEXP_YESNO, "Create database tables?", "yes" );
  1160. if( $mktables eq "yes" )
  1161. {
  1162. create_tables( $repoid );
  1163. }
  1164. }
  1165.  
  1166. # not an option directly!
  1167. sub erase_eprint_files
  1168. {
  1169. my( $repoid ) = @_;
  1170.  
  1171. if( $noise>=1 ) { print "Erasing eprint files...\n"; }
  1172.  
  1173. my $repo = EPrints::Repository->new( $repoid );
  1174. my $documents_path = $repo->get_conf( "documents_path" );
  1175. my $htdocs_path = $repo->get_conf( "htdocs_path" );
  1176. # Get available directories
  1177. opendir DOCSTORE, $documents_path
  1178. or print STDERR "Can't open DOCSTORE\n";
  1179. my @doomeddirs;
  1180. foreach( readdir DOCSTORE )
  1181. {
  1182. next if m/^\.\.?$/; # skip . and ..
  1183. push @doomeddirs,
  1184. $documents_path."/".$_;
  1185. }
  1186. closedir DOCSTORE;
  1187. # Remove the contents of each of the directories.
  1188.  
  1189. push @doomeddirs, $htdocs_path;
  1190. foreach my $dir (@doomeddirs)
  1191. {
  1192. if( $noise>=2 ) { print "Removing stuff in: $dir\n"; }
  1193. my $rc = $repo->exec( "rmall", TARGET=>$dir );
  1194. print STDERR "Warning: Cleaning $dir didn't go smoothly\n" unless( $rc==0 );
  1195. }
  1196. if( $noise>=1 ) { print "...done erasing eprint files.\n"; }
  1197. }
  1198.  
  1199. sub erase_fulltext_index
  1200. {
  1201. my( $repoid ) = @_;
  1202.  
  1203. my $session = new EPrints::Session( 1 , $repoid , $noise );
  1204. exit( 1 ) unless( defined $session );
  1205.  
  1206. my $ds = $session->get_repository->get_dataset( "document" );
  1207. print "Stating to erase caches\n" if( $noise >= 1 );
  1208.  
  1209. $ds->map(
  1210. $session,
  1211. sub
  1212. {
  1213. my( $session , $dataset , $doc ) = @_;
  1214.  
  1215. if( $noise >= 2 )
  1216. {
  1217. print "Removing fulltext index for: ".$doc->get_id."\n";
  1218. }
  1219. my @files = ( $doc->words_file, $doc->indexcodes_file );
  1220. foreach my $file ( @files )
  1221. {
  1222. next unless( -e $file );
  1223. if( $noise >= 2 )
  1224. {
  1225. print "Erasing: $file\n";
  1226. }
  1227. unlink( $file );
  1228. }
  1229. } );
  1230.  
  1231. print "Done erasing\n" if( $noise >= 1 );
  1232. print "Queuing records for re-indexing\n" if( $noise >= 1 );
  1233.  
  1234. my $fn = sub {
  1235. my( $session, $dataset, $item ) = @_;
  1236. $session->get_database->index_queue(
  1237. 'eprint',
  1238. $item->get_id,
  1239. $EPrints::Utils::FULLTEXT );
  1240. if( $session->get_noise() >= 2 )
  1241. {
  1242. print STDERR "Queued item: ".$dataset->id()."/".$item->get_id()."\n";
  1243. }
  1244. };
  1245.  
  1246. my $ep_ds = $session->get_repository->get_dataset( "eprint" );
  1247. $ep_ds->map( $session, $fn );
  1248.  
  1249. print "Done queuing\n" if( $noise >= 1 );
  1250.  
  1251. $session->terminate;
  1252. }
  1253.  
  1254. sub test
  1255. {
  1256. my( $repoid ) = @_;
  1257.  
  1258. my $session = new EPrints::Session( 1 , $repoid , $noise );
  1259. exit( 1 ) unless( defined $session );
  1260. $session->terminate;
  1261.  
  1262. print "Everything seems OK.\n";
  1263. }
  1264.  
  1265. sub rehash
  1266. {
  1267. my( $repoid ) = @_;
  1268.  
  1269. my $session = new EPrints::Session( 1 , $repoid , $noise );
  1270. exit( 1 ) unless( defined $session );
  1271.  
  1272. my $docid = $ARGV[2];
  1273. if( defined $docid )
  1274. {
  1275. my $doc = EPrints::DataObj::Document->new( $session, $docid );
  1276. if( !defined $doc )
  1277. {
  1278. $session->get_repository->log(
  1279. "Document #$docid not found. Can't rehash." );
  1280. }
  1281. else
  1282. {
  1283. $doc->rehash;
  1284. print "Rehashed document #$docid\n" if( $noise > 0);
  1285. }
  1286. }
  1287. else
  1288. {
  1289. print "Rehashing documents\n" if( $noise > 0);
  1290. my $dataset = $session->get_repository->get_dataset( "document" );
  1291. my $info = { count=>0 };
  1292. $dataset->map(
  1293. $session ,
  1294. sub
  1295. {
  1296. my( $session, $dataset, $doc, $info ) = @_;
  1297. $doc->rehash;
  1298. if( $noise > 1 )
  1299. {
  1300. print "Rehashed ".$doc->get_value( "docid" )."\n";
  1301. }
  1302. $info->{count}++;
  1303. },
  1304. $info );
  1305.  
  1306. if( $noise > 0)
  1307. {
  1308. print "Done rehashing ".$info->{count}." documents\n";
  1309. }
  1310. }
  1311.  
  1312. $session->terminate;
  1313. }
  1314.  
  1315. ###################################
  1316. #
  1317. # DATASET related utilities
  1318. #
  1319. ###################################
  1320.  
  1321.  
  1322. sub recommit
  1323. {
  1324. my( $repoid, $datasetid ) = @_;
  1325.  
  1326. my $session = new EPrints::Session( 1 , $repoid , $noise );
  1327. exit( 1 ) unless( defined $session );
  1328.  
  1329. my $dataset = $session->get_repository->get_dataset( $datasetid );
  1330. if( !defined $dataset )
  1331. {
  1332. print "Exiting due to unknown dataset.\n" if( $noise >= 1 );
  1333. $session->terminate();
  1334. exit( 1 );
  1335. }
  1336.  
  1337. if( $noise > 0 )
  1338. {
  1339. print "\n";
  1340. print "You are about to recommit \"$datasetid\" in the $repoid repository.\n";
  1341. print "This can take some time.\n\n";
  1342. print "Number of records in set: ".$dataset->count( $session )."\n";
  1343. }
  1344.  
  1345. my $sure = EPrints::Utils::get_input_confirm( "Continue" );
  1346. unless( $sure )
  1347. {
  1348. print "Aborting then.\n\n";
  1349. $session->terminate();
  1350. exit( 1 );
  1351. }
  1352.  
  1353.  
  1354. my $fn = sub {
  1355. my( $session, $dataset, $item ) = @_;
  1356. if( $session->get_noise() >= 2 )
  1357. {
  1358. print STDERR "Committing item: ".$dataset->id()."/".$item->get_id()."\n";
  1359. }
  1360. $item->commit();
  1361. };
  1362.  
  1363. $dataset->map( $session, $fn );
  1364.  
  1365. print "All items in \"$datasetid\" have been re-commited.\n" if( $noise >= 1 );
  1366.  
  1367. $session->terminate;
  1368. }
  1369.  
  1370. sub reindex
  1371. {
  1372. my( $repoid, $datasetid ) = @_;
  1373.  
  1374. my $session = new EPrints::Session( 1 , $repoid , $noise );
  1375. exit( 1 ) unless( defined $session );
  1376.  
  1377. my $dataset = $session->get_repository->get_dataset( $datasetid );
  1378. if( !defined $dataset )
  1379. {
  1380. print "Exiting due to unknown dataset.\n" if( $noise >= 1 );
  1381. $session->terminate();
  1382. exit( 1 );
  1383. }
  1384.  
  1385. my $fn = sub {
  1386. my( $session, $dataset, $item ) = @_;
  1387. foreach my $field ( $dataset->get_fields() )
  1388. {
  1389. next unless( $field->get_property( "text_index" ) );
  1390. $session->get_database->index_queue(
  1391. $dataset->id,
  1392. $item->get_id,
  1393. $field->get_name );
  1394. }
  1395. if( $dataset->confid eq "eprint" )
  1396. {
  1397. $session->get_database->index_queue(
  1398. $dataset->id,
  1399. $item->get_id,
  1400. $EPrints::Utils::FULLTEXT );
  1401. }
  1402. if( $session->get_noise() >= 2 )
  1403. {
  1404. print STDERR "Queued item: ".$dataset->id()."/".$item->get_id()."\n";
  1405. }
  1406. };
  1407.  
  1408. $dataset->map( $session, $fn );
  1409.  
  1410. $session->terminate;
  1411. }
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420. ####################################################################
  1421. #
  1422. # UPGRADE CODE
  1423. #
  1424. ####################################################################
  1425.  
  1426. sub upgrade
  1427. {
  1428. my( $repoid ) = @_;
  1429.  
  1430. my $session = new EPrints::Session( 1 , $repoid , $noise, 1 );
  1431. exit( 1 ) unless( defined $session );
  1432. my $db = $session->get_db();
  1433. my $dbversion = $db->get_version();
  1434. if( $dbversion eq "3.0" )
  1435. {
  1436. upgrade_3_0_to_3_0_1( $repoid, $db );
  1437. $dbversion="3.0.1";
  1438. $db->set_version( $dbversion );
  1439. }
  1440. if( $dbversion eq "3.0.1" )
  1441. {
  1442. upgrade_3_0_1_to_3_0_2( $repoid, $db );
  1443. $dbversion="3.0.2";
  1444. $db->set_version( $dbversion );
  1445. }
  1446. if( $dbversion eq "3.0.2" )
  1447. {
  1448. upgrade_3_0_2_to_3_0_3( $repoid, $db );
  1449. $dbversion="3.0.3";
  1450. $db->set_version( $dbversion );
  1451. }
  1452. if( $dbversion eq "3.0.3" )
  1453. {
  1454. upgrade_3_0_3_to_3_0_4( $repoid, $db );
  1455. $dbversion="3.0.4";
  1456. $db->set_version( $dbversion );
  1457. }
  1458. if( $dbversion eq "3.0.4" )
  1459. {
  1460. upgrade_3_0_4_to_3_0_5( $repoid, $db );
  1461. $dbversion="3.0.5";
  1462. $db->set_version( $dbversion );
  1463. }
  1464. if( $dbversion eq "3.0.5" )
  1465. {
  1466. upgrade_3_0_5_to_3_0_6( $repoid, $db );
  1467. $dbversion="3.0.6";
  1468. $db->set_version( $dbversion );
  1469. }
  1470.  
  1471. print $dbversion."\n";
  1472. $session->terminate;
  1473. }
  1474.  
  1475. sub upgrade_3_0_to_3_0_1
  1476. {
  1477. my( $repoid, $db ) = @_;
  1478.  
  1479. my $rootdbh = root_dbh( $repoid );
  1480. if( !defined $rootdbh ) { die "Could not open database as root"; }
  1481. my @tables = $db->get_tables;
  1482. my $sql;
  1483.  
  1484. $sql = "ALTER TABLE saved_search ADD public set('TRUE','FALSE') default 'FALSE' AFTER mailempty";
  1485. $rootdbh->do( $sql );
  1486.  
  1487. $sql = "CREATE TABLE user_items_fields ( userid INT NOT NULL, pos INT, items_fields VARCHAR(255) default NULL, KEY userid (userid), KEY pos (pos) ) ";
  1488. $rootdbh->do( $sql );
  1489.  
  1490. $sql = "CREATE TABLE user_review_fields ( userid INT NOT NULL, pos INT, review_fields VARCHAR(255) default NULL, KEY userid (userid), KEY pos (pos) ) ";
  1491. $rootdbh->do( $sql );
  1492.  
  1493. foreach my $table ( @tables )
  1494. {
  1495. if( $table =~ m/^saved_search__ordervalues_.*$/ )
  1496. {
  1497. $sql = "ALTER TABLE $table ADD public TEXT AFTER mailempty",
  1498. $rootdbh->do( $sql );
  1499. }
  1500. if( $table =~ m/^user__ordervalues_.*$/ )
  1501. {
  1502. $sql = "ALTER TABLE $table ADD items_fields TEXT AFTER mailempty",
  1503. $rootdbh->do( $sql );
  1504. $sql = "ALTER TABLE $table ADD review_fields TEXT AFTER items_fields",
  1505. $rootdbh->do( $sql );
  1506. }
  1507. }
  1508.  
  1509. $rootdbh->disconnect;
  1510. }
  1511.  
  1512.  
  1513.  
  1514. sub upgrade_3_0_1_to_3_0_2
  1515. {
  1516. my( $repoid, $db ) = @_;
  1517.  
  1518. my $rootdbh = root_dbh( $repoid );
  1519. if( !defined $rootdbh ) { die "Could not open database as root"; }
  1520. my @tables = $db->get_tables;
  1521. my $sql;
  1522.  
  1523. $sql = "ALTER TABLE saved_search ADD name VARCHAR(255) NOT NULL AFTER pos";
  1524. $rootdbh->do( $sql );
  1525.  
  1526. $sql = "ALTER TABLE cachemap ADD userid INTEGER AFTER lastused";
  1527. $rootdbh->do( $sql );
  1528.  
  1529. foreach my $table ( @tables )
  1530. {
  1531. if( $table =~ m/^saved_search__ordervalues_.*$/ )
  1532. {
  1533. $sql = "ALTER TABLE $table ADD name TEXT AFTER pos",
  1534. $rootdbh->do( $sql );
  1535. }
  1536. }
  1537.  
  1538. $rootdbh->disconnect;
  1539. }
  1540.  
  1541. sub upgrade_3_0_2_to_3_0_3
  1542. {
  1543. my( $repoid, $db ) = @_;
  1544.  
  1545. my $rootdbh = root_dbh( $repoid );
  1546. if( !defined $rootdbh ) { die "Could not open database as root"; }
  1547. my @tables = $db->get_tables;
  1548. my $sql;
  1549.  
  1550. $sql = "ALTER TABLE eprint DROP fileinfo";
  1551. $rootdbh->do( $sql );
  1552.  
  1553. $sql = "ALTER TABLE eprint ADD fileinfo TEXT AFTER contact_email";
  1554. $rootdbh->do( $sql );
  1555.  
  1556. foreach my $table ( @tables )
  1557. {
  1558. if( $table =~ m/^eprint__ordervalues_.*$/ )
  1559. {
  1560. $sql = "ALTER TABLE $table DROP fileinfo";
  1561. $rootdbh->do( $sql );
  1562. $sql = "ALTER TABLE $table ADD fileinfo TEXT AFTER contact_email",
  1563. $rootdbh->do( $sql );
  1564. }
  1565. }
  1566.  
  1567. $rootdbh->disconnect;
  1568. }
  1569.  
  1570.  
  1571.  
  1572. sub upgrade_3_0_3_to_3_0_4
  1573. {
  1574. my( $repoid, $db ) = @_;
  1575.  
  1576. my $rootdbh = root_dbh( $repoid );
  1577. if( !defined $rootdbh ) { die "Could not open database as root"; }
  1578. my @tables = $db->get_tables;
  1579. my $sql;
  1580.  
  1581. $sql = "ALTER TABLE eprint ADD latitude FLOAT AFTER fileinfo";
  1582. $rootdbh->do( $sql );
  1583. $sql = "ALTER TABLE eprint ADD longitude FLOAT AFTER latitude";
  1584. $rootdbh->do( $sql );
  1585. $sql = "ALTER TABLE user ADD latitude FLOAT AFTER mailempty";
  1586. $rootdbh->do( $sql );
  1587. $sql = "ALTER TABLE user ADD longitude FLOAT AFTER latitude";
  1588. $rootdbh->do( $sql );
  1589.  
  1590. foreach my $table ( @tables )
  1591. {
  1592. if( $table =~ m/^eprint__ordervalues_.*$/ )
  1593. {
  1594. $sql = "ALTER TABLE $table ADD latitude TEXT AFTER fileinfo";
  1595. $rootdbh->do( $sql );
  1596. $sql = "ALTER TABLE $table ADD longitude TEXT AFTER latitude";
  1597. $rootdbh->do( $sql );
  1598. }
  1599. if( $table =~ m/^user__ordervalues_.*$/ )
  1600. {
  1601. $sql = "ALTER TABLE $table ADD latitude TEXT AFTER review_fields";
  1602. $rootdbh->do( $sql );
  1603. $sql = "ALTER TABLE $table ADD longitude TEXT AFTER latitude";
  1604. $rootdbh->do( $sql );
  1605. }
  1606. }
  1607.  
  1608. $rootdbh->disconnect;
  1609. }
  1610.  
  1611.  
  1612. sub upgrade_3_0_4_to_3_0_5
  1613. {
  1614. my( $repoid, $db ) = @_;
  1615.  
  1616. $db->_create_messages_table;
  1617. }
  1618.  
  1619.  
  1620.  
  1621. sub upgrade_3_0_5_to_3_0_6
  1622. {
  1623. my( $repoid, $db ) = @_;
  1624.  
  1625. reset_dataset( $repoid, 'subject' );
  1626. }
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.