private function createConnection()
 {
     $connection = new Database(new LazyDBConnectionProvider(DB_SLAVE), new LazyDBConnectionProvider(DB_MASTER));
     $connection->setDBPrefix($GLOBALS['wgDBprefix']);
     $connection->resetTransactionProfiler($this->resetTransactionProfiler);
     return $connection;
 }
Example #2
0
 /**
  * @since 2.1
  *
  * @param integer $id
  *
  * @return DIWikiPage|null
  */
 public function getDataItemForId($id)
 {
     if (!$this->cache->contains($id)) {
         $row = $this->connection->selectRow($this->tableName, array('smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject'), array('smw_id' => $id), __METHOD__);
         if ($row === false) {
             return null;
         }
         $hash = HashBuilder::createHashIdFromSegments($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject);
         $this->saveToCache($id, $hash);
     }
     return HashBuilder::newDiWikiPageFromHash($this->cache->fetch($id));
 }
 /**
  * @since 2.5
  *
  * @param WebRequest $webRequest
  */
 public function doRefresh(WebRequest $webRequest)
 {
     $this->outputFormatter->setPageTitle($this->getMessage('smw_smwadmin_datarefresh'));
     $this->outputFormatter->addParentLink();
     if (!$this->enabledRefreshStore) {
         return $this->outputMessage('smw_smwadmin_return');
     }
     $refreshjob = $this->getRefreshJob();
     $sure = $webRequest->getText('rfsure');
     if ($sure == 'yes') {
         if ($refreshjob === null) {
             // careful, there might be race conditions here
             $newjob = ApplicationFactory::getInstance()->newJobFactory()->newByType('SMW\\RefreshJob', \SpecialPage::getTitleFor('SMWAdmin'), array('spos' => 1, 'prog' => 0, 'rc' => 2));
             $newjob->insert();
             $this->outputMessage('smw_smwadmin_updatestarted');
         } else {
             $this->outputMessage('smw_smwadmin_updatenotstarted');
         }
     } elseif ($sure == 'stop') {
         // delete (all) existing iteration jobs
         $this->connection->delete('job', array('job_cmd' => 'SMW\\RefreshJob'), __METHOD__);
         $this->outputMessage('smw_smwadmin_updatestopped');
     } else {
         $this->outputMessage('smw_smwadmin_updatenotstopped');
     }
 }
 private function doRebuildByPropertyTable($proptable)
 {
     $searchTable = $this->getSearchTable();
     if ($proptable->getDiType() === DataItem::TYPE_URI) {
         $fetchFields = array('s_id', 'p_id', 'o_blob', 'o_serialized');
     } else {
         $fetchFields = array('s_id', 'p_id', 'o_blob', 'o_hash');
     }
     $table = $proptable->getName();
     $pid = '';
     // Fixed tables don't have a p_id column therefore get it
     // from the ID TABLE
     if ($proptable->isFixedPropertyTable()) {
         unset($fetchFields[1]);
         // p_id
         $pid = $searchTable->getPropertyIdBy(new DIProperty($proptable->getFixedProperty()));
         if ($searchTable->isExemptedPropertyById($pid)) {
             return $this->skippedTables[$table] = 'Fixed property table that belongs to the ' . $proptable->getFixedProperty() . ' exempted property.';
         }
     }
     $rows = $this->connection->select($table, $fetchFields, array(), __METHOD__);
     if ($rows === false || $rows === null) {
         return $this->skippedTables[$table] = 'Empty table.';
     }
     $this->doRebuildFromRows($searchTable, $table, $pid, $rows);
 }
 private function getIdInfoAsJson($webRequest, $id)
 {
     if ($id < 1 || $webRequest->getText('action') !== 'idlookup') {
         return '';
     }
     $row = $this->connection->selectRow(\SMWSql3SmwIds::TABLE_NAME, array('smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject', 'smw_sortkey'), 'smw_id=' . $id, __METHOD__);
     return '<pre>' . $this->outputFormatter->encodeAsJson(array($id, $row)) . '</pre>';
 }
 /**
  * After querying, make sure no temporary database tables are left.
  * @todo I might be better to keep the tables and possibly reuse them later
  * on. Being temporary, the tables will vanish with the session anyway.
  */
 public function cleanUp()
 {
     if ($this->queryMode === Query::MODE_DEBUG) {
         return;
     }
     foreach ($this->executedQueries as $table => $log) {
         $this->connection->query("DROP TEMPORARY TABLE " . $this->connection->tableName($table), __METHOD__);
     }
 }
 /**
  * @since 2.3
  *
  * @param string $type
  * @param string $tablename
  * @param string $valueComposite
  *
  * @throws RuntimeException
  */
 public function createHierarchyTempTableFor($type, $tablename, $valueComposite)
 {
     list($smwtable, $depth) = $this->getHierarchyTableDefinitionForType($type);
     if (array_key_exists($valueComposite, $this->hierarchyCache)) {
         // Just copy known result.
         $this->connection->query("INSERT INTO {$tablename} (id) SELECT id" . ' FROM ' . $this->hierarchyCache[$valueComposite], __METHOD__);
         return;
     }
     $this->buildTempTableFor($tablename, $valueComposite, $smwtable, $depth);
 }
 private function canMatchById($id)
 {
     $row = $this->connection->selectRow(\SMWSQLStore3::ID_TABLE, array('smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject'), array('smw_id' => $id), __METHOD__);
     if ($row === false) {
         return false;
     }
     $hash = HashBuilder::createHashIdFromSegments($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject);
     $this->saveToCache($id, $hash);
     return true;
 }
 /**
  * @since 1.9.2
  *
  * @return int
  */
 public function getMaxId()
 {
     if ($this->namespace === NS_CATEGORY) {
         $tableName = 'category';
         $var = 'MAX(cat_id)';
     } else {
         $tableName = 'page';
         $var = 'MAX(page_id)';
     }
     return (int) $this->connection->selectField($tableName, $var, false, __METHOD__);
 }
 /**
  * Get SQL code suitable to create a temporary table of the given name, used to store ids.
  * MySQL can do that simply by creating new temporary tables. PostgreSQL first checks if such
  * a table exists, so the code is ready to reuse existing tables if the code was modified to
  * keep them after query answering. Also, PostgreSQL tables will use a RULE to achieve built-in
  * duplicate elimination. The latter is done using INSERT IGNORE in MySQL.
  *
  * @param string $tableName
  *
  * @return string
  */
 private function getSQLCodeFor($tableName)
 {
     // PostgreSQL: no memory tables, use RULE to emulate INSERT IGNORE
     if ($this->connection->isType('postgres')) {
         // Remove any double quotes from the name
         $tableName = str_replace('"', '', $tableName);
         return "DO \$\$BEGIN " . " IF EXISTS(SELECT NULL FROM pg_tables WHERE tablename='{$tableName}' AND schemaname = ANY (current_schemas(true))) " . " THEN DELETE FROM {$tableName}; " . " ELSE " . "  CREATE TEMPORARY TABLE {$tableName} (id INTEGER PRIMARY KEY); " . "    CREATE RULE {$tableName}_ignore AS ON INSERT TO {$tableName} WHERE (EXISTS (SELECT 1 FROM {$tableName} " . "\t WHERE ({$tableName}.id = new.id))) DO INSTEAD NOTHING; " . " END IF; " . "END\$\$";
     }
     // MySQL_ just a temporary table, use INSERT IGNORE later
     return "CREATE TEMPORARY TABLE " . $tableName . "( id INT UNSIGNED KEY ) ENGINE=MEMORY";
 }
 /**
  * @since 2.1
  *
  * @param integer $id
  *
  * @return DIWikiPage|null
  */
 public function getDataItemForId($id)
 {
     $poolCache = $this->inMemoryPoolCache->getPoolCacheFor('sql.store.dataitem.finder');
     if (!$poolCache->contains($id)) {
         $row = $this->connection->selectRow(\SMWSQLStore3::ID_TABLE, array('smw_title', 'smw_namespace', 'smw_iw', 'smw_subobject'), array('smw_id' => $id), __METHOD__);
         if ($row === false) {
             return null;
         }
         $hash = HashBuilder::createHashIdFromSegments($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject);
         $this->saveToCache($id, $hash);
     }
     return HashBuilder::newDiWikiPageFromHash($poolCache->fetch($id));
 }
Example #12
0
 private function writeConnection()
 {
     if ($this->writeConnection !== null) {
         return $this->writeConnection;
     }
     if (!$this->writeConnectionProvider instanceof DBConnectionProvider) {
         throw new RuntimeException('Expected a DBConnectionProvider instance');
     }
     $this->writeConnection = $this->writeConnectionProvider->getConnection();
     // MW 1.27 (only)
     if ($this->resetTransactionProfiler && method_exists($this->writeConnection, 'setTransactionProfiler')) {
         $this->writeConnection->setTransactionProfiler(new \TransactionProfiler());
     }
     return $this->writeConnection;
 }
 /**
  * @since 2.5
  *
  * @param string $value
  *
  * @return string
  */
 public function addQuotes($value)
 {
     return $this->connection->addQuotes($value);
 }
 public function testMissingWriteConnectionThrowsException()
 {
     $connectionProvider = $this->getMockBuilder('\\SMW\\DBConnectionProvider')->disableOriginalConstructor()->getMock();
     $instance = new Database($connectionProvider);
     $this->setExpectedException('RuntimeException');
     $instance->tableExists('Foo');
 }
 /**
  * After querying, make sure no temporary database tables are left.
  * @todo I might be better to keep the tables and possibly reuse them later
  * on. Being temporary, the tables will vanish with the session anyway.
  */
 public function cleanUp()
 {
     foreach ($this->executedQueries as $table => $log) {
         $this->connection->query("DROP TEMPORARY TABLE " . $this->connection->tableName($table), __METHOD__);
     }
 }
 /**
  * @since 2.5
  */
 public function flushTable()
 {
     $this->connection->delete($this->searchTable->getTableName(), '*', __METHOD__);
 }
 /**
  * After querying, make sure no temporary database tables are left.
  * @todo I might be better to keep the tables and possibly reuse them later
  * on. Being temporary, the tables will vanish with the session anyway.
  */
 public function cleanUp()
 {
     foreach ($this->executedQueries as $table => $log) {
         $this->temporaryTableBuilder->drop($this->connection->tableName($table));
     }
 }
 private function delete($title, $namespace)
 {
     $this->connection->delete(self::TABLENAME, array('s_title' => $title, 's_namespace' => $namespace), __METHOD__);
 }