public function getPages(ResourceLoaderContext $context) { $pages = array(); $missingCallback = $context->getRequest()->getVal('missingCallback'); if ($this->type) { foreach ($this->articles as $article) { $pageKey = $article['title']; $type = isset($article['type']) ? $article['type'] : $this->type; $pageInfo = array('type' => $type); if (isset($article['originalName'])) { $pageInfo['originalName'] = $article['originalName']; } if ($missingCallback) { $pageInfo['missingCallback'] = $missingCallback; } if (!empty($article['cityId'])) { $pageIndex = 'fakename' . $this->id++; $pageInfo['city_id'] = intval($article['cityId']); $pageInfo['title'] = $article['title']; // Unable to resolve wiki to cityId } else { if (isset($article['cityId'])) { $pageInfo['missing'] = true; } } $pages[$pageKey] = $pageInfo; } } return $pages; }
/** * Check if an RL request can be cached. * Caller is responsible for checking if any modules are private. * @param $context ResourceLoaderContext * @return bool */ public static function useFileCache(ResourceLoaderContext $context) { global $wgUseFileCache, $wgDefaultSkin, $wgLanguageCode; if (!$wgUseFileCache) { return false; } // Get all query values $queryVals = $context->getRequest()->getValues(); foreach ($queryVals as $query => $val) { if ($query === 'modules' || $query === 'version' || $query === '*') { continue; // note: &* added as IE fix } elseif ($query === 'skin' && $val === $wgDefaultSkin) { continue; } elseif ($query === 'lang' && $val === $wgLanguageCode) { continue; } elseif ($query === 'only' && in_array($val, array('styles', 'scripts'))) { continue; } elseif ($query === 'debug' && $val === 'false') { continue; } return false; } return true; // cacheable }
/** * @param $context ResourceLoaderContext * @return array */ protected function getPages(ResourceLoaderContext $context) { $username = $context->getUser(); if ($username === null) { return array(); } // Get the normalized title of the user's user page $userpageTitle = Title::makeTitleSafe(NS_USER, $username); if (!$userpageTitle instanceof Title) { return array(); } $userpage = $userpageTitle->getPrefixedDBkey(); // Needed so $excludepages works $pages = array("{$userpage}/common.js" => array('type' => 'script'), "{$userpage}/" . $context->getSkin() . '.js' => array('type' => 'script'), "{$userpage}/common.css" => array('type' => 'style'), "{$userpage}/" . $context->getSkin() . '.css' => array('type' => 'style')); // Hack for bug 26283: if we're on a preview page for a CSS/JS page, // we need to exclude that page from this module. In that case, the excludepage // parameter will be set to the name of the page we need to exclude. $excludepage = $context->getRequest()->getVal('excludepage'); if (isset($pages[$excludepage])) { // This works because $excludepage is generated with getPrefixedDBkey(), // just like the keys in $pages[] above unset($pages[$excludepage]); } return $pages; }
/** * @param ResourceLoaderContext $context * @return array */ protected function getPages(ResourceLoaderContext $context) { $useSiteJs = $this->getConfig()->get('UseSiteJs'); $useSiteCss = $this->getConfig()->get('UseSiteCss'); if (!$useSiteJs && !$useSiteCss) { return array(); } $user = $context->getUserObj(); if (!$user || $user->isAnon()) { return array(); } $pages = array(); foreach ($user->getEffectiveGroups() as $group) { if ($group == '*') { continue; } if ($useSiteJs) { $pages["MediaWiki:Group-{$group}.js"] = array('type' => 'script'); } if ($useSiteCss) { $pages["MediaWiki:Group-{$group}.css"] = array('type' => 'style'); } } return $pages; }
/** * @param ResourceLoaderContext $context * @return array */ public function getStyles(ResourceLoaderContext $context) { if (!$this->getConfig()->get('AllowUserCssPrefs')) { return array(); } $options = $context->getUserObj()->getOptions(); // Build CSS rules $rules = array(); // Underline: 2 = browser default, 1 = always, 0 = never if ($options['underline'] < 2) { $rules[] = "a { text-decoration: " . ($options['underline'] ? 'underline' : 'none') . "; }"; } else { # The scripts of these languages are very hard to read with underlines $rules[] = 'a:lang(ar), a:lang(kk-arab), a:lang(mzn), ' . 'a:lang(ps), a:lang(ur) { text-decoration: none; }'; } if ($options['editfont'] !== 'default') { // Double-check that $options['editfont'] consists of safe characters only if (preg_match('/^[a-zA-Z0-9_, -]+$/', $options['editfont'])) { $rules[] = "textarea { font-family: {$options['editfont']}; }\n"; } } $style = implode("\n", $rules); if ($this->getFlip($context)) { $style = CSSJanus::transform($style, true, false); } return array('all' => $style); }
/** * Check if an RL request can be cached. * Caller is responsible for checking if any modules are private. * @param ResourceLoaderContext $context * @return bool */ public static function useFileCache(ResourceLoaderContext $context) { global $wgUseFileCache, $wgDefaultSkin, $wgLanguageCode; if (!$wgUseFileCache) { return false; } // Get all query values $queryVals = $context->getRequest()->getValues(); foreach ($queryVals as $query => $val) { if (in_array($query, array('modules', 'image', 'variant', 'version', '*'))) { // Use file cache regardless of the value of this parameter continue; // note: &* added as IE fix } elseif ($query === 'skin' && $val === $wgDefaultSkin) { continue; } elseif ($query === 'lang' && $val === $wgLanguageCode) { continue; } elseif ($query === 'only' && in_array($val, array('styles', 'scripts'))) { continue; } elseif ($query === 'debug' && $val === 'false') { continue; } elseif ($query === 'format' && $val === 'rasterized') { continue; } return false; } return true; // cacheable }
/** * @param ResourceLoaderContext $context * @return array List of pages */ protected function getPages(ResourceLoaderContext $context) { $config = $this->getConfig(); $user = $context->getUserObj(); if ($user->isAnon()) { return []; } // Use localised/normalised variant to ensure $excludepage matches $userPage = $user->getUserPage()->getPrefixedDBkey(); $pages = []; if ($config->get('AllowUserCss')) { $pages["{$userPage}/common.css"] = ['type' => 'style']; $pages["{$userPage}/" . $context->getSkin() . '.css'] = ['type' => 'style']; } // User group pages are maintained site-wide and enabled with site JS/CSS. if ($config->get('UseSiteCss')) { foreach ($user->getEffectiveGroups() as $group) { if ($group == '*') { continue; } $pages["MediaWiki:Group-{$group}.css"] = ['type' => 'style']; } } // Hack for T28283: Allow excluding pages for preview on a CSS/JS page. // The excludepage parameter is set by OutputPage. $excludepage = $context->getRequest()->getVal('excludepage'); if (isset($pages[$excludepage])) { unset($pages[$excludepage]); } return $pages; }
/** * Get list of pages used by this module * * @param ResourceLoaderContext $context * @return array List of pages */ protected function getPages(ResourceLoaderContext $context) { $allowUserJs = $this->getConfig()->get('AllowUserJs'); $allowUserCss = $this->getConfig()->get('AllowUserCss'); if (!$allowUserJs && !$allowUserCss) { return array(); } $user = $context->getUserObj(); if (!$user || $user->isAnon()) { return array(); } // Needed so $excludepages works $userPage = $user->getUserPage()->getPrefixedDBkey(); $pages = array(); if ($allowUserJs) { $pages["{$userPage}/common.js"] = array('type' => 'script'); $pages["{$userPage}/" . $context->getSkin() . '.js'] = array('type' => 'script'); } if ($allowUserCss) { $pages["{$userPage}/common.css"] = array('type' => 'style'); $pages["{$userPage}/" . $context->getSkin() . '.css'] = array('type' => 'style'); } // Hack for bug 26283: if we're on a preview page for a CSS/JS page, // we need to exclude that page from this module. In that case, the excludepage // parameter will be set to the name of the page we need to exclude. $excludepage = $context->getRequest()->getVal('excludepage'); if (isset($pages[$excludepage])) { // This works because $excludepage is generated with getPrefixedDBkey(), // just like the keys in $pages[] above unset($pages[$excludepage]); } return $pages; }
/** * @param $context ResourceLoaderContext * @return array */ protected function getPages(ResourceLoaderContext $context) { global $wgUser, $wgUseSiteJs, $wgUseSiteCss; $userName = $context->getUser(); if ($userName === null) { return array(); } if (!$wgUseSiteJs && !$wgUseSiteCss) { return array(); } // Use $wgUser is possible; allows to skip a lot of code if (is_object($wgUser) && $wgUser->getName() == $userName) { $user = $wgUser; } else { $user = User::newFromName($userName); if (!$user instanceof User) { return array(); } } $pages = array(); foreach ($user->getEffectiveGroups() as $group) { if (in_array($group, array('*', 'user'))) { continue; } if ($wgUseSiteJs) { $pages["MediaWiki:Group-{$group}.js"] = array('type' => 'script'); } if ($wgUseSiteCss) { $pages["MediaWiki:Group-{$group}.css"] = array('type' => 'style'); } } return $pages; }
protected function getPages(ResourceLoaderContext $context) { if ($context->getUser()) { $username = $context->getUser(); return array("User:{$username}/common.js" => array('type' => 'script'), "User:{$username}/" . $context->getSkin() . '.js' => array('type' => 'script'), "User:{$username}/common.css" => array('type' => 'style'), "User:{$username}/" . $context->getSkin() . '.css' => array('type' => 'style')); } return array(); }
public function testGetUser() { $ctx = new ResourceLoaderContext($this->getResourceLoader(), new FauxRequest([])); $this->assertSame(null, $ctx->getUser()); $this->assertTrue($ctx->getUserObj()->isAnon()); $ctx = new ResourceLoaderContext($this->getResourceLoader(), new FauxRequest(['user' => 'Example'])); $this->assertSame('Example', $ctx->getUser()); $this->assertEquals('Example', $ctx->getUserObj()->getName()); }
/** * Get list of pages used by this module * * @param ResourceLoaderContext $context * @return array List of pages */ protected function getPages(ResourceLoaderContext $context) { $pages = []; if ($this->getConfig()->get('UseSiteJs')) { $pages['MediaWiki:Common.js'] = ['type' => 'script']; $pages['MediaWiki:' . ucfirst($context->getSkin()) . '.js'] = ['type' => 'script']; } return $pages; }
/** * @param $context ResourceLoaderContext * @return array|int|Mixed */ public function getModifiedTime(ResourceLoaderContext $context) { $hash = $context->getHash(); if (isset($this->modifiedTime[$hash])) { return $this->modifiedTime[$hash]; } global $wgUser; return $this->modifiedTime[$hash] = wfTimestamp(TS_UNIX, $wgUser->getTouched()); }
/** * Gets list of pages used by this module * * @param $context ResourceLoaderContext * * @return Array: List of pages */ protected function getPages(ResourceLoaderContext $context) { global $wgHandheldStyle; $pages = array('MediaWiki:Common.js' => array('type' => 'script'), 'MediaWiki:Common.css' => array('type' => 'style'), 'MediaWiki:' . ucfirst($context->getSkin()) . '.js' => array('type' => 'script'), 'MediaWiki:' . ucfirst($context->getSkin()) . '.css' => array('type' => 'style'), 'MediaWiki:Print.css' => array('type' => 'style', 'media' => 'print')); if ($wgHandheldStyle) { $pages['MediaWiki:Handheld.css'] = array('type' => 'style', 'media' => 'handheld'); } return $pages; }
/** * Fetch the context's user options, or if it doesn't match current user, * the default options. * * @param $context ResourceLoaderContext: Context object * @return Array: List of user options keyed by option name */ protected function contextUserOptions(ResourceLoaderContext $context) { global $wgUser; // Verify identity -- this is a private module if ($context->getUser() === $wgUser->getName()) { return $wgUser->getOptions(); } else { return User::getDefaultOptions(); } }
/** * Get list of pages used by this module * * @param ResourceLoaderContext $context * @return array List of pages */ protected function getPages(ResourceLoaderContext $context) { $pages = []; if ($this->getConfig()->get('UseSiteCss')) { $pages['MediaWiki:Common.css'] = ['type' => 'style']; $pages['MediaWiki:' . ucfirst($context->getSkin()) . '.css'] = ['type' => 'style']; $pages['MediaWiki:Print.css'] = ['type' => 'style', 'media' => 'print']; } return $pages; }
/** * Get language-specific LESS variables for this module. * * @return array */ private function getLessVars(ResourceLoaderContext $context) { $language = Language::factory($context->getLanguage()); // This is very conveniently formatted and we can pass it right through $vars = $language->getImageFiles(); // less.php tries to be helpful and parse our variables as LESS source code foreach ($vars as $key => &$value) { $value = CSSMin::serializeStringValue($value); } return $vars; }
public function getScript(ResourceLoaderContext $context) { // Messages $msgInfo = $this->getMessageInfo(); $parsedMessages = array(); $messages = array(); foreach ($msgInfo['args'] as $msgKey => $msgArgs) { $parsedMessages[$msgKey] = call_user_func_array('wfMessage', $msgArgs)->inLanguage($context->getLanguage())->parse(); } foreach ($msgInfo['vals'] as $msgKey => $msgVal) { $messages[$msgKey] = $msgVal; } return 've.init.platform.addParsedMessages(' . FormatJson::encode($parsedMessages, ResourceLoader::inDebugMode()) . ');' . 've.init.platform.addMessages(' . FormatJson::encode($messages, ResourceLoader::inDebugMode()) . ');'; }
/** * Gets list of pages used by this module * * @param ResourceLoaderContext $context * * @return array List of pages */ protected function getPages(ResourceLoaderContext $context) { $pages = array(); if ($this->getConfig()->get('UseSiteJs')) { $pages['MediaWiki:Common.js'] = array('type' => 'script'); $pages['MediaWiki:' . ucfirst($context->getSkin()) . '.js'] = array('type' => 'script'); } if ($this->getConfig()->get('UseSiteCss')) { $pages['MediaWiki:Common.css'] = array('type' => 'style'); $pages['MediaWiki:' . ucfirst($context->getSkin()) . '.css'] = array('type' => 'style'); } $pages['MediaWiki:Print.css'] = array('type' => 'style', 'media' => 'print'); return $pages; }
/** * @dataProvider provideGetPages * @covers ResourceLoaderWikiModule::getPages */ public function testGetPages($params, Config $config, $expected) { $module = new ResourceLoaderWikiModule($params); $module->setConfig($config); // Use getDefinitionSummary because getPages is protected $summary = $module->getDefinitionSummary(ResourceLoaderContext::newDummyContext()); $this->assertEquals($expected, $summary['pages']); }
/** * Get list of pages used by this module * * @param ResourceLoaderContext $context * @return array List of pages */ protected function getPages(ResourceLoaderContext $context) { $config = $this->getConfig(); $user = $context->getUserObj(); if ($user->isAnon()) { return []; } // Use localised/normalised variant to ensure $excludepage matches $userPage = $user->getUserPage()->getPrefixedDBkey(); $pages = []; if ($config->get('AllowUserJs')) { $pages["{$userPage}/common.js"] = ['type' => 'script']; $pages["{$userPage}/" . $context->getSkin() . '.js'] = ['type' => 'script']; } if ($config->get('AllowUserCss')) { $pages["{$userPage}/common.css"] = ['type' => 'style']; $pages["{$userPage}/" . $context->getSkin() . '.css'] = ['type' => 'style']; } $useSiteJs = $config->get('UseSiteJs'); $useSiteCss = $config->get('UseSiteCss'); // User group pages are maintained site-wide and enabled with site JS/CSS. if ($useSiteJs || $useSiteCss) { foreach ($user->getEffectiveGroups() as $group) { if ($group == '*') { continue; } if ($useSiteJs) { $pages["MediaWiki:Group-{$group}.js"] = ['type' => 'script']; } if ($useSiteCss) { $pages["MediaWiki:Group-{$group}.css"] = ['type' => 'style']; } } } // Hack for bug 26283: if we're on a preview page for a CSS/JS page, // we need to exclude that page from this module. In that case, the excludepage // parameter will be set to the name of the page we need to exclude. $excludepage = $context->getRequest()->getVal('excludepage'); if (isset($pages[$excludepage])) { // This works because $excludepage is generated with getPrefixedDBkey(), // just like the keys in $pages[] above unset($pages[$excludepage]); } return $pages; }
/** * @param $context ResourceLoaderContext * @return array */ protected function getPages(ResourceLoaderContext $context) { if ($context->getUser()) { $user = User::newFromName($context->getUser()); if ($user instanceof User) { $pages = array(); foreach ($user->getEffectiveGroups() as $group) { if (in_array($group, array('*', 'user'))) { continue; } $pages["MediaWiki:Group-{$group}.js"] = array('type' => 'script'); $pages["MediaWiki:Group-{$group}.css"] = array('type' => 'style'); } return $pages; } } return array(); }
/** * @param ResourceLoaderContext $context * @return array */ public function getStyles(ResourceLoaderContext $context) { if (!$this->getConfig()->get('AllowUserCssPrefs')) { return []; } $options = $context->getUserObj()->getOptions(); // Build CSS rules $rules = []; // Underline: 2 = skin default, 1 = always, 0 = never if ($options['underline'] < 2) { $rules[] = "a { text-decoration: " . ($options['underline'] ? 'underline' : 'none') . "; }"; } $style = implode("\n", $rules); if ($this->getFlip($context)) { $style = CSSJanus::transform($style, true, false); } return ['all' => $style]; }
/** * @dataProvider provideGetPages * @covers ResourceLoaderWikiModule::getPages */ public function testGetPages($params, Config $config, $expected) { $module = new ResourceLoaderWikiModule($params); $module->setConfig($config); // Because getPages is protected.. $getPages = new ReflectionMethod($module, 'getPages'); $getPages->setAccessible(true); $out = $getPages->invoke($module, ResourceLoaderContext::newDummyContext()); $this->assertEquals($expected, $out); }
public function getScript(ResourceLoaderContext $context) { // Messages $msgInfo = $this->getMessageInfo(); $parsedMessages = array(); $messages = array(); foreach ($msgInfo['args'] as $msgKey => $msgArgs) { $parsedMessages[$msgKey] = call_user_func_array('wfMessage', $msgArgs)->inLanguage($context->getLanguage())->parse(); } foreach ($msgInfo['vals'] as $msgKey => $msgVal) { $messages[$msgKey] = $msgVal; } // Version information $language = Language::factory($context->getLanguage()); $hash = $this->getGitHeadHash(); $id = $hash ? substr($this->getGitHeadHash(), 0, 7) : false; $url = $this->gitInfo->getHeadViewUrl(); $date = $this->gitInfo->getHeadCommitDate(); $dateString = $date ? $language->timeanddate($date, true) : ''; return 've.init.platform.addParsedMessages(' . FormatJson::encode($parsedMessages, ResourceLoader::inDebugMode()) . ');' . 've.init.platform.addMessages(' . FormatJson::encode($messages, ResourceLoader::inDebugMode()) . ');' . 've.version = ' . FormatJson::encode(array('id' => $id, 'url' => $url, 'timestamp' => $date, 'dateString' => $dateString), ResourceLoader::inDebugMode()) . ';'; }
/** * @param ResourceLoaderContext $context * @return array */ public function getStyles(ResourceLoaderContext $context) { if (!$this->getConfig()->get('AllowUserCssPrefs')) { return []; } $options = $context->getUserObj()->getOptions(); // Build CSS rules $rules = []; // Underline: 2 = skin default, 1 = always, 0 = never if ($options['underline'] < 2) { $rules[] = "a { text-decoration: " . ($options['underline'] ? 'underline' : 'none') . "; }"; } if ($options['editfont'] !== 'default') { // Double-check that $options['editfont'] consists of safe characters only if (preg_match('/^[a-zA-Z0-9_, -]+$/', $options['editfont'])) { $rules[] = "textarea { font-family: {$options['editfont']}; }\n"; } } $style = implode("\n", $rules); if ($this->getFlip($context)) { $style = CSSJanus::transform($style, true, false); } return ['all' => $style]; }
/** * @param $context ResourceLoaderContext * @return array|mixed */ public function getModifiedTime(ResourceLoaderContext $context) { global $IP, $wgCacheEpoch; $hash = $context->getHash(); if (isset($this->modifiedTime[$hash])) { return $this->modifiedTime[$hash]; } // Call preloadModuleInfo() on ALL modules as we're about // to call getModifiedTime() on all of them $loader = $context->getResourceLoader(); $loader->preloadModuleInfo($loader->getModuleNames(), $context); $this->modifiedTime[$hash] = filemtime("{$IP}/resources/startup.js"); // ATTENTION!: Because of the line above, this is not going to cause // infinite recursion - think carefully before making changes to this // code! $time = wfTimestamp(TS_UNIX, $wgCacheEpoch); foreach ($loader->getModuleNames() as $name) { $module = $loader->getModule($name); $time = max($time, $module->getModifiedTime($context)); } return $this->modifiedTime[$hash] = $time; }
/** * Helper for createLoaderURL() * * @since 1.24 * @see makeLoaderQuery * @param ResourceLoaderContext $context * @param array $extraQuery * @return array */ public static function createLoaderQuery(ResourceLoaderContext $context, $extraQuery = array()) { return self::makeLoaderQuery($context->getModules(), $context->getLanguage(), $context->getSkin(), $context->getUser(), $context->getVersion(), $context->getDebug(), $context->getOnly(), $context->getRequest()->getBool('printable'), $context->getRequest()->getBool('handheld'), $extraQuery); }
/** * Get the URL or URLs to load for this module's CSS in debug mode. * The default behavior is to return a load.php?only=styles URL for * the module, but file-based modules will want to override this to * load the files directly. See also getScriptURLsForDebug() * * @param $context ResourceLoaderContext: Context object * @return Array: array( mediaType => array( URL1, URL2, ... ), ... ) */ public function getStyleURLsForDebug(ResourceLoaderContext $context) { $url = ResourceLoader::makeLoaderURL(array($this->getName()), $context->getLanguage(), $context->getSkin(), $context->getUser(), $context->getVersion(), true, 'styles', $context->getRequest()->getBool('printable'), $context->getRequest()->getBool('handheld')); return array('all' => array($url)); }
/** * Fetch the tokens for the current user. * * @param ResourceLoaderContext $context * @return array List of tokens keyed by token type */ protected function contextUserTokens(ResourceLoaderContext $context) { $user = $context->getUserObj(); return array('editToken' => $user->getEditToken(), 'patrolToken' => $user->getEditToken('patrol'), 'watchToken' => $user->getEditToken('watch')); }