<?php
//fix youtube tags in phpbb/xenforo
//new phpbb tag: <YOUTUBE content="sn19xvfoXvk"><s>[youtube]</s>sn19xvfoXvk<e>[/youtube]</e></YOUTUBE>
//old phpbb tag: [youtube][/youtube]
//target: [MEDIA=youtube]HjxZYiTpU3k[/MEDIA]
db::switchDB(2); //use xenforo database
//get array
$postData = db::getArray("SELECT post_id, username, FROM_UNIXTIME(post_date), message FROM xf_post WHERE message LIKE '%youtube%' OR message LIKE '%YOUTUBE content=%'");
zl::quip("Records:" . count($postData));
//html::printTable($origWithUrl);
//accuracy counting
$comprende = 0; $noComprende = 0;
//strips everything to the right of a character, including the character itself.
function stripRightOf($inString, $truncateString)
{
if(strpos($inString, $truncateString) !== false ) { return substr($inString, 0, strpos($inString, $truncateString)); }
else { return $inString; }
}
//returns an array of text that is between tags.
function getBetweenTags($inString, $startTag, $endTag, $includeTags = false)
{
$result = array();
$chunks = explode($startTag, $inString);
foreach($chunks as $chunk)
{
$pos = strpos($chunk, $endTag);
if($pos !== false)
{
if($includeTags) { $result[] = $startTag . substr($chunk, 0, $pos) . $endTag; }
else { $result[] = substr($chunk, 0, $pos); }
}
}
return $result;
}
//chunks to delete ( usually the front )
$replaceArray = array
(
"[youtube][Youtube]",
"[/youtube]",
"[youtube]",
"[youtube",
"[yotube]",
"https://www.youtube.com/edit?o=U&video_id=",
"http://www.youtube.com/watch?time_continue=1&v=",
"https://www.youtube.com/watch?time_continue=1&v=",
"http://www.youtube.com/watch?feature=player_detailpage&v=",
"https://www.youtube.com/watch?feature=player_detailpage&v=",
"https://www.youtube.com/watch?feature=player_embedded&v=",
"http://m.youtube.com/?client=mv-google&rdm=4owzg9jvi#/watch?v=",
"https://www.youtube.com/embed/",
"http://www.youtube.com/embed/",
"http://www.youtube.com/v/",
"https://www.youtube.com/v/",
"http://www.youtube.com/watch?v=",
"https://www.youtube.com/watch?v=",
"https://youtube.com/watch?v=",
"http://youtube.com/watch?v=",
"http://m.youtube.com/watch?v=",
"https://m.youtube.com/watch?v=",
"https://youtu.be/",
"http://youtu.be/",
"www.youtube.com/watch?v=",
"youtube.com/watch?v=",
"player_detailpage&v=",
"youtu.be/",
"watch?v=",
"https://",
"http://",
"[url]",
"[/url]",
"?v=",
"v="
);
?><table class="zl_table">
<tr><th>old</th><th>new</th></tr>
<?php
foreach($postData as $post)
{
$replaceStrings = array(); //what strings to replace?
//pick apart youtube tags
$tags = getBetweenTags(trim($post['message']), "[youtube]" , "[/youtube]", true);
if(!is_empty($tags))
{
foreach($tags as $tag)
{
$newTag = str_replace($replaceArray, "", trim($tag)); //strip what we can
//second phase strip after tag
$newTag = stripRightOf($newTag, "&");
$newTag = stripRightOf($newTag, "?");
$newTag = stripRightOf($newTag, "#");
$newTag = trim($newTag);
//meets valid criteria or nah?
if(strlen($newTag) >= 9 && strlen($newTag) <= 12)
{
$newTag = "[MEDIA=youtube]" . $newTag . "[/MEDIA]";
$replaceStrings[] = array('old' => $tag , 'new' => $newTag);
$comprende++;
}
else
{
//$replaceStrings[] = array('old' => strings::htmlToText($tag), 'new' => "<font color=red>" . strings::htmlToText($newTag) . "</font>");
$noComprende++;
}
}
//replace and update the post in the database.
if(count($replaceStrings) > 0)
{
foreach($replaceStrings as $replaceString)
{
echo "<tr><td>" . $replaceString['old'] . "</td><td>" . $replaceString['new'] . "</td></tr>\n";
$post['message'] = str_replace($replaceString['old'], $replaceString['new'], $post['message']);
}
db::writeRow("UPDATE", "xf_post", array("message" => $post['message']), array("post_id" => $post['post_id']));
}
}
}
?></table><?php
echo "<br>Comprende: " . $comprende . "<br>";
echo "No Comprende: " . $noComprende . "<br><br><br><br><br><br><br><br><br><br>";
?>