r34687 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34686‎ | r34687 | r34688 >
Date:20:34, 12 May 2008
Author:yaron
Status:old
Tags:
Comment:
Replacements are now done as MediaWiki jobs, and replacement is a two-part
process, showing the user a list of pages to be replaced among which they
can choose.
Modified paths:
  • /trunk/extensions/ReplaceText/ReplaceText.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ReplaceText/ReplaceText.php
@@ -16,7 +16,7 @@
1717 * the page provides a warning prompt to the user before doing the
1818 * replacement, since it is not easily reversible.
1919 *
20 - * @version 0.1.1
 20+ * @version 0.2
2121 * @author Yaron Koren
2222 */
2323
@@ -34,6 +34,11 @@
3535 $wgExtensionFunctions[] = 'grfLoadMessagesManually';
3636 }
3737
 38+$wgJobClasses['replaceText'] = 'ReplaceTextJob';
 39+
 40+require_once( "$IP/includes/JobQueue.php" );
 41+require_once($grIP . '/ReplaceTextJob.php');
 42+
3843 function grSetupExtension() {
3944 global $wgVersion, $wgExtensionCredits;
4045
@@ -43,7 +48,7 @@
4449 // credits
4550 $wgExtensionCredits['specialpage'][] = array(
4651 'name' => 'Replace Text',
47 - 'version' => '0.1.1',
 52+ 'version' => '0.2',
4853 'author' => 'Yaron Koren',
4954 'url' => 'http://www.mediawiki.org/wiki/Extension:Text_Replace',
5055 'description' => 'A special page that lets administrators run a global search-and-replace',
@@ -86,12 +91,10 @@
8792 $replacement_str = $wgRequest->getVal('replacement_str');
8893 $continue_label = wfMsg('replacetext_continue');
8994 $cancel_label = wfMsg('replacetext_cancel');
90 - $replace_label = wfMsg('replacetext_replace');
9195 $text =<<<END
9296 <form method="post" action="">
9397 <input type="hidden" name="target_str" value="$target_str">
9498 <input type="hidden" name="replacement_str" value="$replacement_str">
95 - <input type="hidden" name="replace" value="$replace_label">
9699 <p>$message</p>
97100 <p><input type="Submit" name="confirm" value="$continue_label"></p>
98101 <p>$cancel_label</p>
@@ -105,6 +108,33 @@
106109 global $wgUser, $wgOut, $wgRequest;
107110
108111 if ($wgRequest->getCheck('replace')) {
 112+ $target_str = $wgRequest->getVal('target_str');
 113+ $replacement_str = $wgRequest->getVal('replacement_str');
 114+ $replacement_params = array();
 115+ $replacement_params['user_id'] = $wgUser->getId();
 116+ $replacement_params['target_str'] = $target_str;
 117+ $replacement_params['replacement_str'] = $replacement_str;
 118+ $replacement_params['edit_summary'] = wfMsgForContent('replacetext_editsummary', $target_str, $replacement_str);
 119+ foreach ($wgRequest->getValues() as $key => $value) {
 120+ if ($value == 'on') {
 121+ $title = Title::newFromId($key);
 122+/*
 123+ $num_matches;
 124+ $new_text = str_replace($target_str, $replacement_str, $article_text, $num_matches);
 125+ // if there's at least one replacement, modify the page, using an edit
 126+ // summary in the language of the wiki
 127+ if ($num_matches > 0) {
 128+ $edit_summary = wfMsgForContent('replacetext_editsummary', $target_str, $replacement_str);
 129+ $article->doEdit($new_text, $edit_summary);
 130+ }
 131+*/
 132+ $jobs[] = new ReplaceTextJob( $title, $replacement_params );
 133+ }
 134+ }
 135+ Job::batchInsert( $jobs );
 136+ $num_modified_pages = count($jobs);
 137+ $wgOut->addHTML(wfMsg('replacetext_success', $target_str, $replacement_str, $num_modified_pages));
 138+ } elseif ($wgRequest->getCheck('target_str')) {
109139 $dbr =& wfGetDB( DB_SLAVE );
110140 $fname = 'doReplaceText';
111141 $target_str = $wgRequest->getVal('target_str');
@@ -135,60 +165,88 @@
136166 $wgOut->addHTML(displayConfirmForm($text));
137167 return;
138168 } else {
139 - $num_files_with_replacement_str = 0;
 169+ $num_pages_with_replacement_str = 0;
140170 foreach ($titles as $title) {
141171 $article = new Article($title);
142172 $article_text = $article->fetchContent();
143173 if (strpos($article_text, $replacement_str)) {
144 - $num_files_with_replacement_str++;
 174+ $num_pages_with_replacement_str++;
145175 }
146176 }
147 - if ($num_files_with_replacement_str > 0) {
148 - $text = wfMsg('replacetext_warning', $num_files_with_replacement_str, $replacement_str);
 177+ if ($num_pages_with_replacement_str > 0) {
 178+ $text = wfMsg('replacetext_warning', $num_pages_with_replacement_str, $replacement_str);
149179 $wgOut->addHTML(displayConfirmForm($text));
150180 return;
151181 }
152182 }
153183 }
154184
155 - $num_modified_files = 0;
 185+ $jobs = array();
 186+ $num_modified_pages = 0;
 187+ $found_titles = array();
 188+ $angle_brackets = array('<', '>');
 189+ $escaped_angle_brackets = array('&lt;', '&gt;');
156190 foreach ($titles as $title) {
157191 $article = new Article($title);
158192 $article_text = $article->fetchContent();
159 - $num_matches;
160 - $new_text = str_replace($target_str, $replacement_str, $article_text, $num_matches);
161 - // if there's at least one replacement, modify the page, using an edit
162 - // summary in the language of the wiki
163 - if ($num_matches > 0) {
164 - $edit_summary = wfMsgForContent('replacetext_editsummary', $target_str, $replacement_str);
165 - $article->doEdit($new_text, $edit_summary);
166 - $num_modified_files++;
 193+ if ($target_pos = strpos($article_text, $target_str)) {
 194+ $left_padding = min($target_pos, 30);
 195+ $right_padding = min(strlen($article_text) - $target_pos, 30);
 196+ $len = strlen($article_text);
 197+ $context_str = "";
 198+ if ($left_padding == 30)
 199+ $context_str .= "... ";
 200+ $context_str .= str_replace($angle_brackets, $escaped_angle_brackets, substr($article_text, $target_pos - $left_padding, $left_padding)) . "<span class=\"searchmatch\">" . str_replace($angle_brackets, $escaped_angle_brackets, substr($article_text, $target_pos, strlen($target_str))) . "</span>" . str_replace($angle_brackets, $escaped_angle_brackets, substr($article_text, $target_pos + strlen($target_str), $right_padding));
 201+ if ($right_padding == 30)
 202+ $context_str .= " ...";
 203+ $found_titles[] = array($title, $context_str);
 204+ $num_modified_pages++;
167205 }
168206 }
169207
170 - if ($num_modified_files == 0)
 208+ if ($num_modified_pages == 0)
171209 $wgOut->addHTML(wfMsg('replacetext_noreplacement', $target_str));
172 - else
173 - $wgOut->addHTML(wfMsg('replacetext_success', $target_str, $replacement_str, $num_modified_files));
 210+ else {
 211+ $replace_label = wfMsg('replacetext_replace');
 212+ $choose_pages_label = wfMsg('replacetext_choosepages', $target_str, $replacement_str);
 213+ $skin = $wgUser->getSkin();
 214+ $text =<<<END
 215+ <p>$choose_pages_label</p>
 216+ <form method="post">
 217+ <input type="hidden" name="target_str" value="$target_str">
 218+ <input type="hidden" name="replacement_str" value="$replacement_str">
 219+
 220+END;
 221+ foreach ($found_titles as $value_pair) {
 222+ list($title, $context_str) = $value_pair;
 223+ $text .= "<input type=\"checkbox\" name=\"{$title->getArticleID()}\" checked /> {$skin->makeLinkObj( $title, $title->prefix($title->getText()) )} - <small>$context_str</small><br />\n";
 224+ }
 225+ $text .=<<<END
 226+ <p><input type="Submit" name="replace" value="$replace_label"></p>
 227+ </form>
 228+
 229+END;
 230+ $wgOut->addHTML($text);
 231+ }
174232 } else {
175233 $replacement_label = wfMsg('replacetext_docu');
176234 $replacement_note = wfMsg('replacetext_note');
177235 $original_text_label = wfMsg('replacetext_originaltext');
178236 $replacement_text_label = wfMsg('replacetext_replacementtext');
179 - $replace_label = wfMsg('replacetext_replace');
 237+ $continue_label = wfMsg('replacetext_continue');
180238 $text =<<<END
181 - <form method="post" action="">
 239+ <form method="get" action="">
182240 <p>$replacement_label</p>
183241 <p>$replacement_note</p>
184242 <br />
185243 <p>$original_text_label: <input type="text" length="10" name="target_str">
186244 &nbsp;
187245 $replacement_text_label: <input type="text" length="10" name="replacement_str"></p>
188 - <p><input type="Submit" name="replace" value="$replace_label"></p>
 246+ <p><input type="Submit" value="$continue_label"></p>
189247 </form>
190248
191249 END;
 250+ $wgOut->addHTML($text);
192251 }
193252
194 - $wgOut->addHTML($text);
195253 }

Status & tagging log