Fixes for r81936 per Tim's review
[mediawiki.git] / includes / resourceloader / ResourceLoaderModule.php
blob29f39bd1161969b76022f534cdf1fc99fcad8ae3
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.
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.
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
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 /**
24 * Abstraction for resource loader modules, with name registration and maxage functionality.
26 abstract class ResourceLoaderModule {
28 # Type of resource
29 const TYPE_SCRIPTS = 'scripts';
30 const TYPE_STYLES = 'styles';
31 const TYPE_MESSAGES = 'messages';
32 const TYPE_COMBINED = 'combined';
34 # sitewide core module like a skin file or jQuery component
35 const ORIGIN_CORE_SITEWIDE = 1;
37 # per-user module generated by the software
38 const ORIGIN_CORE_INDIVIDUAL = 2;
40 # sitewide module generated from user-editable files, like MediaWiki:Common.js, or
41 # modules accessible to multiple users, such as those generated by the Gadgets extension.
42 const ORIGIN_USER_SITEWIDE = 3;
44 # per-user module generated from user-editable files, like User:Me/vector.js
45 const ORIGIN_USER_INDIVIDUAL = 4;
47 # an access constant; make sure this is kept as the largest number in this group
48 const ORIGIN_ALL = 10;
50 # script and style modules form a hierarchy of trustworthiness, with core modules like
51 # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
52 # limit the types of scripts and styles we allow to load on, say, sensitive special
53 # pages like Special:UserLogin and Special:Preferences
54 protected $origin = self::ORIGIN_CORE_SITEWIDE;
56 /* Protected Members */
58 protected $name = null;
60 // In-object cache for file dependencies
61 protected $fileDeps = array();
62 // In-object cache for message blob mtime
63 protected $msgBlobMtime = array();
65 /* Methods */
67 /**
68 * Get this module's name. This is set when the module is registered
69 * with ResourceLoader::register()
71 * @return Mixed: Name (string) or null if no name was set
73 public function getName() {
74 return $this->name;
77 /**
78 * Set this module's name. This is called by ResourceLodaer::register()
79 * when registering the module. Other code should not call this.
81 * @param $name String: Name
83 public function setName( $name ) {
84 $this->name = $name;
87 /**
88 * Get this module's origin. This is set when the module is registered
89 * with ResourceLoader::register()
91 * @return Int ResourceLoaderModule class constant, the subclass default
92 * if not set manuall
94 public function getOrigin() {
95 return $this->origin;
98 /**
99 * Set this module's origin. This is called by ResourceLodaer::register()
100 * when registering the module. Other code should not call this.
102 * @param $name Int origin
104 public function setOrigin( $origin ) {
105 $this->origin = $origin;
109 * Get whether CSS for this module should be flipped
110 * @param $context ResourceLoaderContext
112 public function getFlip( $context ) {
113 return $context->getDirection() === 'rtl';
117 * Get all JS for this module for a given language and skin.
118 * Includes all relevant JS except loader scripts.
120 * @param $context ResourceLoaderContext: Context object
121 * @return String: JavaScript code
123 public function getScript( ResourceLoaderContext $context ) {
124 // Stub, override expected
125 return '';
129 * Get all CSS for this module for a given skin.
131 * @param $context ResourceLoaderContext: Context object
132 * @return Array: List of CSS strings keyed by media type
134 public function getStyles( ResourceLoaderContext $context ) {
135 // Stub, override expected
136 return '';
140 * Get the messages needed for this module.
142 * To get a JSON blob with messages, use MessageBlobStore::get()
144 * @return Array: List of message keys. Keys may occur more than once
146 public function getMessages() {
147 // Stub, override expected
148 return array();
152 * Get the group this module is in.
154 * @return String: Group name
156 public function getGroup() {
157 // Stub, override expected
158 return null;
162 * Get the loader JS for this module, if set.
164 * @return Mixed: JavaScript loader code as a string or boolean false if no custom loader set
166 public function getLoaderScript() {
167 // Stub, override expected
168 return false;
172 * Get a list of modules this module depends on.
174 * Dependency information is taken into account when loading a module
175 * on the client side. When adding a module on the server side,
176 * dependency information is NOT taken into account and YOU are
177 * responsible for adding dependent modules as well. If you don't do
178 * this, the client side loader will send a second request back to the
179 * server to fetch the missing modules, which kind of defeats the
180 * purpose of the resource loader.
182 * To add dependencies dynamically on the client side, use a custom
183 * loader script, see getLoaderScript()
184 * @return Array: List of module names as strings
186 public function getDependencies() {
187 // Stub, override expected
188 return array();
192 * Get the files this module depends on indirectly for a given skin.
193 * Currently these are only image files referenced by the module's CSS.
195 * @param $skin String: Skin name
196 * @return Array: List of files
198 public function getFileDependencies( $skin ) {
199 // Try in-object cache first
200 if ( isset( $this->fileDeps[$skin] ) ) {
201 return $this->fileDeps[$skin];
204 $dbr = wfGetDB( DB_SLAVE );
205 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
206 'md_module' => $this->getName(),
207 'md_skin' => $skin,
208 ), __METHOD__
210 if ( !is_null( $deps ) ) {
211 $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
212 } else {
213 $this->fileDeps[$skin] = array();
215 return $this->fileDeps[$skin];
219 * Set preloaded file dependency information. Used so we can load this
220 * information for all modules at once.
221 * @param $skin String: Skin name
222 * @param $deps Array: Array of file names
224 public function setFileDependencies( $skin, $deps ) {
225 $this->fileDeps[$skin] = $deps;
229 * Get the last modification timestamp of the message blob for this
230 * module in a given language.
231 * @param $lang String: Language code
232 * @return Integer: UNIX timestamp, or 0 if the module doesn't have messages
234 public function getMsgBlobMtime( $lang ) {
235 if ( !isset( $this->msgBlobMtime[$lang] ) ) {
236 if ( !count( $this->getMessages() ) )
237 return 0;
239 $dbr = wfGetDB( DB_SLAVE );
240 $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
241 'mr_resource' => $this->getName(),
242 'mr_lang' => $lang
243 ), __METHOD__
245 // If no blob was found, but the module does have messages, that means we need
246 // to regenerate it. Return NOW
247 if ( $msgBlobMtime === false ) {
248 $msgBlobMtime = wfTimestampNow();
250 $this->msgBlobMtime[$lang] = wfTimestamp( TS_UNIX, $msgBlobMtime );
252 return $this->msgBlobMtime[$lang];
256 * Set a preloaded message blob last modification timestamp. Used so we
257 * can load this information for all modules at once.
258 * @param $lang String: Language code
259 * @param $mtime Integer: UNIX timestamp or 0 if there is no such blob
261 public function setMsgBlobMtime( $lang, $mtime ) {
262 $this->msgBlobMtime[$lang] = $mtime;
265 /* Abstract Methods */
268 * Get this module's last modification timestamp for a given
269 * combination of language, skin and debug mode flag. This is typically
270 * the highest of each of the relevant components' modification
271 * timestamps. Whenever anything happens that changes the module's
272 * contents for these parameters, the mtime should increase.
274 * @param $context ResourceLoaderContext: Context object
275 * @return Integer: UNIX timestamp
277 public function getModifiedTime( ResourceLoaderContext $context ) {
278 // 0 would mean now
279 return 1;