function addMissingImage($filename, $fullpath) { global $wgContLang; $timestamp = $this->dbw->timestamp($this->getRepo()->getFileTimestamp($fullpath)); $altname = $wgContLang->checkTitleEncoding($filename); if ($altname != $filename) { if ($this->dryrun) { $filename = $altname; $this->output("Estimating transcoding... {$altname}\n"); } else { # @todo FIXME: create renameFile() $filename = $this->renameFile($filename); } } if ($filename == '') { $this->output("Empty filename for {$fullpath}\n"); return; } if (!$this->dryrun) { $file = wfLocalFile($filename); if (!$file->recordUpload('', '(recovered file, missing upload log entry)', '', '', '', false, $timestamp)) { $this->output("Error uploading file {$fullpath}\n"); return; } } $this->output($fullpath . "\n"); }
private function addCoreDBData() { if ($this->db->getType() == 'oracle') { # Insert 0 user to prevent FK violations # Anonymous user if (!$this->db->selectField('user', '1', ['user_id' => 0])) { $this->db->insert('user', ['user_id' => 0, 'user_name' => 'Anonymous'], __METHOD__, ['IGNORE']); } # Insert 0 page to prevent FK violations # Blank page if (!$this->db->selectField('page', '1', ['page_id' => 0])) { $this->db->insert('page', ['page_id' => 0, 'page_namespace' => 0, 'page_title' => ' ', 'page_restrictions' => null, 'page_is_redirect' => 0, 'page_is_new' => 0, 'page_random' => 0, 'page_touched' => $this->db->timestamp(), 'page_latest' => 0, 'page_len' => 0], __METHOD__, ['IGNORE']); } } User::resetIdByNameCache(); // Make sysop user $user = static::getTestSysop()->getUser(); // Make 1 page with 1 revision $page = WikiPage::factory(Title::newFromText('UTPage')); if ($page->getId() == 0) { $page->doEditContent(new WikitextContent('UTContent'), 'UTPageSummary', EDIT_NEW, false, $user); // doEditContent() probably started the session via // User::loadFromSession(). Close it now. if (session_id() !== '') { session_write_close(); session_id(''); } } }
/** * Add data about uploads to the new test DB, and set up the upload * directory. This should be called after either setDatabase() or * setupDatabase(). * * @param ScopedCallback|null $nextTeardown The next teardown object * @return ScopedCallback The teardown object */ public function setupUploads($nextTeardown = null) { $teardown = []; $this->checkSetupDone('setupDatabase', 'setDatabase'); $teardown[] = $this->markSetupDone('setupUploads'); // Create the files in the upload directory (or pretend to create them // in a MockFileBackend). Append teardown callback. $teardown[] = $this->setupUploadBackend(); // Create a user $user = User::createNew('WikiSysop'); // Register the uploads in the database $image = wfLocalFile(Title::makeTitle(NS_FILE, 'Foobar.jpg')); # note that the size/width/height/bits/etc of the file # are actually set by inspecting the file itself; the arguments # to recordUpload2 have no effect. That said, we try to make things # match up so it is less confusing to readers of the code & tests. $image->recordUpload2('', 'Upload of some lame file', 'Some lame file', ['size' => 7881, 'width' => 1941, 'height' => 220, 'bits' => 8, 'media_type' => MEDIATYPE_BITMAP, 'mime' => 'image/jpeg', 'metadata' => serialize([]), 'sha1' => Wikimedia\base_convert('1', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20010115123500'), $user); $image = wfLocalFile(Title::makeTitle(NS_FILE, 'Thumb.png')); # again, note that size/width/height below are ignored; see above. $image->recordUpload2('', 'Upload of some lame thumbnail', 'Some lame thumbnail', ['size' => 22589, 'width' => 135, 'height' => 135, 'bits' => 8, 'media_type' => MEDIATYPE_BITMAP, 'mime' => 'image/png', 'metadata' => serialize([]), 'sha1' => Wikimedia\base_convert('2', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20130225203040'), $user); $image = wfLocalFile(Title::makeTitle(NS_FILE, 'Foobar.svg')); $image->recordUpload2('', 'Upload of some lame SVG', 'Some lame SVG', ['size' => 12345, 'width' => 240, 'height' => 180, 'bits' => 0, 'media_type' => MEDIATYPE_DRAWING, 'mime' => 'image/svg+xml', 'metadata' => serialize([]), 'sha1' => Wikimedia\base_convert('', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20010115123500'), $user); # This image will be blacklisted in [[MediaWiki:Bad image list]] $image = wfLocalFile(Title::makeTitle(NS_FILE, 'Bad.jpg')); $image->recordUpload2('', 'zomgnotcensored', 'Borderline image', ['size' => 12345, 'width' => 320, 'height' => 240, 'bits' => 24, 'media_type' => MEDIATYPE_BITMAP, 'mime' => 'image/jpeg', 'metadata' => serialize([]), 'sha1' => Wikimedia\base_convert('3', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20010115123500'), $user); $image = wfLocalFile(Title::makeTitle(NS_FILE, 'Video.ogv')); $image->recordUpload2('', 'A pretty movie', 'Will it play', ['size' => 12345, 'width' => 320, 'height' => 240, 'bits' => 0, 'media_type' => MEDIATYPE_VIDEO, 'mime' => 'application/ogg', 'metadata' => serialize([]), 'sha1' => Wikimedia\base_convert('', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20010115123500'), $user); $image = wfLocalFile(Title::makeTitle(NS_FILE, 'Audio.oga')); $image->recordUpload2('', 'An awesome hitsong', 'Will it play', ['size' => 12345, 'width' => 0, 'height' => 0, 'bits' => 0, 'media_type' => MEDIATYPE_AUDIO, 'mime' => 'application/ogg', 'metadata' => serialize([]), 'sha1' => Wikimedia\base_convert('', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20010115123500'), $user); # A DjVu file $image = wfLocalFile(Title::makeTitle(NS_FILE, 'LoremIpsum.djvu')); $image->recordUpload2('', 'Upload a DjVu', 'A DjVu', ['size' => 3249, 'width' => 2480, 'height' => 3508, 'bits' => 0, 'media_type' => MEDIATYPE_BITMAP, 'mime' => 'image/vnd.djvu', 'metadata' => '<?xml version="1.0" ?> <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd"> <DjVuXML> <HEAD></HEAD> <BODY><OBJECT height="3508" width="2480"> <PARAM name="DPI" value="300" /> <PARAM name="GAMMA" value="2.2" /> </OBJECT> <OBJECT height="3508" width="2480"> <PARAM name="DPI" value="300" /> <PARAM name="GAMMA" value="2.2" /> </OBJECT> <OBJECT height="3508" width="2480"> <PARAM name="DPI" value="300" /> <PARAM name="GAMMA" value="2.2" /> </OBJECT> <OBJECT height="3508" width="2480"> <PARAM name="DPI" value="300" /> <PARAM name="GAMMA" value="2.2" /> </OBJECT> <OBJECT height="3508" width="2480"> <PARAM name="DPI" value="300" /> <PARAM name="GAMMA" value="2.2" /> </OBJECT> </BODY> </DjVuXML>', 'sha1' => Wikimedia\base_convert('', 16, 36, 31), 'fileExists' => true], $this->db->timestamp('20010115123600'), $user); return $this->createTeardownObject($teardown, $nextTeardown); }
/** * Insert a new revision into the database, returning the new revision ID * number on success and dies horribly on failure. * * @param Database $dbw * @return int */ public function insertOn($dbw) { global $wgDefaultExternalStore; wfProfileIn(__METHOD__); $data = $this->mText; $flags = Revision::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 (!isset($this->mTextId)) { $old_id = $dbw->nextSequenceValue('text_old_id_val'); $dbw->insert('text', array('old_id' => $old_id, 'old_text' => $data, 'old_flags' => $flags), __METHOD__); $this->mTextId = $dbw->insertId(); } # Record the edit in revisions $rev_id = isset($this->mId) ? $this->mId : $dbw->nextSequenceValue('rev_rev_id_val'); $dbw->insert('revision', 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' => is_null($this->mParentId) ? $this->getPreviousRevisionId($dbw) : $this->mParentId), __METHOD__); $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId(); wfRunHooks('RevisionInsertComplete', array(&$this, $data, $flags)); wfProfileOut(__METHOD__); return $this->mId; }
/** * Update the page record to point to a newly saved revision. * * @param Database $dbw * @param Revision $revision For ID number, and text used to set length and redirect status fields * @param int $lastRevision If given, will not overwrite the page field * when different from the currently set value. * Giving 0 indicates the new page flag should * be set on. * @param bool $lastRevIsRedirect If given, will optimize adding and * removing rows in redirect table. * @return bool true on success, false on failure * @private */ function updateRevisionOn(&$dbw, $revision, $lastRevision = null, $lastRevIsRedirect = null) { wfProfileIn(__METHOD__); $text = $revision->getText(); $rt = Title::newFromRedirect($text); $conditions = array('page_id' => $this->getId()); if (!is_null($lastRevision)) { # An extra check against threads stepping on each other $conditions['page_latest'] = $lastRevision; } $dbw->update('page', array('page_latest' => $revision->getId(), 'page_touched' => $dbw->timestamp(), 'page_is_new' => $lastRevision === 0 ? 1 : 0, 'page_is_redirect' => $rt !== NULL ? 1 : 0, 'page_len' => strlen($text)), $conditions, __METHOD__); $result = $dbw->affectedRows() != 0; if ($result) { // FIXME: Should the result from updateRedirectOn() be returned instead? $this->updateRedirectOn($dbw, $rt, $lastRevIsRedirect); } wfProfileOut(__METHOD__); return $result; }
/** * Insert a new revision into the database, returning the new revision ID * number on success and dies horribly on failure. * * @param Database $dbw * @return int */ function insertOn(&$dbw) { global $wgDefaultExternalStore; $fname = 'Revision::insertOn'; wfProfileIn($fname); $data = $this->mText; $flags = Revision::compressRevisionText($data); # Write to external storage if required if ($wgDefaultExternalStore) { if (is_array($wgDefaultExternalStore)) { // Distribute storage across multiple clusters $store = $wgDefaultExternalStore[mt_rand(0, count($wgDefaultExternalStore) - 1)]; } else { $store = $wgDefaultExternalStore; } require_once 'ExternalStore.php'; // Store and get the URL $data = ExternalStore::insert($store, $data); if (!$data) { # This should only happen in the case of a configuration error, where the external store is not valid throw new MWException("Unable to store text to external storage {$store}"); } if ($flags) { $flags .= ','; } $flags .= 'external'; } # Record the text (or external storage URL) to the text table if (!isset($this->mTextId)) { $old_id = $dbw->nextSequenceValue('text_old_id_val'); $dbw->insert('text', array('old_id' => $old_id, 'old_text' => $data, 'old_flags' => $flags), $fname); $this->mTextId = $dbw->insertId(); } # Record the edit in revisions $rev_id = isset($this->mId) ? $this->mId : $dbw->nextSequenceValue('rev_rev_id_val'); $dbw->insert('revision', 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), $fname); $this->mId = !is_null($rev_id) ? $rev_id : $dbw->insertId(); wfProfileOut($fname); return $this->mId; }
/** * Update the page record to point to a newly saved revision. * * @param Database $dbw * @param Revision $revision For ID number, and text used to set length and redirect status fields * @param int $lastRevision If given, will not overwrite the page field * when different from the currently set value. * Giving 0 indicates the new page flag should * be set on. * @return bool true on success, false on failure * @private */ function updateRevisionOn(&$dbw, $revision, $lastRevision = null) { wfProfileIn(__METHOD__); $conditions = array('page_id' => $this->getId()); if (!is_null($lastRevision)) { # An extra check against threads stepping on each other $conditions['page_latest'] = $lastRevision; } $text = $revision->getText(); $dbw->update('page', array('page_latest' => $revision->getId(), 'page_touched' => $dbw->timestamp(), 'page_is_new' => $lastRevision === 0 ? 1 : 0, 'page_is_redirect' => Article::isRedirect($text) ? 1 : 0, 'page_len' => strlen($text)), $conditions, __METHOD__); wfProfileOut(__METHOD__); return $dbw->affectedRows() != 0; }
/** * Insert a new revision into the database, returning the new revision ID * number on success and dies horribly on failure. * * @param Database $dbw * @return int */ function insertOn(&$dbw) { $fname = 'Revision::insertOn'; wfProfileIn($fname); $mungedText = $this->mText; $flags = Revision::compressRevisionText($mungedText); # Record the text to the text table if (!isset($this->mTextId)) { $old_id = $dbw->nextSequenceValue('text_old_id_val'); $dbw->insert('text', array('old_id' => $old_id, 'old_text' => $mungedText, 'old_flags' => $flags), $fname); $this->mTextId = $dbw->insertId(); } # Record the edit in revisions $rev_id = isset($this->mId) ? $this->mId : $dbw->nextSequenceValue('rev_rev_id_val'); $dbw->insert('revision', 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), $fname); $this->mId = $dbw->insertId(); wfProfileOut($fname); return $this->mId; }
/** * Builds update conditions. Additional conditions may be added to $conditions to * protected against race conditions using a compare-and-set (CAS) mechanism * based on comparing $this->mTouched with the user_touched field. * * @param Database $db * @param array $conditions WHERE conditions for use with Database::update * @return array WHERE conditions for use with Database::update */ protected function makeUpdateConditions(Database $db, array $conditions) { if ($this->mTouched) { // CAS check: only update if the row wasn't changed sicne it was loaded. $conditions['user_touched'] = $db->timestamp($this->mTouched); } return $conditions; }