Index: trunk/phase3/includes/Message.php |
— | — | @@ -1,78 +1,133 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * This class provides methods for fetching interface messages and |
5 | | - * processing them into variety of formats that are needed in MediaWiki. |
| 4 | + * The Message class provides methods which fullfil two basic services: |
| 5 | + * - fetching interface messages |
| 6 | + * - processing messages into a variety of formats |
6 | 7 | * |
7 | | - * It is intented to replace the old wfMsg* functions that over time grew |
8 | | - * unusable. |
9 | | - * @see https://www.mediawiki.org/wiki/New_messages_API for |
10 | | - * equivalence between old and new functions. |
| 8 | + * First implemented with MediaWiki 1.17, the Message class is intented to |
| 9 | + * replace the old wfMsg* functions that over time grew unusable. |
| 10 | + * @see https://www.mediawiki.org/wiki/New_messages_API for equivalences |
| 11 | + * between old and new functions. |
11 | 12 | * |
12 | | - * Below, you will find several examples of wfMessage() usage. |
| 13 | + * You should use the wfMessage() global function which acts as a wrapper for |
| 14 | + * the Message class. The wrapper let you pass parameters as arguments. |
13 | 15 | * |
| 16 | + * The most basic usage cases would be: |
14 | 17 | * |
15 | | - * Fetching a message text for interface message |
16 | 18 | * @code |
17 | | - * $button = Xml::button( wfMessage( 'submit' )->text() ); |
| 19 | + * // Initialize a Message object using the 'some_key' message key |
| 20 | + * $message = wfMessage( 'some_key' ); |
| 21 | + * |
| 22 | + * // Using two parameters those values are strings 'value1' and 'value2': |
| 23 | + * $message = wfMessage( 'some_key', |
| 24 | + * 'value1', 'value2' |
| 25 | + * ); |
18 | 26 | * @endcode |
19 | 27 | * |
20 | | - * Messages can have parameters: |
| 28 | + * @section message_global_fn Global function wrapper: |
21 | 29 | * |
| 30 | + * Since wfMessage() returns a Message instance, you can chain its call with |
| 31 | + * a method. Some of them return a Message instance too so you can chain them. |
| 32 | + * You will find below several examples of wfMessage() usage. |
| 33 | + * |
| 34 | + * Fetching a message text for interface message: |
| 35 | + * |
22 | 36 | * @code |
23 | | - * wfMessage( 'welcome-to' )->params( $wgSitename )->text(); |
| 37 | + * $button = Xml::button( |
| 38 | + * wfMessage( 'submit' )->text() |
| 39 | + * ); |
24 | 40 | * @endcode |
25 | 41 | * |
26 | | - * {{GRAMMAR}} and friends work correctly |
| 42 | + * A Message instance can be passed parameters after it has been constructed, |
| 43 | + * use the params() method to do so: |
| 44 | + * |
27 | 45 | * @code |
28 | | - * wfMessage( 'are-friends', $user, $friend ); |
29 | | - * wfMessage( 'bad-message' )->rawParams( '<script>...</script>' )->escaped(); |
| 46 | + * wfMessage( 'welcome-to' ) |
| 47 | + * ->params( $wgSitename ) |
| 48 | + * ->text(); |
30 | 49 | * @endcode |
31 | 50 | * |
32 | | - * Sometimes the message text ends up in the database, so content language is needed. |
| 51 | + * {{GRAMMAR}} and friends work correctly: |
| 52 | + * |
33 | 53 | * @code |
34 | | - * wfMessage( 'file-log', $user, $filename )->inContentLanguage()->text() |
35 | | - * </pre> |
| 54 | + * wfMessage( 'are-friends', |
| 55 | + * $user, $friend |
| 56 | + * ); |
| 57 | + * wfMessage( 'bad-message' ) |
| 58 | + * ->rawParams( '<script>...</script>' ) |
| 59 | + * ->escaped(); |
36 | 60 | * @endcode |
37 | 61 | * |
| 62 | + * @section message_language Changing language: |
| 63 | + * |
| 64 | + * Messages can be requested in a different language or in whatever current |
| 65 | + * content language is being used. The methods are: |
| 66 | + * - Message->inContentLanguage() |
| 67 | + * - Message->inLanguage() |
| 68 | + * |
| 69 | + * Sometimes the message text ends up in the database, so content language is |
| 70 | + * needed: |
| 71 | + * |
| 72 | + * @code |
| 73 | + * wfMessage( 'file-log', |
| 74 | + * $user, $filename |
| 75 | + * )->inContentLanguage()->text(); |
| 76 | + * @endcode |
| 77 | + * |
38 | 78 | * Checking whether a message exists: |
| 79 | + * |
39 | 80 | * @code |
40 | 81 | * wfMessage( 'mysterious-message' )->exists() |
| 82 | + * // returns a boolean whether the 'mysterious-message' key exist. |
41 | 83 | * @endcode |
42 | 84 | * |
43 | 85 | * If you want to use a different language: |
| 86 | + * |
44 | 87 | * @code |
45 | | - * wfMessage( 'email-header' )->inLanguage( $user->getOption( 'language' ) )->plain() |
| 88 | + * $userLanguage = $user->getOption( 'language' ); |
| 89 | + * wfMessage( 'email-header' ) |
| 90 | + * ->inLanguage( $userLanguage ) |
| 91 | + * ->plain(); |
46 | 92 | * @endcode |
47 | 93 | * |
48 | 94 | * @note You cannot parse the text except in the content or interface |
49 | 95 | * @note languages |
50 | 96 | * |
51 | | - * Comparison with old wfMsg* functions: |
| 97 | + * @section message_compare_old Comparison with old wfMsg* functions: |
52 | 98 | * |
53 | | - * Use full parsing. |
| 99 | + * Use full parsing: |
54 | 100 | * |
55 | 101 | * @code |
| 102 | + * // old style: |
56 | 103 | * wfMsgExt( 'key', array( 'parseinline' ), 'apple' ); |
57 | | - * === wfMessage( 'key', 'apple' )->parse(); |
| 104 | + * // new style: |
| 105 | + * wfMessage( 'key', 'apple' )->parse(); |
58 | 106 | * @endcode |
59 | 107 | * |
60 | | - * Parseinline is used because it is more useful when pre-building html. |
| 108 | + * Parseinline is used because it is more useful when pre-building HTML. |
61 | 109 | * In normal use it is better to use OutputPage::(add|wrap)WikiMsg. |
62 | 110 | * |
63 | | - * Places where html cannot be used. {{-transformation is done. |
| 111 | + * Places where HTML cannot be used. {{-transformation is done. |
64 | 112 | * @code |
| 113 | + * // old style: |
65 | 114 | * wfMsgExt( 'key', array( 'parsemag' ), 'apple', 'pear' ); |
66 | | - * === wfMessage( 'key', 'apple', 'pear' )->text(); |
| 115 | + * // new style: |
| 116 | + * wfMessage( 'key', 'apple', 'pear' )->text(); |
67 | 117 | * @endcode |
68 | 118 | * |
69 | | - * Shortcut for escaping the message too, similar to wfMsgHTML, but |
| 119 | + * Shortcut for escaping the message too, similar to wfMsgHTML(), but |
70 | 120 | * parameters are not replaced after escaping by default. |
71 | 121 | * @code |
72 | | - * $escaped = wfMessage( 'key' )->rawParams( 'apple' )->escaped(); |
| 122 | + * $escaped = wfMessage( 'key' ) |
| 123 | + * ->rawParams( 'apple' ) |
| 124 | + * ->escaped(); |
73 | 125 | * @endcode |
74 | 126 | * |
| 127 | + * @section message_appendix Appendix: |
| 128 | + * |
75 | 129 | * @todo |
76 | 130 | * - test, can we have tests? |
| 131 | + * - this documentation needs to be extended |
77 | 132 | * |
78 | 133 | * @see https://www.mediawiki.org/wiki/WfMessage() |
79 | 134 | * @see https://www.mediawiki.org/wiki/New_messages_API |