Esempio n. 1
0
bool CompatibilityManager::razorUpgrade(const String &folder)
{
	try
	{
		if(FileSystem::instance()->directoryExists(folder))
		{
			StringList portals;
			FileSystem::instance()->getDirectories(folder, portals, false);
			for(StringList::const_iterator i = portals.begin(); i != portals.end(); ++i)
			{
				// 000000018F94C9554C8B227CF000C6E30EB281AEAE1A77E7
				String name = *i;
				if(name.length() == 48)
				{
					// Portal < Razor
					std::string oldID = name.to_ascii();

					// Options
					String oldPath = utils::standardisePath(folder + oldID);
					shared_ptr<PortalOptions> options(OS_NEW PortalOptions());
					// Carica le impostazioni del portale
					if(options->readFromPath(oldPath) == false)
						return false;
					
					shared_ptr<IDbDriver> driver = DatabasesSystem::instance()->getDriver("sqlite");

					// Query Database
					shared_ptr<IDbConnection> connection = driver->createConnection(oldID, driver->createOptions());

					connection->open();
					
					String portalName = options->getName();
					//String portalDescription = static_cast<String>(options->getOptionValue("description"));


					NotificationsManager::instance()->notify(_S("Migration of '") + portalName + _S("' to 1.0 series"));
					PlatformManager::instance()->sleep(500);

					//ObjectID userID = static_cast<String>(m_portalOptions.getOption(options::users_reference)).to_ascii();
					ObjectID userID = static_cast<String>(options->getOptionValue("users.reference")).to_ascii();

					//bool monarchic = (options->getPortalID().getPortalType() == portalTypeMonarchic);
					ID oldPortalID(oldID);
					uint8 portalType = OS_UINT8_LO(oldPortalID.getHeader().getFirst());
					bool monarchic = (portalType == 1);

					// Calcolo UserID.
					// Se non lo posso dedurre, uso il primo che trovo loggato.					
					if(userID.empty())
					{
						if(monarchic)
						{
							//userID = options->getPortalID().getAdministratorID();
#ifdef OS_NOOBJECTID
							userID = oldPortalID.getHash();
#else
							userID = ObjectID::generate(portalObjectTypeUser, oldPortalID.getHash());
#endif
						}
						else
						{
							String sql = String::EMPTY;
							sql += _S(" select tp.profile, tu.reference ");
							sql += _S(" from ");
							sql += _S(" os_snapshot_profiles tp, ");
							sql += _S(" os_snapshot_users tu ");
							sql += _S(" where  ");
							sql += _S(" tp.profile=tu.profile and ");
							sql += _S(" tu.score=1 and ");
							sql += _S(" tu.follow=1 and ");
							sql += _S(" tu.description='Auto valutation'");

							shared_ptr<IDbResult> result = connection->query(sql);
							DataTable table;
							result->init(table);
							DataTableRow row = table.addRow();

							while(result->end() == false)
							{
								result->bind(row);
								String objectID = static_cast<String>(*row[1]);

								userID = objectID.to_ascii();

								result->moveNext();

								break;
							}
						}
					}
					
#ifdef OS_NOOBJECTID
					PovID povID = userID;
#else
					PovID povID = userID.getHash();
#endif
					
					if(userID.empty() == false)
					{
						// Clean old snapshot
						connection->execute(_S("delete from os_snapshot_objects"));
						connection->execute(_S("delete from os_snapshot_users"));
						connection->execute(_S("delete from os_snapshot_profiles"));
						connection->execute(_S("delete from os_discussions_stats"));
						//connection->execute(_S("delete from os_forums_forum_stats"));
						//connection->execute(_S("delete from os_forums_section_stats"));
						connection->execute(_S("delete from os_polls_stats"));
						connection->execute(_S("delete from os_polls_options_stats"));
						connection->execute(_S("delete from os_users_stats"));

						// Old
						connection->execute(_S("drop table if exists os_forums_forum_stats"));
						connection->execute(_S("drop table if exists os_forums_section_stats"));

						// Reset acceptable
						connection->execute(_S("update os_entries set rank=-2"));

						// Clean signatures
#ifdef OS_NEWSIGNATURE
						connection->execute(_S("update os_attributes set signature=x'00'"));
						connection->execute(_S("update os_avatars set signature=x'00'"));
						connection->execute(_S("update os_calendar_events set signature=x'00'"));
						connection->execute(_S("update os_files set signature=x'00'"));
						connection->execute(_S("update os_instances set signature=x'00'"));
						connection->execute(_S("update os_models set signature=x'00'"));
						connection->execute(_S("update os_polls set signature=x'00'"));
						connection->execute(_S("update os_polls_options set signature=x'00'"));
						connection->execute(_S("update os_polls_votes set signature=x'00'"));
						connection->execute(_S("update os_posts set signature=x'00'"));
						connection->execute(_S("update os_private_messages set signature=x'00'"));
						connection->execute(_S("update os_reputations set signature=x'00'"));
						connection->execute(_S("update os_sections set signature=x'00'"));
						connection->execute(_S("update os_tags set signature=x'00'"));
						connection->execute(_S("update os_texts set signature=x'00'"));
						connection->execute(_S("update os_users set signature=x'00'"));
						connection->execute(_S("update os_votes set signature=x'00'"));
#endif
						

						// identificarli:
						// select * from os_users where signature=x'00'		

						
					}
					

					connection->close();

					if(userID.empty() == false)
					{					
						String newPortalIDKey = oldID.substr(8);
						PortalID newPortalID = CryptManager:: instance()->SHA(newPortalIDKey.buffer(), newPortalIDKey.buffer_size()).toHex();

						String newPovID = Portal::generatePovID(newPortalID, povID);
						String newPath = utils::standardisePath(folder + newPovID);

						options->setPortalID(newPortalID);
						options->setPovID(povID);
						options->setDatabaseName(newPovID);

						FileSystem::instance()->ensureDirectory(newPath);
					
						options->writeToPath(newPath);

						String sqlitePluginID = _S("532E9E0A68EB22E08240965CFA9366DFA6A26A62");
						String databaseRootPath = utils::makeFolderPath(Options::instance()->getDataPath(), utils::makeFolderPath(OS_STORE_PATH, sqlitePluginID));
						String oldDatabasePath = utils::makeFilePath(databaseRootPath,oldID) + _S(".db");
						String newDatabasePath = utils::makeFilePath(databaseRootPath,newPovID) + _S(".db");
		
						FileSystem::instance()->copyFile(oldDatabasePath, newDatabasePath);						

						FileSystem::instance()->remove(oldDatabasePath);
						FileSystem::instance()->removeDirectory(oldPath);
					}
					else
					{
						OS_LOG_ERROR("Unable to migrate portal '" + portalName + "', ID:" + oldID + ". Contact our forum, thanks.");
					}

				}			
			}
		}
	}
	catch(std::exception &e)
	{
		OS_LOG_ERROR(e.what());
		return false;
	}
	catch(...)
	{
		OS_LOG_ERROR(_S("Unknown error during Razor upgrade.'"));
		return false;
	}

	return true;
}