Hi here is just a quick shot. I would like to do some more refactoring if you let me.
I'm using a parser in AS to get a simple object of the xml structure. I'd like to port this code to armory.class.php and parse it directly from the xml output. Right now the xml data is parsed twice. First to make an array. Then to make a simple object out of the array.
There are some more little changes. Locale is not needed anymore...
I think i can do this changes and keep compatible to previous versions.
What do you think?
- Code: Select all
--- C:/Dokumente und Einstellungen/Daniel/Lokale Einstellungen/Temp/armory.class.php-revBASE.svn000.tmp.php Wed Jan 09 21:06:29 2008
+++ D:/xampp/roster.com/htdocs/lib/armory.class.php Wed Jan 09 21:06:10 2008
@@ -56,22 +56,23 @@
}
/**
- * Fetches $item_id Tooltip from the Armory
- * Accepts optional $character if used $realm is also required
- * Returns Array of the parsed XML page
+ * General armory fetch class
+ * Returns XML, HTML or an array of the parsed XML page
*
- * @param int $item_id
- * @param string $locale
+ * @param int $type
* @param string $character
+ * @param string $guild
* @param string $realm
+ * @param int $item_id
+ * @param string $locale
* @param string $fetch_type
* @return array
*/
- function fetchItemTooltip( $item_id, $locale, $character=false, $realm=false, $fetch_type='array' )
+ function _fetchArmory( $type = false, $character = false, $guild = false, $realm = false, $item_id = false, $locale = false, $fetch_type = 'array' )
{
global $roster;
$locale = substr($locale, 0, 2);
- $cache_tag = $item_id.$locale.$character.$realm.$fetch_type;
+ $cache_tag = $type.$character.$guild.$realm.$item_id.$locale.$fetch_type;
if( $roster->cache->check($cache_tag) )
{
@@ -84,7 +85,7 @@
}
else
{
- $url = $this->_makeUrl( 0, $locale, $item_id, $character, $realm );
+ $url = $this->_makeUrl( $type, $locale, $item_id, $character, $realm, $guild );
if( $this->_requestXml($url) )
{
// unparsed fetches
@@ -95,16 +96,16 @@
}
// otherwise parse and return array
$this->xmlParser->Parse($this->xml);
- $item = $this->xmlParser->getParsedData();
- if( !isset($item['page'][0]['child']['errorhtml']) )
+ $data = $this->xmlParser->getParsedData();
+ if( !isset($data['page'][0]['child']['errorhtml']) )
{
- $roster->cache->put($item, $cache_tag);
+ $roster->cache->put($data, $cache_tag);
}
else
{
trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
}
- return $item;
+ return $data;
}
else
{
@@ -117,6 +118,23 @@
/**
* Fetches $item_id Tooltip from the Armory
* Accepts optional $character if used $realm is also required
+ * Returns Array of the parsed XML page
+ *
+ * @param int $item_id
+ * @param string $locale
+ * @param string $character
+ * @param string $realm
+ * @param string $fetch_type
+ * @return array
+ */
+ function fetchItemTooltip( $item_id, $locale, $character=false, $realm=false, $fetch_type='array' )
+ {
+ return $this->_fetchArmory( 0, $character, false, $realm, $item_id, $locale, $fetch_type );
+ }
+
+ /**
+ * Fetches $item_id Tooltip from the Armory
+ * Accepts optional $character if used $realm is also required
* Returns XML string
*
* @param string $item_id
@@ -143,48 +161,7 @@
*/
function fetchItemInfo( $item_id, $locale, $fetch_type='array' )
{
- global $roster;
- $locale = substr($locale, 0, 2);
- $cache_tag = $item_id.$locale.$fetch_type;
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 1, $locale, $item_id );
- if( $this->_requestXml($url) )
- {
- //unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($this->xml, $cache_tag);
- return $this->xml;
- }
- // otherwise parse and return array
- $this->xmlParser->Parse($this->xml);
- $item = $this->xmlParser->getParsedData();
- if( !isset($item['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($item, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $item;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 1, false, false, false, $item_id, $locale, $fetch_type );
}
/**
@@ -228,49 +205,7 @@
*/
function fetchCharacter( $character, $locale, $realm, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = $character.$locale.$realm.$fetch_type;
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 2, $locale, false, $character, $realm );
- if( $this->_requestXml($url) )
- {
- // unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- // else parse and return array
- $this->xmlParser->Parse($this->xml);
- $char = $this->xmlParser->getParsedData();
- if( !isset($guild['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($guild, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $char;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 2, $character, $guild, $realm, false, $locale, $fetch_type );
}
/**
@@ -321,42 +256,7 @@
*/
function fetchGuild( $guild, $locale, $realm, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = $guild.$locale.$realm.$fetch_type;
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 3, $locale, false, false, $realm, $guild );
- if( $this->_requestXml($url) )
- {
- //unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- //else parse and return array
- $this->xmlParser->Parse($this->xml);
- $guild = $this->xmlParser->getParsedData();
- $roster->cache->put($guild, $cache_tag);
- return $guild;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 3, false, $guild, $realm, false, $locale, $fetch_type );
}
/**
@@ -404,49 +304,7 @@
*/
function fetchCharacterTalents( $character, $locale, $realm, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = $character.$locale.$realm.$fetch_type.'talents';
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 4, $locale, false, $character, $realm );
- if( $this->_requestXml($url) )
- {
- //unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- //else parse and return array
- $this->xmlParser->Parse($this->xml);
- $talents = $this->xmlParser->getParsedData();
- if ( !isset($char['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($talents, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $talents;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 4, $character, $guild, $realm, false, $locale, $fetch_type );
}
/**
@@ -493,49 +351,7 @@
*/
function fetchCharacterSkills( $character, $locale, $realm, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = $character.$locale.$realm.$fetch_type.'skills';
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 5, $locale, false, $character, $realm );
- if( $this->_requestXml($url) )
- {
- //unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- // else parse and return array
- $this->xmlParser->Parse($this->xml);
- $char = $this->xmlParser->getParsedData();
- if( !isset($char['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($char, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $char;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 5, $character, $guild, $realm, false, $locale, $fetch_type );
}
/**
@@ -584,49 +400,7 @@
*/
function fetchCharacterReputation( $character, $locale, $realm, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = $character.$locale.$realm.$fetch_type.'reputation';
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 6, $locale, false, $character, $realm );
- if( $this->_requestXml($url) )
- {
- // unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- // else parse and return array
- $this->xmlParser->Parse($this->xml);
- $char = $this->xmlParser->getParsedData();
- if ( ! isset($char['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($char, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $char;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 6, $character, $guild, $realm, false, $locale, $fetch_type );
}
/**
@@ -675,49 +449,7 @@
*/
function fetchCharacterArenaTeams( $character, $locale, $realm, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = $character.$locale.$realm.$fetch_type.'arenateams';
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 7, $locale, false, $character, $realm );
- if( $this->_requestXml($url) )
- {
- // unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- // else parse and return array
- $this->xmlParser->Parse($this->xml);
- $char = $this->xmlParser->getParsedData();
- if( !isset($char['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($char, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $char;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 7, $character, $guild, $realm, false, $locale, $fetch_type );
}
/**
@@ -755,49 +487,7 @@
function fetchStrings( $locale, $fetch_type='array' )
{
- global $roster;
-
- $locale = substr($locale, 0, 2);
- $cache_tag = 'stings'.$locale;
-
- if( $roster->cache->check($cache_tag) )
- {
- if( $this->debug_cachehits )
- {
- trigger_error(__FUNCTION__ . " ::: Cache Hit: [ $cache_tag ]", E_NOTICE);
- }
- return $roster->cache->get($cache_tag);
- }
- else
- {
- $url = $this->_makeUrl( 8, $locale );
- if( $this->_requestXml($url) )
- {
- // unparsed fetches
- if( $fetch_type != 'array' )
- {
- $roster->cache->put($cache_tag);
- return $this->xml;
- }
- // else parse and return array
- $this->xmlParser->Parse($this->xml);
- $strings = $this->xmlParser->getParsedData();
- if( !isset($strings['page'][0]['child']['errorhtml']) )
- {
- $roster->cache->put($strings, $cache_tag);
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url. '. Armory is in maintenance mode');
- }
- return $strings;
- }
- else
- {
- trigger_error('RosterArmory:: Failed to fetch ' . $url);
- return false;
- }
- }
+ return $this->_fetchArmory( 8, false, false, false, false, $locale, $fetch_type );
}
/**