' );
} elseif ( $wgRequest->getCheck('target_str') ) { // very long elseif, look for "end elseif"
$dbr =& wfGetDB( DB_SLAVE );
$fname = 'doSpecialReplaceText';
$target_str = $wgRequest->getVal('target_str');
$replacement_str = $wgRequest->getVal('replacement_str');
if (! $wgRequest->getCheck('confirm')) {
// display a page to make the user confirm the replacement, if the
// replacement string is either blank or found elsewhere on the wiki
// (since undoing the replacement would be difficult in either case)
if ($replacement_str == '') {
$text = wfMsg('replacetext_blankwarning');
$wgOut->addHTML(ReplaceText::displayConfirmForm($text));
return;
} else {
// get the number of pages in which the replacement string appears
$page_table = $dbr->tableName('page');
$revision_table = $dbr->tableName('revision');
$text_table = $dbr->tableName('text');
$talk_ns = NS_TALK;
$usertalk_ns = NS_USER_TALK;
$mediawiki_ns = NS_MEDIAWIKI;
$sql_replacement_str = str_replace("'", "\'", $replacement_str);
$sql = "SELECT count(*)
FROM $page_table p
JOIN $revision_table r ON p.page_latest = r.rev_id
JOIN $text_table t ON r.rev_text_id = t.old_id
WHERE t.old_text LIKE '%$sql_replacement_str%'
AND p.page_namespace != $talk_ns
AND p.page_namespace != $usertalk_ns
AND p.page_namespace != $mediawiki_ns";
$res = $dbr->query($sql);
$row = $dbr->fetchRow($res);
$num_pages_with_replacement_str = $row[0];
// if there are any, the user most confirm the replacement
if ($num_pages_with_replacement_str > 0) {
$text = wfMsgExt( 'replacetext_warning', array( 'escape', 'parsemag' ), $num_pages_with_replacement_str, $replacement_str );
$wgOut->addHTML(ReplaceText::displayConfirmForm($text));
return;
}
}
}
$jobs = array();
$num_modified_pages = 0;
$found_titles = array();
$titles_for_move = array();
$unmoveable_titles = array();
$angle_brackets = array('<', '>');
$escaped_angle_brackets = array('<', '>');
// get the set of pages that contain the target string, and display
// the name and "context" (the text around the string) of each
$page_table = $dbr->tableName('page');
$revision_table = $dbr->tableName('revision');
$text_table = $dbr->tableName('text');
$talk_ns = NS_TALK;
$usertalk_ns = NS_USER_TALK;
$mediawiki_ns = NS_MEDIAWIKI;
// escape single quote and backslash for SQL - for some reason, the
// backslash needs to be escaped twice (plus once for PHP)
$sql_target_str = str_replace(array("\\", "'"), array("\\\\\\\\", "\'"), $target_str);
# FIXME: suggestions for improvement at http://www.mediawiki.org/wiki/Special:Code/MediaWiki/43239#c608
$sql = "SELECT p.page_title AS title, p.page_namespace AS namespace, t.old_text AS text
FROM $page_table p
JOIN $revision_table r ON p.page_latest = r.rev_id
JOIN $text_table t ON r.rev_text_id = t.old_id
WHERE t.old_text LIKE '%$sql_target_str%'
AND p.page_namespace != $talk_ns
AND p.page_namespace != $usertalk_ns
AND p.page_namespace != $mediawiki_ns
ORDER BY p.page_namespace, p.page_title";
$res = $dbr->query($sql);
$contextchars = $wgUser->getOption( 'contextchars', 40 );
while( $row = $dbr->fetchObject( $res ) ) {
$title = Title::makeTitleSafe($row->namespace, $row->title);
$article_text = $row->text;
$target_pos = strpos($article_text, $target_str);
$context_str = str_replace($angle_brackets, $escaped_angle_brackets, $wgContLang->truncate(substr($article_text, 0, $target_pos), -$contextchars ) );
$context_str .= "" . str_replace($angle_brackets, $escaped_angle_brackets, substr($article_text, $target_pos, strlen($target_str))) . "";
$context_str .= str_replace($angle_brackets, $escaped_angle_brackets, $wgContLang->truncate(substr($article_text, $target_pos + strlen($target_str)), $contextchars ) );
$found_titles[] = array($title, $context_str);
$num_modified_pages++;
}
if ($wgRequest->getCheck('move_pages')) {
$sql_target_str2 = str_replace(' ', '_', $sql_target_str);
$sql2 = "SELECT p.page_title AS title, p.page_namespace AS namespace
FROM $page_table p
WHERE p.page_title LIKE '%$sql_target_str2%'
ORDER BY p.page_namespace, p.page_title";
$res = $dbr->query($sql2);
while( $row = $dbr->fetchObject( $res ) ) {
$title = Title::makeTitleSafe($row->namespace, $row->title);
// see if this move can happen
$cur_page_name = str_replace('_', ' ', $row->title);
$new_page_name = str_replace($target_str, $replacement_str, $cur_page_name);
$new_title = Title::makeTitleSafe($row->namespace, $new_page_name);
$err = $title->isValidMoveOperation($new_title);
if ($title->userCanMove(true) && (! is_array($err))) {
$titles_for_move[] = $title;
$num_modified_pages++;
} else {
$unmoveable_titles[] = $title;
}
}
}
if ($num_modified_pages == 0) {
$wgOut->addHTML(wfMsg('replacetext_noreplacement', $target_str));
// link back to starting form
$rt_title = Title::makeTitleSafe( NS_SPECIAL, 'ReplaceText' );
$sk = $wgUser->getSkin();
$wgOut->addHTML( '