+
+ # Returns the text associated with a "link" type old table row
+ /* static */ function followLink( $link ) {
+ # Split the link into fields and values
+ $lines = explode( '\n', $link );
+ $hash = '';
+ $locations = array();
+ foreach ( $lines as $line ) {
+ # Comments
+ if ( $line{0} == '#' ) {
+ continue;
+ }
+ # Field/value pairs
+ if ( preg_match( '/^(.*?)\s*:\s*(.*)$/', $line, $matches ) ) {
+ $field = strtolower($matches[1]);
+ $value = $matches[2];
+ if ( $field == 'hash' ) {
+ $hash = $value;
+ } elseif ( $field == 'location' ) {
+ $locations[] = $value;
+ }
+ }
+ }
+
+ if ( $hash === '' ) {
+ return false;
+ }
+
+ # Look in each specified location for the text
+ $text = false;
+ foreach ( $locations as $location ) {
+ $text = Article::fetchFromLocation( $location, $hash );
+ if ( $text !== false ) {
+ break;
+ }
+ }
+
+ return $text;
+ }
+
+ /* static */ function fetchFromLocation( $location, $hash ) {
+ global $wgLoadBalancer;
+ $fname = 'fetchFromLocation';
+ wfProfileIn( $fname );
+
+ $p = strpos( $location, ':' );
+ if ( $p === false ) {
+ wfProfileOut( $fname );
+ return false;
+ }
+
+ $type = substr( $location, 0, $p );
+ $text = false;
+ switch ( $type ) {
+ case 'mysql':
+ # MySQL locations are specified by mysql://<machineID>/<dbname>/<tblname>/<index>
+ # Machine ID 0 is the current connection
+ if ( preg_match( '/^mysql:\/\/(\d+)\/([A-Za-z_]+)\/([A-Za-z_]+)\/([A-Za-z_]+)$/',
+ $location, $matches ) ) {
+ $machineID = $matches[1];
+ $dbName = $matches[2];
+ $tblName = $matches[3];
+ $index = $matches[4];
+ if ( $machineID == 0 ) {
+ # Current connection
+ $db =& wfGetDB( DB_SLAVE );
+ } else {
+ # Alternate connection
+ $db =& $wgLoadBalancer->getConnection( $machineID );
+
+ if ( array_key_exists( $machineId, $wgKnownMysqlServers ) ) {
+ # Try to open, return false on failure
+ $params = $wgKnownDBServers[$machineId];
+ $db = Database::newFromParams( $params['server'], $params['user'], $params['password'],
+ $dbName, 1, DBO_IGNORE );
+ }
+ }
+ if ( $db->isOpen() ) {
+ $index = $db->strencode( $index );
+ $res = $db->query( "SELECT blob_data FROM $dbName.$tblName WHERE blob_index='$index'", $fname );
+ $row = $db->fetchObject( $res );
+ $text = $row->text_data;
+ }
+ }
+ break;
+ case 'file':
+ # File locations are of the form file://<filename>, relative to the current directory
+ if ( preg_match( '/^file:\/\/(.*)$', $location, $matches ) )
+ $filename = strstr( $location, 'file://' );
+ $text = @file_get_contents( $matches[1] );
+ }
+ if ( $text !== false ) {
+ # Got text, now we need to interpret it
+ # The first line contains information about how to do this
+ $p = strpos( $text, '\n' );
+ $type = substr( $text, 0, $p );
+ $text = substr( $text, $p + 1 );
+ switch ( $type ) {
+ case 'plain':
+ break;
+ case 'gzip':
+ $text = gzinflate( $text );
+ break;
+ case 'object':
+ $object = unserialize( $text );
+ $text = $object->getItem( $hash );
+ break;
+ default:
+ $text = false;
+ }
+ }
+ wfProfileOut( $fname );
+ return $text;
+ }
+