r48877 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48876‎ | r48877 | r48878 >
Date:13:31, 26 March 2009
Author:werdna
Status:ok (Comments)
Tags:
Comment:
Move WikiMap class from CentralAuth to core, since it's used in 2 extensions at least now, and doesn't depend on CentralAuth
Modified paths:
  • /trunk/extensions/CentralAuth/CentralAuth.php (modified) (history)
  • /trunk/extensions/CentralAuth/WikiMap.php (deleted) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/WikiMap.php (added) (history)

Diff [purge]

Index: trunk/phase3/includes/AutoLoader.php
@@ -209,6 +209,8 @@
210210 'WikiError' => 'includes/WikiError.php',
211211 'WikiErrorMsg' => 'includes/WikiError.php',
212212 'WikiExporter' => 'includes/Export.php',
 213+ 'WikiMap' => 'includes/WikiMap.php',
 214+ 'WikiReference' => 'includes/WikiReference.php',
213215 'WikiXmlError' => 'includes/WikiError.php',
214216 'XCacheBagOStuff' => 'includes/BagOStuff.php',
215217 'XmlDumpWriter' => 'includes/Export.php',
Index: trunk/phase3/includes/WikiMap.php
@@ -0,0 +1,112 @@
 2+<?php
 3+
 4+/**
 5+ * Helper tools for dealing with other locally-hosted wikis
 6+ */
 7+
 8+class WikiMap {
 9+ static function getWiki( $wikiID ) {
 10+ global $wgConf, $IP;
 11+ static $initialiseSettingsDone = false;
 12+
 13+ // This is a damn dirty hack
 14+ if ( !$initialiseSettingsDone ) {
 15+ $initialiseSettingsDone = true;
 16+ if( file_exists( "$IP/InitialiseSettings.php" ) ) {
 17+ require_once "$IP/InitialiseSettings.php";
 18+ }
 19+ }
 20+
 21+ list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
 22+ if( isset( $major ) ) {
 23+ $server = $wgConf->get( 'wgServer', $wikiID, $major,
 24+ array( 'lang' => $minor, 'site' => $major ) );
 25+ $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
 26+ array( 'lang' => $minor, 'site' => $major ) );
 27+ return new WikiReference( $major, $minor, $server, $path );
 28+ } else {
 29+ return null;
 30+ }
 31+ }
 32+
 33+ // Convenience functions from GlobalBlocking
 34+ static function getWikiName( $wiki_id ) {
 35+ // We can give more info than just the wiki id!
 36+ $wiki = WikiMap::getWiki( $wiki_id );
 37+
 38+ if ($wiki) {
 39+ return $wiki->getDisplayName();
 40+ }
 41+ return $wiki_id;
 42+ }
 43+
 44+ static function foreignUserLink( $wiki_id, $user ) {
 45+ return self::makeForeignLink( $wiki_id, "User:$user" );
 46+ }
 47+
 48+ static function makeForeignLink( $wiki_id, $page, $text=null ) {
 49+ global $wgUser;
 50+ $sk = $wgUser->getSkin();
 51+
 52+ if (!$text)
 53+ $text=$page;
 54+
 55+ return $sk->makeExternalLink( self::getForeignURL( $wiki_id, $page ) , $text );
 56+ }
 57+
 58+ static function getForeignURL( $wiki_id, $page ) {
 59+ $wiki = WikiMap::getWiki( $wiki_id );
 60+
 61+ if ($wiki)
 62+ return $wiki->getUrl( $page );
 63+
 64+ return false;
 65+ }
 66+}
 67+
 68+class WikiReference {
 69+ private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
 70+ private $mMajor; ///< 'wiki', 'wiktionary', etc
 71+ private $mServer; ///< server override, 'www.mediawiki.org'
 72+ private $mPath; ///< path override, '/wiki/$1'
 73+
 74+ function __construct( $major, $minor, $server, $path ) {
 75+ $this->mMajor = $major;
 76+ $this->mMinor = $minor;
 77+ $this->mServer = $server;
 78+ $this->mPath = $path;
 79+ }
 80+
 81+ function getHostname() {
 82+ $prefixes = array( 'http://', 'https://' );
 83+ foreach ( $prefixes as $prefix ) {
 84+ if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) {
 85+ return substr( $this->mServer, strlen( $prefix ) );
 86+ }
 87+ }
 88+ throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
 89+ }
 90+
 91+ /**
 92+ * pretty it up
 93+ */
 94+ function getDisplayName() {
 95+ $url = $this->getUrl( '' );
 96+ $url = preg_replace( '!^https?://!', '', $url );
 97+ $url = preg_replace( '!/index\.php(\?title=|/)$!', '/', $url );
 98+ $url = preg_replace( '!/wiki/$!', '/', $url );
 99+ $url = preg_replace( '!/$!', '', $url );
 100+ return $url;
 101+ }
 102+
 103+ private function getLocalUrl( $page ) {
 104+ // FIXME: this may be generalized...
 105+ return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
 106+ }
 107+
 108+ function getUrl( $page ) {
 109+ return
 110+ $this->mServer .
 111+ $this->getLocalUrl( $page );
 112+ }
 113+}
Property changes on: trunk/phase3/includes/WikiMap.php
___________________________________________________________________
Name: svn:eol-style
1114 + native
Index: trunk/extensions/CentralAuth/WikiMap.php
@@ -1,79 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Helper tools for dealing with other locally-hosted wikis
6 - */
7 -
8 -class WikiMap {
9 - static function getWiki( $wikiID ) {
10 - global $wgConf, $IP;
11 - static $initialiseSettingsDone = false;
12 -
13 - // This is a damn dirty hack
14 - if ( !$initialiseSettingsDone ) {
15 - $initialiseSettingsDone = true;
16 - if( file_exists( "$IP/InitialiseSettings.php" ) ) {
17 - require_once "$IP/InitialiseSettings.php";
18 - }
19 - }
20 -
21 - list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
22 - if( isset( $major ) ) {
23 - $server = $wgConf->get( 'wgServer', $wikiID, $major,
24 - array( 'lang' => $minor, 'site' => $major ) );
25 - $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
26 - array( 'lang' => $minor, 'site' => $major ) );
27 - return new WikiReference( $major, $minor, $server, $path );
28 - } else {
29 - return null;
30 - }
31 -
32 - }
33 -}
34 -
35 -class WikiReference {
36 - private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
37 - private $mMajor; ///< 'wiki', 'wiktionary', etc
38 - private $mServer; ///< server override, 'www.mediawiki.org'
39 - private $mPath; ///< path override, '/wiki/$1'
40 -
41 - function __construct( $major, $minor, $server, $path ) {
42 - $this->mMajor = $major;
43 - $this->mMinor = $minor;
44 - $this->mServer = $server;
45 - $this->mPath = $path;
46 - }
47 -
48 - function getHostname() {
49 - $prefixes = array( 'http://', 'https://' );
50 - foreach ( $prefixes as $prefix ) {
51 - if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) {
52 - return substr( $this->mServer, strlen( $prefix ) );
53 - }
54 - }
55 - throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
56 - }
57 -
58 - /**
59 - * pretty it up
60 - */
61 - function getDisplayName() {
62 - $url = $this->getUrl( '' );
63 - $url = preg_replace( '!^https?://!', '', $url );
64 - $url = preg_replace( '!/index\.php(\?title=|/)$!', '/', $url );
65 - $url = preg_replace( '!/wiki/$!', '/', $url );
66 - $url = preg_replace( '!/$!', '', $url );
67 - return $url;
68 - }
69 -
70 - private function getLocalUrl( $page ) {
71 - // FIXME: this may be generalized...
72 - return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
73 - }
74 -
75 - function getUrl( $page ) {
76 - return
77 - $this->mServer .
78 - $this->getLocalUrl( $page );
79 - }
80 -}
Index: trunk/extensions/CentralAuth/CentralAuth.php
@@ -138,8 +138,6 @@
139139 $wgAutoloadClasses['CentralAuthUser'] = "$caBase/CentralAuthUser.php";
140140 $wgAutoloadClasses['CentralAuthPlugin'] = "$caBase/CentralAuthPlugin.php";
141141 $wgAutoloadClasses['CentralAuthHooks'] = "$caBase/CentralAuthHooks.php";
142 -$wgAutoloadClasses['WikiMap'] = "$caBase/WikiMap.php";
143 -$wgAutoloadClasses['WikiReference'] = "$caBase/WikiMap.php";
144142 $wgAutoloadClasses['WikiSet'] = "$caBase/WikiSet.php";
145143 $wgAutoloadClasses['SpecialAutoLogin'] = "$caBase/SpecialAutoLogin.php";
146144 $wgAutoloadClasses['CentralAuthUserArray'] = "$caBase/CentralAuthUserArray.php";

Comments

#Comment by Tim Starling (talk | contribs)   03:18, 15 May 2009

Would it be possible to add a suitable abstraction layer, so that we can get rid of the Wikimedia-specific hacks and make this reasonably general, some time before 1.16?

#Comment by Werdna (talk | contribs)   09:00, 15 May 2009
+		// This is a damn dirty hack
+		if ( !$initialiseSettingsDone ) {
+			$initialiseSettingsDone = true;
+			if( file_exists( "$IP/InitialiseSettings.php" ) ) {
+				require_once "$IP/InitialiseSettings.php";
+			}
+		}

I assume that's what you mean. In that case, it's probably sensible to have a "loadFullSettings" or better-named member of the $wgConf object, which could hold a callback and be given a value when this behaviour is required.

#Comment by Tim Starling (talk | contribs)   07:26, 19 May 2009

That, and getDisplayName, and also SiteConfiguration generally. The language/site (major/minor) split is Wikimedia-specific, and the way that DB names consist of languages and sites concatenated in the wrong order with no delimiter, is a Wikimedia b/c hack. I'd like to see a SiteConfiguration subclass for Wikimedia. Also I think SiteConfiguration should be a factory for these WikiReference objects instead of them being created by static functions.

Status & tagging log