Index: trunk/extensions/LiveTranslate/LiveTranslate.php |
— | — | @@ -45,6 +45,13 @@ |
46 | 46 | |
47 | 47 | $wgExtensionMessagesFiles['LiveTranslate'] = $egLiveTranslateIP . '/LiveTranslate.i18n.php'; |
48 | 48 | |
49 | | -$wgAutoloadClasses['LiveTranslate'] = $egLiveTranslateIP . '/LiveTranslate.hooks.php'; |
| 49 | +$wgAutoloadClasses['LiveTranslateHooks'] = $egLiveTranslateIP . '/LiveTranslate.hooks.php'; |
| 50 | +$wgAutoloadClasses['ApiLiveTranslate'] = $egLiveTranslateIP . '/api/ApiLiveTranslate.php'; |
50 | 51 | |
| 52 | +$wgAPIModules['livetranslate'] = 'ApiLiveTranslate'; |
| 53 | + |
| 54 | +$wgHooks['ArticleViewHeader'][] = 'LiveTranslateHooks::onArticleViewHeader'; |
| 55 | +$wgHooks['LoadExtensionSchemaUpdates'][] = 'LiveTranslateHooks::onSchemaUpdate'; |
| 56 | +$wgHooks['ArticleSaveComplete'][] = 'LiveTranslateHooks::onArticleSaveComplete'; |
| 57 | + |
51 | 58 | require_once 'LiveTranslate_Settings.php'; |
Index: trunk/extensions/LiveTranslate/LiveTranslate_Settings.php |
— | — | @@ -18,3 +18,4 @@ |
19 | 19 | die( 'Not an entry point.' ); |
20 | 20 | } |
21 | 21 | |
| 22 | +$egLiveTranslateDirPage = 'Live Translate Dictionary'; |
Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php |
— | — | @@ -1 +1,141 @@ |
2 | 2 | <?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Static class for hooks handled by the Live Translate extension. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file LiveTranslate.hooks.php |
| 10 | + * @ingroup LiveTranslate |
| 11 | + * |
| 12 | + * @author Jeroen De Dauw |
| 13 | + */ |
| 14 | +final class LiveTranslateHooks { |
| 15 | + |
| 16 | + /** |
| 17 | + * Adds the translation interface to articles. |
| 18 | + * |
| 19 | + * @since 0.1 |
| 20 | + * |
| 21 | + * @param Article &$article |
| 22 | + * @param boolean $outputDone |
| 23 | + * @param boolean $useParserCache |
| 24 | + * |
| 25 | + * @return true |
| 26 | + */ |
| 27 | + public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) { |
| 28 | + global $wgOut; |
| 29 | + |
| 30 | + if ( $article->exists() ) { |
| 31 | + // TODO |
| 32 | + } |
| 33 | + |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Schema update to set up the needed database tables. |
| 39 | + * |
| 40 | + * @since 0.1 |
| 41 | + * |
| 42 | + * @param DatabaseUpdater $updater |
| 43 | + * |
| 44 | + * @return true |
| 45 | + */ |
| 46 | + public static function onSchemaUpdate( DatabaseUpdater $updater ) { |
| 47 | + global $wgDBtype, $egLiveTranslateIP; |
| 48 | + |
| 49 | + if ( $wgDBtype == 'mysql' ) { |
| 50 | + // Set up the current schema. |
| 51 | + $updater->addExtensionUpdate( array( |
| 52 | + 'addTable', |
| 53 | + 'livetranslate', |
| 54 | + $egLiveTranslateIP . '/LiveTranslate.sql', |
| 55 | + true |
| 56 | + ) ); |
| 57 | + } |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Handles edits to the dictionary page to save the translations into the db. |
| 64 | + * |
| 65 | + * @since 0.1 |
| 66 | + * |
| 67 | + * @return true |
| 68 | + */ |
| 69 | + public static function onArticleSaveComplete( &$article, &$user, $text, $summary, |
| 70 | + $minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId, &$redirect ) { |
| 71 | + |
| 72 | + global $egLiveTranslateDirPage; |
| 73 | + |
| 74 | + $title = $article->getTitle(); |
| 75 | + |
| 76 | + if ( $title->getFullText() == $egLiveTranslateDirPage ) { |
| 77 | + self::saveTranslations( self::parseTranslations( $text ) ); |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Parses the provided dictionary content and returns it as an |
| 85 | + * array of associative arrays. |
| 86 | + * |
| 87 | + * @since 0.1 |
| 88 | + * |
| 89 | + * @param string $content |
| 90 | + * |
| 91 | + * @return array |
| 92 | + */ |
| 93 | + protected static function parseTranslations( $content ) { |
| 94 | + $translationSets = array(); |
| 95 | + |
| 96 | + $lines = explode( "\n", $content ); |
| 97 | + $languages = array_map( 'trim', explode( ',', array_shift( $lines ) ) ); |
| 98 | + |
| 99 | + foreach ( $lines as $line ) { |
| 100 | + $values = array_map( 'trim', explode( ',', $line ) ); |
| 101 | + |
| 102 | + $translations = array(); |
| 103 | + |
| 104 | + foreach ( $values as $nr => $value ) { |
| 105 | + if ( array_key_exists( $nr, $languages ) ) { |
| 106 | + $translations[$languages[$nr]] = $value; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $translationSets[] = $translations; |
| 111 | + } |
| 112 | + |
| 113 | + return $translationSets; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Replaces the current translations with the provided ones. |
| 118 | + * |
| 119 | + * @since 0.1 |
| 120 | + * |
| 121 | + * @param array $translationSets |
| 122 | + */ |
| 123 | + protected static function saveTranslations( array $translationSets ) { |
| 124 | + $dbw = wfGetDB( DB_MASTER ); |
| 125 | + |
| 126 | + $dbw->query( 'TRUNCATE TABLE ' . $dbw->tableName( 'live_translate' ) ); |
| 127 | + |
| 128 | + foreach ( $translationSets as $wordId => $translations ) { |
| 129 | + foreach ( $translations as $language => $translation ) { |
| 130 | + $dbw->insert( |
| 131 | + 'live_translate', |
| 132 | + array( |
| 133 | + 'word_id' => $wordId, |
| 134 | + 'word_language' => $language, |
| 135 | + 'word_translation' => $translation |
| 136 | + ) |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} |
\ No newline at end of file |
Index: trunk/extensions/LiveTranslate/api/ApiLiveTranslate.php |
— | — | @@ -0,0 +1,27 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * API module to get special translations stored by the Live Translate extension. |
| 6 | + * |
| 7 | + * @since 0.1 |
| 8 | + * |
| 9 | + * @file ApiLiveTranslate.php |
| 10 | + * @ingroup LiveTranslate |
| 11 | + * |
| 12 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 13 | + */ |
| 14 | +class ApiLiveTranslate extends ApiBase { |
| 15 | + |
| 16 | + public function __construct( $main, $action ) { |
| 17 | + parent::__construct( $main, $action ); |
| 18 | + } |
| 19 | + |
| 20 | + public function execute() { |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + public function getVersion() { |
| 25 | + return __CLASS__ . ': $Id$'; |
| 26 | + } |
| 27 | + |
| 28 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/LiveTranslate/api/ApiLiveTranslate.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 29 | + native |
Added: svn:keywords |
2 | 30 | + Id |
Index: trunk/extensions/LiveTranslate/LiveTranslate.sql |
— | — | @@ -0,0 +1,10 @@ |
| 2 | +-- MySQL version of the database schema for the Live Translate extension. |
| 3 | + |
| 4 | +-- Special translations table |
| 5 | +CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/live_translate ( |
| 6 | + word_id INT(8) unsigned NOT NULL, |
| 7 | + word_language VARCHAR(255) NOT NULL, |
| 8 | + word_translation VARCHAR(255) NOT NULL |
| 9 | +) /*$wgDBTableOptions*/; |
| 10 | + |
| 11 | +CREATE UNIQUE INDEX word_translation ON /*$wgDBprefix*/live_translate (word_id, word_language); |
\ No newline at end of file |
Property changes on: trunk/extensions/LiveTranslate/LiveTranslate.sql |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 12 | + native |