dmBridge PHP API
DMAPIObjectController.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 
00013 class DMAPIObjectController extends DMAbstractController {
00014 
00020    public function bitstream($aliasptr) {
00021       $tmp = explode(",", $aliasptr);
00022       try {
00023          $col = DMCollectionFactory::getCollection(
00024                DMCollection::getSanitizedAlias($tmp[0]));
00025          $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00026          
00027          $file = $obj->getFile();
00028          $media_type = $file->getMediaType();
00029          if (!$media_type) {
00030             $media_type = new DMMediaType("application", "octet-stream");
00031          }
00032 
00033          $response = new DMHTTPResponse();
00034          $rep = new DMHTTPRepresentation();
00035          $rep->setMediaType($media_type);
00036          $rep->setBody($file);
00037          $response->setRepresentation($rep);
00038          $response->send();
00039          die;
00040       } catch (DMException $e) {
00041          bailOut($e);
00042       }
00043    }
00044    
00050    public function comments($aliasptr) {
00051       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00052          $this->postComment($aliasptr);
00053       } else {
00054          $this->viewComments($aliasptr);
00055       }
00056    }
00057 
00064    public function excerpt($aliasptr) {
00065       $tmp = explode(",", $aliasptr);
00066       try {
00067          $col = DMCollectionFactory::getCollection(
00068                DMCollection::getSanitizedAlias($tmp[0]));
00069          $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00070 
00071          $response = new DMHTTPResponse();
00072          $rep = new DMHTTPRepresentation();
00073 
00074          $file = $obj->getFile();
00075          $media_type = $file->getMediaType();
00076 
00077          if ($media_type->equals(new DMMediaType("application", "pdf"))) {
00078             $rep->setMediaType($media_type);
00079             $uri = $this->getHTTPRequest()->getURI();
00080             $page = $uri->getQueryValue("page");
00081             if (!$page || round($page) != $page) {
00082                throw new DMIllegalArgumentException("Invalid page number.");
00083             }
00084             
00085             $cmd = new DMShellCommand(sprintf("%s -q -sDEVICE=pdfwrite "
00086                . "-dNOPAUSE -dBATCH -dSAFER -dFirstPage=%d -dLastPage=%d "
00087                . "-sOutputFile=%%stdout %s",
00088                DMConfigIni::getInstance()->getString("dmbridge.ghostscript_path"),
00089                $page, $page, $obj->getFile()->getPathname()));
00090             $rep->setBody($cmd);
00091          } else {
00092             $e = new DMException("This object does not support excerpts.");
00093             $e->setHTTPResponseCode(404);
00094             throw $e;
00095          }
00096 
00097          $response->setRepresentation($rep);
00098          $response->send();
00099          die;
00100       } catch (DMException $e) {
00101          bailOut($e);
00102       }
00103    }
00104 
00105    private function postComment($alias_ptr) {
00106       $tmp = explode(",", $alias_ptr);
00107       $response = new DMHTTPResponse();
00108       $rep = DMHTTPResponseFactory::getRepresentation();
00109       $response->setRepresentation($rep);
00110 
00111       try {
00112          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00113          $ptr = (int) $tmp[1];
00114          $col = DMCollectionFactory::getCollection($alias);
00115          $obj = DMObjectFactory::getObject($col, $ptr);
00116 
00117          $moderation = true;
00118          $msg = "";
00119          if (DMConfigXML::getInstance()->isCommentModerationEnabled()) {
00120             $msg = "Comment added, subject to moderation.";
00121          } else {
00122             $msg = "Comment added.";
00123             $moderation = false;
00124          }
00125 
00126          $obj = DMObjectFactory::getObject(
00127                DMCollectionFactory::getCollection($alias), $ptr);
00128 
00129          $req_rep = DMHTTPRequest::getCurrent()->getRepresentation();
00130          $c = new DMComment();
00131          $c->setName(substr($req_rep->getFormValue("name"), 0, 100));
00132          $c->setEmail($req_rep->getFormValue("email"));
00133 
00134          $c->setValue(substr($req_rep->getFormValue("text"), 0, 10000));
00135          $c->setTimestamp(new DMDateTime());
00136          $c->setApproved(!$moderation);
00137 
00138          $obj->addComment($c);
00139          if (DMConfigXML::getInstance()->isCommentNotificationEnabled()) {
00140             $mailer = new DMMailer();
00141             $mailer->sendNotificationOfNewComment($c);
00142          }
00143          if ($moderation) {
00144             $response->setStatusCode(202);
00145          } else {
00146             $response->setStatusCode(201);
00147             $response->addHeader("Location",
00148                   $c->getURI(DMBridgeComponent::HTTPAPI));
00149          }
00150 
00151          $response->send();
00152          die;
00153       } catch (DMUnavailableModelException $e) {
00154          bailOut($e);
00155       } catch (Exception $e) {
00156          bailOut($e);
00157       }
00158    }
00159 
00160    private function viewComments($aliasptr) {
00161       $tmp = explode(",", $aliasptr);
00162       $uri = DMHTTPRequest::getCurrent()->getURI();
00163 
00164       $page = abs((int) substr($uri->getQueryValue("page"), 0, 7));
00165       $page = ($page < 1) ? 1 : $page;
00166       $rpp = abs((int) substr($uri->getQueryValue("rpp"), 0, 4));
00167       $rpp = ($rpp > 100) ? 100 : $rpp;
00168       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00169 
00170       try {
00171          $col = DMCollectionFactory::getCollection(
00172                DMCollection::getSanitizedAlias($tmp[0]));
00173          $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00174 
00175          $response = new DMHTTPResponse();
00176          $rep = DMHTTPResponseFactory::getRepresentation();
00177          $trans = DMHTTPResponseFactory::getTransformer();
00178          $rep->setBody($trans->transformObjectComments($obj, $page, $rpp));
00179          $response->setRepresentation($rep);
00180          $response->send();
00181          die;
00182       } catch (DMException $e) {
00183          bailOut($e);
00184       }
00185    }
00186 
00190    public function highestRated($alias) {
00191       $col = DMCollectionFactory::getCollection(
00192             DMCollection::getSanitizedAlias($alias));
00193       $uri = DMHTTPRequest::getCurrent()->getURI();
00194 
00195       $rpp = abs((int) substr($uri->getQueryValue("rpp"), 0, 4));
00196       $rpp = ($rpp > 100) ? 100 : $rpp;
00197       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00198 
00199 
00200       $q = new DMObjectQuery();
00201       $q->addCollection($col);
00202       $q->setNumResultsPerPage($rpp);
00203       $q->setRatingRange(0, 1);
00204       // "prime" the query
00205       $q->getSearchResults();
00206 
00207       $response = new DMHTTPResponse();
00208       $rep = DMHTTPResponseFactory::getRepresentation();
00209       $trans = DMHTTPResponseFactory::getTransformer();
00210       $rep->setBody($trans->transformRatings($q));
00211       $response->setRepresentation($rep);
00212       $response->send();
00213       die;
00214    }
00215 
00224    public function index($alias) {
00225       $alias = DMCollection::getSanitizedAlias($alias);
00226       if (strlen($alias) < 2) {
00227          $alias = "/dmdefault";
00228       }
00229       $alias = DMCollection::getSanitizedAlias($alias);
00230       $collection = DMCollectionFactory::getCollection($alias);
00231 
00232       // check for user input from a search
00233       $input = new DMInput(DMHTTPRequest::getCurrent()->getURI());
00234       $query = null;
00235       if ($input->isPresent()) {
00236          $query = $this->search($input, $collection);
00237       } else {
00238          $query = $this->browse($collection);
00239       }
00240       // execute search
00241       $query->getSearchResults();
00242 
00243       $response = new DMHTTPResponse();
00244       $rep = DMHTTPResponseFactory::getRepresentation();
00245       $trans = DMHTTPResponseFactory::getTransformer();
00246       $rep->setBody($trans->transformResults($query));
00247       $response->setRepresentation($rep);
00248       $response->send();
00249       die;
00250    }
00251 
00257    private function browse(DMCollection $c) {
00258       $uri = DMHTTPRequest::getCurrent()->getURI();
00259       $sort = $uri->getQueryValue("sort")
00260             ? substr($uri->getQueryValue("sort"), 0, 20) : null;
00261       $order = $uri->getQueryValue("order")
00262             ? substr($uri->getQueryValue("order"), 0, 10) : null;
00263       $page = $uri->getQueryValue("page")
00264             ? abs((int) $uri->getQueryValue("page")) : 1;
00265       $rpp = $uri->getQueryValue("rpp")
00266             ? abs((int) $uri->getQueryValue("rpp")) : 20;
00267       if ($uri->getQueryValue("view")
00268             && $uri->getQueryValue("view") == "feed") {
00269          $sort = array("dmmodified" => "desc");
00270          $page = 1;
00271       }
00272 
00273       // cap max results per page at 100
00274       $rpp = ($rpp > 100) ? 100 : $rpp;
00275       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00276 
00277       $q = new DMObjectQuery();
00278       $q->setCollections(array($c));
00279       $q->setSortFields(array($sort => $order));
00280       $q->setPage($page);
00281       $q->setNumResultsPerPage($rpp);
00282       return $q;
00283    }
00284 
00285    public function rating($alias_ptr) {
00286       $response = new DMHTTPResponse();
00287       $rep = DMHTTPResponseFactory::getRepresentation();
00288       $trans = DMHTTPResponseFactory::getTransformer();
00289       $response->setRepresentation($rep);
00290 
00291       $tmp = explode(",", $alias_ptr);
00292       try {
00293          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00294          $ptr = (int) $tmp[1];
00295          $col = DMCollectionFactory::getCollection($alias);
00296          $obj = DMObjectFactory::getObject($col, $ptr);
00297 
00298          if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00299             $req_rep = DMHTTPRequest::getCurrent()->getRepresentation();
00300             $rating = new DMRating($req_rep->getFormValue("value"), 100);
00301             $obj->addRating($rating);
00302             $response->setStatusCode(204);
00303             $response->send();
00304             die;
00305          }
00306 
00307          $rep->setBody($trans->transformObjectRating($obj));
00308       } catch (UnavailableModelDMException $e) {
00309          $response->setStatusCode(404);
00310          $response->send();
00311          die;
00312       } catch (DMIllegalArgumentException $e) {
00313          $response->setStatusCode(400);
00314          $response->send();
00315          die;
00316       }
00317 
00318       $response->send();
00319       die;
00320    }
00321 
00327    private function search(DMInput $input, DMCollection $col) {
00328       $uri = DMHTTPRequest::getCurrent()->getURI();
00329       $input->validate();
00330 
00331       $sort = $uri->getQueryValue("sort")
00332             ? substr($uri->getQueryValue("sort"), 0, 10) : null;
00333       $order = $uri->getQueryValue("order")
00334             ? substr($uri->getQueryValue("order"), 0, 4) : null;
00335       $page = $uri->getQueryValue("page")
00336             ? abs((int) $uri->getQueryValue("page")) : 1;
00337       $rpp = $uri->getQueryValue("rpp")
00338             ? abs((int) $uri->getQueryValue("rpp")) : 20;
00339       $rpp = ($rpp > 100) ? 100 : $rpp;
00340       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00341 
00342       $min_rating = $uri->getQueryValue("min_rating")
00343             ? abs($uri->getQueryValue("min_rating")) : null;
00344       $max_rating = $uri->getQueryValue("max_rating")
00345             ? abs($uri->getQueryValue("max_rating")) : null;
00346 
00347       $query = new DMObjectQuery();
00348       $query->setPredicates($input->getTerms());
00349       $query->setCollections($input->getCollections());
00350       $query->setSortFields(array($sort => $order));
00351       $query->setNumResultsPerPage($rpp);
00352       $query->setPage($page);
00353       if ($min_rating || $max_rating) {
00354          if ($max_rating > 100) $max_rating = 100;
00355          if ($min_rating < 0) $min_rating = 0;
00356          $qf = new DMRatingFilter();
00357          $qf->setRange($min_rating, $max_rating);
00358          $query->addFilter($qf);
00359       }
00360 
00361       foreach ($col->getFacets() as $f) {
00362          $query->addFacetTerm($f);
00363       }
00364       return $query;
00365    }
00366 
00370    public function compoundSearch($aliasptr) {
00371       $response = new DMHTTPResponse();
00372       $rep = DMHTTPResponseFactory::getRepresentation();
00373       $trans = DMHTTPResponseFactory::getTransformer();
00374       $response->setRepresentation($rep);
00375       $uri = DMHTTPRequest::getCurrent()->getURI();
00376 
00377       $tmp = explode(",", $aliasptr);
00378       $col = DMCollectionFactory::getCollection(
00379             DMCollection::getSanitizedAlias($tmp[0]));
00380       $ptr = abs((int) substr($tmp[1], 0, 8));
00381       $term = substr($uri->getQueryValue("term"), 0, 150);
00382 
00383       if (!strlen($term)) {
00384          $response->setStatusCode(400);
00385          $response->send();
00386          die;
00387       }
00388 
00389       $page = $uri->getQueryValue("page")
00390             ? abs((int) $uri->getQueryValue("page")) : 1;
00391       $rpp = $uri->getQueryValue("rpp")
00392             ? abs((int) $uri->getQueryValue("rpp")) : 20;
00393       $rpp = ($rpp > 100) ? 100 : $rpp;
00394       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00395 
00396       // instantiate an object so we can find its parent to search within
00397       $obj = DMObjectFactory::getObject($col, $ptr);
00398       $obj = ($obj->isChild()) ? $obj->getParent() : $obj;
00399 
00400       $qt = new DMQueryPredicate();
00401       $qt->setString($term);
00402       $qt->setField(new DMDCElement("full"));
00403       $qt->setMode("exact");
00404 
00405       $query = new DMObjectQuery();
00406       $query->setPredicates(array($qt));
00407       $query->setCollections(array($col));
00408       $query->setSortFields(array("title" => "asc"));
00409       $query->setNumResultsPerPage($rpp);
00410       $query->setPage($page);
00411       $query->addObject($obj);
00412 
00413       $query->getSearchResults();
00414 
00415       $rep->setBody($trans->transformResults($query));
00416       $response->send();
00417       die;
00418    }
00419 
00420    public function searchByRating() {
00421       $response = new DMHTTPResponse();
00422       $rep = DMHTTPResponseFactory::getRepresentation();
00423       $trans = DMHTTPResponseFactory::getTransformer();
00424       $response->setRepresentation($rep);
00425       $uri = DMHTTPRequest::getCurrent()->getURI();
00426 
00427       $page = (int) substr($uri->getQueryValue("page"), 0, 7);
00428       $rpp = (int) $uri->getQueryValue("rpp");
00429       $rpp = ($rpp > 100) ? 100 : $rpp;
00430       $rpp = ($rpp == 1) ? 50 : ($rpp < 10) ? 10 : $rpp;
00431       $min_rating = $uri->getQueryValue("min_rating");
00432       $max_rating = $uri->getQueryValue("max_rating");
00433       $collections = explode(",", $uri->getQueryValue("collections"));
00434 
00435       $q = new DMObjectQuery();
00436       $q->setPage($page);
00437       $q->setNumResultsPerPage($rpp);
00438       $q->setRatingRange((float) $min_rating, (float) $max_rating);
00439       foreach ($collections as $alias) {
00440          if ($alias) {
00441             $col = DMCollectionFactory::getCollection(
00442                   DMCollection::getSanitizedAlias($alias));
00443             $q->addCollection($col);
00444          }
00445       }
00446 
00447       if ($min_rating == null || $max_rating == null
00448             || $min_rating >= $max_rating) {
00449          $response->setStatusCode(400);
00450          $rep->setBody("Invalid rating.");
00451          $response->send();
00452          die;
00453       }
00454 
00455       // "prime" the query
00456       $q->getSearchResults();
00457 
00458       $rep->setBody($trans->transformRatings($q));
00459       $response->send();
00460       die;
00461    }
00462 
00463    public function tags($aliasptr) {
00464       if (DMHTTPRequest::getCurrent()->getMethod() == DMHTTPMethod::POST) {
00465          $this->postTag($aliasptr);
00466       } else {
00467          $this->viewTags($aliasptr);
00468       }
00469    }
00470 
00471    private function postTag($alias_ptr) {
00472       $tmp = explode(",", $alias_ptr);
00473 
00474       $response = new DMHTTPResponse();
00475       $rep = DMHTTPResponseFactory::getRepresentation();
00476       $trans = DMHTTPResponseFactory::getTransformer();
00477       $response->setRepresentation($rep);
00478       $req = DMHTTPRequest::getCurrent();
00479 
00480       try {
00481          $alias = DMCollection::getSanitizedAlias($tmp[0]);
00482          $ptr = (int) $tmp[1];
00483          $col = DMCollectionFactory::getCollection($alias);
00484          $obj = DMObjectFactory::getObject($col, $ptr);
00485 
00486          $req_rep = $req->getRepresentation();
00487          $t = new DMTag();
00488          $t->setValue(substr($req_rep->getFormValue("value"), 0, 100));
00489          $t->setTimestamp(new DMDateTime());
00490 
00491          $moderation = true;
00492          $msg = "";
00493          if (DMConfigXML::getInstance()->isTagModerationEnabled()) {
00494             $msg = "Tag added, subject to moderation.";
00495          } else {
00496             $msg = "Tag added.";
00497             $moderation = false;
00498          }
00499          $t->setApproved(!$moderation);
00500          $obj->addTag($t);
00501 
00502          if ($moderation) {
00503             $response->setStatusCode(202);
00504          } else {
00505             $response->setStatusCode(201);
00506             $response->addHeader("Location",
00507                   $t->getURI(DMBridgeComponent::HTTPAPI));
00508          }
00509       } catch (DMException $e) {
00510          $response->setStatusCode($e->getHTTPResponseCode());
00511          $rep->setBody($trans->transformException($e));
00512       }
00513 
00514       $response->send();
00515       die;
00516    }
00517 
00523    public function thumbnail($aliasptr) {
00524       $tmp = explode(",", $aliasptr);
00525       try {
00526          $col = DMCollectionFactory::getCollection(
00527                DMCollection::getSanitizedAlias($tmp[0]));
00528          $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00529 
00530          $file = $obj->getThumbnail();
00531          $media_type = $file->getMediaType();
00532          if (!$media_type) {
00533             $media_type = new DMMediaType("image", "jpeg");
00534          }
00535 
00536          $response = new DMHTTPResponse();
00537          $rep = new DMHTTPRepresentation();
00538          $rep->setMediaType($media_type);
00539          $rep->setBody($file);
00540          $response->setRepresentation($rep);
00541          $response->send();
00542          die;
00543       } catch (DMException $e) {
00544          bailOut($e);
00545       }
00546    }
00547 
00548    private function viewTags($aliasptr) {
00549       $tmp = explode(",", $aliasptr);
00550 
00551       $col = DMCollectionFactory::getCollection(
00552             DMCollection::getSanitizedAlias($tmp[0]));
00553       $obj = DMObjectFactory::getObject($col, (int) $tmp[1]);
00554 
00555       $response = new DMHTTPResponse();
00556       $rep = DMHTTPResponseFactory::getRepresentation();
00557       $trans = DMHTTPResponseFactory::getTransformer();
00558       $rep->setBody($trans->transformObjectTags($obj));
00559       $response->setRepresentation($rep);
00560       $response->send();
00561       die;
00562    }
00563 
00567    public function view($aliasptr) {
00568       $response = new DMHTTPResponse();
00569       $rep = DMHTTPResponseFactory::getRepresentation();
00570       $trans = DMHTTPResponseFactory::getTransformer();
00571       $response->setRepresentation($rep);
00572 
00573       $tmp = explode(',', $aliasptr);
00574       $alias = DMCollection::getSanitizedAlias($tmp[0]);
00575       $col = DMCollectionFactory::getCollection($alias);
00576       $ptr = (int) $tmp[1];
00577 
00578       try {
00579          $obj = DMObjectFactory::getObject($col, $ptr);
00580          $rep->setBody($trans->transformObject($obj));
00581       } catch (DMUnavailableModelException $e) {
00582          $response->setStatusCode(404);
00583          $rep->setBody($trans->transformException($e));
00584       }
00585 
00586       $response->send();
00587       die;
00588    }
00589 
00590 }
 All Data Structures Functions Variables