1: <?php
2:
3: namespace cli_db;
4:
5: require_once SHARED . '/classes/CLI_Command.php';
6:
7: class Publication extends AbstractTable {
8:
9: 10: 11:
12: public static function getKeys() {
13: return array(
14: 'id' => array(
15: 'colname' => 'PubId',
16: 'actions' => array(
17: 'details' => 'required',
18: 'delete' => 'required',
19: ),
20: 'description' => 'publication id'
21: ),
22: 'title' => array('colname' => 'Title'),
23: 'volumetitle' => array('colname' => 'Volumetitle'),
24: 'miniref' => array('colname' => 'Miniref'),
25: 'bibsonomy_internal_link' => array(
26: 'short_name' => '-b',
27: 'actions' => array(
28: 'link_bibsonomy' => 'required',
29: ),
30: 'description' => 'bibsonomy "internal link", you can find this on the publication post page. looks like this: [[publication/<resource>/<username>]]'
31: ),
32: 'bibsonomy_api_key' => array(
33: 'short_name' => '-k',
34: 'actions' => array(
35: 'link_bibsonomy' => 'required',
36: ),
37: 'description' => 'you can find your api key at http://www.bibsonomy.org/settings?selTab=1'
38: ),
39: 'bibsonomy_username' => array(
40: 'short_name' => '-u',
41: 'actions' => array(
42: 'link_bibsonomy' => 'required',
43: ),
44: 'description' => 'bibsonomy user name'
45: ),
46: 'feature_id' => array(
47: 'short_name' => '-f',
48: 'actions' => array(
49: 'link_bibsonomy' => 'required',
50: ),
51: 'description' => 'feature id.'
52: ),
53: 'unlink_feature_id' => array(
54: 'short_name' => '-f',
55: 'actions' => array(
56: 'delete' => 'optional',
57: ),
58: 'description' => 'feature id. if you specify this option, the publication will only be unlinked from the given feature. if the given feature was the last feature, the publication will be deleted nonetheless.'
59: )
60: );
61: }
62:
63: 64: 65:
66: public static function CLI_commandDescription() {
67: return 'Add or remove publications.';
68: }
69:
70: 71: 72:
73: public static function CLI_commandName() {
74: return 'publication';
75: }
76:
77: 78: 79:
80: public static function CLI_longHelp() {
81:
82: }
83:
84: 85: 86:
87: public static function getSubCommands() {
88: return array('link_bibsonomy', 'delete', 'details', 'list');
89: }
90:
91: 92: 93:
94: public static function getPropelClass() {
95: return '\\cli_db\\propel\\Pub';
96: }
97:
98: 99: 100: 101: 102: 103: 104:
105: public static function getPropelPubFromBibsonomy($bibsonomy_link, $username, $api_key) {
106: $matches = null;
107: if (!preg_match('{^\[\[(?<type>.*)/(?<resource>.*)/(?<user>.*)\]\]$}', $bibsonomy_link, $matches)) {
108: trigger_error('wrong format for option --bibsonomy_internal_link', E_USER_ERROR);
109: }
110:
111: $url = sprintf('http://www.bibsonomy.org/api/posts?resource=%s&resourcetype=bibtex', $matches['resource']);
112:
113: $curl = curl_init();
114: curl_setopt($curl, CURLOPT_URL, $url);
115: curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
116: curl_setopt($curl, CURLOPT_USERPWD, sprintf('%s:%s', $username, $api_key));
117: curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
118:
119: $xmlStr = curl_exec($curl);
120: curl_close($curl);
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: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166:
167:
168:
169: $bibsonomy = new \SimpleXMLElement($xmlStr);
170: if ($bibsonomy['stat'] == 'fail') {
171: trigger_error($bibsonomy->error, E_USER_ERROR);
172: }
173:
174:
175: if (count($bibsonomy->posts->post) != 1) {
176: trigger_error(sprintf('Bibsonomy post %s not found!', $bibsonomy_link), E_USER_ERROR);
177: }
178: $bibtex = $bibsonomy->posts->post->bibtex;
179:
180: $uniquename = sprintf('bibsonomy_%s', $matches['resource']);
181:
182: $pubq = new propel\PubQuery();
183: $pub = $pubq->findOneByUniquename($uniquename);
184: if ($pub == null) {
185: $pub = new propel\Pub();
186: $pub->setTitle($bibtex['title']);
187: $pub->setUniquename($uniquename);
188: $pub->setVolumeTitle($bibtex['journal']);
189: $pub->setVolume($bibtex['volume']);
190: $pub->setSeriesName($bibtex['series']);
191: $pub->setIssue($bibtex['number']);
192: $pub->setPyear($bibtex['year']);
193: $pub->setPages($bibtex['pages']);
194: $pub->setMiniref(sprintf('http://www.bibsonomy.org/bibtex/%s/%s', $bibtex['intrahash'], $matches['user']));
195: $pub->setPublisher($bibtex['publisher']);
196: $typequery = new propel\CvtermQuery();
197: $type = $typequery->findOneByName($bibtex['entrytype']);
198: if ($type != null) {
199: $pub->setTypeId($type->getPrimaryKey());
200: } else {
201: trigger_error(sprintf('type %s not found in cvterm, proceeding without type', $bibtex['entrytype']), E_USER_WARNING);
202: }
203:
204: $authors = explode(' and ', $bibtex['author']);
205: $i = 0;
206: foreach ($authors as $author) {
207: $authnames = explode(',', $author, 2);
208: $surname = $authnames[0];
209: $givennames = isset($authnames[1]) ? $authnames[1] : '';
210:
211: $pubauthor = new propel\Pubauthor();
212: $pubauthor->setGivennames($givennames);
213: $pubauthor->setSurname($surname);
214: $pubauthor->setRank($i++);
215:
216: $pub->addPubauthor($pubauthor);
217: }
218: }
219: return $pub;
220: }
221:
222: 223: 224: 225: 226:
227: public static function command_link_bibsonomy($options, $keys) {
228: $pub = self::getPropelPubFromBibsonomy($options['bibsonomy_internal_link'], $options['bibsonomy_username'], $options['bibsonomy_api_key']);
229:
230: $fq = new propel\FeatureQuery();
231: $feature = $fq->findOneByFeatureId($options['feature_id']);
232:
233: $feature_pub = new propel\FeaturePub();
234: $feature_pub->setFeature($feature);
235:
236: $pub->addFeaturePub($feature_pub);
237:
238: $pub->save();
239:
240: printf("successfully added Pub '%s' to Feature '%s'\n", $pub->getTitle(), $feature->getUniquename());
241: }
242:
243: 244: 245: 246: 247: 248:
249: protected static function command_delete($options, $keys) {
250: $pubq = new propel\PubQuery();
251: $pub = $pubq->findOneByPubId($options['id']);
252: if ($pub == null) {
253: trigger_error(sprintf("No Publication found for ID %d. Exiting.\n", $options['id']), E_USER_ERROR);
254: }
255: $featurepubs = $pub->getFeaturePubs();
256: if (isset($options['unlink_feature_id']) && count($featurepubs) > 1) {
257: foreach ($featurepubs as $featurepub) {
258: if ($featurepub->getFeatureId() == $options['unlink_feature_id'])
259: break;
260: }
261: if ($featurepub->getFeatureId() != $options['unlink_feature_id']) {
262: trigger_error(printf("No Link from Publication %d to Feature %d found. Exiting.\n", $options['id'], $options['unlink_feature_id']), E_USER_ERROR);
263: }
264: if (!self::command_delete_confirm($options, sprintf("Delete Link from Publication %d to Feature %d.\n", $options['id'], $options['unlink_feature_id']))) {
265: return;
266: }
267: $featurepub->delete();
268: printf("Row successfully deleted.\n");
269: return;
270: }
271: if (!self::command_delete_confirm($options, sprintf("This Publication is linked to %d Features.\n", count($featurepubs)))) {
272: return;
273: }
274: $pub->delete();
275: printf("Row successfully deleted.\n");
276: }
277:
278: }
279:
280: ?>
281: