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
19 * @author Trevor Parscal
20 * @author Roan Kattouw
24 * Abstraction for resource loader modules, with name registration and maxage functionality.
26 abstract class ResourceLoaderModule
{
28 /* Protected Members */
30 protected $name = null;
32 // In-object cache for file dependencies
33 protected $fileDeps = array();
34 // In-object cache for message blob mtime
35 protected $msgBlobMtime = array();
40 * Get this module's name. This is set when the module is registered
41 * with ResourceLoader::register()
43 * @return Mixed: name (string) or null if no name was set
45 public function getName() {
50 * Set this module's name. This is called by ResourceLodaer::register()
51 * when registering the module. Other code should not call this.
53 * @param $name String: name
55 public function setName( $name ) {
60 * Get whether CSS for this module should be flipped
62 public function getFlip( $context ) {
63 return $context->getDirection() === 'rtl';
67 * Get all JS for this module for a given language and skin.
68 * Includes all relevant JS except loader scripts.
70 * @param $context ResourceLoaderContext object
73 public function getScript( ResourceLoaderContext
$context ) {
74 // Stub, override expected
79 * Get all CSS for this module for a given skin.
81 * @param $context ResourceLoaderContext object
82 * @return array: strings of CSS keyed by media type
84 public function getStyles( ResourceLoaderContext
$context ) {
85 // Stub, override expected
90 * Get the messages needed for this module.
92 * To get a JSON blob with messages, use MessageBlobStore::get()
94 * @return array of message keys. Keys may occur more than once
96 public function getMessages() {
97 // Stub, override expected
102 * Get the group this module is in.
104 * @return string of group name
106 public function getGroup() {
107 // Stub, override expected
112 * Get the loader JS for this module, if set.
114 * @return Mixed: loader JS (string) or false if no custom loader set
116 public function getLoaderScript() {
117 // Stub, override expected
122 * Get a list of modules this module depends on.
124 * Dependency information is taken into account when loading a module
125 * on the client side. When adding a module on the server side,
126 * dependency information is NOT taken into account and YOU are
127 * responsible for adding dependent modules as well. If you don't do
128 * this, the client side loader will send a second request back to the
129 * server to fetch the missing modules, which kind of defeats the
130 * purpose of the resource loader.
132 * To add dependencies dynamically on the client side, use a custom
133 * loader script, see getLoaderScript()
134 * @return Array of module names (strings)
136 public function getDependencies() {
137 // Stub, override expected
142 * Get the files this module depends on indirectly for a given skin.
143 * Currently these are only image files referenced by the module's CSS.
145 * @param $skin String: skin name
146 * @return array of files
148 public function getFileDependencies( $skin ) {
149 // Try in-object cache first
150 if ( isset( $this->fileDeps
[$skin] ) ) {
151 return $this->fileDeps
[$skin];
154 $dbr = wfGetDB( DB_SLAVE
);
155 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
156 'md_module' => $this->getName(),
160 if ( !is_null( $deps ) ) {
161 $this->fileDeps
[$skin] = (array) FormatJson
::decode( $deps, true );
163 $this->fileDeps
[$skin] = array();
165 return $this->fileDeps
[$skin];
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
174 public function setFileDependencies( $skin, $deps ) {
175 $this->fileDeps
[$skin] = $deps;
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
184 public function getMsgBlobMtime( $lang ) {
185 if ( !count( $this->getMessages() ) )
188 $dbr = wfGetDB( DB_SLAVE
);
189 $msgBlobMtime = $dbr->selectField( 'msg_resource', 'mr_timestamp', array(
190 'mr_resource' => $this->getName(),
194 $this->msgBlobMtime
[$lang] = $msgBlobMtime ?
wfTimestamp( TS_UNIX
, $msgBlobMtime ) : 0;
195 return $this->msgBlobMtime
[$lang];
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
204 public function setMsgBlobMtime( $lang, $mtime ) {
205 $this->msgBlobMtime
[$lang] = $mtime;
208 /* Abstract Methods */
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.
217 * @param $context ResourceLoaderContext object
218 * @return int UNIX timestamp
220 public function getModifiedTime( ResourceLoaderContext
$context ) {