2008-08-06 16:09:35 +00:00
|
|
|
<?php
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) die();
|
2008-08-06 16:09:35 +00:00
|
|
|
|
|
|
|
class ReplaceText extends SpecialPage {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2008-09-18 16:29:10 +00:00
|
|
|
public function __construct() {
|
2009-04-27 09:02:48 +00:00
|
|
|
parent::__construct( 'ReplaceText', 'replacetext' );
|
|
|
|
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();
|
2009-04-27 08:52:10 +00:00
|
|
|
$this->doSpecialReplaceText();
|
2008-08-06 16:09:35 +00:00
|
|
|
}
|
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
function displayConfirmForm( $message ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
global $wgOut;
|
|
|
|
|
|
|
|
$formOpts = array( 'method' => 'post', 'action' => $this->getTitle()->getFullUrl() );
|
|
|
|
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::openElement( 'form', $formOpts ) .
|
|
|
|
Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
|
2009-04-30 18:55:08 +00:00
|
|
|
Xml::hidden( 'target', $this->target ) .
|
|
|
|
Xml::hidden( 'replacement', $this->replacement ) .
|
|
|
|
Xml::hidden( 'edit_pages', $this->edit_pages ) .
|
|
|
|
Xml::hidden( 'move_pages', $this->move_pages ) .
|
2009-04-27 08:52:10 +00:00
|
|
|
Xml::hidden( 'confirm', 1 )
|
|
|
|
);
|
|
|
|
$wgOut->wrapWikiMsg( '$1', $message );
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::submitButton( wfMsg( 'replacetext_continue' ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_cancel' );
|
|
|
|
$wgOut->addHTML( Xml::closeElement( 'form' ) );
|
2008-08-06 16:09:35 +00:00
|
|
|
}
|
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' );
|
|
|
|
$this->edit_pages = $wgRequest->getCheck( 'edit_pages' );
|
|
|
|
$this->move_pages = $wgRequest->getCheck( 'move_pages' );
|
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;
|
|
|
|
$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 ) {
|
|
|
|
if ( $value == '1' ) {
|
|
|
|
if ( strpos( $key, 'move-' ) !== false ) {
|
|
|
|
$title = Title::newFromId( substr( $key, 5 ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
$replacement_params['move_page'] = true;
|
|
|
|
} else {
|
2009-04-27 09:02:48 +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 );
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
$count = $wgLang->formatNum( count( $jobs ) );
|
2009-04-30 18:55:08 +00:00
|
|
|
$wgOut->addWikiMsg( 'replacetext_success', $this->target, $this->replacement, $count );
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
// Link back
|
2009-04-27 18:48:27 +00:00
|
|
|
$sk = $this->user->getSkin();
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML( $sk->link( $this->getTitle(), wfMsgHtml( 'replacetext_return' ) ) );
|
2009-04-27 09:02:48 +00:00
|
|
|
} elseif ( $wgRequest->getCheck( 'target' ) ) { // very long elseif, look for "end elseif"
|
2009-04-27 08:52:10 +00:00
|
|
|
|
2009-04-27 18:48:27 +00:00
|
|
|
// first, check that either editing or moving pages
|
|
|
|
// has been selected
|
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
|
|
|
// 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 ( !$wgRequest->getCheck( 'confirm' ) ) {
|
|
|
|
|
|
|
|
$message = false;
|
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( $this->replacement === '' ) {
|
2009-04-27 20:55:00 +00:00
|
|
|
$message = 'replacetext_blankwarning';
|
2009-04-30 18:55:08 +00:00
|
|
|
} elseif ( $this->edit_pages ) {
|
|
|
|
$res = $this->doSearchQuery( $this->replacement );
|
2009-04-27 20:55:00 +00:00
|
|
|
$count = $res->numRows();
|
|
|
|
if ( $count > 0 ) {
|
2009-04-30 18:55:08 +00:00
|
|
|
$message = array( 'replacetext_warning', $wgLang->formatNum( $count ), $this->replacement );
|
2009-04-27 18:48:27 +00:00
|
|
|
}
|
2009-04-30 18:55:08 +00:00
|
|
|
} elseif ( $this->move_pages ) {
|
|
|
|
$res = $this->getMatchingTitles( $this->replacement );
|
2009-04-27 20:55:00 +00:00
|
|
|
$count = $res->numRows();
|
|
|
|
if ( $count > 0 ) {
|
2009-04-30 18:55:08 +00:00
|
|
|
$message = array( 'replacetext_warning', $wgLang->formatNum( $count ), $this->replacement );
|
2009-04-27 18:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-27 20:55:00 +00:00
|
|
|
|
|
|
|
if ( $message ) {
|
2009-04-30 18:55:08 +00:00
|
|
|
$this->displayConfirmForm( $message );
|
2009-04-27 20:55:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if user is replacing text within pages...
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( $this->edit_pages ) {
|
|
|
|
$res = $this->doSearchQuery( $this->target );
|
2009-04-27 18:48:27 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
|
2009-04-30 18:55:08 +00:00
|
|
|
$context = $this->extractContext( $row->old_text, $this->target );
|
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 ) {
|
|
|
|
$res = $this->getMatchingTitles( $this->target );
|
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
|
2009-04-30 22:23:59 +00:00
|
|
|
$cur_page_name = str_replace('_', ' ', $row->page_title);
|
|
|
|
$new_page_name = str_replace( $this->target, $this->replacement, $cur_page_name );
|
|
|
|
$new_title = Title::makeTitleSafe( $row->page_namespace, $new_page_name );
|
2009-04-27 09:02:48 +00:00
|
|
|
$err = $title->isValidMoveOperation( $new_title );
|
|
|
|
if ( $title->userCanMove( 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-04-27 18:48:27 +00:00
|
|
|
if ( count($titles_for_edit) == 0 && count($titles_for_move) == 0 ) {
|
2009-04-30 18:55:08 +00:00
|
|
|
if ( $this->edit_pages )
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_noreplacement', $this->target );
|
|
|
|
if ( $this->move_pages )
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_nomove', $this->target );
|
2009-04-27 08:52:10 +00:00
|
|
|
// link back to starting form
|
|
|
|
$sk = $this->user->getSkin();
|
|
|
|
$wgOut->addHTML( '<p>' . $sk->makeKnownLinkObj( $this->getTitle(), wfMsg( 'replacetext_return' ) ) . '</p>' );
|
|
|
|
} else {
|
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
|
|
|
|
$this->showForm( 'replacetext_docu' );
|
2008-08-06 16:09:35 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
function showForm( $message ) {
|
|
|
|
global $wgOut;
|
|
|
|
$wgOut->addHTML(
|
|
|
|
Xml::openElement( 'form', array( 'action' => $this->getTitle()->getFullUrl(), 'method' => 'post' ) ) .
|
2009-04-30 18:55:08 +00:00
|
|
|
Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
|
|
|
|
Xml::hidden( 'continue', 1 )
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
|
|
|
$wgOut->addWikiMsg( $message );
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_note' );
|
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-04-30 18:55:08 +00:00
|
|
|
$wgOut->addHTML( Xml::textarea( 'target', $this->target, 50, 2 ) );
|
|
|
|
$wgOut->addHTML( Xml::closeElement( 'textarea' ) );
|
|
|
|
$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-04-30 18:55:08 +00:00
|
|
|
$wgOut->addHTML( Xml::textarea( 'replacement', $this->replacement, 50, 2 ) );
|
|
|
|
$wgOut->addHTML( Xml::closeElement( 'textarea' ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
$wgOut->addHTML( '</td></tr></table>' );
|
|
|
|
$wgOut->addHTML(
|
2009-04-27 18:48:27 +00:00
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_editpages' ), 'edit_pages', 'edit_pages', true ) . '<br />' .
|
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_movepages' ), 'move_pages', 'move_pages' ) . '<br /><br />' .
|
2009-04-27 08:52:10 +00:00
|
|
|
Xml::submitButton( wfMsg( 'replacetext_continue' ) ) .
|
|
|
|
Xml::closeElement( 'form' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 ) .
|
|
|
|
Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
|
2009-04-30 18:55:08 +00:00
|
|
|
Xml::hidden( 'target', $this->target ) .
|
|
|
|
Xml::hidden( 'replacement', $this->replacement )
|
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 ) {
|
|
|
|
$wgOut->addWikiMsg( 'replacetext_choosepagesforedit', $this->target, $this->replacement,
|
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 ) .
|
|
|
|
$skin->makeKnownLinkObj( $title, $title->getPrefixedText() ) . " - <small>$context</small><br />\n"
|
|
|
|
);
|
|
|
|
}
|
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 ) .
|
|
|
|
$skin->makeLinkObj( $title, $title->prefix( $title->getText() ) ) . "<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" .
|
|
|
|
Xml::checkLabel( wfMsg( 'replacetext_watchmovedpages' ), 'watch-pages', 'watch-pages', true )
|
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' ) ) .
|
2009-04-27 08:52:10 +00:00
|
|
|
Xml::hidden( 'replace', 1 )
|
|
|
|
);
|
|
|
|
|
2008-10-13 12:34:00 +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 ) {
|
2009-04-27 08:52:10 +00:00
|
|
|
$text .= "<li>{$skin->makeKnownLinkObj( $title, $title->getPrefixedText() )}<br />\n";
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function extractContext( $text, $target ) {
|
|
|
|
global $wgLang;
|
|
|
|
$cw = $this->user->getOption( 'contextchars', 40 );
|
|
|
|
|
|
|
|
// Get all indexes
|
|
|
|
$targetq = preg_quote( $target, '/' );
|
|
|
|
preg_match_all( "/$targetq/i", $text, $matches, PREG_OFFSET_CAPTURE );
|
|
|
|
|
|
|
|
$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, ) = $_;
|
2009-04-27 09:02:48 +00:00
|
|
|
$context .= self::convertWhiteSpaceToHTML( $wgLang->truncate( substr( $text, 0, $index ), - $cw ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
$snippet = self::convertWhiteSpaceToHTML( substr( $text, $index, $len ) );
|
|
|
|
$targetq = preg_quote( self::convertWhiteSpaceToHTML( $target ), '/' );
|
|
|
|
$context .= preg_replace( "/$targetq/i", '<span class="searchmatch">\0</span>', $snippet );
|
2009-04-27 09:02:48 +00:00
|
|
|
$context .= self::convertWhiteSpaceToHTML( $wgLang->truncate( substr( $text, $index + $len ), $cw ) );
|
2009-04-27 08:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function convertWhiteSpaceToHTML( $msg ) {
|
|
|
|
$msg = htmlspecialchars( $msg );
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2009-04-30 18:55:08 +00:00
|
|
|
function getMatchingTitles( $str) {
|
|
|
|
$title = Title::newFromText( $str );
|
2009-04-27 08:52:10 +00:00
|
|
|
if ( !$title ) return array();
|
|
|
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2009-04-30 18:55:08 +00:00
|
|
|
$sql_str = $dbr->escapeLike( $title->getDbKey() );
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
return $dbr->select(
|
|
|
|
'page',
|
|
|
|
array( 'page_title', 'page_namespace' ),
|
2009-04-30 18:55:08 +00:00
|
|
|
"page_title like '%$sql_str%'",
|
2009-04-27 08:52:10 +00:00
|
|
|
__METHOD__,
|
2009-04-27 09:02:48 +00:00
|
|
|
array( 'ORDER BY' => 'page_namespace, page_title' )
|
2009-04-27 08:52:10 +00:00
|
|
|
);
|
2008-10-13 12:34:00 +00:00
|
|
|
}
|
2009-04-27 08:52:10 +00:00
|
|
|
|
|
|
|
function doSearchQuery( $search ) {
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
|
|
|
$search = $dbr->escapeLike( $search );
|
|
|
|
$exemptNS = $dbr->makeList( array( NS_TALK, NS_USER_TALK ) );
|
|
|
|
|
|
|
|
$tables = array( 'page', 'revision', 'text' );
|
|
|
|
$vars = array( 'page_id', 'page_namespace', 'page_title', 'old_text' );
|
|
|
|
$conds = array(
|
|
|
|
"old_text like '%$search%'",
|
|
|
|
"page_namespace not in ($exemptNS)",
|
|
|
|
'rev_id = page_latest',
|
|
|
|
'rev_text_id = old_id'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $dbr->select( $tables, $vars, $conds, __METHOD__ );
|
|
|
|
}
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
}
|