public function finishWrite() { if ($this->readOnly) { return; } elseif (is_null($this->currentLang)) { throw new MWException(__CLASS__ . ': must call startWrite() before finishWrite()'); } $this->dbw->startAtomic(__METHOD__); try { $this->dbw->delete('l10n_cache', ['lc_lang' => $this->currentLang], __METHOD__); foreach (array_chunk($this->batch, 500) as $rows) { $this->dbw->insert('l10n_cache', $rows, __METHOD__); } $this->writesDone = true; } catch (DBQueryError $e) { if ($this->dbw->wasReadOnlyError()) { $this->readOnly = true; // just avoid site down time } else { throw $e; } } $this->dbw->endAtomic(__METHOD__); $this->currentLang = null; $this->batch = []; }
/** * 插入一条新的记录到表 * @param $data Array 必须是键值(表的字段对应值)对应 * @return int */ public final function put($data) { if (empty($data) or !is_array($data)) { return false; } $this->db->insert($data, $this->table); return $this->db->lastInsertId(); }
/** * 插入一条新的记录到表 * @param $data Array 必须是键值(表的字段对应值)对应 * @return int */ public function put($data) { if (empty($data) or !is_array($data)) { return false; } if ($this->db->insert($data, $this->table)) { $lastInsertId = $this->db->lastInsertId(); if ($lastInsertId == 0) { return true; } else { return $lastInsertId; } } else { return false; } }
/** * Insert a new empty page record for this article. * This *must* be followed up by creating a revision * and running $this->updateRevisionOn( ... ); * or else the record will be left in a funky state. * Best if all done inside a transaction. * * @param IDatabase $dbw * @param int|null $pageId Custom page ID that will be used for the insert statement * * @return bool|int The newly created page_id key; false if the row was not * inserted, e.g. because the title already existed or because the specified * page ID is already in use. */ public function insertOn($dbw, $pageId = null) { $pageIdForInsert = $pageId ?: $dbw->nextSequenceValue('page_page_id_seq'); $dbw->insert('page', ['page_id' => $pageIdForInsert, 'page_namespace' => $this->mTitle->getNamespace(), 'page_title' => $this->mTitle->getDBkey(), 'page_restrictions' => '', 'page_is_redirect' => 0, 'page_is_new' => 1, 'page_random' => wfRandom(), 'page_touched' => $dbw->timestamp(), 'page_latest' => 0, 'page_len' => 0], __METHOD__, 'IGNORE'); if ($dbw->affectedRows() > 0) { $newid = $pageId ?: $dbw->insertId(); $this->mId = $newid; $this->mTitle->resetArticleID($newid); return $newid; } else { return false; // nothing changed } }
/** * This function should *not* be called outside of JobQueueDB * * @param IDatabase $dbw * @param IJobSpecification[] $jobs * @param int $flags * @param string $method * @throws DBError * @return void */ public function doBatchPushInternal(IDatabase $dbw, array $jobs, $flags, $method) { if (!count($jobs)) { return; } $rowSet = array(); // (sha1 => job) map for jobs that are de-duplicated $rowList = array(); // list of jobs for jobs that are not de-duplicated foreach ($jobs as $job) { $row = $this->insertFields($job); if ($job->ignoreDuplicates()) { $rowSet[$row['job_sha1']] = $row; } else { $rowList[] = $row; } } if ($flags & self::QOS_ATOMIC) { $dbw->startAtomic($method); // wrap all the job additions in one transaction } try { // Strip out any duplicate jobs that are already in the queue... if (count($rowSet)) { $res = $dbw->select('job', 'job_sha1', array('job_sha1' => array_keys($rowSet), 'job_token' => ''), $method); foreach ($res as $row) { wfDebug("Job with hash '{$row->job_sha1}' is a duplicate.\n"); unset($rowSet[$row->job_sha1]); // already enqueued } } // Build the full list of job rows to insert $rows = array_merge($rowList, array_values($rowSet)); // Insert the job rows in chunks to avoid slave lag... foreach (array_chunk($rows, 50) as $rowBatch) { $dbw->insert('job', $rowBatch, $method); } JobQueue::incrStats('inserts', $this->type, count($rows)); JobQueue::incrStats('dupe_inserts', $this->type, count($rowSet) + count($rowList) - count($rows)); } catch (DBError $e) { if ($flags & self::QOS_ATOMIC) { $dbw->rollback($method); } throw $e; } if ($flags & self::QOS_ATOMIC) { $dbw->endAtomic($method); } return; }
/** * Insert a new revision into the database, returning the new revision ID * number on success and dies horribly on failure. * * @param IDatabase $dbw (master connection) * @throws MWException * @return int */ public function insertOn($dbw) { global $wgDefaultExternalStore, $wgContentHandlerUseDB; // Not allowed to have rev_page equal to 0, false, etc. if (!$this->mPage) { $title = $this->getTitle(); if ($title instanceof Title) { $titleText = ' for page ' . $title->getPrefixedText(); } else { $titleText = ''; } throw new MWException("Cannot insert revision{$titleText}: page ID must be nonzero"); } $this->checkContentModel(); $data = $this->mText; $flags = self::compressRevisionText($data); # Write to external storage if required if ($wgDefaultExternalStore) { // Store and get the URL $data = ExternalStore::insertToDefault($data); if (!$data) { throw new MWException("Unable to store text to external storage"); } if ($flags) { $flags .= ','; } $flags .= 'external'; } # Record the text (or external storage URL) to the text table if ($this->mTextId === null) { $old_id = $dbw->nextSequenceValue('text_old_id_seq'); $dbw->insert('text', array('old_id' => $old_id, 'old_text' => $data, 'old_flags' => $flags), __METHOD__); $this->mTextId = $dbw->insertId(); } if ($this->mComment === null) { $this->mComment = ""; } # Record the edit in revisions $rev_id = $this->mId !== null ? $this->mId : $dbw->nextSequenceValue('revision_rev_id_seq'); $row = array('rev_id' => $rev_id, 'rev_page' => $this->mPage, 'rev_text_id' => $this->mTextId, 'rev_comment' => $this->mComment, 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0, 'rev_user' => $this->mUser, 'rev_user_text' => $this->mUserText, 'rev_timestamp' => $dbw->timestamp($this->mTimestamp), 'rev_deleted' => $this->mDeleted, 'rev_len' => $this->mSize, 'rev_parent_id' => $this->mParentId === null ? $this->getPreviousRevisionId($dbw) : $this->mParentId, 'rev_sha1' => $this->mSha1 === null ? Revision::base36Sha1($this->mText) : $this->mSha1); if ($wgContentHandlerUseDB) { // NOTE: Store null for the default model and format, to save space. // XXX: Makes the DB sensitive to changed defaults. // Make this behavior optional? Only in miser mode? $model = $this->getContentModel(); $format = $this->getContentFormat(); $title = $this->getTitle(); if ($title === null) { throw new MWException("Insufficient information to determine the title of the " . "revision's page!"); } $defaultModel = ContentHandler::getDefaultModelFor($title); $defaultFormat = ContentHandler::getForModelID($defaultModel)->getDefaultFormat(); $row['rev_content_model'] = $model === $defaultModel ? null : $model; $row['rev_content_format'] = $format === $defaultFormat ? null : $format; } $dbw->insert('revision', $row, __METHOD__); $this->mId = $rev_id !== null ? $rev_id : $dbw->insertId(); // Assertion to try to catch T92046 if ((int) $this->mId === 0) { throw new UnexpectedValueException('After insert, Revision mId is ' . var_export($this->mId, 1) . ': ' . var_export($row, 1)); } Hooks::run('RevisionInsertComplete', array(&$this, $data, $flags)); return $this->mId; }