Part 2 of 2, moved ResourceLoader*Module classes to their own files - this commit...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
21 */
22
23 defined( 'MEDIAWIKI' ) || die( 1 );
24
25 /**
26 * Abstraction for resource loader modules, with name registration and maxage functionality.
27 */
28 abstract class ResourceLoaderModule {
29
30 /* Protected Members */
31
32 protected $name = null;
33
34 // In-object cache for file dependencies
35 protected $fileDeps = array();
36 // In-object cache for message blob mtime
37 protected $msgBlobMtime = array();
38
39 /* Methods */
40
41 /**
42 * Get this module's name. This is set when the module is registered
43 * with ResourceLoader::register()
44 *
45 * @return Mixed: name (string) or null if no name was set
46 */
47 public function getName() {
48 return $this->name;
49 }
50
51 /**
52 * Set this module's name. This is called by ResourceLodaer::register()
53 * when registering the module. Other code should not call this.
54 *
55 * @param $name String: name
56 */
57 public function setName( $name ) {
58 $this->name = $name;
59 }
60
61 /**
62 * Get whether CSS for this module should be flipped
63 */
64 public function getFlip( $context ) {
65 return $context->getDirection() === 'rtl';
66 }
67
68 /**
69 * Get all JS for this module for a given language and skin.
70 * Includes all relevant JS except loader scripts.
71 *
72 * @param $context ResourceLoaderContext object
73 * @return String: JS
74 */
75 public function getScript( ResourceLoaderContext $context ) {
76 // Stub, override expected
77 return '';
78 }
79
80 /**
81 * Get all CSS for this module for a given skin.
82 *
83 * @param $context ResourceLoaderContext object
84 * @return array: strings of CSS keyed by media type
85 */
86 public function getStyles( ResourceLoaderContext $context ) {
87 // Stub, override expected
88 return '';
89 }
90
91 /**
92 * Get the messages needed for this module.
93 *
94 * To get a JSON blob with messages, use MessageBlobStore::get()
95 *
96 * @return array of message keys. Keys may occur more than once
97 */
98 public function getMessages() {
99 // Stub, override expected
100 return array();
101 }
102
103 /**
104 * Get the group this module is in.
105 *
106 * @return string of group name
107 */
108 public function getGroup() {
109 // Stub, override expected
110 return null;
111 }
112
113 /**
114 * Get the loader JS for this module, if set.
115 *
116 * @return Mixed: loader JS (string) or false if no custom loader set
117 */
118 public function getLoaderScript() {
119 // Stub, override expected
120 return false;
121 }
122
123 /**
124 * Get a list of modules this module depends on.
125 *
126 * Dependency information is taken into account when loading a module
127 * on the client side. When adding a module on the server side,
128 * dependency information is NOT taken into account and YOU are
129 * responsible for adding dependent modules as well. If you don't do
130 * this, the client side loader will send a second request back to the
131 * server to fetch the missing modules, which kind of defeats the
132 * purpose of the resource loader.
133 *
134 * To add dependencies dynamically on the client side, use a custom
135 * loader script, see getLoaderScript()
136 * @return Array of module names (strings)
137 */
138 public function getDependencies() {
139 // Stub, override expected
140 return array();
141 }
142
143 /**
144 * Get the files this module depends on indirectly for a given skin.
145 * Currently these are only image files referenced by the module's CSS.
146 *
147 * @param $skin String: skin name
148 * @return array of files
149 */
150 public function getFileDependencies( $skin ) {
151 // Try in-object cache first
152 if ( isset( $this->fileDeps[$skin] ) ) {
153 return $this->fileDeps[$skin];
154 }
155
156 $dbr = wfGetDB( DB_SLAVE );
157 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
158 'md_module' => $this->getName(),
159 'md_skin' => $skin,
160 ), __METHOD__
161 );
162 if ( !is_null( $deps ) ) {
163 return $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
164 }
165 return $this->fileDeps[$skin] = array();
166 }
167
168 /**
169 * Set preloaded file dependency information. Used so we can load this
170 * information for all modules at once.
171 * @param $skin string Skin name
172 * @param $deps array Array of file names
173 */
174 public function setFileDependencies( $skin, $deps ) {
175 $this->fileDeps[$skin] = $deps;
176 }
177
178 /**
179 * Get the last modification timestamp of the message blob for this
180 * module in a given language.
181 * @param $lang string Language code
182 * @return int UNIX timestamp, or 0 if no blob found
183 */
184 public function getMsgBlobMtime( $lang ) {
185 if ( !count( $this->getMessages() ) )
186 return 0;
187
188 $dbr = wfGetDB( DB_SLAVE );
189 $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
190 'mr_resource' => $this->getName(),
191 'mr_lang' => $lang
192 ), __METHOD__
193 );
194 $this->msgBlobMtime[$lang] = $msgBlobMtime ? wfTimestamp( TS_UNIX, $msgBlobMtime ) : 0;
195 return $this->msgBlobMtime[$lang];
196 }
197
198 /**
199 * Set a preloaded message blob last modification timestamp. Used so we
200 * can load this information for all modules at once.
201 * @param $lang string Language code
202 * @param $mtime int UNIX timestamp or 0 if there is no such blob
203 */
204 public function setMsgBlobMtime( $lang, $mtime ) {
205 $this->msgBlobMtime[$lang] = $mtime;
206 }
207
208 /* Abstract Methods */
209
210 /**
211 * Get this module's last modification timestamp for a given
212 * combination of language, skin and debug mode flag. This is typically
213 * the highest of each of the relevant components' modification
214 * timestamps. Whenever anything happens that changes the module's
215 * contents for these parameters, the mtime should increase.
216 *
217 * @param $context ResourceLoaderContext object
218 * @return int UNIX timestamp
219 */
220 public function getModifiedTime( ResourceLoaderContext $context ) {
221 // 0 would mean now
222 return 1;
223 }
224 }