Use wfGetCache( CACHE_ANYTHING ) instead of $wgMemc to store the result of ResourceLo...
[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 /**
24 * Abstraction for resource loader modules, with name registration and maxage functionality.
25 */
26 abstract class ResourceLoaderModule {
27
28 /* Protected Members */
29
30 protected $name = null;
31
32 // In-object cache for file dependencies
33 protected $fileDeps = array();
34 // In-object cache for message blob mtime
35 protected $msgBlobMtime = array();
36
37 /* Methods */
38
39 /**
40 * Get this module's name. This is set when the module is registered
41 * with ResourceLoader::register()
42 *
43 * @return Mixed: Name (string) or null if no name was set
44 */
45 public function getName() {
46 return $this->name;
47 }
48
49 /**
50 * Set this module's name. This is called by ResourceLodaer::register()
51 * when registering the module. Other code should not call this.
52 *
53 * @param $name String: Name
54 */
55 public function setName( $name ) {
56 $this->name = $name;
57 }
58
59 /**
60 * Get whether CSS for this module should be flipped
61 */
62 public function getFlip( $context ) {
63 return $context->getDirection() === 'rtl';
64 }
65
66 /**
67 * Get all JS for this module for a given language and skin.
68 * Includes all relevant JS except loader scripts.
69 *
70 * @param $context ResourceLoaderContext: Context object
71 * @return String: JavaScript code
72 */
73 public function getScript( ResourceLoaderContext $context ) {
74 // Stub, override expected
75 return '';
76 }
77
78 /**
79 * Get all CSS for this module for a given skin.
80 *
81 * @param $context ResourceLoaderContext: Context object
82 * @return Array: List of CSS strings keyed by media type
83 */
84 public function getStyles( ResourceLoaderContext $context ) {
85 // Stub, override expected
86 return '';
87 }
88
89 /**
90 * Get the messages needed for this module.
91 *
92 * To get a JSON blob with messages, use MessageBlobStore::get()
93 *
94 * @return Array: List of message keys. Keys may occur more than once
95 */
96 public function getMessages() {
97 // Stub, override expected
98 return array();
99 }
100
101 /**
102 * Get the group this module is in.
103 *
104 * @return String: Group name
105 */
106 public function getGroup() {
107 // Stub, override expected
108 return null;
109 }
110
111 /**
112 * Get the loader JS for this module, if set.
113 *
114 * @return Mixed: JavaScript loader code as a string or boolean false if no custom loader set
115 */
116 public function getLoaderScript() {
117 // Stub, override expected
118 return false;
119 }
120
121 /**
122 * Get a list of modules this module depends on.
123 *
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.
131 *
132 * To add dependencies dynamically on the client side, use a custom
133 * loader script, see getLoaderScript()
134 * @return Array: List of module names as strings
135 */
136 public function getDependencies() {
137 // Stub, override expected
138 return array();
139 }
140
141 /**
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.
144 *
145 * @param $skin String: Skin name
146 * @return Array: List of files
147 */
148 public function getFileDependencies( $skin ) {
149 // Try in-object cache first
150 if ( isset( $this->fileDeps[$skin] ) ) {
151 return $this->fileDeps[$skin];
152 }
153
154 $dbr = wfGetDB( DB_SLAVE );
155 $deps = $dbr->selectField( 'module_deps', 'md_deps', array(
156 'md_module' => $this->getName(),
157 'md_skin' => $skin,
158 ), __METHOD__
159 );
160 if ( !is_null( $deps ) ) {
161 $this->fileDeps[$skin] = (array) FormatJson::decode( $deps, true );
162 } else {
163 $this->fileDeps[$skin] = array();
164 }
165 return $this->fileDeps[$skin];
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 Integer: 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 Integer: 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: Context object
218 * @return Integer: UNIX timestamp
219 */
220 public function getModifiedTime( ResourceLoaderContext $context ) {
221 // 0 would mean now
222 return 1;
223 }
224 }