r50770 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50769‎ | r50770 | r50771 >
Date:15:19, 19 May 2009
Author:yaron
Status:deferred
Tags:
Comment:
Added namespace selector to page, fixed bad-page-ID bug
Modified paths:
  • /trunk/extensions/ReplaceText/SpecialReplaceText.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ReplaceText/SpecialReplaceText.php
@@ -48,6 +48,18 @@
4949 $wgOut->addHTML( Xml::closeElement( 'form' ) );
5050 }
5151
 52+ static function getSelectedNamespaces() {
 53+ global $wgRequest;
 54+ $all_namespaces = SearchEngine::searchableNamespaces();
 55+ $selected_namespaces = array();
 56+ foreach ($all_namespaces as $ns => $name) {
 57+ if ($wgRequest->getCheck('ns' . $ns)) {
 58+ $selected_namespaces[] = $ns;
 59+ }
 60+ }
 61+ return $selected_namespaces;
 62+ }
 63+
5264 function doSpecialReplaceText() {
5365 global $wgUser, $wgOut, $wgRequest, $wgLang;
5466
@@ -81,11 +93,13 @@
8294 $jobs = array();
8395 foreach ( $wgRequest->getValues() as $key => $value ) {
8496 if ( $value == '1' ) {
85 - if ( strpos( $key, 'move-' ) !== false ) {
86 - $title = Title::newFromId( substr( $key, 5 ) );
 97+ if ($key === 'replace') {
 98+ // ignore this value
 99+ } elseif ( strpos( $key, 'move-' ) !== false ) {
 100+ $title = Title::newFromID( substr( $key, 5 ) );
87101 $replacement_params['move_page'] = true;
88102 } else {
89 - $title = Title::newFromId( $key );
 103+ $title = Title::newFromID( $key );
90104 }
91105 if ( $title !== null )
92106 $jobs[] = new ReplaceTextJob( $title, $replacement_params );
@@ -126,13 +140,15 @@
127141 if ( $this->replacement === '' ) {
128142 $message = 'replacetext_blankwarning';
129143 } elseif ( $this->edit_pages ) {
130 - $res = $this->doSearchQuery( $this->replacement );
 144+ $selected_namespaces = self::getSelectedNamespaces();
 145+ $res = $this->doSearchQuery( $this->replacement, $selected_namespaces );
131146 $count = $res->numRows();
132147 if ( $count > 0 ) {
133148 $message = array( 'replacetext_warning', $wgLang->formatNum( $count ), $this->replacement );
134149 }
135150 } elseif ( $this->move_pages ) {
136 - $res = $this->getMatchingTitles( $this->replacement );
 151+ $selected_namespaces = self::getSelectedNamespaces();
 152+ $res = $this->getMatchingTitles( $this->replacement, $selected_namespaces );
137153 $count = $res->numRows();
138154 if ( $count > 0 ) {
139155 $message = array( 'replacetext_warning', $wgLang->formatNum( $count ), $this->replacement );
@@ -147,7 +163,8 @@
148164
149165 // if user is replacing text within pages...
150166 if ( $this->edit_pages ) {
151 - $res = $this->doSearchQuery( $this->target );
 167+ $selected_namespaces = self::getSelectedNamespaces();
 168+ $res = $this->doSearchQuery( $this->target, $selected_namespaces );
152169 foreach ( $res as $row ) {
153170 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
154171 $context = $this->extractContext( $row->old_text, $this->target );
@@ -155,7 +172,8 @@
156173 }
157174 }
158175 if ( $this->move_pages ) {
159 - $res = $this->getMatchingTitles( $this->target );
 176+ $selected_namespaces = self::getSelectedNamespaces();
 177+ $res = $this->getMatchingTitles( $this->target, $selected_namespaces );
160178 foreach ( $res as $row ) {
161179 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
162180 // see if this move can happen
@@ -208,6 +226,11 @@
209227 $wgOut->addHTML( Xml::textarea( 'replacement', $this->replacement, 50, 2 ) );
210228 $wgOut->addHTML( Xml::closeElement( 'textarea' ) );
211229 $wgOut->addHTML( '</td></tr></table>' );
 230+
 231+ $search_label = wfMsg('powersearch-ns');
 232+ $namespaces = SearchEngine::searchableNamespaces();
 233+ $tables = $this->namespaceTables( $namespaces );
 234+ $wgOut->addHTML( "<fieldset>\n<p>$search_label</p\n>$tables\n</fieldset>" );
212235 $wgOut->addHTML(
213236 Xml::checkLabel( wfMsg( 'replacetext_editpages' ), 'edit_pages', 'edit_pages', true ) . '<br />' .
214237 Xml::checkLabel( wfMsg( 'replacetext_movepages' ), 'move_pages', 'move_pages' ) . '<br /><br />' .
@@ -216,6 +239,48 @@
217240 );
218241 }
219242
 243+ /**
 244+ * Copied almost exactly from MediaWiki's SpecialSearch class, i.e.
 245+ * the search page
 246+ */
 247+ function namespaceTables( $namespaces, $rowsPerTable = 3 ) {
 248+ global $wgContLang;
 249+ // Group namespaces into rows according to subject.
 250+ // Try not to make too many assumptions about namespace numbering.
 251+ $rows = array();
 252+ $tables = "";
 253+ foreach( $namespaces as $ns => $name ) {
 254+ $subj = MWNamespace::getSubject( $ns );
 255+ if( !array_key_exists( $subj, $rows ) ) {
 256+ $rows[$subj] = "";
 257+ }
 258+ $name = str_replace( '_', ' ', $name );
 259+ if( '' == $name ) {
 260+ $name = wfMsg( 'blanknamespace' );
 261+ }
 262+ $rows[$subj] .= Xml::openElement( 'td', array( 'style' => 'white-space: nowrap' ) ) .
 263+ Xml::checkLabel( $name, "ns{$ns}", "mw-search-ns{$ns}", in_array( $ns, $namespaces ) ) .
 264+ Xml::closeElement( 'td' ) . "\n";
 265+ }
 266+ $rows = array_values( $rows );
 267+ $numRows = count( $rows );
 268+ // Lay out namespaces in multiple floating two-column tables so they'll
 269+ // be arranged nicely while still accommodating different screen widths
 270+ // Float to the right on RTL wikis
 271+ $tableStyle = $wgContLang->isRTL() ?
 272+ 'float: right; margin: 0 0 0em 1em' : 'float: left; margin: 0 1em 0em 0';
 273+ // Build the final HTML table...
 274+ for( $i = 0; $i < $numRows; $i += $rowsPerTable ) {
 275+ $tables .= Xml::openElement( 'table', array( 'style' => $tableStyle ) );
 276+ for( $j = $i; $j < $i + $rowsPerTable && $j < $numRows; $j++ ) {
 277+ $tables .= "<tr>\n" . $rows[$j] . "</tr>";
 278+ }
 279+ $tables .= Xml::closeElement( 'table' ) . "\n";
 280+ }
 281+ return $tables;
 282+ }
 283+
 284+
220285 function pageListForm( $titles_for_edit, $titles_for_move, $unmoveable_titles ) {
221286 global $wgOut, $wgLang, $wgScript;
222287
@@ -306,7 +371,7 @@
307372
308373 // Get all indexes
309374 $targetq = preg_quote( $target, '/' );
310 - preg_match_all( "/$targetq/i", $text, $matches, PREG_OFFSET_CAPTURE );
 375+ preg_match_all( "/$targetq/", $text, $matches, PREG_OFFSET_CAPTURE );
311376
312377 $poss = array();
313378 foreach ( $matches[0] as $_ ) {
@@ -352,34 +417,35 @@
353418 return $msg;
354419 }
355420
356 - function getMatchingTitles( $str) {
 421+ function getMatchingTitles( $str, $namespaces ) {
357422 $title = Title::newFromText( $str );
358423 if ( !$title ) return array();
359424
360425 $dbr = wfGetDB( DB_SLAVE );
361426 $sql_str = $dbr->escapeLike( $title->getDbKey() );
 427+ $include_ns = $dbr->makeList( $namespaces );
362428
363429 return $dbr->select(
364430 'page',
365431 array( 'page_title', 'page_namespace' ),
366 - "page_title like '%$sql_str%'",
 432+ "page_title LIKE '%$sql_str%' AND page_namespace IN ($include_ns)",
367433 __METHOD__,
368434 array( 'ORDER BY' => 'page_namespace, page_title' )
369435 );
370436 }
371437
372 - function doSearchQuery( $search ) {
 438+ function doSearchQuery( $search, $namespaces ) {
373439 $dbr = wfGetDB( DB_SLAVE );
374440
375441 $search = $dbr->escapeLike( $search );
376442 $search = str_replace(array("\\", "'"), array("\\\\", "\'"), $search);
377 - $exemptNS = $dbr->makeList( array( NS_TALK, NS_USER_TALK ) );
 443+ $include_ns = $dbr->makeList( $namespaces );
378444
379445 $tables = array( 'page', 'revision', 'text' );
380446 $vars = array( 'page_id', 'page_namespace', 'page_title', 'old_text' );
381447 $conds = array(
382448 "old_text like '%$search%'",
383 - "page_namespace not in ($exemptNS)",
 449+ "page_namespace in ($include_ns)",
384450 'rev_id = page_latest',
385451 'rev_text_id = old_id'
386452 );

Status & tagging log