Exemplo n.º 1
0
    private function loadPrimaryData($allowFail = false)
    {
        Z_Core::debug("Loading primary data for item {$this->libraryID}/{$this->key}");
        if ($this->loaded['primaryData']) {
            throw new Exception("Primary data already loaded for item {$this->libraryID}/{$this->key}");
        }
        $libraryID = $this->libraryID;
        $id = $this->id;
        $key = $this->key;
        if (!$libraryID) {
            throw new Exception("Library ID not set");
        }
        if (!$id && !$key) {
            throw new Exception("ID or key not set");
        }
        // Use cached check for existence if possible
        if ($libraryID && $key) {
            if (!Zotero_Items::existsByLibraryAndKey($libraryID, $key)) {
                $this->loaded['primaryData'] = true;
                if ($allowFail) {
                    return false;
                }
                throw new Exception("Item " . ($id ? $id : "{$libraryID}/{$key}") . " not found");
            }
        }
        $columns = array();
        foreach (Zotero_Items::$primaryFields as $field) {
            $colSQL = '';
            if (is_null($field == 'itemID' ? $this->id : $this->{$field})) {
                switch ($field) {
                    case 'itemID':
                    case 'itemTypeID':
                    case 'dateAdded':
                    case 'dateModified':
                    case 'libraryID':
                    case 'key':
                    case 'serverDateModified':
                        $colSQL = 'I.' . $field;
                        break;
                    case 'itemVersion':
                        $colSQL = 'I.version AS itemVersion';
                        break;
                    case 'numNotes':
                        $colSQL = '(SELECT COUNT(*) FROM itemNotes INo
							WHERE sourceItemID=I.itemID AND INo.itemID NOT IN
							(SELECT itemID FROM deletedItems)) AS numNotes';
                        break;
                    case 'numAttachments':
                        $colSQL = '(SELECT COUNT(*) FROM itemAttachments IA
							WHERE sourceItemID=I.itemID AND IA.itemID NOT IN
							(SELECT itemID FROM deletedItems)) AS numAttachments';
                        break;
                    case 'numNotes':
                        $colSQL = '(SELECT COUNT(*) FROM itemNotes
									WHERE sourceItemID=I.itemID) AS numNotes';
                        break;
                    case 'numAttachments':
                        $colSQL = '(SELECT COUNT(*) FROM itemAttachments
									WHERE sourceItemID=I.itemID) AS numAttachments';
                        break;
                }
                if ($colSQL) {
                    $columns[] = $colSQL;
                }
            }
        }
        if (!$columns) {
            Z_Core::debug("No primary columns to load");
            return;
        }
        $sql = 'SELECT ' . implode(', ', $columns) . " FROM items I WHERE ";
        if ($id) {
            if (!is_numeric($id)) {
                trigger_error("Invalid itemID '{$id}'", E_USER_ERROR);
            }
            $sql .= "itemID=?";
            $stmt = Zotero_DB::getStatement($sql, 'loadPrimaryData_id', Zotero_Shards::getByLibraryID($libraryID));
            $row = Zotero_DB::rowQueryFromStatement($stmt, array($id));
        } else {
            if (!is_numeric($libraryID)) {
                trigger_error("Invalid libraryID '{$libraryID}'", E_USER_ERROR);
            }
            if (!preg_match('/[A-Z0-9]{8}/', $key)) {
                trigger_error("Invalid key '{$key}'!", E_USER_ERROR);
            }
            $sql .= "libraryID=? AND `key`=?";
            $stmt = Zotero_DB::getStatement($sql, 'loadPrimaryData_key', Zotero_Shards::getByLibraryID($libraryID));
            $row = Zotero_DB::rowQueryFromStatement($stmt, array($libraryID, $key));
        }
        $this->loaded['primaryData'] = true;
        if (!$row) {
            if ($allowFail) {
                return false;
            }
            throw new Exception("Item " . ($id ? $id : "{$libraryID}/{$key}") . " not found");
        }
        $this->loadFromRow($row);
        return true;
    }
Exemplo n.º 2
0
 private function load()
 {
     $libraryID = $this->libraryID;
     $id = $this->id;
     $key = $this->key;
     if (!$libraryID) {
         throw new Exception("Library ID not set");
     }
     if (!$id && !$key) {
         throw new Exception("ID or key not set");
     }
     // Cache tag data for the entire library
     if (true) {
         if ($id) {
             Z_Core::debug("Loading data for tag {$this->libraryID}/{$this->id}");
             $row = Zotero_Tags::getPrimaryDataByID($libraryID, $id);
         } else {
             Z_Core::debug("Loading data for tag {$this->libraryID}/{$this->key}");
             $row = Zotero_Tags::getPrimaryDataByKey($libraryID, $key);
         }
         $this->loaded = true;
         if (!$row) {
             return;
         }
         if ($row['libraryID'] != $libraryID) {
             throw new Exception("libraryID {$row['libraryID']} != {$this->libraryID}");
         }
         foreach ($row as $key => $val) {
             $this->{$key} = $val;
         }
     } else {
         // Use cached check for existence if possible
         if ($libraryID && $key) {
             if (!Zotero_Tags::existsByLibraryAndKey($libraryID, $key)) {
                 $this->loaded = true;
                 return;
             }
         }
         $shardID = Zotero_Shards::getByLibraryID($libraryID);
         $sql = Zotero_Tags::getPrimaryDataSQL();
         if ($id) {
             $sql .= "tagID=?";
             $stmt = Zotero_DB::getStatement($sql, false, $shardID);
             $data = Zotero_DB::rowQueryFromStatement($stmt, $id);
         } else {
             $sql .= "libraryID=? AND `key`=?";
             $stmt = Zotero_DB::getStatement($sql, false, $shardID);
             $data = Zotero_DB::rowQueryFromStatement($stmt, array($libraryID, $key));
         }
         $this->loaded = true;
         if (!$data) {
             return;
         }
         if ($data['libraryID'] != $libraryID) {
             throw new Exception("libraryID {$data['libraryID']} != {$libraryID}");
         }
         foreach ($data as $k => $v) {
             $this->{$k} = $v;
         }
     }
 }