예제 #1
0
 /**
  * Create category.
  *
  * @param $category String: Name of category to create.
  * @param $code String: Code of language that the category is for.
  * @param $level String: Level that the category is for.
  */
 public static function create($category, $code, $level = null)
 {
     $category = strip_tags($category);
     $title = Title::makeTitleSafe(NS_CATEGORY, $category);
     if ($title === null || $title->exists()) {
         return;
     }
     global $wgLanguageCode;
     $language = BabelLanguageCodes::getName($code, $wgLanguageCode);
     if ($level === null) {
         $text = wfMsgForContent('babel-autocreate-text-main', $language, $code);
     } else {
         $text = wfMsgForContent('babel-autocreate-text-levels', $level, $language, $code);
     }
     $user = self::user();
     # Do not add a message if the username is invalid or if the account that adds it, is blocked
     if (!$user || $user->isBlocked()) {
         return;
     }
     if (!$title->quickUserCan('create', $user)) {
         return;
         # The Babel AutoCreate account is not allowed to create the page
     }
     /* $article->doEdit will call $wgParser->parse.
      * Calling Parser::parse recursively is baaaadd... (bug 29245)
      * @todo FIXME: surely there is a better way?
      */
     global $wgParser, $wgParserConf;
     $oldParser = $wgParser;
     $parserClass = $wgParserConf['class'];
     $wgParser = new $parserClass($wgParserConf);
     $article = new Article($title, 0);
     $article->doEdit($text, wfMsgForContent('babel-autocreate-reason', wfMsgForContent('babel-url')), EDIT_FORCE_BOT, false, $user);
     $wgParser = $oldParser;
 }
예제 #2
0
 /**
  * Gets the list of languages a user has set up with Babel
  *
  * TODO Can be done much smarter, e.g. by saving the languages in the DB and getting them there
  * TODO There could be an API module that returns the result of this function
  *
  * @param User $user
  * @param string $level minimal level as given in $wgBabelCategoryNames
  * @return string[] List of language codes
  *
  * @since Version 1.9.0
  */
 public static function getUserLanguages(User $user, $level = null)
 {
     // Right now the function only returns something if the user is categorized appropriately
     // (as defined by the $wgBabelMainCategory setting). If categorization is off, this function
     // will return an empty array.
     // If Babel would save the languages of the user in a Database table, this workaround using
     // the categories would not be needed.
     global $wgBabelMainCategory;
     // If Babel is not configured as required, return nothing.
     // Note also that "Set to false to disable main category".
     if ($wgBabelMainCategory === false) {
         return array();
     }
     // The string we construct here will be a pony, it will not be a valid category
     $babelCategoryTitle = Title::makeTitle(NS_CATEGORY, $wgBabelMainCategory);
     // Quote everything to avoid unexpected matches due to parenthesis form
     // It is not necessary to quote any additional chars except the special chars for the regex
     // and perhaps the limiting char, but that should not be respected as anything other than
     // edge delimiter.
     $babelCategoryString = preg_quote($babelCategoryTitle->getPrefixedDBkey(), '/');
     // Look for the %code% inside the string and put a group match in the same place
     // This will only work if the previous works so the string isn't misinterpreted as a regular
     // expression itself
     $codeRegex = '/^' . preg_replace('/%code%/', '(.+?)(-([0-5N]))?', $babelCategoryString) . '$/';
     $categories = array_keys($user->getUserPage()->getParentCategories());
     // We sort on proficiency level
     $result = array();
     foreach ($categories as $category) {
         // Only process categories that matches, $match will be created if necessary
         $res = preg_match($codeRegex, $category, $match);
         if ($res) {
             // lowercase the first char, but stay away from the others in case of region codes
             $code = BabelLanguageCodes::getCode(lcfirst($match[1]));
             if ($code !== false) {
                 $result[$code] = isset($match[3]) ? $match[3] : 'N';
             }
         }
     }
     if (isset($level)) {
         $level = (string) $level;
         // filter down the set, note that this uses a text sort!
         $result = array_filter($result, function ($value) use($level) {
             return strcmp($value, $level) >= 0;
         });
         // sort and retain keys
         uasort($result, function ($a, $b) {
             return -strcmp($a, $b);
         });
     }
     return array_keys($result);
 }
예제 #3
0
 /**
  * Replace the placeholder variables from the category names configurtion
  * array with actual values.
  *
  * @param $category String: Category name (containing variables).
  * @param $code String: Language code of category.
  * @return String: Category name with variables replaced.
  */
 protected static function mReplaceCategoryVariables($category, $code)
 {
     global $wgLanguageCode;
     $category = strtr($category, array('%code%' => $code, '%wikiname%' => BabelLanguageCodes::getName($code, $wgLanguageCode), '%nativename%' => BabelLanguageCodes::getName($code)));
     return $category;
 }