/** * Update an article already in the database. * * If you set the article's sectionIDs to NULL, it will be unmapped from * any sections it was previously mapped to. * * @param OnpubArticle $article The article to be updated. * @param bool $overwriteAAMaps False by default. If set to TRUE, the * article-author maps for this article will be deleted and recreated, if * applicable. * @return int 1 if the article was updated. 0 if the article does not exist in the database. */ public function update(OnpubArticle $article, $overwriteAAMaps = FALSE) { $oaamaps = new OnpubAAMaps($this->pdo, FALSE); $oauthors = new OnpubAuthors($this->pdo, FALSE); $osamaps = new OnpubSAMaps($this->pdo, FALSE); $oimages = new OnpubImages($this->pdo, FALSE); $now = new DateTime(); if ($this->enableTransactions) { $status = $this->pdo->beginTransaction(); OnpubDatabase::verifyTransaction($this->pdo, $status); } $stmt = $this->pdo->prepare("UPDATE OnpubArticles SET imageID = :imageID, title = :title, content = :content, url = :url, created = :created, modified = :modified WHERE ID = :ID"); OnpubDatabase::verifyPrepare($this->pdo, $stmt, $this->enableTransactions); if ($article->image) { try { $imageID = $oimages->insert($article->image); $article->imageID = $imageID; } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } $ID = $article->ID; $imageID = $article->imageID; $title = OnpubDatabase::utf8Decode(trim($article->title)); $content = OnpubDatabase::utf8Decode(trim($article->content)); $url = OnpubDatabase::utf8Decode(trim($article->url)); $created = $article->getCreated()->format('Y-m-d H:i:s'); $modified = $now->format('Y-m-d H:i:s'); $stmt->bindParam(':ID', $ID); $stmt->bindParam(':imageID', $imageID); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); $stmt->bindParam(':url', $url); $stmt->bindParam(':created', $created); $stmt->bindParam(':modified', $modified); $result = $stmt->execute(); OnpubDatabase::verifyExecute($this->pdo, $result, $this->enableTransactions, $stmt->errorInfo()); if ($overwriteAAMaps) { try { $oaamaps->delete($article->ID, NULL); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } $authors = $article->authors; foreach ($authors as $author) { if ($author->ID) { try { $oauthors->update($author); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } else { try { $oauthors->insert($author); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } try { $aamap = new OnpubAAMap(); $aamap->articleID = $article->ID; $aamap->authorID = $author->ID; $oaamaps->insert($aamap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } $sectionIDs = $article->sectionIDs; if ($sectionIDs === NULL) { try { $samaps = $osamaps->delete(NULL, $article->ID); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } elseif (sizeof($sectionIDs)) { $queryOptions = new OnpubQueryOptions(); $queryOptions->orderBy = "ID"; $queryOptions->order = "ASC"; try { $samaps = $osamaps->select($queryOptions, NULL, $article->ID); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } // Unmap sections not included in $sectionIDs. foreach ($samaps as $samap) { if (!in_array($samap->sectionID, $sectionIDs)) { try { $osamaps->delete($samap->sectionID, $article->ID); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } } foreach ($sectionIDs as $sectionID) { $samap = new OnpubSAMap(); $samap->sectionID = $sectionID; $samap->articleID = $article->ID; $samap->setCreated($article->getCreated()); $samap->setModified($article->getModified()); try { $samapID = $osamaps->getID($samap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } if ($samapID) { $samap->ID = $samapID; try { $osamaps->update($samap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } else { try { $osamaps->insert($samap); } catch (PDOException $e) { if ($this->enableTransactions) { $this->pdo->rollBack(); } throw $e; } } } } if ($this->enableTransactions) { $status = $this->pdo->commit(); OnpubDatabase::verifyTransaction($this->pdo, $status); } return $stmt->rowCount(); }
/** * Check the status of the Onpub schema. * * @return mixed The version of the schema in the database as an int. An array * of PDOException objects will be returned if the schema is incomplete or * not installed. */ public function status() { $oaamaps = new OnpubAAMaps($this->pdo); $oarticles = new OnpubArticles($this->pdo); $oauthors = new OnpubAuthors($this->pdo); $oimages = new OnpubImages($this->pdo); $osamaps = new OnpubSAMaps($this->pdo); $osections = new OnpubSections($this->pdo); $owebsites = new OnpubWebsites($this->pdo); $owsmaps = new OnpubWSMaps($this->pdo); $queryOptions = new OnpubQueryOptions($this->pdo); $queryOptions->setPage(1, 1); $exceptions = array(); $version = 0; try { $oaamaps->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $oarticles->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $oauthors->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $oimages->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $osamaps->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $osections->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $owebsites->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } try { $owsmaps->select($queryOptions); } catch (PDOException $e) { $exceptions[] = $e; } if (sizeof($exceptions)) { return $exceptions; } $version = 1; return $version; }