Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1051849
phrases_cache.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jul 4, 5:18 AM
Size
5 KB
Mime Type
text/x-php
Expires
Sun, Jul 6, 5:18 AM (18 h, 52 m)
Engine
blob
Format
Raw Data
Handle
679384
Attached To
rINP In-Portal
phrases_cache.php
View Options
<?php
class
PhrasesCache
extends
kBase
{
/**
* Connection to database
*
* @var kDBConnection
* @access public
*/
var
$Conn
;
var
$Phrases
=
Array
();
var
$Ids
=
Array
();
var
$OriginalIds
=
Array
();
//for comparing cache
var
$LanguageId
=
1
;
var
$fromTag
=
false
;
function
PhrasesCache
()
{
parent
::
kBase
();
$this
->
Conn
=&
$this
->
Application
->
GetADODBConnection
();
}
function
Init
(
$prefix
,
$special
=
''
)
{
$this
->
LanguageId
=
$this
->
Application
->
GetVar
(
'm_lang'
);
$this
->
LoadPhrases
(
$this
->
GetCachedIds
()
);
}
function
GetCachedIds
()
{
$query
=
sprintf
(
"SELECT PhraseList FROM %s WHERE Template = %s"
,
TABLE_PREFIX
.
'PhraseCache'
,
$this
->
Conn
->
Qstr
(
md5
(
$this
->
Application
->
GetVar
(
't'
))));
$phrases_ids
=
$this
->
Conn
->
GetOne
(
$query
);
return
(
$phrases_ids
===
false
)
?
Array
()
:
explode
(
','
,
$phrases_ids
);
}
function
LoadPhrases
(
$ids
)
{
if
(
!
is_array
(
$ids
)
||
!
implode
(
''
,
$ids
)
)
return
;
$query
=
sprintf
(
"SELECT Translation,Phrase FROM %s WHERE LanguageId = %s AND PhraseId IN (%s)"
,
TABLE_PREFIX
.
'Phrase'
,
$this
->
LanguageId
,
join
(
','
,
$ids
));
$phrases
=
$this
->
Conn
->
GetCol
(
$query
,
'Phrase'
);
foreach
(
$phrases
as
$phrase
=>
$tanslation
)
{
$this
->
AddCachedPhrase
(
strtoupper
(
$phrase
),
$tanslation
);
}
$this
->
Ids
=
$ids
;
$this
->
OriginalIds
=
$ids
;
}
function
AddCachedPhrase
(
$label
,
$value
)
{
$label
=
strtoupper
(
$label
);
$this
->
Phrases
[
$label
]
=
$value
;
}
function
UpdateCache
()
{
if
(!
is_array
(
$this
->
Ids
)
||
count
(
$this
->
Ids
)
==
0
)
return
;
if
(
$this
->
Ids
==
$this
->
OriginalIds
)
return
;
//nothing changed
$query
=
sprintf
(
"REPLACE %s (PhraseList, CacheDate, Template)
VALUES (%s, %s, %s)"
,
TABLE_PREFIX
.
'PhraseCache'
,
$this
->
Conn
->
Qstr
(
join
(
','
,
$this
->
Ids
)),
adodb_mktime
(),
$this
->
Conn
->
Qstr
(
md5
(
$this
->
Application
->
GetVar
(
't'
))));
$this
->
Conn
->
Query
(
$query
);
}
function
GetPhrase
(
$label
)
{
if
(
ereg
(
"^!.+!$"
,
$label
)
>
0
)
{
$label
=
substr
(
$label
,
1
,
-
1
);
//cut exclamation marks
}
if
(
strlen
(
$label
)
==
0
)
return
''
;
$original_label
=
$label
;
$label
=
strtoupper
(
$label
);
if
(
isset
(
$this
->
Phrases
[
$label
])
)
{
$translated_label
=
$this
->
Phrases
[
$label
];
if
(
$this
->
Application
->
isDebugMode
()
&&
dbg_ConstOn
(
'DBG_PHRASES_HILIGHT'
)
&&
!
$this
->
Application
->
IsAdmin
())
$translated_label
=
'<span style="border: 1px solid #999999; background-color: #cccccc; color: #999999; ">'
.
$translated_label
.
'</span></a> <span style="color: red; background-color:#ffffcc">'
.
$original_label
.
'</span>'
;
return
$translated_label
;
}
$this
->
LoadPhraseByLabel
(
$label
,
$original_label
);
return
$this
->
GetPhrase
(
$label
);
}
function
LoadPhraseByLabel
(
$label
,
$original_label
)
{
$query
=
sprintf
(
"SELECT PhraseId, Translation FROM %s WHERE LanguageId = %s AND UPPER(Phrase) = UPPER(%s)"
,
TABLE_PREFIX
.
'Phrase'
,
$this
->
LanguageId
,
$this
->
Conn
->
qstr
(
$label
));
$res
=
$this
->
Conn
->
GetRow
(
$query
);
if
(
$res
===
false
||
count
(
$res
)
==
0
)
{
$translation
=
'!'
.
$label
.
'!'
;
if
(
$this
->
Application
->
isDebugMode
()
&&
dbg_ConstOn
(
'DBG_PHRASES'
)
)
{
list
(
$edit_tpl
,
$index_file
)
=
$this
->
Application
->
IsAdmin
()
?
Array
(
'regional/phrases_edit'
,
'index4.php'
)
:
Array
(
'phrases_edit'
,
'index.php'
);
$edit_url
=
$this
->
Application
->
HREF
(
$edit_tpl
,
''
,
Array
(
'm_opener'
=>
'd'
,
'phrases_label'
=>
$original_label
,
'phrases_event'
=>
'OnNew'
,
'pass'
=>
'all,phrases'
),
$index_file
);
$translation
=
'<a href="'
.
$edit_url
.
'">!'
.
$label
.
'!</a>'
;
if
(
$this
->
fromTag
)
$translation
=
$this
->
escapeTagReserved
(
$translation
);
}
$this
->
AddCachedPhrase
(
$label
,
$translation
);
//add it as already cached, as long as we dont need to cache not found phrase
return
false
;
}
$this
->
Phrases
[
$label
]
=
$res
[
'Translation'
];
array_push
(
$this
->
Ids
,
$res
[
'PhraseId'
]);
$this
->
Ids
=
array_unique
(
$this
->
Ids
);
//just to make sure
return
true
;
}
/**
* Sort params by name and then by length
*
* @param string $a
* @param string $b
* @return int
* @access private
*/
function
CmpParams
(
$a
,
$b
)
{
$a_len
=
strlen
(
$a
);
$b_len
=
strlen
(
$b
);
if
(
$a_len
==
$b_len
)
return
0
;
return
$a_len
>
$b_len
?
-
1
:
1
;
}
/**
* Replace language tags in exclamation marks found in text
*
* @param string $text
* @param bool $force_escape force escaping, not escaping of resulting string
* @return string
* @access public
*/
function
ReplaceLanguageTags
(
$text
,
$forse_escaping
=
null
)
{
$this
->
fromTag
=
true
;
if
(
isset
(
$forse_escaping
)
)
$this
->
fromTag
=
$forse_escaping
;
preg_match_all
(
"(!(la|lu)[^!]+!)"
,
$text
,
$res
,
PREG_PATTERN_ORDER
);
$language_tags
=
$res
[
0
];
uasort
(
$language_tags
,
Array
(&
$this
,
'CmpParams'
)
);
$values
=
Array
();
$i
=
0
;
foreach
(
$language_tags
as
$label
)
{
array_push
(
$values
,
$this
->
GetPhrase
(
$label
)
);
//array_push($values, $this->Application->Phrase($label) );
$language_tags
[
$i
]
=
'/'
.
$language_tags
[
$i
]
.
'/'
;
$i
++;
}
$this
->
fromTag
=
false
;
return
preg_replace
(
$language_tags
,
$values
,
$text
);
}
/**
* Escape chars in phrase translation, that could harm parser to process tag
*
* @param string $text
* @return string
* @access private
*/
function
escapeTagReserved
(
$text
)
{
$reserved
=
Array
(
'"'
,
"'"
);
// =
$replacement
=
Array
(
'
\"
'
,
"
\'
"
);
// \=
return
str_replace
(
$reserved
,
$replacement
,
$text
);
}
}
?>
Event Timeline
Log In to Comment