Index: trunk/extensions/MessageCommons/MessageCommons.php |
— | — | @@ -0,0 +1,103 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * MessageCommons |
| 5 | + * @package MessageCommons |
| 6 | + * @author Daniel Friesen (http://mediawiki.org/wiki/User:Dantman) <mediawiki@danielfriesen.name> |
| 7 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 8 | + * @note Some techniques borrowed from Wikia's SharedMessages system |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or |
| 11 | + * modify it under the terms of the GNU General Public License |
| 12 | + * as published by the Free Software Foundation; either version 2 |
| 13 | + * of the License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | + */ |
| 24 | + |
| 25 | +if( !defined( 'MEDIAWIKI' ) ) die( "This is an extension to the MediaWiki package and cannot be run standalone." ); |
| 26 | + |
| 27 | +$wgExtensionFunctions[] = 'efMessageCommonsSetup'; |
| 28 | +$wgHooks['MessagesPreLoad'][] = 'efMessageCommonsPreload'; |
| 29 | +$wgExtensionCredits['other'][] = array ( |
| 30 | + 'name' => 'MessageCommons', |
| 31 | + 'url' => 'http://mediawiki.org/wiki/Extension:MessageCommons', |
| 32 | + 'version' => '1.0a', |
| 33 | + 'author' => "[http://mediawiki.org/wiki/User:Dantman Daniel Friesen] [mailto:Daniel%20Friesen%20%3Cmediawiki@danielfriesen.name%3E <mediawiki@danielfriesen.name>]", |
| 34 | + 'description' => "Allows mediawiki messages to be shared from a central database" |
| 35 | +); |
| 36 | + |
| 37 | +function efMessageCommonsSetup() { |
| 38 | + global $wgDBprefix, $wgSharedDB, $wgSharedPrefix, $wgSharedUploadDBname, $wgSharedUploadDBprefix; |
| 39 | + global $egMessageCommonsDatabase, $egMessageCommonsPrefix; |
| 40 | + |
| 41 | + if( !isset($egMessageCommonsDatabase) ) { |
| 42 | + if( !isset($wgSharedUploadDBname) ) { |
| 43 | + $egMessageCommonsDatabase = $wgSharedUploadDBname; |
| 44 | + $egMessageCommonsPrefix = $wgSharedUploadDBprefix; |
| 45 | + } elseif( !isset($wgSharedDB) ) { |
| 46 | + $egMessageCommonsDatabase = $wgSharedDB; |
| 47 | + if( isset($wgSharedPrefix) ) { |
| 48 | + $egMessageCommonsPrefix = $wgSharedPrefix; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + if( !isset($egMessageCommonsPrefix) ) { |
| 53 | + $egMessageCommonsPrefix = $wgDBprefix; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function efMessageCommonsPreload( $title, &$message ) { |
| 58 | + global $wgContLang, $wgLang, $egMessageCommonsIsCommons; |
| 59 | + if( $egMessageCommonsIsCommons ) return true; |
| 60 | + |
| 61 | + $text = null; |
| 62 | + |
| 63 | + $msgNames[] = $msgName = $title; |
| 64 | + if( strpos( $msgName, '/' ) !== false ) { |
| 65 | + $msgNames[] = sprintf( '%s/%s', $msgName, $wgLang->getCode() ); |
| 66 | + $msgNames[] = sprintf( '%s/%s', $msgName, $wgContLang->getCode() ); |
| 67 | + } |
| 68 | + $msgNames = array_unique($msgNames); |
| 69 | + |
| 70 | + foreach( $msgNames as $msgName ) { |
| 71 | + $text = efMessageCommonsGetMsg( $msgName ); |
| 72 | + if( isset($text) ) break; |
| 73 | + } |
| 74 | + |
| 75 | + if( isset($text) ) { |
| 76 | + $message = $text; |
| 77 | + return false; |
| 78 | + } |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +function efMessageCommonsGetMsg( $msg ) { |
| 83 | + global $egMessageCommonsDatabase, $egMessageCommonsPrefix ; |
| 84 | + $title = Title::makeTitle(NS_MEDIAWIKI, $msg ); |
| 85 | + $dbr =& wfGetDB( DB_SLAVE ); |
| 86 | + $row = $dbr->selectRow( array( |
| 87 | + "`{$egMessageCommonsDatabase}`.`{$egMessageCommonsPrefix}page`", |
| 88 | + "`{$egMessageCommonsDatabase}`.`{$egMessageCommonsPrefix}revision`", |
| 89 | + "`{$egMessageCommonsDatabase}`.`{$egMessageCommonsPrefix}text`" |
| 90 | + ), array('*'), array( |
| 91 | + 'page_namespace' => $title->getNamespace(), |
| 92 | + 'page_title' => $title->getDBkey(), |
| 93 | + 'page_latest = rev_id', |
| 94 | + 'old_id = rev_text_id' |
| 95 | + ) ); |
| 96 | + if( !$row ) return null; |
| 97 | + return Revision::getRevisionText($row); |
| 98 | +} |
| 99 | + |
| 100 | +/** MessageCommons configuration **/ |
| 101 | +$egMessageCommonsIsCommons = false; |
| 102 | +$egMessageCommonsLang = 'en'; |
| 103 | +$egMessageCommonsDatabase = null; |
| 104 | +$egMessageCommonsPrefix = null; |