Understanding Effective Data Portability and Data Serialization in WordPress

by in WordPress on 21st Nov 2012 · 0 Comment

WordPress is a popular blogging software package that is compatible with websites running on Linux servers. WordPress is free to use and features a user friendly interface. In addition to these two qualities, WordPress boasts many powerful and cutting edge features for the advanced user to capitalize on.

Understanding Effective Data Portability and Data Serialization in WordPress

For example, WordPress can be used to make data portable using a technique called data serialization. This is very important should a website ever need to be migrated to another server.

Effective Data Portability and Data Serialization in WordPress

Data serialization converts data into a format that is easily stored. WordPress stores blog entries in an SQL database, which tends to be large and unsuitable for migration. In order to prepare the database for migration, WordPress supplies a few built-in functions that serialize the data into a slimmer and more compact form. Once serialized, the data from a WordPress blog can be efficiently transmitted to a new server error free.

WordPress Data Migration With Serialization Fundamentals

WordPress is built using the PHP scripting language. As such, the built in serialize functions that WordPress provides make use of the PHP syntax. PHP has its own serialize functions, but it is recommended to use the WordPress versions, as they are custom tailored to the blog database system.

The WordPress serialize function takes SQL database entries and converts them into a single compact string object. This string object contains all of the column and row information of the database, as well as the contents of any occupied database cells. Since unoccupied database cells are not included in the output string, the final result is a more compact copy of the entire database.

This string can be saved to disk, transferred over the network, or archived for later retrieval. Serializing data is an important step when it comes time to back up the data in a blog, or transmit it to a new host. Once serialized, the string can be reverted to a database using another built-in function. This automatically populates the database on the new webserver with a carbon copy of the original data.

The data migration process for WordPress therefore consists of three basic steps: Serialize the data contained in the SQL database, transmit the resultant string to the new webhost, and populate the new database with the serialized data.

Since WordPress provides custom functions for the serialization and restoration procedures, all that is left up to the administrator is the transference of the string. This can be accomplished using a simple network file transfer application, such as FTP.

WordPress "maybe_serialize" Function Example

The function that WordPress provides for serializing data is named maybe_serialize. This functions takes a single parameter as an argument. This parameter contains the data that is to be serialized. As the name suggest, this function does not always serialize the input data. Instead, it checks to see if the data is suitable for serialization.

If the data is suitable for serialization, it may be serialized. Otherwise, the function does not modify the input data. To prevent nested serialization, it is best to check the input to verify that it is suitable for serialization before proceeding with the maybe_serialize function. This can be accomplished using the is_serialized function.

This function simply checks the input data and determines if it has already been serialized. If it has been, then the function returns true. Otherwise, it returns false. By placing this function as the predicate in a simple if statement, the maybe_serialize function may be used safely.

Therefore, the best practice when it comes to serializing data is to call maybe_serialize on data that has been verified as being un-serialized by the is_serialized function. This can be accomplished using the following code:

<?php
if(!is_serialized( $databaseContents ))
{
$serializedData = maybe_serialize($databaseContents);
}
?>

In the above code block, the input data is represented by the variable $databaseContents. The function is_serialized checks to see whether or not this data is serialized. If it is not serialized, the body of the if statement is processed. Otherwise, no work is performed.

If the data contained in $databaseContents is not already serialized, then the function maybe_serialize processes the data and converts it into a compact string. This string is then stored in the variable $serializedData.

Tools For Transferring Serialized Data

Once the data has been serialized, it may be transferred to the new web server. There are many tools available for performing this step. The most straightforward method is to use the Linux utility FTP. This ubiquitous utility comes bundled with all Linux Operating Systems. It can be accessed using the system terminal.

In order to establish a network connection with a destination server located at remote.server.address, the following command may be entered from the terminal prompt:

ftp remote.server.address

Once entered, this command will prompt the user for a username and password. Once these are successfully entered, the command prompt changes to this:

ftp>

Commands may now be issued to the FTP server. To place the serialized data on the remote server, the put command may be issued. For example, to upload the serialized data contained in a file named serializedData, the following command may be issued:

ftp> put serializedData

A network transmission then occurs, which uploads the data file to the remote server. Once the upload is finished, the serialized data may be used to restore the database in the remote server.

WordPress "maybe_unserialize" Function Example

Once the data has been serialized, it can be stored for backup or transmittal to a new web server. Once the database on the new server is ready to receive the serialized data, the function maybe_unserialize may be used to populate the SQL database. This function accepts any string as an input parameter.

To ensure that no errors occur during the processing of this function, it is wise to first verify that this string contains serialized data. The is_serialized function serves as an effective tool for this verification process.

Once the string has been verified as containing serialized data, the maybe_unserialized function can be safely called. After it has finished processing the input string, the maybe_unserialize function returns a variable containing the expanded data.

As with the maybe_serialize function, the best practice is to nest the maybe_unserialize function in an if statement, as shown below:

<?php
if(is_serialized( $serializedData))
{
$databaseContents = maybe_unserialize($serializedData);
}
?>

In the above example, WordPress will only decompress a string if the is_serialized function returns true, which will only occur if the string contains serialized data. This will prevent the database from receiving corrupt, garbage information.

Advantages of Data Serialization

Data serialization offers many advantages. As more entries are added to a blog, its database must necessarily grow. Databases grow in proportion to the data they contain, but they are often larger than the sum of the contained data. This is a design feature of databases, which can provide random access to any data member it contains. This is possible since databases store data in tables.

To access any field in a database, the user merely has to enter the cell coordinates via an SQL query. Databases are optimized for access speed, which provides users with prompt access to data. The trade-off is that databases take up more room than other data structures.

More Resources

Conclusion

Serialization is a common practice that is used to compress the data held within a database. This data becomes optimized for storage space, meaning it is smaller but no longer capable of providing quick access. This data is most suitable for archival and migration purposes only. Serialized data is stored as one long string, with no capabilities for random access.

In order to access a piece of data, the entire string must be parsed in a serial fashion. This is time consuming and best used sparingly. However, since data migration and backup occurs infrequently, data serialization is an effective means for saving and transmitting entire databases with a few simple steps.

Terrance is a versatile web developer and the technical editor at OXP. He enjoys creating functional websites and is particularly engrossed in all the tiny details mixed together to construct great user experiences. He always believe that every web user deserves the best!