<commit_before><?php
// (c) Copyright 2002-2010 by authors of the Tiki Wiki/CMS/Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

/** \file
 * $Id: /cvsroot/tikiwiki/tiki/get_strings.php
 * \brief Update the language.php files
 * call example: get_strings.php?lang=fr&close

 * \param lang=xx    : Only translate lang 'xx' - if the parameter is not given all languages are translated

 * \param comments   : Generate all comments (equal to close&module)

 * \param close      : Looks for similar strings that are already translated and generate a comment if a 'match' is made

 * \param module     : Generate comments that describe in which .php and/or .tpl\n module(s) a certain string was found (useful for checking translations in context)

 * \param patch      : Looks for the file 'language.patch' in the same directory as the corresponding language.php and overrides any strings in language.php - good if a user does not agree with some translations or if only changes are sent to the maintainer

 * \param spelling   : Generate a file spellcheck_me.txt in the applicable languages directory that contains all the words used in the translated text. This makes it simple to use a spellchecker on the resulting file

 * \param groupwrite : Sets the generated files permissions to allow the generated language.php also be group writable. This is good for translators if they do not have root access to tiki but are in the same group as the webserver. Please remember to have write access removed when translation is finished for security reasons. (Run script again whithout this parameter)

 * \param sort ='n'  : Don't sort the filenames 

 * \param completion=y: Produce only the completion status

 * \param nosections : Don't print sections delimiters

 * \param nohelp     : Don't print help section

 * \param tagunused  : Tags the unused strings with "// ## UNUSED".

 * \param verbose=y  : Display content of language files as they are created.

 * \param quiet  : Do not display listing all files even if completion != y

 */

require_once('lib/language/Language.php');

////////////////////////////////////////////////////////////////////////////
/// functions

// Only accept to call this script:
// - through command line interface
// - through the web interface if authenticated as an admin
//
$script_mode = ! isset( $_SERVER['REQUEST_METHOD'] ) && isset($_SERVER['argc']);

$punctuations = array(':', '!', ';', '.', ',', '?'); // Modify lib/init/tra.php accordingly

/**
 * Reads all the permission descriptions in tiki database and writes
 *   it to the file $file. All the strings will be surrounded by smarty translate tags
 *     ex: {tr}perm description{/tr}
 *
 * @param $file string: target file for the perms
 * @returns: nothing but creates the file with the perms (take care about the acl's in the target directory !)
 */
function collect_perms_desc($file)
{
	global $tikilib;
	if ( isset($tikilib) ) {

		$result = $tikilib->query("SELECT DISTINCT(permDesc) FROM users_permissions ORDER BY permDesc");

		$perm_strings = array();
		while( $row = $result->fetchRow() )
			$perm_strings[] = $row['permDesc'];

	} elseif ( is_readable('db/tiki.sql') ) {

		// Used when called in $script_mode if no DB has been found
		$matches = array();
		preg_match_all(
				'/insert\s+into\s+\`?users_permissions\`?\s*\([^\)]+\)\s*values\s*\(\'(tiki_p_[^\'"]+)\',\s*\'(.*)\',/Uim',
				file_get_contents('db/tiki.sql'),
				$matches
				);

		foreach ( $matches[2] as $permDesc ) {
			$perm_strings[] = str_replace("\'", "'", $permDesc);
		}
		unset($matches);

	} else {
		die('File db/tiki.sql is missing');
	}

	$pstr = fopen($file,'w');
	if (!$pstr) {
		echo "The file $file can not be written";
	} else {
		foreach ($perm_strings as $strg)
		{
			fwrite ($pstr,  "{tr}" . $strg . "{/tr}" . "\n");
		}
		fclose($pstr);
	}
}

/**
 * Get all preferences names from get_default_prefs() function or reads them all from tiki database
 * and writes it to the file $file. All the strings will be surrounded by smarty translate tags
 *     ex: {tr}preference name{/tr}
 *
 * @param $file string: target file for the pref names
 * @returns: nothing but creates the file with the pref names (take care about the acl's in the target directory !)
 */
function collect_prefs_names($file) {

	global $tikilib;
	if ( isset($tikilib) ) {

		$prefs_strings = array();
		$result = $tikilib->query("select `name` from `tiki_preferences`");
		while ( $row = $result->fetchRow() ) $prefs_strings[] = $row['name'];

	} elseif ( function_exists('get_default_prefs') ) {

		// Used when called in $script_mode if no DB has been found
		$prefs_strings = array_keys(get_default_prefs());

	} else {
		die("No 'get_default_prefs' function is available");
	}

	$pstr = fopen($file,'w');
	if (!$pstr) {
		echo "The file $file can not be written";
	} else {
		foreach ($prefs_strings as $strg)
		{
			fwrite ($pstr,  "{tr}" . str_replace('_',' ',$strg) . "{/tr}" . "\n");
		}
		fclose($pstr);
	}
}

function hardwire_file ($file) {
	global $files, $completion, $script_mode, $quiet;
	$files[] = $file;
	if ( ! $completion && ! $quiet ) {
		formatted_print("File (hardwired): $file\n");
	}
}

function collect_files ($dir)
{
	global $files, $completion, $script_mode, $quiet;

	$handle = opendir ($dir);
	while (false !== ($file = readdir ($handle))) {
		// Skip current and parent directory
		// also skip other directories which may contain source code
		// that should not be translated (the directories normally contain
		// temporary results etc.)
		// Please note that these directories will be skipped on all levels
		if ('.'  === $file || '..' === $file || 'Zend' === $file || 
				'htmlpurifier' === $file || 'adodb' === $file || 'smarty' === $file ||
				'ezcomponents' === $file || 'phpcas' === $file || 
				'jscalendar' === $file || 'pclzip' === $file || 'jquery' === $file ||
				'phplayers' === $file || 'pear' === $file || 'ckeditor' === $file ||
				'lang' === $file || 'templates_c' === $file || 'dump'  === $file || 
				'temp' === $file || 'img' === $file || 'cache' === $file) {
			continue;
		}

		$filepath = $dir . '/' . $file;
		if (preg_match ("/.*\.(tpl|php)$/", $file)) {
			if ( ! $completion && ! $quiet ) {
				formatted_print("File: $filepath\n");
			}
			$files[] = $filepath; 
		}
		else {
			if (is_dir ($filepath)) {
				collect_files ($filepath);
			}
		}
	}
	closedir ($handle);
}

function addToWordlist (&$wordlist, &$sentence) {
	global $spelling;<commit_msg>[ENH] get_strings.php ignore php unit tests dir (lib/test)<commit_after><?php
// (c) Copyright 2002-2010 by authors of the Tiki Wiki/CMS/Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

/** \file
 * $Id: /cvsroot/tikiwiki/tiki/get_strings.php
 * \brief Update the language.php files
 * call example: get_strings.php?lang=fr&close

 * \param lang=xx    : Only translate lang 'xx' - if the parameter is not given all languages are translated

 * \param comments   : Generate all comments (equal to close&module)

 * \param close      : Looks for similar strings that are already translated and generate a comment if a 'match' is made

 * \param module     : Generate comments that describe in which .php and/or .tpl\n module(s) a certain string was found (useful for checking translations in context)

 * \param patch      : Looks for the file 'language.patch' in the same directory as the corresponding language.php and overrides any strings in language.php - good if a user does not agree with some translations or if only changes are sent to the maintainer

 * \param spelling   : Generate a file spellcheck_me.txt in the applicable languages directory that contains all the words used in the translated text. This makes it simple to use a spellchecker on the resulting file

 * \param groupwrite : Sets the generated files permissions to allow the generated language.php also be group writable. This is good for translators if they do not have root access to tiki but are in the same group as the webserver. Please remember to have write access removed when translation is finished for security reasons. (Run script again whithout this parameter)

 * \param sort ='n'  : Don't sort the filenames 

 * \param completion=y: Produce only the completion status

 * \param nosections : Don't print sections delimiters

 * \param nohelp     : Don't print help section

 * \param tagunused  : Tags the unused strings with "// ## UNUSED".

 * \param verbose=y  : Display content of language files as they are created.

 * \param quiet  : Do not display listing all files even if completion != y

 */

require_once('lib/language/Language.php');

////////////////////////////////////////////////////////////////////////////
/// functions

// Only accept to call this script:
// - through command line interface
// - through the web interface if authenticated as an admin
//
$script_mode = ! isset( $_SERVER['REQUEST_METHOD'] ) && isset($_SERVER['argc']);

$punctuations = array(':', '!', ';', '.', ',', '?'); // Modify lib/init/tra.php accordingly

/**
 * Reads all the permission descriptions in tiki database and writes
 *   it to the file $file. All the strings will be surrounded by smarty translate tags
 *     ex: {tr}perm description{/tr}
 *
 * @param $file string: target file for the perms
 * @returns: nothing but creates the file with the perms (take care about the acl's in the target directory !)
 */
function collect_perms_desc($file)
{
	global $tikilib;
	if ( isset($tikilib) ) {

		$result = $tikilib->query("SELECT DISTINCT(permDesc) FROM users_permissions ORDER BY permDesc");

		$perm_strings = array();
		while( $row = $result->fetchRow() )
			$perm_strings[] = $row['permDesc'];

	} elseif ( is_readable('db/tiki.sql') ) {

		// Used when called in $script_mode if no DB has been found
		$matches = array();
		preg_match_all(
				'/insert\s+into\s+\`?users_permissions\`?\s*\([^\)]+\)\s*values\s*\(\'(tiki_p_[^\'"]+)\',\s*\'(.*)\',/Uim',
				file_get_contents('db/tiki.sql'),
				$matches
				);

		foreach ( $matches[2] as $permDesc ) {
			$perm_strings[] = str_replace("\'", "'", $permDesc);
		}
		unset($matches);

	} else {
		die('File db/tiki.sql is missing');
	}

	$pstr = fopen($file,'w');
	if (!$pstr) {
		echo "The file $file can not be written";
	} else {
		foreach ($perm_strings as $strg)
		{
			fwrite ($pstr,  "{tr}" . $strg . "{/tr}" . "\n");
		}
		fclose($pstr);
	}
}

/**
 * Get all preferences names from get_default_prefs() function or reads them all from tiki database
 * and writes it to the file $file. All the strings will be surrounded by smarty translate tags
 *     ex: {tr}preference name{/tr}
 *
 * @param $file string: target file for the pref names
 * @returns: nothing but creates the file with the pref names (take care about the acl's in the target directory !)
 */
function collect_prefs_names($file) {

	global $tikilib;
	if ( isset($tikilib) ) {

		$prefs_strings = array();
		$result = $tikilib->query("select `name` from `tiki_preferences`");
		while ( $row = $result->fetchRow() ) $prefs_strings[] = $row['name'];

	} elseif ( function_exists('get_default_prefs') ) {

		// Used when called in $script_mode if no DB has been found
		$prefs_strings = array_keys(get_default_prefs());

	} else {
		die("No 'get_default_prefs' function is available");
	}

	$pstr = fopen($file,'w');
	if (!$pstr) {
		echo "The file $file can not be written";
	} else {
		foreach ($prefs_strings as $strg)
		{
			fwrite ($pstr,  "{tr}" . str_replace('_',' ',$strg) . "{/tr}" . "\n");
		}
		fclose($pstr);
	}
}

function hardwire_file ($file) {
	global $files, $completion, $script_mode, $quiet;
	$files[] = $file;
	if ( ! $completion && ! $quiet ) {
		formatted_print("File (hardwired): $file\n");
	}
}

function collect_files ($dir)
{
	global $files, $completion, $script_mode, $quiet;

	$handle = opendir ($dir);
	while (false !== ($file = readdir ($handle))) {
		// Skip current and parent directory
		// also skip other directories which may contain source code
		// that should not be translated (the directories normally contain
		// temporary results etc.)
		// Please note that these directories will be skipped on all levels
		if ('.'  === $file || '..' === $file || 'Zend' === $file || 
				'htmlpurifier' === $file || 'adodb' === $file || 'smarty' === $file ||
				'ezcomponents' === $file || 'phpcas' === $file || 
				'jscalendar' === $file || 'pclzip' === $file || 'jquery' === $file ||
				'phplayers' === $file || 'pear' === $file || 'ckeditor' === $file ||
				'lang' === $file || 'templates_c' === $file || 'dump'  === $file || 
				'temp' === $file || 'img' === $file || 'cache' === $file ||
				'test' === $file) {
			continue;
		}

		$filepath = $dir . '/' . $file;
		if (preg_match ("/.*\.(tpl|php)$/", $file)) {
			if ( ! $completion && ! $quiet ) {
				formatted_print("File: $filepath\n");
			}
			$files[] = $filepath; 
		}
		else {
			if (is_dir ($filepath)) {
				collect_files ($filepath);
			}
		}
	}
	closedir ($handle);
}

function addToWordlist (&$wordlist, &$sentence) {
	global $spelling;
{'fraction_non_alphanumeric': 0.15823780624859518, 'fraction_numerical': 0.0013486176668914363, 'mean_word_length': 3.4286662242866623, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4961438', 'n_tokens_mistral': 4438, 'n_tokens_neox': 4122, 'n_words': 1462}