Automatically refactor legacy mysql functions to mysqli (PHP5 to PHP7)


I have to migrate a old PHP5 Project to a new Server running PHP7.
All mysql_* functions has been marked deprecated in PHP 5.5 and been finally removed in PHP 7.

So I have to refactor a large legacy code base.

Fortunately there is already a litte tool, which is unbelievably helpful for this task.
I just have to run these three lines.
git clone https://github.com/philip/MySQLConverterTool.git
cd MySQLConverterTool/
php cli.php -u -d "<pathToYourFiles>" -p "*.php"

Before:
<?php
$connect = @mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connect) {
die('Page not available');
}
mysql_select_db($dbname) or die(mysql_error());
mysql_set_charset("utf8");
?>

After:
<?php
$connect = @($GLOBALS["___mysqli_ston"] = mysqli_connect($dbhost, $dbuser, $dbpass));
if (!$connect) {
die('Page not available');
}
mysqli_select_db($GLOBALS["___mysqli_ston"], $dbname) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
((bool)mysqli_set_charset($GLOBALS["___mysqli_ston"], "utf8"));
?>

As you can see there are a problem with the @ before the mysql_connect so you have to check these code locations manually.

All other files in this project where converted without any problems, I was surprised by myself 🙂


Use apt-get to install Java in a Debian Jessie


The “WebUpd8” team fortunately take care of some Packages that will make your life a little bit easier. In this case the Oracle Java (JDK) Installer!

First we need to add the WebUpd8 repository to our /etc/apt/sources.list

echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" >> /etc/apt/sources.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" >> /etc/apt/sources.list

Then we get the Keys:
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886

Lets update the new repositories
apt-get update

And thats it, now we can install Java with one simple apt-get
apt-get install oracle-java8-installer

Problems with the JAVA_HOME enviroment?
Take a look at http://stackoverflow.com/questions/29269829/webupd8-java-home-not-set-after-installing-oracle-java8-set-default


Use Maven to add JDBC-Driver and Datasource to Wildfly



<plugin>
	<groupId>org.wildfly.plugins</groupId>
	<artifactId>wildfly-maven-plugin</artifactId>
	<version>1.1.0.Alpha8</version>
	<executions>
		<execution>
			<id>deploy-postgresql</id>
			<phase>package</phase>
			<goals>
				<goal>deploy-artifact</goal>
			</goals>
			<configuration>
				<force>true</force>
				<groupId>org.postgresql</groupId>
				<artifactId>postgresql</artifactId>
				<name>postgresql-9.4-1206-jdbc42.jar</name>
			</configuration>
		</execution>
		<execution>
			<id>add-datasource</id>
			<phase>install</phase>
			<goals>
				<goal>add-resource</goal>
			</goals>
			<configuration>
				<skip>true</skip>
				<force>false</force>
				<address>subsystem=datasources</address>
				<resources>
					<resource>
						<address>xa-data-source=java:/myDS</address>
						<properties>
							<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
							<jndi-name>java:/myDS</jndi-name>
							<enabled>true</enabled>
							<driver-name>postgresql-9.4-1206-jdbc42.jar</driver-name>
						</properties>
						<resources>
							<resource>
								<address>xa-datasource-properties=DatabaseName</address>
								<properties>
									<value>myDatabase</value>
								</properties>
							</resource>
							<resource>
								<address>xa-datasource-properties=ServerName</address>
								<properties>
									<value>myLocalhost</value>
								</properties>
							</resource>
							<resource>
								<address>xa-datasource-properties=User</address>
								<properties>
									<value>myUser</value>
								</properties>
							</resource>
							<resource>
								<address>xa-datasource-properties=Password</address>
								<properties>
									<value>myPassword</value>
								</properties>
							</resource>
						</resources>
					</resource>
				</resources>
			</configuration>
		</execution>
	</executions>
</plugin>

Use Maven to unpack static resources from webjar for JS-Testing



<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>unpack</id>
			<phase>generate-test-sources</phase>
			<goals>
				<goal>unpack</goal>
			</goals>
			<configuration>
				<artifactItems>
					<artifactItem>
						<groupId>org.webjars</groupId>
						<artifactId>openlayers</artifactId>
						<version>${openlayers.version}</version>									
						<type>jar</type>
						<includes>META-INF/resources/webjars/**/*</includes>
						<outputDirectory>${project.basedir}/src/main/webapp/webjars</outputDirectory>
					</artifactItem>
				</artifactItems>
			</configuration>
		</execution>
	</executions>
</plugin>			
<plugin>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.8</version>
	<executions>
		<execution>
			<phase>process-test-resources</phase>
			<goals>
				<goal>run</goal>
			</goals>						
			<configuration>
				<target>
					<copy todir="${project.basedir}/src/main/webapp/webjars">
						<fileset dir="${project.basedir}/src/main/webapp/webjars/META-INF/resources/webjars" includes="**/*" />
					</copy>
					<!-- TODO: Delete Marker in target\dependency-maven-plugin-markers -->
					<!-- <delete includeemptydirs="true">
						<fileset dir="${project.basedir}/src/main/webapp/webjars/META-INF" includes="**/*"/>
					</delete> -->
				</target>
			</configuration>
		</execution>
	</executions>
</plugin>