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
23 defined( 'MEDIAWIKI' ) ||
die( 1 );
26 * Abstraction for resource loader modules, with name registration and maxage functionality.
28 abstract class ResourceLoaderModule
{
30 /* Protected Members */
32 protected $name = null;
34 // In-object cache for file dependencies
35 protected $fileDeps = array();
36 // In-object cache for message blob mtime
37 protected $msgBlobMtime = array();
42 * Get this module's name. This is set when the module is registered
43 * with ResourceLoader::register()
45 * @return Mixed: name (string) or null if no name was set
47 public function getName() {
52 * Set this module's name. This is called by ResourceLodaer::register()
53 * when registering the module. Other code should not call this.
55 * @param $name String: name
57 public function setName( $name ) {
62 * Get whether CSS for this module should be flipped
64 public function getFlip( $context ) {
65 return $context->getDirection() === 'rtl';
69 * Get all JS for this module for a given language and skin.
70 * Includes all relevant JS except loader scripts.
72 * @param $context ResourceLoaderContext object
75 public function getScript( ResourceLoaderContext
$context ) {
76 // Stub, override expected
81 * Get all CSS for this module for a given skin.
83 * @param $context ResourceLoaderContext object
84 * @return array: strings of CSS keyed by media type
86 public function getStyles( ResourceLoaderContext
$context ) {
87 // Stub, override expected
92 * Get the messages needed for this module.
94 * To get a JSON blob with messages, use MessageBlobStore::get()
96 * @return array of message keys. Keys may occur more than once
98 public function getMessages() {
99 // Stub, override expected
104 * Get the group this module is in.
106 * @return string of group name
108 public function getGroup() {
109 // Stub, override expected
114 * Get the loader JS for this module, if set.
116 * @return Mixed: loader JS (string) or false if no custom loader set
118 public function getLoaderScript() {
119 // Stub, override expected
124 * Get a list of modules this module depends on.
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.
134 * To add dependencies dynamically on the client side, use a custom
135 * loader script, see getLoaderScript()
136 * @return Array of module names (strings)
138 public function getDependencies() {
139 // Stub, override expected
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.
147 * @param $skin String: skin name
148 * @return array of files
150 public function getFileDependencies( $skin ) {
151 // Try in-object cache first
152 if ( isset( $this->fileDeps
[$skin] ) ) {
153 return $this->fileDeps
[$skin];
156 $dbr = wfGetDB( DB_SLAVE
);
157 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
158 'md_module' => $this->getName(),
162 if ( !is_null( $deps ) ) {
163 return $this->fileDeps
[$skin] = (array) FormatJson
::decode( $deps, true );
165 return $this->fileDeps
[$skin] = array();
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 ) {