Index: trunk/extensions/Translate/tag/Tag.php |
— | — | @@ -1,8 +1,13 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Hook attachments for the <translate> tag. |
| 5 | + * Code for handling pages marked with <translate> tag. This class does parsing. |
6 | 6 | * |
| 7 | + * Section: piece of text, separated usually with two new lines, that is used as |
| 8 | + * an single translation unit for change tracking and so on. |
| 9 | + * Occurance: contents of <translate>..</translate>, which there can be many on |
| 10 | + * one page. |
| 11 | + * |
7 | 12 | * @addtogroup Extensions |
8 | 13 | * |
9 | 14 | * @author Niklas Laxström |
— | — | @@ -10,15 +15,18 @@ |
11 | 16 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
12 | 17 | */ |
13 | 18 | class TranslateTag { |
14 | | - |
| 19 | + /** Counter for nth <translate>..</translate> invocation to know to which |
| 20 | + * occurance each section belongs to, when doing save-time parsing. */ |
15 | 21 | var $invocation = 0; |
16 | 22 | |
| 23 | + // Deprecated, TODO: write a suitable factory functions for replacement |
17 | 24 | public static function getInstance() { |
18 | 25 | $obj = new self; |
19 | 26 | //$obj->reset(); |
20 | 27 | return $obj; |
21 | 28 | } |
22 | 29 | |
| 30 | + /** Factory for creating a new instance from Title object */ |
23 | 31 | public static function newFromTitle( Title $title ) { |
24 | 32 | $obj = new self(); |
25 | 33 | $obj->invocation = 0; |
— | — | @@ -31,6 +39,7 @@ |
32 | 40 | return $obj; |
33 | 41 | } |
34 | 42 | |
| 43 | + /** Factory for creating a new instance from occurance */ |
35 | 44 | public static function newFromTagContents( &$text, $code ) { |
36 | 45 | $obj = new self(); |
37 | 46 | $obj->invocation = -1; |
— | — | @@ -42,6 +51,7 @@ |
43 | 52 | return $obj; |
44 | 53 | } |
45 | 54 | |
| 55 | + // TODO: Move to hook or utils? |
46 | 56 | // Remember to to use TranslateUtils::injectCSS() |
47 | 57 | public function getHeader( Title $title ) { |
48 | 58 | list( , $code ) = TranslateTagUtils::keyAndCode( $title ); |
— | — | @@ -68,15 +78,18 @@ |
69 | 79 | $legend .= " | $legendText <span class=\"mw-translate-other\">$legendOther</span>"; |
70 | 80 | $legend .= " <span class=\"mw-translate-fuzzy\">$legendFuzzy</span>"; |
71 | 81 | } |
| 82 | + // TODO: the following text will of course be removed :) |
72 | 83 | $legend .= ' | This page is translatable using the experimental wiki page translation feature.</div>'; |
73 | 84 | $legend .= "\n----\n"; |
74 | 85 | return $legend; |
75 | 86 | } |
76 | 87 | |
| 88 | + // Some regexps |
77 | 89 | const METADATA = '~\n?<!--TS(.*?)-->\n?~us'; |
78 | 90 | const PATTERN_COMMENT = '~\n?<!--T[=:;](.*?)-->\n?~u'; |
79 | 91 | const PATTERN_TAG = '~(<translate>)\n?(.+?)(</translate>)~us'; |
80 | 92 | |
| 93 | + // Renders a translation page to given language |
81 | 94 | public function renderPage( $text, Title $title, $code = false ) { |
82 | 95 | $this->renderTitle = $title; |
83 | 96 | $this->renderCode = $code; |
— | — | @@ -135,6 +148,7 @@ |
136 | 149 | } |
137 | 150 | } |
138 | 151 | |
| 152 | + // Clean any comments there may be left |
139 | 153 | $input = preg_replace( self::PATTERN_PLACEHOLDER, '', $input ); |
140 | 154 | $input = preg_replace( self::METADATA, '', $input ); |
141 | 155 | |
— | — | @@ -226,6 +240,7 @@ |
227 | 241 | return "~$regex~u"; |
228 | 242 | } |
229 | 243 | |
| 244 | + // Deprecated |
230 | 245 | public function reset() { |
231 | 246 | $this->sections = array(); |
232 | 247 | $this->placeholders = array(); |
— | — | @@ -262,6 +277,7 @@ |
263 | 278 | |
264 | 279 | } |
265 | 280 | |
| 281 | + /** Use this to get the location of translations */ |
266 | 282 | public function getTranslationPage( Title $title, $key, $code = false ) { |
267 | 283 | global $wgTranslateTagTranslationLocation; |
268 | 284 | list( , $format ) = $wgTranslateTagTranslationLocation; |
— | — | @@ -280,6 +296,9 @@ |
281 | 297 | return $pagename; |
282 | 298 | } |
283 | 299 | |
| 300 | + /** Use this to get the namespace for page names provided with |
| 301 | + * getTranslationPage |
| 302 | + */ |
284 | 303 | public function getNamespace( Title $title ) { |
285 | 304 | global $wgTranslateTagTranslationLocation; |
286 | 305 | list( $nsId, ) = $wgTranslateTagTranslationLocation; |
— | — | @@ -287,6 +306,7 @@ |
288 | 307 | return $nsId; |
289 | 308 | } |
290 | 309 | |
| 310 | + // Initiate fuzzyjobs on changed sections |
291 | 311 | public function onArticleSaveComplete( |
292 | 312 | $article, $user, $text, $summary, $isminor, $_, $_, $flags, $revision |
293 | 313 | ) { |
— | — | @@ -328,6 +348,10 @@ |
329 | 349 | $cb = array( $obj, 'saveCb' ); |
330 | 350 | $text = preg_replace_callback( self::PATTERN_TAG, $cb, $text ); |
331 | 351 | |
| 352 | + // Trim trailing whitespace. It is not allowed in wikitext and shows up in |
| 353 | + // diffs |
| 354 | + $text = rtrim( $text ); |
| 355 | + |
332 | 356 | if ( count($obj->changed) ) { |
333 | 357 | // Register fuzzier |
334 | 358 | // We need to do it later, so that we know the revision number |