r34541 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34540‎ | r34541 | r34542 >
Date:23:36, 9 May 2008
Author:dantman
Status:old
Tags:
Comment:
Fixing up a variety of GlobalFunctions and also improving queries in Titke.php.
* getFullURL and getLocalUrl now accept arrays and objects as valid input.
* Use wfAppendQuery in getLocalUrl to match up with getFullURL.
* wfArrayToCgi is now a alias to wfBuildQuery (Parameters are in the OPPOSITE order so wfBuildQuery takes defaults as second param and wfArrayToCgi takes them as first parameter like it always did)
* New function wfBuildQuery.
** The code moved here from what once was wfArrayToCgi has been changed from a set of plain loops to a wrapper around http_build_query so that complex data is handled correctly.
** We now support strings and objects as input, and we even parse strings when necessary to merge queries.
* New function wfForeignWikiID to pair with wfWikiID like wfForeignMemcKey. The foreign id can take 2 parameters, if omitted it falls back to the shared db, then to the local db if not set.
* wfMemcKey and wfForeignMemcKey now call the respective wf(Foreign)WikiID function, this simplifies the functions, and avoids code duplication making sure things always match even if for some strange reason it's changed.

I'll likely be using the forign functions later to improve the use of a shared interwiki map to avoid redundant caches.

I could always make the local MemcKey and WikiID functions depend on the foreign ones in a sane way
Modified paths:
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/GlobalFunctions.php
@@ -983,25 +983,40 @@
984984 }
985985
986986 /**
987 - * This function takes two arrays as input, and returns a CGI-style string, e.g.
 987+ * This function takes one or two arrays, objects, or strings as input, and returns a CGI-style string, e.g.
988988 * "days=7&limit=100". Options in the first array override options in the second.
989989 * Options set to "" will not be output.
 990+ * @depreciated
990991 */
991 -function wfArrayToCGI( $array1, $array2 = NULL )
992 -{
993 - if ( !is_null( $array2 ) ) {
994 - $array1 = $array1 + $array2;
995 - }
996 -
997 - $cgi = '';
998 - foreach ( $array1 as $key => $value ) {
999 - if ( '' !== $value ) {
1000 - if ( '' != $cgi ) {
1001 - $cgi .= '&';
 992+function wfArrayToCGI( $query1, $query2 = null ) {
 993+ if( is_null($query2) ) wfBuildQuery( $query1 );
 994+ else wfBuildQuery( $query2, $query1 );
 995+}
 996+/**
 997+ * wfBuildQuery is a improved wrapper for http_build_query.
 998+ * We support a defaults array which the query may be merged with. As well we also support
 999+ * arrays, objects, and strings as input.
 1000+ */
 1001+function wfBuildQuery( $query, $defaults = null ) {
 1002+ if( !is_null($defaults) ) {
 1003+ ## If either array is a string, then parse it and make sure to fix magic quotes.
 1004+ foreach( array( 'query', 'defaults' ) as $var ) {
 1005+ if( is_string($$var) ) {
 1006+ $arr = array();
 1007+ parse_str($$var, &$arr);
 1008+ if( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
 1009+ global $wgRequest;
 1010+ $wgRequest->fix_magic_quotes( $arr );
 1011+ }
 1012+ $$var = $arr;
10021013 }
1003 - $cgi .= urlencode( $key ) . '=' . urlencode( $value );
10041014 }
 1015+ # Merge, make sure they are arrays, not objects.
 1016+ $query = ((array)$defaults) + ((array)$query);
10051017 }
 1018+
 1019+ # Note that we must specify & because the default is sometimes &
 1020+ $cgi = is_string($query) ? $query : http_build_query( $query, null, '&' );
10061021 return $cgi;
10071022 }
10081023
@@ -2337,13 +2352,8 @@
23382353 * Get a cache key
23392354 */
23402355 function wfMemcKey( /*... */ ) {
2341 - global $wgDBprefix, $wgDBname;
23422356 $args = func_get_args();
2343 - if ( $wgDBprefix ) {
2344 - $key = "$wgDBname-$wgDBprefix:" . implode( ':', $args );
2345 - } else {
2346 - $key = $wgDBname . ':' . implode( ':', $args );
2347 - }
 2357+ $key = wfWikiID() . ':' . implode( ':', $args );
23482358 return $key;
23492359 }
23502360
@@ -2352,11 +2362,7 @@
23532363 */
23542364 function wfForeignMemcKey( $db, $prefix /*, ... */ ) {
23552365 $args = array_slice( func_get_args(), 2 );
2356 - if ( $prefix ) {
2357 - $key = "$db-$prefix:" . implode( ':', $args );
2358 - } else {
2359 - $key = $db . ':' . implode( ':', $args );
2360 - }
 2366+ wfForeignWikiID($db,$prefix) . ':' . implode( ':', $args );
23612367 return $key;
23622368 }
23632369
@@ -2365,7 +2371,7 @@
23662372 * This is used as a prefix in memcached keys
23672373 */
23682374 function wfWikiID() {
2369 - global $wgDBprefix, $wgDBname;
 2375+ global $wgDBname, wgDBprefix;
23702376 if ( $wgDBprefix ) {
23712377 return "$wgDBname-$wgDBprefix";
23722378 } else {
@@ -2374,6 +2380,22 @@
23752381 }
23762382
23772383 /**
 2384+ * Get an ASCII string identifying a foreign wiki or shared db
 2385+ * This is used as a prefix in foreign memcached keys
 2386+ */
 2387+function wfForeignWikiID( $db = null, $prefix = null ) {
 2388+ global $wgSharedDB, $wgSharedPrefix, $wgDBname, $wgDBprefix;
 2389+ if( !isset($db) ) $db = (isset($wgSharedDB) ? $wgSharedDB, $wgDBname);
 2390+ if( !isset($prefix) ) $prefix = ($wgSharedPrefix ? $wgSharedPrefix, $wgDBprefix);
 2391+
 2392+ if ( $prefix ) {
 2393+ return "$db-$prefix";
 2394+ } else {
 2395+ return $db;
 2396+ }
 2397+}
 2398+
 2399+/**
23782400 * Split a wiki ID into DB name and table prefix
23792401 */
23802402 function wfSplitWikiID( $wiki ) {
Index: trunk/phase3/includes/Title.php
@@ -761,10 +761,12 @@
762762 */
763763 public function getFullURL( $query = '', $variant = false ) {
764764 global $wgContLang, $wgServer, $wgRequest;
765 -
 765+
 766+ $query = wfBuildQuery( $query ); # Support query input other than strings.
 767+
766768 if ( '' == $this->mInterwiki ) {
767769 $url = $this->getLocalUrl( $query, $variant );
768 -
 770+
769771 // Ugly quick hack to avoid duplicate prefixes (bug 4571 etc)
770772 // Correct fix would be to move the prepending elsewhere.
771773 if ($wgRequest->getVal('action') != 'render') {
@@ -772,7 +774,7 @@
773775 }
774776 } else {
775777 $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
776 -
 778+
777779 $namespace = wfUrlencode( $this->getNsText() );
778780 if ( '' != $namespace ) {
779781 # Can this actually happen? Interwikis shouldn't be parsed.
@@ -801,14 +803,16 @@
802804 public function getLocalURL( $query = '', $variant = false ) {
803805 global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
804806 global $wgVariantArticlePath, $wgContLang, $wgUser;
805 -
 807+
 808+ $query = wfBuildQuery( $query ); # Support query input other than strings.
 809+
806810 // internal links should point to same variant as current page (only anonymous users)
807811 if($variant == false && $wgContLang->hasVariants() && !$wgUser->isLoggedIn()){
808812 $pref = $wgContLang->getPreferredVariant(false);
809813 if($pref != $wgContLang->getCode())
810814 $variant = $pref;
811815 }
812 -
 816+
813817 if ( $this->isExternal() ) {
814818 $url = $this->getFullURL();
815819 if ( $query ) {
@@ -844,7 +848,7 @@
845849 $query = $matches[1];
846850 if( isset( $matches[4] ) ) $query .= $matches[4];
847851 $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
848 - if( $query != '' ) $url .= '?' . $query;
 852+ $url = wfAppendQuery( $url, $query );
849853 }
850854 }
851855 if ( $url === false ) {
@@ -854,7 +858,7 @@
855859 $url = "{$wgScript}?title={$dbkey}&{$query}";
856860 }
857861 }
858 -
 862+
859863 // FIXME: this causes breakage in various places when we
860864 // actually expected a local URL and end up with dupe prefixes.
861865 if ($wgRequest->getVal('action') == 'render') {

Follow-up revisions

RevisionCommit summaryAuthorDate
r34542Revert r34541 for the moment pending further review & discussion...brion23:52, 9 May 2008

Status & tagging log