dmBridge PHP API
DMAPIVocabularyController.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00015 class DMAPIVocabularyController extends DMAbstractController {
00016 
00020    public function frequency($alias_fields) {
00021       $response = new DMHTTPResponse();
00022       $rep = DMHTTPResponseFactory::getRepresentation();
00023       $trans = DMHTTPResponseFactory::getTransformer();
00024       $response->setRepresentation($rep);
00025 
00026       $field_nicks = explode(",", $alias_fields);
00027       $alias = "/" . trim(array_shift($field_nicks), "/");
00028       $alias = DMCollection::getSanitizedAlias($alias);
00029       $collection = DMCollectionFactory::getCollection($alias);
00030 
00031       $fields = array();
00032       foreach ($field_nicks as $f) {
00033          $f = substr(DMString::paranoid($f), 0, 10);
00034          if (!$collection->getField($f)) {
00035             $response->setStatusCode(404);
00036             $rep->setBody(sprintf(
00037                   "Field %s not available in collection %s.",
00038                   $f, $alias));
00039             $response->send();
00040          }
00041          $fields[] = new DMDCElement($f);
00042       }
00043 
00044       $terms = array();
00045       $limit = 1024;
00046 
00047       // grab the first page of results
00048       $results = dmQuery(array($alias), $terms, $field_nicks,
00049          null, $limit, 1, $total, 1);
00050 
00051       $vocab = array();
00052       // loop through each page of results; this is horribly expensive but
00053       // there is no alternative
00054       for ($i = $limit; $i < $total + $limit; $i = $i + $limit) {
00055          if ($i > $limit) { // if 2nd iteration
00056             $results = dmQuery(array($alias), $terms, $field_nicks,
00057             null, $limit, $i - $maxresults, $total, 1);
00058          }
00059          // compile all unique terms in the $vocab array
00060          foreach ($fields as $f) {
00061             foreach ($results as $r) {
00062                if (!in_array($f->getNick(), array_keys($r))) {
00063                   continue;
00064                }
00065                if (!$r[$f->getNick()]) {
00066                   continue; // ignore empty fields
00067                }
00068                // split up semicolon-separated terms
00069                $tmp = explode("; ", $r[$f->getNick()]);
00070                foreach ($tmp as &$t) {
00071                   $t = rtrim(trim($t), ".,;");
00072                   if (!$t) {
00073                      unset($t);
00074                   }
00075                }
00076                $vocab = array_merge($vocab, $tmp);
00077             }
00078          }
00079       }
00080 
00081       $counts = array_count_values($vocab);
00082       arsort($counts, SORT_NUMERIC);
00083 
00084       $rep->setBody($trans->transformVocabularyFrequencies($counts));
00085       $response->send();
00086       die;
00087    }
00088 
00097    public function suggest() {
00098       $response = new DMHTTPResponse();
00099       $rep = DMHTTPResponseFactory::getRepresentation();
00100       $trans = DMHTTPResponseFactory::getTransformer();
00101       $response->setRepresentation($rep);
00102 
00103       $uri = DMHTTPRequest::getCurrent()->getURI();
00104       $alias = $term = null;
00105       $nicks = array();
00106 
00107       if ($uri->getQueryValue("CISOROOT")
00108             && $uri->getQueryValue("CISOBOX1")) {
00109          $alias = DMCollection::getSanitizedAlias(
00110                $uri->getQueryValue("CISOROOT"));
00111          $term = DMString::paranoid(
00112             substr($uri->getQueryValue("CISOBOX1"), 0, 100),
00113                array('-', ':', ',', '.'));
00114          foreach ($uri->getQuery() as $kv) {
00115             if (strpos($kv['key'], "CISOFIELD") !== false) {
00116                $nicks[] = $kv['value'];
00117             }
00118          }
00119       }
00120 
00121       if (!count($nicks) || !$alias || !$term) {
00122          $response->setStatusCode(400);
00123          $rep->setBody("Invalid parameters.");
00124          $response->send();
00125          die;
00126       }
00127 
00128       $col = DMCollectionFactory::getCollection($alias);
00129 
00130       $fields = array();
00131       foreach ($nicks as $n) {
00132          $f = new DMDCElement(DMString::paranoid(substr($n, 0, 6)));
00133          $f->setCollection($col);
00134          $fields[] = $f;
00135       }
00136 
00137       $suggestions = array();
00138       foreach ($fields as $f) {
00139          foreach ($f->getVocabulary() as $v) {
00140             if (stripos($v, substr($term, 0, 100)) === 0) {
00141                $suggestions[] = $v;
00142             }
00143          }
00144       }
00145 
00146       $rep->setBody($trans->transformVocabularySuggestions($suggestions));
00147       $response->send();
00148       die;
00149    }
00150 
00151 }
 All Data Structures Functions Variables