getUser()->isAllowed( 'replacetext' ) ) {
throw new PermissionsError( 'replacetext' );
}
$this->setHeaders();
$out = $this->getOutput();
if ( !is_null( $out->getResourceLoader()->getModule( 'mediawiki.special' ) ) ) {
$out->addModuleStyles( 'mediawiki.special' );
}
$this->doSpecialReplaceText();
}
function getSelectedNamespaces() {
$all_namespaces = SearchEngine::searchableNamespaces();
$selected_namespaces = [];
foreach ( $all_namespaces as $ns => $name ) {
if ( $this->getRequest()->getCheck( 'ns' . $ns ) ) {
$selected_namespaces[] = $ns;
}
}
return $selected_namespaces;
}
function doSpecialReplaceText() {
$out = $this->getOutput();
$request = $this->getRequest();
$this->target = $request->getText( 'target' );
$this->replacement = $request->getText( 'replacement' );
$this->use_regex = $request->getBool( 'use_regex' );
$this->category = $request->getText( 'category' );
$this->prefix = $request->getText( 'prefix' );
$this->edit_pages = $request->getBool( 'edit_pages' );
$this->move_pages = $request->getBool( 'move_pages' );
$this->selected_namespaces = $this->getSelectedNamespaces();
if ( $request->getCheck( 'continue' ) && $this->target === '' ) {
$this->showForm( 'replacetext_givetarget' );
return;
}
if ( $request->getCheck( 'replace' ) ) {
global $wgReplaceTextUser;
$replacement_params = [];
if ( $wgReplaceTextUser != null ) {
$user = User::newFromName( $wgReplaceTextUser );
} else {
$user = $this->getUser();
}
$replacement_params['user_id'] = $user->getId();
$replacement_params['target_str'] = $this->target;
$replacement_params['replacement_str'] = $this->replacement;
$replacement_params['use_regex'] = $this->use_regex;
$replacement_params['edit_summary'] = $this->msg(
'replacetext_editsummary',
$this->target, $this->replacement
)->inContentLanguage()->plain();
$replacement_params['create_redirect'] = false;
$replacement_params['watch_page'] = false;
foreach ( $request->getValues() as $key => $value ) {
if ( $key == 'create-redirect' && $value == '1' ) {
$replacement_params['create_redirect'] = true;
} elseif ( $key == 'watch-pages' && $value == '1' ) {
$replacement_params['watch_page'] = true;
}
}
$jobs = [];
foreach ( $request->getValues() as $key => $value ) {
if ( $value == '1' && $key !== 'replace' && $key !== 'use_regex' ) {
if ( strpos( $key, 'move-' ) !== false ) {
$title = Title::newFromID( substr( $key, 5 ) );
$replacement_params['move_page'] = true;
} else {
$title = Title::newFromID( $key );
}
if ( $title !== null ) {
$jobs[] = new ReplaceTextJob( $title, $replacement_params );
}
}
}
JobQueueGroup::singleton()->push( $jobs );
$count = $this->getLanguage()->formatNum( count( $jobs ) );
$out->addWikiMsg(
'replacetext_success',
"
",
"
",
$count
);
// Link back
$out->addHTML(
Linker::link( $this->getTitle(),
$this->msg( 'replacetext_return' )->escaped() )
);
return;
} elseif ( $request->getCheck( 'target' ) ) { // very long elseif, look for "end elseif"
// first, check that at least one namespace has been
// picked, and that either editing or moving pages
// has been selected
if ( count( $this->selected_namespaces ) == 0 ) {
$this->showForm( 'replacetext_nonamespace' );
return;
}
if ( ! $this->edit_pages && ! $this->move_pages ) {
$this->showForm( 'replacetext_editormove' );
return;
}
$titles_for_edit = [];
$titles_for_move = [];
$unmoveable_titles = [];
// if user is replacing text within pages...
if ( $this->edit_pages ) {
$res = ReplaceTextSearch::doSearchQuery(
$this->target,
$this->selected_namespaces,
$this->category,
$this->prefix,
$this->use_regex
);
foreach ( $res as $row ) {
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
if ( $title == null ) {
continue;
}
$context = $this->extractContext( $row->old_text, $this->target, $this->use_regex );
$titles_for_edit[] = [ $title, $context ];
}
}
if ( $this->move_pages ) {
$res = $this->getMatchingTitles(
$this->target,
$this->selected_namespaces,
$this->category,
$this->prefix,
$this->use_regex
);
foreach ( $res as $row ) {
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
if ( $title == null ) {
continue;
}
// See if this move can happen.
$cur_page_name = str_replace( '_', ' ', $row->page_title );
if ( $this->use_regex ) {
$new_page_name =
preg_replace( "/" . $this->target . "/Uu", $this->replacement, $cur_page_name );
} else {
$new_page_name =
str_replace( $this->target, $this->replacement, $cur_page_name );
}
$new_title = Title::makeTitleSafe( $row->page_namespace, $new_page_name );
$err = $title->isValidMoveOperation( $new_title );
if ( $title->userCan( 'move' ) && !is_array( $err ) ) {
$titles_for_move[] = $title;
} else {
$unmoveable_titles[] = $title;
}
}
}
// If no results were found, check to see if a bad
// category name was entered.
if ( count( $titles_for_edit ) == 0 && count( $titles_for_move ) == 0 ) {
$bad_cat_name = false;
if ( !empty( $this->category ) ) {
$category_title = Title::makeTitleSafe( NS_CATEGORY, $this->category );
if ( !$category_title->exists() ) {
$bad_cat_name = true;
}
}
if ( $bad_cat_name ) {
$link = Linker::link( $category_title, htmlspecialchars( ucfirst( $this->category ) ) );
$out->addHTML(
$this->msg( 'replacetext_nosuchcategory' )->rawParams( $link )->escaped()
);
} else {
if ( $this->edit_pages ) {
$out->addWikiMsg(
'replacetext_noreplacement', "
"
);
}
if ( $this->move_pages ) {
$out->addWikiMsg( 'replacetext_nomove', "
" );
}
}
// link back to starting form
$out->addHTML(
'
' . Linker::link( $this->getTitle(), $this->msg( 'replacetext_return' )->escaped() ) . '
' ); } else { // Show a warning message if the replacement // string is either blank or found elsewhere on // the wiki (since undoing the replacement // would be difficult in either case). $warning_msg = null; if ( $this->replacement === '' ) { $warning_msg = $this->msg( 'replacetext_blankwarning' )->text(); } elseif ( $this->use_regex ) { // If it's a regex, don't bother // checking for existing pages - if // the replacement string includes // wildcards, it's a meaningless check. } elseif ( count( $titles_for_edit ) > 0 ) { $res = ReplaceTextSearch::doSearchQuery( $this->replacement, $this->selected_namespaces, $this->category, $this->prefix, $this->use_regex ); $count = $res->numRows(); if ( $count > 0 ) { $warning_msg = $this->msg( 'replacetext_warning' )->numParams( $count ) ->params( "{$this->replacement}
" )->text();
}
} elseif ( count( $titles_for_move ) > 0 ) {
$res = $this->getMatchingTitles(
$this->replacement,
$this->selected_namespaces,
$this->category,
$this->prefix, $this->use_regex
);
$count = $res->numRows();
if ( $count > 0 ) {
$warning_msg = $this->msg( 'replacetext_warning' )->numParams( $count )
->params( $this->replacement )->text();
}
}
if ( ! is_null( $warning_msg ) ) {
$out->addWikiText( "' ); $out->addWikiMsg( 'replacetext_originaltext' ); $out->addHTML( ' | ' ); // 'width: auto' style is needed to override MediaWiki's // normal 'width: 100%', which causes the textarea to get // zero width in IE $out->addHTML( Xml::textarea( 'target', $this->target, 100, 5, [ 'style' => 'width: auto;' ] ) ); $out->addHTML( ' |
' ); $out->addWikiMsg( 'replacetext_replacementtext' ); $out->addHTML( ' | ' ); $out->addHTML( Xml::textarea( 'replacement', $this->replacement, 100, 5, [ 'style' => 'width: auto;' ] ) ); $out->addHTML( ' |
\n" .
Xml::checkLabel(
$this->msg( 'replacetext_editpages' )->text(), 'edit_pages', 'edit_pages', true
) . '
' .
Xml::checkLabel( $this->msg( 'replacetext_movepages' )->text(), 'move_pages', 'move_pages' ) .
"
{$this->target}
",
"{$this->replacement}
",
$wgLang->formatNum( count( $titles_for_edit ) )
);
foreach ( $titles_for_edit as $title_and_context ) {
/**
* @var $title Title
*/
list( $title, $context ) = $title_and_context;
$out->addHTML(
Xml::check( $title->getArticleID(), true ) .
Linker::link( $title ) . " - $context