2011-02-16 13:22:10 +00:00
|
|
|
<?php
|
2008-08-06 16:09:35 +00:00
|
|
|
|
|
|
|
class ReplaceText extends SpecialPage {
|
2011-02-16 03:11:20 +00:00
|
|
|
|
2008-08-06 16:09:35 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2008-09-18 16:29:10 +00:00
|
|
|
public function __construct() {
|
2011-03-24 17:38:40 +00:00
|
|
|
global $wgVersion;
|
2009-04-27 09:02:48 +00:00
|
|
|
parent::__construct( 'ReplaceText', 'replacetext' );
|
2011-03-24 17:38:40 +00:00
|
|
|
if ( version_compare( $wgVersion, '1.16', '<' ) ) {
|
|
|
|
wfLoadExtensionMessages( 'ReplaceText' );
|
|
|
|
}
|
2008-08-06 16:09:35 +00:00
|
|
|
}
|
|
|
|
|
2009-01-08 05:48:04 +00:00
|
|
|
function execute( $query ) {
|
2008-09-18 16:29:10 +00:00
|
|
|
global $wgUser, $wgOut;
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( ! $wgUser->isAllowed( 'replacetext' ) ) {
|
|
|
|
$wgOut->permissionRequired( 'replacetext' );
|
2008-09-18 16:29:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-27 08:52:10 +00:00
|
|
|
$this->user = $wgUser;
|
2008-08-06 16:09:35 +00:00
|
|
|
$this->setHeaders();
|
2011-03-17 16:06:58 +00:00
|
|
|
if ( method_exists( $wgOut, 'addModuleStyles' ) &&
|
|
|
|
!is_null( $wgOut->getResourceLoader()->getModule( 'mediawiki.special' ) ) ) {
|
2011-03-17 13:29:01 +00:00
|
|
|
$wgOut->addModuleStyles( 'mediawiki.special' );
|
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
$this->doSpecialReplaceText();
|
2008-08-06 16:09:35 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 15:19:15 +00:00
|
|
|
static function getSelectedNamespaces() {
|
|
|
|
global $wgRequest;
|
|
|
|
$all_namespaces = SearchEngine::searchableNamespaces();
|
|
|
|
$selected_namespaces = array();
|
2010-03-14 18:10:47 +00:00
|
|
|
foreach ( $all_namespaces as $ns => $name ) {
|
|
|
|
if ( $wgRequest->getCheck( 'ns' . $ns ) ) {
|
2009-05-19 15:19:15 +00:00
|
|
|
$selected_namespaces[] = $ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $selected_namespaces;
|
|
|
|
}
|
|
|
|
|
2011-02-22 19:04:28 +00:00
|
|
|
/**
|
|
|
|
* Helper function to generate a link.
|
|
|
|
*/
|
|
|
|
static function linkToTitle( $skin, $title, $linkText ) {
|
|
|
|
// link() method was added in MW 1.16
|
|
|
|
if ( method_exists( $skin, 'link' ) ) {
|
|
|
|
return $skin->link( $title, $linkText );
|
|
|
|
} else {
|
|
|
|
return $skin->makeKnownLinkObj( $title, $linkText );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-27 08:52:10 +00:00
|
|
|
function doSpecialReplaceText() {
|
|
|
|
global $wgUser, $wgOut, $wgRequest, $wgLang;
|
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
$this->target = $wgRequest->getText( 'target' );
|
|
|
|
$this->replacement = $wgRequest->getText( 'replacement' );
|
2011-02-16 03:11:20 +00:00
|
|
|
$this->use_regex = ( $wgRequest->getVal( 'use_regex' ) == 1 );
|
2009-08-14 17:28:25 +00:00
|
|
|
$this->category = $wgRequest->getText( 'category' );
|
|
|
|
$this->prefix = $wgRequest->getText( 'prefix' );
|
2010-03-14 18:10:47 +00:00
|
|
|
$this->edit_pages = ( $wgRequest->getVal( 'edit_pages' ) == 1 );
|
|
|
|
$this->move_pages = ( $wgRequest->getVal( 'move_pages' ) == 1 );
|
2009-05-21 13:27:58 +00:00
|
|
|
$this->selected_namespaces = self::getSelectedNamespaces();
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( $wgRequest->getCheck( 'continue' ) ) {
|
|
|
|
if ( $this->target === '' ) {
|
|
|
|
$this->showForm( 'replacetext_givetarget' );
|
|
|
|
return;
|
|
|
|
}
|
2009-01-22 23:40:30 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( $wgRequest->getCheck( 'replace' ) ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$replacement_params = array();
|
|
|
|
$replacement_params['user_id'] = $wgUser->getId();
|
2009-04-30 18:55:08 +00:00
|
|
|
$replacement_params['target_str'] = $this->target;
|
|
|
|
$replacement_params['replacement_str'] = $this->replacement;
|
2011-02-16 03:11:20 +00:00
|
|
|
$replacement_params['use_regex'] = $this->use_regex;
|
2009-04-30 18:55:08 +00:00
|
|
|
$replacement_params['edit_summary'] = wfMsgForContent( 'replacetext_editsummary', $this->target, $this->replacement );
|
2009-04-27 08:52:10 +00:00
|
|
|
$replacement_params['create_redirect'] = false;
|
2009-04-27 18:48:27 +00:00
|
|
|
$replacement_params['watch_page'] = false;
|
2009-04-27 09:02:48 +00:00
|
|
|
foreach ( $wgRequest->getValues() as $key => $value ) {
|
|
|
|
if ( $key == 'create-redirect' && $value == '1' ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$replacement_params['create_redirect'] = true;
|
2009-04-27 18:48:27 +00:00
|
|
|
} elseif ( $key == 'watch-pages' && $value == '1' ) {
|
|
|
|
$replacement_params['watch_page'] = true;
|
2008-11-05 20:37:50 +00:00
|
|
|
}
|
2008-10-13 12:34:00 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
$jobs = array();
|
2009-04-27 09:02:48 +00:00
|
|
|
foreach ( $wgRequest->getValues() as $key => $value ) {
|
2009-07-16 19:54:10 +00:00
|
|
|
if ( $value == '1' && $key !== 'replace' ) {
|
|
|
|
if ( strpos( $key, 'move-' ) !== false ) {
|
2009-05-19 15:19:15 +00:00
|
|
|
$title = Title::newFromID( substr( $key, 5 ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
$replacement_params['move_page'] = true;
|
|
|
|
} else {
|
2009-05-19 15:19:15 +00:00
|
|
|
$title = Title::newFromID( $key );
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( $title !== null )
|
2009-04-27 08:52:10 +00:00
|
|
|
$jobs[] = new ReplaceTextJob( $title, $replacement_params );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Job::batchInsert( $jobs );
|
|
|
|
|
2011-02-16 03:11:20 +00:00
|
|
|
$count = $wgLang->formatNum( count( $jobs ) );
|
2009-05-19 16:55:59 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_success', "<tt><nowiki>{$this->target}</nowiki></tt>", "<tt><nowiki>{$this->replacement}</nowiki></tt>", $count );
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
// Link back
|
2009-04-27 18:48:27 +00:00
|
|
|
$sk = $this->user->getSkin();
|
2011-02-22 19:04:28 +00:00
|
|
|
$wgOut->addHTML( $this->linkToTitle( $sk, $this->getTitle(), wfMsgHtml( 'replacetext_return' ) ) );
|
2009-05-05 20:09:54 +00:00
|
|
|
return;
|
2009-04-27 09:02:48 +00:00
|
|
|
} elseif ( $wgRequest->getCheck( 'target' ) ) { // very long elseif, look for "end elseif"
|
2009-05-21 13:27:58 +00:00
|
|
|
// first, check that at least one namespace has been
|
|
|
|
// picked, and that either editing or moving pages
|
2009-04-27 18:48:27 +00:00
|
|
|
// has been selected
|
2009-05-21 13:27:58 +00:00
|
|
|
if ( count( $this->selected_namespaces ) == 0 ) {
|
|
|
|
$this->showForm( 'replacetext_nonamespace' );
|
|
|
|
return;
|
|
|
|
}
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( ! $this->edit_pages && ! $this->move_pages ) {
|
2009-04-27 18:48:27 +00:00
|
|
|
$this->showForm( 'replacetext_editormove' );
|
|
|
|
return;
|
2008-10-13 12:34:00 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
$jobs = array();
|
2009-04-27 18:48:27 +00:00
|
|
|
$titles_for_edit = array();
|
2009-04-27 08:52:10 +00:00
|
|
|
$titles_for_move = array();
|
|
|
|
$unmoveable_titles = array();
|
|
|
|
|
2009-04-27 20:55:00 +00:00
|
|
|
// if user is replacing text within pages...
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( $this->edit_pages ) {
|
2011-02-16 03:11:20 +00:00
|
|
|
$res = $this->doSearchQuery( $this->target, $this->selected_namespaces, $this->category, $this->prefix , $this->use_regex );
|
2009-04-27 18:48:27 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
|
2011-02-16 03:11:20 +00:00
|
|
|
$context = $this->extractContext( $row->old_text, $this->target, $this->use_regex );
|
2009-04-27 18:48:27 +00:00
|
|
|
$titles_for_edit[] = array( $title, $context );
|
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( $this->move_pages ) {
|
2011-02-16 03:11:20 +00:00
|
|
|
$res = $this->getMatchingTitles( $this->target, $this->selected_namespaces, $this->category, $this->prefix, $this->use_regex );
|
2009-04-27 08:52:10 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-04-27 09:02:48 +00:00
|
|
|
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
|
2009-04-27 08:52:10 +00:00
|
|
|
// see if this move can happen
|
2010-03-14 18:10:47 +00:00
|
|
|
$cur_page_name = str_replace( '_', ' ', $row->page_title );
|
2011-02-16 03:11:20 +00:00
|
|
|
if ( $this->use_regex ) {
|
|
|
|
$new_page_name = preg_replace( "/".$this->target."/U", $this->replacement, $cur_page_name );
|
|
|
|
} else {
|
|
|
|
$new_page_name = str_replace( $this->target, $this->replacement, $cur_page_name );
|
|
|
|
}
|
2009-04-30 22:23:59 +00:00
|
|
|
$new_title = Title::makeTitleSafe( $row->page_namespace, $new_page_name );
|
2009-04-27 09:02:48 +00:00
|
|
|
$err = $title->isValidMoveOperation( $new_title );
|
2009-05-24 08:52:19 +00:00
|
|
|
if ( $title->userCan( 'move', true ) && !is_array( $err ) ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$titles_for_move[] = $title;
|
|
|
|
} else {
|
|
|
|
$unmoveable_titles[] = $title;
|
|
|
|
}
|
2008-11-05 20:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-08 00:04:25 +00:00
|
|
|
// if no results were found, check to see if a bad
|
|
|
|
// category name was entered
|
2010-03-14 18:10:47 +00:00
|
|
|
if ( count( $titles_for_edit ) == 0 && count( $titles_for_move ) == 0 ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$sk = $this->user->getSkin();
|
2009-09-08 00:04:25 +00:00
|
|
|
$bad_cat_name = false;
|
2010-03-14 18:10:47 +00:00
|
|
|
if ( ! empty( $this->category ) ) {
|
|
|
|
$category_title = Title::makeTitleSafe( NS_CATEGORY, $this->category );
|
|
|
|
if ( ! $category_title->exists() ) $bad_cat_name = true;
|
2009-09-08 00:04:25 +00:00
|
|
|
}
|
2010-03-14 18:10:47 +00:00
|
|
|
if ( $bad_cat_name ) {
|
2010-08-07 23:36:37 +00:00
|
|
|
//FIXME: raw html message
|
2011-02-22 19:04:28 +00:00
|
|
|
$wgOut->addHTML( wfMsg( 'replacetext_nosuchcategory', $this->linkToTitle( $sk, $category_title, ucfirst( $this->category ) ) ) );
|
2009-09-08 00:04:25 +00:00
|
|
|
} else {
|
|
|
|
if ( $this->edit_pages )
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_noreplacement', "<tt><nowiki>{$this->target}</nowiki></tt>" );
|
|
|
|
if ( $this->move_pages )
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_nomove', "<tt><nowiki>{$this->target}</nowiki></tt>" );
|
|
|
|
}
|
|
|
|
// link back to starting form
|
2010-08-07 23:36:37 +00:00
|
|
|
//FIXME: raw html message
|
2011-02-22 19:04:28 +00:00
|
|
|
$wgOut->addHTML( '<p>' . $this->linkToTitle( $sk, $this->getTitle(), wfMsg( 'replacetext_return' ) ) . '</p>' );
|
2009-04-27 08:52:10 +00:00
|
|
|
} else {
|
2011-02-16 03:11:20 +00:00
|
|
|
// 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).
|
2010-03-18 03:39:45 +00:00
|
|
|
$warning_msg = null;
|
|
|
|
|
|
|
|
if ( $this->replacement === '' ) {
|
|
|
|
$warning_msg = wfMsg('replacetext_blankwarning');
|
|
|
|
} elseif ( count( $titles_for_edit ) > 0 ) {
|
2011-02-16 03:11:20 +00:00
|
|
|
$res = $this->doSearchQuery( $this->replacement, $this->selected_namespaces, $this->category, $this->prefix, $this->use_regex );
|
2010-03-18 03:39:45 +00:00
|
|
|
$count = $res->numRows();
|
|
|
|
if ( $count > 0 ) {
|
2010-03-19 12:36:01 +00:00
|
|
|
$warning_msg = wfMsgExt( 'replacetext_warning', 'parsemag',
|
|
|
|
$wgLang->formatNum( $count ),
|
|
|
|
"<tt><nowiki>{$this->replacement}</nowiki></tt>"
|
|
|
|
);
|
2010-03-18 03:39:45 +00:00
|
|
|
}
|
|
|
|
} elseif ( count( $titles_for_move ) > 0 ) {
|
2011-02-16 03:11:20 +00:00
|
|
|
$res = $this->getMatchingTitles( $this->replacement, $this->selected_namespaces, $this->category, $this->prefix, $this->use_regex );
|
2010-03-18 03:39:45 +00:00
|
|
|
$count = $res->numRows();
|
|
|
|
if ( $count > 0 ) {
|
2010-03-19 12:36:01 +00:00
|
|
|
$warning_msg = wfMsgExt( 'replacetext_warning', 'parsemag',
|
|
|
|
$wgLang->formatNum( $count ),
|
|
|
|
$this->replacement
|
|
|
|
);
|
2010-03-18 03:39:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_null( $warning_msg ) ) {
|
|
|
|
$wgOut->addWikiText("<div class=\"errorbox\">$warning_msg</div><br clear=\"both\" />");
|
|
|
|
}
|
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
$this->pageListForm( $titles_for_edit, $titles_for_move, $unmoveable_titles );
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
2009-04-30 18:55:08 +00:00
|
|
|
return;
|
2008-11-05 20:37:50 +00:00
|
|
|
}
|
2009-04-30 18:55:08 +00:00
|
|
|
|
|
|
|
// if we're still here, show the starting form
|
2010-03-18 03:39:45 +00:00
|
|
|
$this->showForm();
|
2008-08-06 16:09:35 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2010-03-18 03:39:45 +00:00
|
|
|
function showForm( $warning_msg = null ) {
|
2011-02-16 03:11:20 +00:00
|
|
|
global $wgOut;
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML(
|
2010-03-18 03:39:45 +00:00
|
|
|
Xml::openElement( 'form', array( 'id' => 'powersearch', 'action' => $this->getTitle()->getFullUrl(), 'method' => 'post' ) ) .
|
2011-05-06 23:13:09 +00:00
|
|
|
Html::Hidden( 'title', $this->getTitle()->getPrefixedText() ) .
|
|
|
|
Html::Hidden( 'continue', 1 )
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
2010-03-18 03:39:45 +00:00
|
|
|
if ( is_null( $warning_msg ) ) {
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_docu' );
|
|
|
|
} else {
|
2010-08-07 23:36:37 +00:00
|
|
|
$wgOut->wrapWikiMsg( "<div class=\"errorbox\">\n$1\n</div><br clear=\"both\" />", $warning_msg );
|
2010-03-18 03:39:45 +00:00
|
|
|
}
|
2009-04-30 18:55:08 +00:00
|
|
|
$wgOut->addHTML( '<table><tr><td style="vertical-align: top;">' );
|
2009-04-27 18:48:27 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_originaltext' );
|
|
|
|
$wgOut->addHTML( '</td><td>' );
|
2009-05-27 04:59:00 +00:00
|
|
|
// 'width: auto' style is needed to override MediaWiki's
|
|
|
|
// normal 'width: 100%', which causes the textarea to get
|
|
|
|
// zero width in IE
|
|
|
|
$wgOut->addHTML( Xml::textarea( 'target', $this->target, 50, 2, array( 'style' => 'width: auto;' ) ) );
|
2009-04-30 18:55:08 +00:00
|
|
|
$wgOut->addHTML( '</td></tr><tr><td style="vertical-align: top;">' );
|
2009-04-27 18:48:27 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_replacementtext' );
|
|
|
|
$wgOut->addHTML( '</td><td>' );
|
2009-05-27 04:59:00 +00:00
|
|
|
$wgOut->addHTML( Xml::textarea( 'replacement', $this->replacement, 50, 2, array( 'style' => 'width: auto;' ) ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML( '</td></tr></table>' );
|
2011-02-16 03:11:20 +00:00
|
|
|
$wgOut->addHTML( Xml::tags( 'p', null,
|
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_useregex' ), 'use_regex', 'use_regex' ) ) . "\n" .
|
|
|
|
Xml::element( 'p', array( 'style' => 'font-style: italic' ),
|
|
|
|
wfMsg( 'replacetext_regexdocu' ) )
|
|
|
|
);
|
2009-05-19 15:19:15 +00:00
|
|
|
|
2011-02-16 03:11:20 +00:00
|
|
|
// The interface is heavily based on the one in Special:Search.
|
2010-03-14 18:10:47 +00:00
|
|
|
$search_label = wfMsg( 'powersearch-ns' );
|
2009-08-14 17:28:25 +00:00
|
|
|
$namespaces = SearchEngine::searchableNamespaces();
|
|
|
|
$tables = $this->namespaceTables( $namespaces );
|
2010-03-18 03:39:45 +00:00
|
|
|
$wgOut->addHTML(
|
|
|
|
"<div class=\"mw-search-formheader\"></div>\n" .
|
|
|
|
"<fieldset id=\"mw-searchoptions\">\n" .
|
|
|
|
Xml::tags( 'h4', null, wfMsgExt( 'powersearch-ns', array( 'parseinline' ) ) )
|
|
|
|
);
|
2011-02-16 03:11:20 +00:00
|
|
|
// The ability to select/unselect groups of namespaces in the
|
2010-10-04 16:20:30 +00:00
|
|
|
// search interface exists only in some skins, like Vector -
|
|
|
|
// check for the presence of the 'powersearch-togglelabel'
|
2011-02-16 03:11:20 +00:00
|
|
|
// message to see if we can use this functionality here.
|
2010-08-07 23:36:37 +00:00
|
|
|
if ( !wfEmptyMsg( 'powersearch-togglelabel', wfMsg( 'powersearch-togglelabel' ) ) ) {
|
2010-03-18 03:39:45 +00:00
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::tags(
|
|
|
|
'div',
|
|
|
|
array( 'id' => 'mw-search-togglebox' ),
|
|
|
|
Xml::label( wfMsg( 'powersearch-togglelabel' ), 'mw-search-togglelabel' ) .
|
|
|
|
Xml::element(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type'=>'button',
|
|
|
|
'id' => 'mw-search-toggleall',
|
|
|
|
'onclick' => 'mwToggleSearchCheckboxes("all");',
|
|
|
|
'value' => wfMsg( 'powersearch-toggleall' )
|
|
|
|
)
|
|
|
|
) .
|
|
|
|
Xml::element(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type'=>'button',
|
|
|
|
'id' => 'mw-search-togglenone',
|
|
|
|
'onclick' => 'mwToggleSearchCheckboxes("none");',
|
|
|
|
'value' => wfMsg( 'powersearch-togglenone' )
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} // end if
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
|
|
|
|
"$tables\n</fieldset>"
|
|
|
|
);
|
2010-08-07 23:36:37 +00:00
|
|
|
//FIXME: raw html messages
|
2010-03-14 18:10:47 +00:00
|
|
|
$optional_filters_label = wfMsg( 'replacetext_optionalfilters' );
|
|
|
|
$category_search_label = wfMsg( 'replacetext_categorysearch' );
|
|
|
|
$prefix_search_label = wfMsg( 'replacetext_prefixsearch' );
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML(
|
2010-03-18 03:39:45 +00:00
|
|
|
"<fieldset id=\"mw-searchoptions\">\n" .
|
|
|
|
Xml::tags( 'h4', null, wfMsgExt( 'replacetext_optionalfilters', array( 'parseinline' ) ) ) .
|
|
|
|
Xml::element( 'div', array( 'class' => 'divider' ), '', false ) .
|
2009-08-14 18:50:41 +00:00
|
|
|
"<p>$category_search_label\n" .
|
2009-08-14 17:28:25 +00:00
|
|
|
Xml::input( 'category', 20, $this->category ) . '</p>' .
|
2009-08-14 18:50:41 +00:00
|
|
|
"<p>$prefix_search_label\n" .
|
2009-08-14 17:28:25 +00:00
|
|
|
Xml::input( 'prefix', 20, $this->prefix ) . '</p>' .
|
|
|
|
"</fieldset>\n" .
|
2010-03-18 03:39:45 +00:00
|
|
|
"<p>\n" .
|
2009-04-27 18:48:27 +00:00
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_editpages' ), 'edit_pages', 'edit_pages', true ) . '<br />' .
|
2010-03-18 03:39:45 +00:00
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_movepages' ), 'move_pages', 'move_pages' ) .
|
|
|
|
"</p>\n" .
|
2009-04-27 08:52:10 +00:00
|
|
|
Xml::submitButton( wfMsg( 'replacetext_continue' ) ) .
|
|
|
|
Xml::closeElement( 'form' )
|
|
|
|
);
|
2010-03-18 03:39:45 +00:00
|
|
|
// add javascript specific to special:search
|
|
|
|
$wgOut->addScriptFile( 'search.js' );
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
|
|
|
|
2009-05-19 15:19:15 +00:00
|
|
|
/**
|
|
|
|
* Copied almost exactly from MediaWiki's SpecialSearch class, i.e.
|
|
|
|
* the search page
|
|
|
|
*/
|
2010-03-18 03:39:45 +00:00
|
|
|
function namespaceTables( $namespaces, $rowsPerTable = 3 ) {
|
|
|
|
global $wgContLang;
|
|
|
|
// Group namespaces into rows according to subject.
|
|
|
|
// Try not to make too many assumptions about namespace numbering.
|
|
|
|
$rows = array();
|
|
|
|
$tables = "";
|
|
|
|
foreach ( $namespaces as $ns => $name ) {
|
|
|
|
$subj = MWNamespace::getSubject( $ns );
|
|
|
|
if ( !array_key_exists( $subj, $rows ) ) {
|
|
|
|
$rows[$subj] = "";
|
|
|
|
}
|
|
|
|
$name = str_replace( '_', ' ', $name );
|
|
|
|
if ( '' == $name ) {
|
|
|
|
$name = wfMsg( 'blanknamespace' );
|
|
|
|
}
|
|
|
|
$rows[$subj] .= Xml::openElement( 'td', array( 'style' => 'white-space: nowrap' ) ) .
|
|
|
|
Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $namespaces ) ) .
|
|
|
|
Xml::closeElement( 'td' ) . "\n";
|
|
|
|
}
|
|
|
|
$rows = array_values( $rows );
|
|
|
|
$numRows = count( $rows );
|
|
|
|
// Lay out namespaces in multiple floating two-column tables so they'll
|
|
|
|
// be arranged nicely while still accommodating different screen widths
|
|
|
|
// Float to the right on RTL wikis
|
|
|
|
$tableStyle = $wgContLang->isRTL() ?
|
|
|
|
'float: right; margin: 0 0 0em 1em' : 'float: left; margin: 0 1em 0em 0';
|
|
|
|
// Build the final HTML table...
|
|
|
|
for ( $i = 0; $i < $numRows; $i += $rowsPerTable ) {
|
|
|
|
$tables .= Xml::openElement( 'table', array( 'style' => $tableStyle ) );
|
|
|
|
for ( $j = $i; $j < $i + $rowsPerTable && $j < $numRows; $j++ ) {
|
|
|
|
$tables .= "<tr>\n" . $rows[$j] . "</tr>";
|
|
|
|
}
|
|
|
|
$tables .= Xml::closeElement( 'table' ) . "\n";
|
|
|
|
}
|
|
|
|
return $tables;
|
|
|
|
}
|
2009-05-19 15:19:15 +00:00
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
function pageListForm( $titles_for_edit, $titles_for_move, $unmoveable_titles ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
global $wgOut, $wgLang, $wgScript;
|
|
|
|
|
2009-04-27 18:48:27 +00:00
|
|
|
$skin = $this->user->getSkin();
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
$formOpts = array( 'id' => 'choose_pages', 'method' => 'post', 'action' => $this->getTitle()->getFullUrl() );
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::openElement( 'form', $formOpts ) .
|
2011-05-06 23:13:09 +00:00
|
|
|
Html::Hidden( 'title', $this->getTitle()->getPrefixedText() ) .
|
|
|
|
Html::Hidden( 'target', $this->target ) .
|
|
|
|
Html::Hidden( 'replacement', $this->replacement ) .
|
|
|
|
Html::Hidden( 'use_regex', $this->use_regex )
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
$js = file_get_contents( dirname( __FILE__ ) . '/ReplaceText.js' );
|
|
|
|
$js = '<script type="text/javascript">' . $js . '</script>';
|
|
|
|
$wgOut->addScript( $js );
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( count( $titles_for_edit ) > 0 ) {
|
2009-05-08 20:02:06 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_choosepagesforedit', "<tt><nowiki>{$this->target}</nowiki></tt>", "<tt><nowiki>{$this->replacement}</nowiki></tt>",
|
2009-04-27 18:48:27 +00:00
|
|
|
$wgLang->formatNum( count( $titles_for_edit ) ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2009-04-27 18:48:27 +00:00
|
|
|
foreach ( $titles_for_edit as $title_and_context ) {
|
|
|
|
list( $title, $context ) = $title_and_context;
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::check( $title->getArticleID(), true ) .
|
2011-02-22 19:04:28 +00:00
|
|
|
$this->linkToTitle( $skin, $title, $title->getPrefixedText() ) . " - <small>$context</small><br />\n"
|
2009-04-27 18:48:27 +00:00
|
|
|
);
|
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML( '<br />' );
|
2009-04-27 18:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $titles_for_move ) > 0 ) {
|
2009-04-30 18:55:08 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_choosepagesformove', $this->target, $this->replacement, $wgLang->formatNum( count( $titles_for_move ) ) );
|
2009-04-27 09:02:48 +00:00
|
|
|
foreach ( $titles_for_move as $title ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML(
|
2009-04-27 09:02:48 +00:00
|
|
|
Xml::check( 'move-' . $title->getArticleID(), true ) .
|
2011-05-08 03:16:03 +00:00
|
|
|
$skin->makeLinkObj( $title, htmlspecialchars( $title->getPrefixedText() ) ) . "<br />\n"
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
|
|
|
}
|
2009-04-27 18:48:27 +00:00
|
|
|
$wgOut->addHTML( '<br />' );
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_formovedpages' );
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML(
|
2009-04-27 18:48:27 +00:00
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_savemovedpages' ), 'create-redirect', 'create-redirect', true ) . "<br />\n" .
|
2009-05-05 20:09:54 +00:00
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_watchmovedpages' ), 'watch-pages', 'watch-pages', false )
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
2009-04-27 18:48:27 +00:00
|
|
|
$wgOut->addHTML( '<br />' );
|
2008-11-05 20:37:50 +00:00
|
|
|
}
|
2008-09-02 17:34:40 +00:00
|
|
|
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML(
|
2009-04-27 18:48:27 +00:00
|
|
|
"<br />\n" .
|
2009-04-27 09:02:48 +00:00
|
|
|
Xml::submitButton( wfMsg( 'replacetext_replace' ) ) .
|
2011-05-06 23:13:09 +00:00
|
|
|
Html::Hidden( 'replace', 1 )
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
|
|
|
|
2011-02-16 03:11:20 +00:00
|
|
|
// Only show "invert selections" link if there are more than
|
|
|
|
// five pages.
|
2009-04-27 18:48:27 +00:00
|
|
|
if ( count( $titles_for_edit ) + count( $titles_for_move ) > 5 ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$buttonOpts = array(
|
|
|
|
'type' => 'button',
|
|
|
|
'value' => wfMsg( 'replacetext_invertselections' ),
|
|
|
|
'onclick' => 'invertSelections(); return false;'
|
|
|
|
);
|
|
|
|
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::element( 'input', $buttonOpts )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$wgOut->addHTML( '</form>' );
|
2008-08-06 16:09:35 +00:00
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( count( $unmoveable_titles ) > 0 ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_cannotmove', $wgLang->formatNum( count( $unmoveable_titles ) ) );
|
|
|
|
$text = "<ul>\n";
|
2009-04-27 09:02:48 +00:00
|
|
|
foreach ( $unmoveable_titles as $title ) {
|
2011-02-22 19:04:28 +00:00
|
|
|
$text .= "<li>{$this->linkToTitle( $skin, $title, $title->getPrefixedText() )}<br />\n";
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
2008-11-05 20:37:50 +00:00
|
|
|
$text .= "</ul>\n";
|
2009-04-27 09:02:48 +00:00
|
|
|
$wgOut->addHTML( $text );
|
2008-10-13 12:34:00 +00:00
|
|
|
}
|
2008-11-05 20:37:50 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract context and highlights search text
|
2011-02-16 03:11:20 +00:00
|
|
|
*
|
|
|
|
* TODO: The bolding needs to be fixed for regular expressions.
|
2009-04-27 08:52:10 +00:00
|
|
|
*/
|
2011-02-16 03:11:20 +00:00
|
|
|
function extractContext( $text, $target, $use_regex = false ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
global $wgLang;
|
2011-02-16 03:11:20 +00:00
|
|
|
|
2009-04-27 08:52:10 +00:00
|
|
|
$cw = $this->user->getOption( 'contextchars', 40 );
|
|
|
|
|
|
|
|
// Get all indexes
|
2011-02-16 03:11:20 +00:00
|
|
|
if ( $use_regex ) {
|
|
|
|
preg_match_all( "/$target/", $text, $matches, PREG_OFFSET_CAPTURE );
|
|
|
|
} else {
|
|
|
|
$targetq = preg_quote( $target, '/' );
|
|
|
|
preg_match_all( "/$targetq/", $text, $matches, PREG_OFFSET_CAPTURE );
|
|
|
|
}
|
|
|
|
|
2009-04-27 08:52:10 +00:00
|
|
|
$poss = array();
|
|
|
|
foreach ( $matches[0] as $_ ) {
|
|
|
|
$poss[] = $_[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$cuts = array();
|
2009-04-27 09:02:48 +00:00
|
|
|
for ( $i = 0; $i < count( $poss ); $i++ ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$index = $poss[$i];
|
2009-04-27 09:02:48 +00:00
|
|
|
$len = strlen( $target );
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
// Merge to the next if possible
|
2009-04-27 09:02:48 +00:00
|
|
|
while ( isset( $poss[$i + 1] ) ) {
|
|
|
|
if ( $poss[$i + 1] < $index + $len + $cw * 2 ) {
|
|
|
|
$len += $poss[$i + 1] - $poss[$i];
|
2009-04-27 08:52:10 +00:00
|
|
|
$i++;
|
|
|
|
} else {
|
|
|
|
break; // Can't merge, exit the inner loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cuts[] = array( $index, $len );
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = '';
|
2009-04-27 09:02:48 +00:00
|
|
|
foreach ( $cuts as $_ ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
list( $index, $len, ) = $_;
|
2011-03-24 03:43:27 +00:00
|
|
|
$context .= self::convertWhiteSpaceToHTML(
|
|
|
|
$wgLang->truncate( substr( $text, 0, $index ), - $cw, '...', false )
|
|
|
|
);
|
2009-04-27 08:52:10 +00:00
|
|
|
$snippet = self::convertWhiteSpaceToHTML( substr( $text, $index, $len ) );
|
2011-02-16 03:11:20 +00:00
|
|
|
if ( $use_regex ) {
|
|
|
|
$targetStr = "/$target/U";
|
|
|
|
} else {
|
|
|
|
$targetq = preg_quote( self::convertWhiteSpaceToHTML( $target ), '/' );
|
|
|
|
$targetStr = "/$targetq/i";
|
|
|
|
}
|
|
|
|
$context .= preg_replace( $targetStr, '<span class="searchmatch">\0</span>', $snippet );
|
|
|
|
|
2011-03-24 03:43:27 +00:00
|
|
|
$context .= self::convertWhiteSpaceToHTML(
|
|
|
|
$wgLang->truncate( substr( $text, $index + $len ), $cw, '...', false )
|
|
|
|
);
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function convertWhiteSpaceToHTML( $msg ) {
|
|
|
|
$msg = htmlspecialchars( $msg );
|
2010-05-30 17:33:59 +00:00
|
|
|
$msg = preg_replace( '/^ /m', '  ', $msg );
|
|
|
|
$msg = preg_replace( '/ $/m', '  ', $msg );
|
|
|
|
$msg = preg_replace( '/ /', '  ', $msg );
|
2009-04-27 09:02:48 +00:00
|
|
|
# $msg = str_replace( "\n", '<br />', $msg );
|
2009-04-27 08:52:10 +00:00
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2011-02-16 03:11:20 +00:00
|
|
|
function getMatchingTitles( $str, $namespaces, $category, $prefix, $use_regex = false ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2010-07-30 17:45:36 +00:00
|
|
|
|
2009-08-14 17:28:25 +00:00
|
|
|
$tables = array( 'page' );
|
|
|
|
$vars = array( 'page_title', 'page_namespace' );
|
2011-03-15 20:30:32 +00:00
|
|
|
|
|
|
|
$str = str_replace( ' ', '_', $str );
|
2011-02-16 03:11:20 +00:00
|
|
|
if ( $use_regex ) {
|
2011-03-11 18:39:52 +00:00
|
|
|
$comparisonCond = 'page_title REGEXP ' . $dbr->addQuotes( $str );
|
2010-10-04 16:20:30 +00:00
|
|
|
} else {
|
2011-02-16 03:11:20 +00:00
|
|
|
// anyString() method was added in MW 1.16
|
|
|
|
if ( method_exists( $dbr, 'anyString' ) ) {
|
|
|
|
$any = $dbr->anyString();
|
|
|
|
$comparisonCond = 'page_title ' . $dbr->buildLike( $any, $str, $any );
|
|
|
|
} else {
|
2011-03-11 18:39:52 +00:00
|
|
|
$comparisonCond = 'page_title LIKE ' . $dbr->addQuotes( "%$str%" );
|
2011-02-16 03:11:20 +00:00
|
|
|
}
|
2010-10-04 16:20:30 +00:00
|
|
|
}
|
2011-02-16 03:11:20 +00:00
|
|
|
$conds = array(
|
|
|
|
$comparisonCond,
|
2011-02-16 19:04:56 +00:00
|
|
|
'page_namespace' => $namespaces,
|
2011-02-16 03:11:20 +00:00
|
|
|
);
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2010-10-04 16:20:30 +00:00
|
|
|
$this->categoryCondition( $category, $tables, $conds );
|
2010-07-30 17:45:36 +00:00
|
|
|
$this->prefixCondition( $prefix, $conds );
|
|
|
|
$sort = array( 'ORDER BY' => 'page_namespace, page_title' );
|
|
|
|
|
|
|
|
return $dbr->select( $tables, $vars, $conds, __METHOD__ , $sort );
|
2008-10-13 12:34:00 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2011-02-16 03:11:20 +00:00
|
|
|
function doSearchQuery( $search, $namespaces, $category, $prefix, $use_regex = false ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$tables = array( 'page', 'revision', 'text' );
|
|
|
|
$vars = array( 'page_id', 'page_namespace', 'page_title', 'old_text' );
|
2011-02-16 03:11:20 +00:00
|
|
|
if ( $use_regex ) {
|
2011-03-11 18:39:52 +00:00
|
|
|
$comparisonCond = 'old_text REGEXP ' . $dbr->addQuotes( $search );
|
2010-10-04 16:20:30 +00:00
|
|
|
} else {
|
2011-02-16 03:11:20 +00:00
|
|
|
// anyString() method was added in MW 1.16
|
|
|
|
if ( method_exists( $dbr, 'anyString' ) ) {
|
|
|
|
$any = $dbr->anyString();
|
|
|
|
$comparisonCond = 'old_text ' . $dbr->buildLike( $any, $search, $any );
|
|
|
|
} else {
|
2011-03-11 18:39:52 +00:00
|
|
|
$comparisonCond = 'old_text LIKE ' . $dbr->addQuotes( "%$search%" );
|
2011-02-16 03:11:20 +00:00
|
|
|
}
|
2010-10-04 16:20:30 +00:00
|
|
|
}
|
2011-02-16 03:11:20 +00:00
|
|
|
$conds = array(
|
|
|
|
$comparisonCond,
|
2011-02-16 19:04:56 +00:00
|
|
|
'page_namespace' => $namespaces,
|
2011-02-16 03:11:20 +00:00
|
|
|
'rev_id = page_latest',
|
|
|
|
'rev_text_id = old_id'
|
|
|
|
);
|
2010-07-30 17:45:36 +00:00
|
|
|
|
2010-10-04 16:20:30 +00:00
|
|
|
$this->categoryCondition( $category, $tables, $conds );
|
2010-07-30 17:45:36 +00:00
|
|
|
$this->prefixCondition( $prefix, $conds );
|
|
|
|
$sort = array( 'ORDER BY' => 'page_namespace, page_title' );
|
|
|
|
|
|
|
|
return $dbr->select( $tables, $vars, $conds, __METHOD__ , $sort );
|
|
|
|
}
|
|
|
|
|
2010-10-04 16:20:30 +00:00
|
|
|
protected function categoryCondition( $category, &$tables, &$conds ) {
|
2010-07-30 17:45:36 +00:00
|
|
|
if ( !empty( $category ) ) {
|
|
|
|
$category = Title::newFromText( $category )->getDbKey();
|
2009-08-14 17:28:25 +00:00
|
|
|
$tables[] = 'categorylinks';
|
|
|
|
$conds[] = 'page_id = cl_from';
|
2010-07-30 17:45:36 +00:00
|
|
|
$conds['cl_to'] = $category;
|
2009-08-14 17:28:25 +00:00
|
|
|
}
|
2010-07-30 17:45:36 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2010-07-30 17:45:36 +00:00
|
|
|
protected function prefixCondition( $prefix, &$conds ) {
|
|
|
|
if ( !empty( $prefix ) ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
$prefix = Title::newFromText( $prefix )->getDbKey();
|
2010-10-04 16:20:30 +00:00
|
|
|
// anyString() function was added in MW 1.16
|
|
|
|
if ( method_exists( $dbr, 'anyString' ) ) {
|
|
|
|
$any = $dbr->anyString();
|
|
|
|
$conds[] = 'page_title ' . $dbr->buildLike( $prefix, $any );
|
|
|
|
} else {
|
|
|
|
$conds[] = "page_title LIKE '$prefix%'";
|
|
|
|
}
|
2010-07-30 17:45:36 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
}
|