'Calais Configuration', 'description' => 'Configurations for Calais', 'page callback' => 'drupal_get_form', 'page arguments' => array('calais_api_admin_settings'), 'access arguments' => array('administer calais api'), ); $items['admin/settings/calais/calais-api'] = array( 'title' => 'Calais API Settings', 'description' => 'Configurations for Calais API', 'type' => MENU_DEFAULT_LOCAL_TASK, ); return $items; } /** * Build the admin settings form. */ function calais_api_admin_settings() { $form = array(); $calais_url = array( '!calaisurl' => l(t('Need One'), 'http://www.opencalais.com/user/register') ); $form['calais_api_key'] = array( '#type' => 'textfield', '#title' => t('Calais API Key'), '#default_value' => variable_get('calais_api_key', NULL), '#size' => 60, '#description' => t('Required to utilize the Calais service. !calaisurl?', $calais_url), ); $form['calais_api_server'] = array( '#type' => 'textfield', '#title' => t('Calais Server'), '#default_value' => variable_get('calais_api_server', 'api1.opencalais.com'), '#size' => 60, '#description' => t('The domain name for the server to use. Typically you will not have to change this unless you want to test beta functionality.'), ); $form['calais_api_allow_searching'] = array( '#type' => 'checkbox', '#title' => t('Allow Calais Searching'), '#default_value' => variable_get('calais_api_allow_searching', TRUE), '#description' => t('Indicates whether future searches can be performed on the extracted metadata by Calais'), ); $form['calais_api_allow_distribution'] = array( '#type' => 'checkbox', '#title' => t('Allow Calais Distribution'), '#default_value' => variable_get('calais_api_allow_distribution', TRUE), '#description' => t('Indicates whether the extracted metadata can be distributed by Calais'), ); $form = system_settings_form($form); return $form; } /** * Analyze the content via Calais. * * @param $content The content to ship off to Calais for analysis * @param $parameters Array of Calais parameters for overriding defaults. * @see http://www.opencalais.com/APIcalls#inputparameters * * @return The analyzed content */ function calais_api_analyze($content, $parameters = array()) { $calais = new Calais($parameters); return $calais->analyze($content); } /** * Analyze the content via the Calais XML interface. * * @param $title The title of the content * @param $body The entire body of the content * @param $date The date of the content (can be created or updated date) * This date is used to base calculations of words like "tomorrow" or "yesterday" * @param $parameters Array of Calais parameters for overriding defaults. * @see http://www.opencalais.com/APIcalls#inputparameters * * @return The analyzed content */ function calais_api_analyze_xml($title, $body, $date, $parameters = array()) { $calais = new Calais($parameters); return $calais->analyzeXML($title, $body, $date); } /** * Takes a CamelCase word and adds spaces to make it Camel Case * * @return an formated string */ function calais_api_make_readable($camel_case) { return preg_replace('/(.*?[a-z]{1})([A-Z]{1}.*?)/', '${1} ${2}', $camel_case); } /** * Declare the Calais namespace to the RDF API module (in case we use it) */ function calais_api_rdf_namespaces() { return array( 'c' => 'http://s.opencalais.com/1/pred/', 'sys' => 'http://s.opencalais.com/1/type/sys/', 'lid' => 'http://s.opencalais.com/1/type/lid/', 'cat' => 'http://s.opencalais.com/1/type/cat/', 'resolved' => 'http://s.opencalais.com/1/type/er/', 'cgeo' => 'http://s.opencalais.com/1/type/er/Geo/', 'eventfact' => 'http://s.opencalais.com/1/type/em/r/', 'entity' => 'http://s.opencalais.com/1/type/em/e/', 'cld' => "http://s.opencalais.com/1/linkeddata/pred/", ); } /** * Get a list of the entities that Calais API defines: * http://opencalais.mashery.com/page/calaissemanticmetadata * * TODO: When Calais updates to have a static list at a URL or via API call, return that instead. * * @return flat array listing of Calais entities */ function calais_api_get_all_entities() { return array( 'Anniversary', 'City', 'Company', 'Continent', 'Country', 'Currency', 'EmailAddress', 'EntertainmentAwardEvent', 'Facility', 'FaxNumber', 'Holiday', 'IndustryTerm', 'MarketIndex', 'MedicalCondition', 'MedicalTreatment', 'Movie', 'MusicAlbum', 'MusicGroup', 'NaturalDisaster', 'NaturalFeature', 'OperatingSystem', 'Organization', 'Person', 'PhoneNumber', 'Product', 'ProgrammingLanguage', 'ProvinceOrState', 'PublishedMedium', 'RadioProgram', 'RadioStation', 'Region', 'SportsEvent', 'SportsGame', 'SportsLeague', 'Technology', 'TVShow', 'TVStation', 'URL', 'CalaisDocumentCategory', // Special reserved vocab for Calais Document Categorization 'EventsFacts', // Special reserved vocab for Events & Facts ); } ////////////////////////////////////////////////////////////////////////////// // ARC2 interoperability // Copied from rdf.api.inc to fix bug http://drupal.org/node/348758 // When that is resolved and in a release, remove this hackitude. function _calais_api_deconstruct_arc2_triple($triple) { $subject = _calais_api_deconstruct_arc2_value($triple, 's'); $predicate = _calais_api_deconstruct_arc2_value($triple, 'p'); $object = _calais_api_deconstruct_arc2_value($triple, 'o'); return array($subject, $predicate, $object); } function _calais_api_deconstruct_arc2_value($triple, $name) { switch ($name == 'p' ? 'iri' : $triple[$name .'_type']) { case 'uri': // THIS IS THE FIX case 'iri': return rdf_uri($triple[$name]); case 'bnode': return rdf_bnode($triple[$name]); case 'literal': case 'literal1': case 'literal2': case 'literal_long1': case 'literal_long2': return rdf_literal($triple[$name], $triple[$name .'_lang'], $triple[$name .'_datatype']); case 'var': return rdf_var($triple[$name]); } } /** * This function exists as long as http://drupal.org/node/350485 does. * * This makes sure all of the extra Calais namespaces are in there. The original * RDF from Calais did not use namespaces as everything was an rdf:Description with * a type, but the RDF module serialization moves that type to the root tag level * and as such additional namespaces are needed. */ function _calais_api_fix_rdf_namespaces($xml) { $matches = array(); preg_match('//i', $xml, $matches); $ns = explode(' ', $matches[1]); $calaisns = calais_api_rdf_namespaces(); // Remove already specified namespaces from our list of those to add so we dont duplicate foreach($ns as $index => $namespace) { list($key, $value) = explode('=', $namespace); // get the attribute name list($xmlns, $prefix) = explode(':', $key); // strip the xmlns: off the front unset($calaisns[$prefix]); } // Add the namespaces that are left foreach ($calaisns as $prefix => $uri) { $ns[] = "xmlns:$prefix=\"$uri\""; } $namespaces = implode(' ', $ns); $rdf = preg_replace('//i', "", $xml, 1); return $rdf; }