r46724 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r46723‎ | r46724 | r46725 >
Date:19:14, 2 February 2009
Author:dale
Status:deferred
Tags:
Comment:
added simple url cache for external data extension
Modified paths:
  • /trunk/extensions/ExternalData/ED_ParserFunctions.php (modified) (history)
  • /trunk/extensions/ExternalData/ExternalData.sql (added) (history)
  • /trunk/extensions/ExternalData/README (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalData/ED_ParserFunctions.php
@@ -10,7 +10,8 @@
1111 * @author Yaron Koren
1212 */
1313 class EDParserFunctions {
14 -
 14+ //how many times to try an http request:
 15+ private $http_number_of_tries=3;
1516 // XML-handling functions based on code found at
1617 // http://us.php.net/xml_set_element_handler
1718 static function startElement( $parser, $name, $attrs ) {
@@ -92,7 +93,9 @@
9394 $params = func_get_args();
9495 array_shift( $params ); // we already know the $parser ...
9596 $url = array_shift( $params );
96 - $url_contents = file_get_contents( $url );
 97+
 98+ $url_contents = EDParserFunctions::doRequest( $url );
 99+
97100 $format = array_shift( $params );
98101 $external_values = array();
99102 if ($format == 'xml') {
@@ -126,4 +129,38 @@
127130 else
128131 return '';
129132 }
 133+
 134+ static function doRequest( $url, $post_vars = array(), $get_fresh=false, $try_count=1 ) {
 135+ $dbr = wfGetDB( DB_SLAVE );
 136+ //do any special variable replace (right now just sunlight api key)
 137+ global $mvSunlightAPIKey;
 138+ $url = str_replace('$$SLAPIKEY', $mvSunlightAPIKey, $url );
 139+
 140+ // check the cache (only the first 254 chars of the url)
 141+ $res = $dbr->select( 'mv_url_cache', '*', array( 'url' => substr($url,0,254) ), 'EDParserFunctions::doRequest' );
 142+ // @@todo check date
 143+ if ( $res->numRows() == 0 || $get_fresh) {
 144+ //echo "do web request: " . $url . "\n";
 145+ $page = @file_get_contents( $url );
 146+ if ( $page === false ) {
 147+ //echo( "error getting url retrying (".$try_count." of $this->http_number_of_tries)" );
 148+ sleep( 1 );
 149+ if( $try_count >= $this->http_number_of_tries ){
 150+ echo "could not get url after $this->http_number_of_tries \n\n";
 151+ return '';
 152+ }
 153+ $try_count++;
 154+ return $this->doRequest( $url, $post_vars, $get_fresh, $try_count );
 155+ }
 156+ if ( $page != '' ) {
 157+ $dbw = wfGetDB( DB_MASTER );
 158+ // insert back into the db:
 159+ $dbw->insert( 'mv_url_cache', array( 'url' => substr($url,0,254), 'result' => $page, 'req_time' => time() ) );
 160+ return $page;
 161+ }
 162+ } else {
 163+ $row = $dbr->fetchObject( $res );
 164+ return $row->result;
 165+ }
 166+ }
130167 }
Index: trunk/extensions/ExternalData/ExternalData.sql
@@ -0,0 +1,9 @@
 2+CREATE TABLE IF NOT EXISTS `mv_url_cache` (
 3+ `id` int(10) unsigned NOT NULL auto_increment,
 4+ `url` varchar(255) NOT NULL,
 5+ `post_vars` text,
 6+ `req_time` int(11) NOT NULL,
 7+ `result` longtext character set utf8 collate utf8_unicode_ci,
 8+ UNIQUE KEY `id` (`id`),
 9+ KEY `url` (`url`)
 10+) ENGINE=MyISAM DEFAULT CHARSET=latin1;
\ No newline at end of file
Index: trunk/extensions/ExternalData/README
@@ -2,6 +2,7 @@
33
44 Version 0.2
55 Yaron Koren
 6+ Michael Dale (quick add of url cache)
67
78 This is free software licensed under the GNU General Public License. Please
89 see http://www.gnu.org/copyleft/gpl.html for further details, including the

Status & tagging log