-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezproxy_stanza.install
147 lines (127 loc) · 4.55 KB
/
ezproxy_stanza.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
use Drupal\node\Entity\Node;
use GitWrapper\GitWrapper;
use Drupal\ezproxy_stanza\Git\PrivateRepo;
/**
* Implements hook_requirements().
*/
function ezproxy_stanza_requirements($phase) {
$requirements = [];
if ($phase == 'install') {
// make sure git is installed
if (empty(shell_exec("which git 2>/dev/null"))) {
$requirements['git'] = [
'description' => t("Git version control software needs to be installed on your server."),
'severity' => REQUIREMENT_ERROR,
];
}
$path = 'private://ezproxy_stanza';
$priv_path = $path . DIRECTORY_SEPARATOR . 'priv';
if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY) ||
!file_prepare_directory($priv_path, FILE_CREATE_DIRECTORY)) {
$requirements['repo_creation'] = [
'description' => t("Failed to create the necessary directory. Please check that your private filesystem is configured correctly."),
'severity' => REQUIREMENT_ERROR,
];
}
if (!class_exists('\GitWrapper\GitWrapper')) {
$requirements['vendor'] = [
'description' => t("EZProxy Stanza requires cpliakas/git-wrapper."),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function ezproxy_stanza_install() {
$git = new GitWrapper();
$remote = 'https://github.com/kent-state-university-libraries/oclc-ezproxy-database-stanzas';
$path = \Drupal::service('file_system')->realpath('private://ezproxy_stanza');
// clone the OCLC stanzas repo
$git->cloneRepository($remote, $path . DIRECTORY_SEPARATOR . 'public');
// create a local/private repo to store site's EZProxy config.txt
$git->init($path . DIRECTORY_SEPARATOR . 'priv');
// get the path to the OCLC repo's directory where the stanzas are stored
$stanzas_repo_path = $path . DIRECTORY_SEPARATOR . 'public'. DIRECTORY_SEPARATOR .'stanzas';
$stanza_dirs = scandir($stanzas_repo_path);
foreach ($stanza_dirs as $stanza_dir) {
if ($stanza_dir === '.' || $stanza_dir === '..') {
continue;
}
$oclc_stanza_dir = $stanzas_repo_path . DIRECTORY_SEPARATOR . $stanza_dir;
$stanza_file = $oclc_stanza_dir . DIRECTORY_SEPARATOR . 'stanza.txt';
$stanza = file_get_contents($stanza_file);
$lines = explode("\n", $stanza);
$title = FALSE;
foreach ($lines as $line) {
$spaces = explode(' ', trim($line));
$start = array_shift($spaces);
$start = strtolower($start);
if ($start === 't' || $start === 'title'
|| substr($start, 0, 1) === 't' || substr($start, 0, 5) === 'title') {
$title = implode(' ', $spaces);
if (substr($title, 0, 6) === '-hide ') {
$title = substr($title, 7);
}
break;
}
}
if (!$title) {
$title = ucfirst($stanza_dir);
}
$node = Node::create([
'title' => $title,
'type' => 'resource',
'status' => NODE_NOT_PUBLISHED,
]);
$node->set('field_ezproxy_url', 'https://www.oclc.org/support/services/ezproxy/documentation/db/'.$stanza_dir.'.en.html');
$node->set('field_ezproxy_stanza', $stanza);
$node->set('field_ezproxy_review', file_exists($oclc_stanza_dir . DIRECTORY_SEPARATOR . 'README.md'));
$node->save();
}
$private_repo = new PrivateRepo();
// create a .gitignore file
$gitignore = "audit/*\n";
$gitignore .= "cookies/*\n";
$gitignore .= "docs/*\n";
$gitignore .= "ezproxy\n";
$gitignore .= "ezproxy.hst\n";
$gitignore .= "ezproxy.ipc\n";
$gitignore .= "ezproxy.key\n";
$gitignore .= "ezproxy.log*\n";
$gitignore .= "ezproxy.rnd\n";
$gitignore .= "GeoLiteCity.dat*\n";
$gitignore .= "license.txt\n";
$gitignore .= "logrotate.sh\n";
$gitignore .= "messages.txt\n";
$gitignore .= "mimetype\n";
$gitignore .= "ServiceValidateURL\n";
$gitignore .= "ssl/*\n";
$gitignore .= "wskey.key\n";
$private_repo->setFileContents('.gitignore', $gitignore, 1);
// add a very basic config.txt
$config_txt = "# EZProxy Settings\n";
$config_txt .= "### DO NOT WRITE BELOW THIS LINE. IT WILL BE OVERWRITTEN.\n";
$private_repo->setFileContents('config.txt', $config_txt, 1);
// add the files
$private_repo->add('.');
// make the initial commit
$private_repo->commit('.', 'Initial commit.');
}
/**
* Implements hook_uninstall().
*/
function ezproxy_stanza_uninstall() {
file_unmanaged_delete_recursive('private://ezproxy_stanza');
// delete all nodes of type "resource"
$nids = \Drupal::entityQuery('node')
->condition('type', 'resource')
->execute();
foreach ($nids as $nid) {
$node = Node::load($nid);
$node->delete();
}
}