Exemplo n.º 1
0
 /**
  * JSON type/field data
  */
 public function mappings()
 {
     if (!empty($_GET['locale']) && $_GET['locale'] != 'en-US') {
         $this->e400("Non-English locales are not yet supported");
     }
     $locale = empty($_GET['locale']) ? 'en-US' : $_GET['locale'];
     if ($this->subset == 'itemTypeFields') {
         if (empty($_GET['itemType'])) {
             $this->e400("'itemType' not provided");
         }
         $itemType = $_GET['itemType'];
         $itemTypeID = Zotero_ItemTypes::getID($itemType);
         if (!$itemTypeID) {
             $this->e400("Invalid item type '{$itemType}'");
         }
     } else {
         if ($this->subset == 'itemTypeCreatorTypes') {
             if (empty($_GET['itemType'])) {
                 $this->e400("'itemType' not provided");
             }
             $itemType = $_GET['itemType'];
             $itemTypeID = Zotero_ItemTypes::getID($itemType);
             if (!$itemTypeID) {
                 $this->e400("Invalid item type '{$itemType}'");
             }
             // Notes and attachments don't have creators
             if ($itemType == 'note' || $itemType == 'attachment') {
                 echo "[]";
                 exit;
             }
         }
     }
     // TODO: check If-Modified-Since and return 304 if not changed
     $cacheKey = $this->subset . "JSON";
     if (isset($itemTypeID)) {
         $cacheKey .= "_" . $itemTypeID;
     }
     $ttl = 60;
     if ($this->queryParams['pprint']) {
         $cacheKey .= "_pprint";
     }
     $json = Z_Core::$MC->get($cacheKey);
     if ($json) {
         if ($this->queryParams['pprint']) {
             header("Content-Type: text/plain");
         } else {
             header("Content-Type: application/json");
         }
         echo $json;
         exit;
     }
     switch ($this->subset) {
         case 'itemTypes':
             $rows = Zotero_ItemTypes::getAll($locale);
             $propName = 'itemType';
             break;
         case 'itemTypeFields':
             $fieldIDs = Zotero_ItemFields::getItemTypeFields($itemTypeID);
             $rows = array();
             foreach ($fieldIDs as $fieldID) {
                 $fieldName = Zotero_ItemFields::getName($fieldID);
                 $rows[] = array('name' => $fieldName, 'localized' => Zotero_ItemFields::getLocalizedString($itemTypeID, $fieldName, $locale));
             }
             $propName = 'field';
             break;
         case 'itemFields':
             $rows = Zotero_ItemFields::getAll($locale);
             $propName = 'field';
             break;
         case 'itemTypeCreatorTypes':
             $rows = Zotero_CreatorTypes::getTypesForItemType($itemTypeID, $locale);
             $propName = 'creatorType';
             break;
         case 'creatorFields':
             $rows = Zotero_Creators::getLocalizedFieldNames();
             $propName = 'field';
             break;
     }
     $json = array();
     foreach ($rows as $row) {
         $json[] = array($propName => $row['name'], 'localized' => $row['localized']);
     }
     if ($this->queryParams['pprint']) {
         header("Content-Type: text/plain");
         $json = Zotero_Utilities::json_encode_pretty($json);
         Z_Core::$MC->set($cacheKey, $json, $ttl);
     } else {
         header("Content-Type: application/json");
         $json = json_encode($json);
         Z_Core::$MC->set($cacheKey, $json, $ttl);
     }
     echo $json;
     exit;
 }
Exemplo n.º 2
0
 /**
  * JSON type/field data
  */
 public function mappings()
 {
     if (!empty($_GET['locale']) && $_GET['locale'] != 'en-US') {
         $this->e400("Non-English locales are not yet supported");
     }
     $locale = empty($_GET['locale']) ? 'en-US' : $_GET['locale'];
     if ($this->subset == 'itemTypeFields') {
         if (empty($_GET['itemType'])) {
             $this->e400("'itemType' not provided");
         }
         $itemType = $_GET['itemType'];
         $itemTypeID = Zotero_ItemTypes::getID($itemType);
         if (!$itemTypeID) {
             $this->e400("Invalid item type '{$itemType}'");
         }
     } else {
         if ($this->subset == 'itemTypeCreatorTypes') {
             if (empty($_GET['itemType'])) {
                 $this->e400("'itemType' not provided");
             }
             $itemType = $_GET['itemType'];
             $itemTypeID = Zotero_ItemTypes::getID($itemType);
             if (!$itemTypeID) {
                 $this->e400("Invalid item type '{$itemType}'");
             }
             // Notes and attachments don't have creators
             if ($itemType == 'note' || $itemType == 'attachment') {
                 echo "[]";
                 exit;
             }
         }
     }
     // TODO: check If-Modified-Since and return 304 if not changed
     $cacheKey = $this->subset . "JSON";
     if (isset($itemTypeID)) {
         $cacheKey .= "_" . $itemTypeID;
     }
     $cacheKey .= '_' . $this->apiVersion;
     $ttl = 60;
     $json = Z_Core::$MC->get($cacheKey);
     if ($json) {
         header("Content-Type: application/json");
         echo $json;
         exit;
     }
     switch ($this->subset) {
         case 'itemTypes':
             $rows = Zotero_ItemTypes::getAll($locale);
             $propName = 'itemType';
             break;
         case 'itemTypeFields':
             $fieldIDs = Zotero_ItemFields::getItemTypeFields($itemTypeID);
             $rows = array();
             foreach ($fieldIDs as $fieldID) {
                 $fieldName = Zotero_ItemFields::getName($fieldID);
                 $rows[] = array('id' => $fieldID, 'name' => $fieldName, 'localized' => Zotero_ItemFields::getLocalizedString($itemTypeID, $fieldName, $locale));
             }
             $propName = 'field';
             break;
         case 'itemFields':
             $rows = Zotero_ItemFields::getAll($locale);
             $propName = 'field';
             break;
         case 'itemTypeCreatorTypes':
             $rows = Zotero_CreatorTypes::getTypesForItemType($itemTypeID, $locale);
             $propName = 'creatorType';
             break;
         case 'creatorFields':
             $rows = Zotero_Creators::getLocalizedFieldNames();
             $propName = 'field';
             break;
     }
     $json = array();
     foreach ($rows as $row) {
         // Before v3, computerProgram's 'versionNumber' was just 'version'
         if ($this->apiVersion < 3 && ($this->subset == 'itemTypeFields' || $this->subset == 'itemFields') && $row['id'] == 81) {
             $row['name'] = 'version';
         }
         $json[] = array($propName => $row['name'], 'localized' => $row['localized']);
     }
     header("Content-Type: application/json");
     $json = Zotero_Utilities::formatJSON($json);
     Z_Core::$MC->set($cacheKey, $json, $ttl);
     echo $json;
     exit;
 }