Index: trunk/extensions/ReplaceText/SpecialReplaceText.php |
— | — | @@ -52,115 +52,113 @@ |
53 | 53 | } |
54 | 54 | |
55 | 55 | function doSpecialReplaceText() { |
56 | | - global $wgUser, $wgOut, $wgRequest, $wgContLang; |
| 56 | + global $wgUser, $wgOut, $wgRequest, $wgContLang; |
57 | 57 | |
58 | | - // set 'title' as hidden field, in case there's no URL niceness |
59 | | - $mw_namespace_labels = $wgContLang->getNamespaces(); |
60 | | - $special_namespace = $mw_namespace_labels[NS_SPECIAL]; |
| 58 | + // set 'title' as hidden field, in case there's no URL niceness |
| 59 | + $mw_namespace_labels = $wgContLang->getNamespaces(); |
| 60 | + $special_namespace = $mw_namespace_labels[NS_SPECIAL]; |
61 | 61 | |
62 | | - if ($wgRequest->getCheck('replace')) { |
63 | | - $target_str = $wgRequest->getVal('target_str'); |
64 | | - $replacement_str = $wgRequest->getVal('replacement_str'); |
65 | | - $replacement_params = array(); |
66 | | - $replacement_params['user_id'] = $wgUser->getId(); |
67 | | - $replacement_params['target_str'] = $target_str; |
68 | | - $replacement_params['replacement_str'] = $replacement_str; |
69 | | - $replacement_params['edit_summary'] = wfMsgForContent('replacetext_editsummary', $target_str, $replacement_str); |
70 | | - foreach ($wgRequest->getValues() as $key => $value) { |
71 | | - if ($value == 'on') { |
72 | | - $title = Title::newFromId($key); |
73 | | - $jobs[] = new ReplaceTextJob( $title, $replacement_params ); |
74 | | - } |
75 | | - } |
76 | | - Job::batchInsert( $jobs ); |
77 | | - $num_modified_pages = count($jobs); |
78 | | - $wgOut->addHTML(wfMsg('replacetext_success', $target_str, $replacement_str, $num_modified_pages)); |
79 | | - } elseif ($wgRequest->getCheck('target_str')) { |
80 | | - $dbr =& wfGetDB( DB_SLAVE ); |
81 | | - $fname = 'doSpecialReplaceText'; |
82 | | - $target_str = $wgRequest->getVal('target_str'); |
83 | | - $replacement_str = $wgRequest->getVal('replacement_str'); |
| 62 | + if ($wgRequest->getCheck('replace')) { |
| 63 | + $target_str = $wgRequest->getVal('target_str'); |
| 64 | + $replacement_str = $wgRequest->getVal('replacement_str'); |
| 65 | + $replacement_params = array(); |
| 66 | + $replacement_params['user_id'] = $wgUser->getId(); |
| 67 | + $replacement_params['target_str'] = $target_str; |
| 68 | + $replacement_params['replacement_str'] = $replacement_str; |
| 69 | + $replacement_params['edit_summary'] = wfMsgForContent('replacetext_editsummary', $target_str, $replacement_str); |
| 70 | + foreach ($wgRequest->getValues() as $key => $value) { |
| 71 | + if ($value == 'on') { |
| 72 | + $title = Title::newFromId($key); |
| 73 | + $jobs[] = new ReplaceTextJob( $title, $replacement_params ); |
| 74 | + } |
| 75 | + } |
| 76 | + Job::batchInsert( $jobs ); |
| 77 | + $num_modified_pages = count($jobs); |
| 78 | + $wgOut->addHTML(wfMsg('replacetext_success', $target_str, $replacement_str, $num_modified_pages)); |
| 79 | + } elseif ( $wgRequest->getCheck('target_str') ) { // very long elseif, look for "end elseif" |
| 80 | + $dbr =& wfGetDB( DB_SLAVE ); |
| 81 | + $fname = 'doSpecialReplaceText'; |
| 82 | + $target_str = $wgRequest->getVal('target_str'); |
| 83 | + $replacement_str = $wgRequest->getVal('replacement_str'); |
84 | 84 | |
85 | | - if (! $wgRequest->getCheck('confirm')) { |
86 | | - // display a page to make the user confirm the replacement, if the |
87 | | - // replacement string is either blank or found elsewhere on the wiki |
88 | | - // (since undoing the replacement would be difficult in either case) |
89 | | - if ($replacement_str == '') { |
90 | | - $text = wfMsg('replacetext_blankwarning'); |
91 | | - $wgOut->addHTML(ReplaceText::displayConfirmForm($text)); |
92 | | - return; |
93 | | - } else { |
94 | | - // get the number of pages in which the replacement string appears |
95 | | - $page_table = $dbr->tableName('page'); |
96 | | - $revision_table = $dbr->tableName('revision'); |
97 | | - $text_table = $dbr->tableName('text'); |
98 | | - $talk_ns = NS_TALK; |
99 | | - $usertalk_ns = NS_USER_TALK; |
100 | | - $mediawiki_ns = NS_MEDIAWIKI; |
101 | | - $sql_replacement_str = str_replace("'", "\'", $replacement_str); |
102 | | - $sql = "SELECT count(*) |
103 | | - FROM $page_table p |
104 | | - JOIN $revision_table r ON p.page_latest = r.rev_id |
105 | | - JOIN $text_table t ON r.rev_text_id = t.old_id |
106 | | - WHERE t.old_text LIKE '%$sql_replacement_str%' |
107 | | - AND p.page_namespace != $talk_ns |
108 | | - AND p.page_namespace != $usertalk_ns |
109 | | - AND p.page_namespace != $mediawiki_ns"; |
110 | | - $res = $dbr->query($sql); |
111 | | - $row = $dbr->fetchRow($res); |
112 | | - $num_pages_with_replacement_str = $row[0]; |
113 | | - // if there are any, the user most confirm the replacement |
114 | | - if ($num_pages_with_replacement_str > 0) { |
115 | | - $text = wfMsg('replacetext_warning', $num_pages_with_replacement_str, $replacement_str); |
116 | | - $wgOut->addHTML(ReplaceText::displayConfirmForm($text)); |
117 | | - return; |
118 | | - } |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - $jobs = array(); |
123 | | - $num_modified_pages = 0; |
124 | | - $found_titles = array(); |
125 | | - $angle_brackets = array('<', '>'); |
126 | | - $escaped_angle_brackets = array('<', '>'); |
127 | | - |
128 | | - // get the set of pages that contain the target string, and display |
129 | | - // the name and "context" (the text around the string) of each |
130 | | - $page_table = $dbr->tableName('page'); |
131 | | - $revision_table = $dbr->tableName('revision'); |
132 | | - $text_table = $dbr->tableName('text'); |
133 | | - $talk_ns = NS_TALK; |
134 | | - $usertalk_ns = NS_USER_TALK; |
135 | | - $mediawiki_ns = NS_MEDIAWIKI; |
136 | | - // escape single quote and backslash for SQL - for some reason, the |
137 | | - // backslash needs to be escaped twice (plus once for PHP) |
138 | | - $sql_target_str = str_replace(array("\\", "'"), array("\\\\\\\\", "\'"), $target_str); |
139 | | - $sql = "SELECT p.page_title AS title, p.page_namespace AS namespace, t.old_text AS text |
140 | | - FROM $page_table p |
141 | | - JOIN $revision_table r ON p.page_latest = r.rev_id |
142 | | - JOIN $text_table t ON r.rev_text_id = t.old_id |
143 | | - WHERE t.old_text LIKE '%$sql_target_str%' |
144 | | - AND p.page_namespace != $talk_ns |
145 | | - AND p.page_namespace != $usertalk_ns |
146 | | - AND p.page_namespace != $mediawiki_ns |
147 | | - ORDER BY p.page_namespace, p.page_title"; |
148 | | - $res = $dbr->query($sql); |
149 | | - $contextchars = $wgUser->getOption( 'contextchars', 40 ); |
150 | | - while( $row = $dbr->fetchObject( $res ) ) { |
151 | | - $title = Title::newFromText($row->title, $row->namespace); |
152 | | - $article_text = $row->text; |
153 | | - $target_pos = strpos($article_text, $target_str); |
154 | | - $context_str = str_replace($angle_brackets, $escaped_angle_brackets, $wgContLang->truncate(substr($article_text, 0, $target_pos), -$contextchars, '...' )); |
155 | | - $context_str .= "<span class=\"searchmatch\">" . str_replace($angle_brackets, $escaped_angle_brackets, substr($article_text, $target_pos, strlen($target_str))) . "</span>"; |
156 | | - $context_str .= str_replace($angle_brackets, $escaped_angle_brackets, $wgContLang->truncate(substr($article_text, $target_pos + strlen($target_str)), $contextchars, '...' )); |
157 | | - $found_titles[] = array($title, $context_str); |
158 | | - $num_modified_pages++; |
159 | | - } |
160 | | - |
161 | | - if ($num_modified_pages == 0) |
162 | | - $wgOut->addHTML(wfMsg('replacetext_noreplacement', $target_str)); |
163 | | - else { |
164 | | - $javascript_text =<<<END |
| 85 | + if (! $wgRequest->getCheck('confirm')) { |
| 86 | + // display a page to make the user confirm the replacement, if the |
| 87 | + // replacement string is either blank or found elsewhere on the wiki |
| 88 | + // (since undoing the replacement would be difficult in either case) |
| 89 | + if ($replacement_str == '') { |
| 90 | + $text = wfMsg('replacetext_blankwarning'); |
| 91 | + $wgOut->addHTML(ReplaceText::displayConfirmForm($text)); |
| 92 | + return; |
| 93 | + } else { |
| 94 | + // get the number of pages in which the replacement string appears |
| 95 | + $page_table = $dbr->tableName('page'); |
| 96 | + $revision_table = $dbr->tableName('revision'); |
| 97 | + $text_table = $dbr->tableName('text'); |
| 98 | + $talk_ns = NS_TALK; |
| 99 | + $usertalk_ns = NS_USER_TALK; |
| 100 | + $mediawiki_ns = NS_MEDIAWIKI; |
| 101 | + $sql_replacement_str = str_replace("'", "\'", $replacement_str); |
| 102 | + $sql = "SELECT count(*) |
| 103 | + FROM $page_table p |
| 104 | + JOIN $revision_table r ON p.page_latest = r.rev_id |
| 105 | + JOIN $text_table t ON r.rev_text_id = t.old_id |
| 106 | + WHERE t.old_text LIKE '%$sql_replacement_str%' |
| 107 | + AND p.page_namespace != $talk_ns |
| 108 | + AND p.page_namespace != $usertalk_ns |
| 109 | + AND p.page_namespace != $mediawiki_ns"; |
| 110 | + $res = $dbr->query($sql); |
| 111 | + $row = $dbr->fetchRow($res); |
| 112 | + $num_pages_with_replacement_str = $row[0]; |
| 113 | + // if there are any, the user most confirm the replacement |
| 114 | + if ($num_pages_with_replacement_str > 0) { |
| 115 | + $text = wfMsg('replacetext_warning', $num_pages_with_replacement_str, $replacement_str); |
| 116 | + $wgOut->addHTML(ReplaceText::displayConfirmForm($text)); |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + $jobs = array(); |
| 122 | + $num_modified_pages = 0; |
| 123 | + $found_titles = array(); |
| 124 | + $angle_brackets = array('<', '>'); |
| 125 | + $escaped_angle_brackets = array('<', '>'); |
| 126 | + |
| 127 | + // get the set of pages that contain the target string, and display |
| 128 | + // the name and "context" (the text around the string) of each |
| 129 | + $page_table = $dbr->tableName('page'); |
| 130 | + $revision_table = $dbr->tableName('revision'); |
| 131 | + $text_table = $dbr->tableName('text'); |
| 132 | + $talk_ns = NS_TALK; |
| 133 | + $usertalk_ns = NS_USER_TALK; |
| 134 | + $mediawiki_ns = NS_MEDIAWIKI; |
| 135 | + // escape single quote and backslash for SQL - for some reason, the |
| 136 | + // backslash needs to be escaped twice (plus once for PHP) |
| 137 | + $sql_target_str = str_replace(array("\\", "'"), array("\\\\\\\\", "\'"), $target_str); |
| 138 | + $sql = "SELECT p.page_title AS title, p.page_namespace AS namespace, t.old_text AS text |
| 139 | + FROM $page_table p |
| 140 | + JOIN $revision_table r ON p.page_latest = r.rev_id |
| 141 | + JOIN $text_table t ON r.rev_text_id = t.old_id |
| 142 | + WHERE t.old_text LIKE '%$sql_target_str%' |
| 143 | + AND p.page_namespace != $talk_ns |
| 144 | + AND p.page_namespace != $usertalk_ns |
| 145 | + AND p.page_namespace != $mediawiki_ns |
| 146 | + ORDER BY p.page_namespace, p.page_title"; |
| 147 | + $res = $dbr->query($sql); |
| 148 | + $contextchars = $wgUser->getOption( 'contextchars', 40 ); |
| 149 | + while( $row = $dbr->fetchObject( $res ) ) { |
| 150 | + $title = Title::newFromText($row->title, $row->namespace); |
| 151 | + $article_text = $row->text; |
| 152 | + $target_pos = strpos($article_text, $target_str); |
| 153 | + $context_str = str_replace($angle_brackets, $escaped_angle_brackets, $wgContLang->truncate(substr($article_text, 0, $target_pos), -$contextchars, '...' )); |
| 154 | + $context_str .= "<span class=\"searchmatch\">" . str_replace($angle_brackets, $escaped_angle_brackets, substr($article_text, $target_pos, strlen($target_str))) . "</span>"; |
| 155 | + $context_str .= str_replace($angle_brackets, $escaped_angle_brackets, $wgContLang->truncate(substr($article_text, $target_pos + strlen($target_str)), $contextchars, '...' )); |
| 156 | + $found_titles[] = array($title, $context_str); |
| 157 | + $num_modified_pages++; |
| 158 | + } |
| 159 | + if ($num_modified_pages == 0) { |
| 160 | + $wgOut->addHTML(wfMsg('replacetext_noreplacement', $target_str)); |
| 161 | + } else { |
| 162 | + $javascript_text =<<<END |
165 | 163 | <script type="text/javascript"> |
166 | 164 | function invertSelections() { |
167 | 165 | form = document.getElementById('choose_pages'); |
— | — | @@ -177,14 +175,14 @@ |
178 | 176 | </script> |
179 | 177 | |
180 | 178 | END; |
181 | | - $wgOut->addScript($javascript_text); |
182 | | - $replace_label = wfMsg('replacetext_replace'); |
183 | | - $choose_pages_label = wfMsg('replacetext_choosepages', $target_str, $replacement_str); |
184 | | - $skin = $wgUser->getSkin(); |
185 | | - // escape quotes for inclusion in HTML |
186 | | - $target_str = str_replace('"', '"', $target_str); |
187 | | - $replacement_str = str_replace('"', '"', $replacement_str); |
188 | | - $text =<<<END |
| 179 | + $wgOut->addScript($javascript_text); |
| 180 | + $replace_label = wfMsg('replacetext_replace'); |
| 181 | + $choose_pages_label = wfMsg('replacetext_choosepages', $target_str, $replacement_str); |
| 182 | + $skin = $wgUser->getSkin(); |
| 183 | + // escape quotes for inclusion in HTML |
| 184 | + $target_str = str_replace('"', '"', $target_str); |
| 185 | + $replacement_str = str_replace('"', '"', $replacement_str); |
| 186 | + $text =<<<END |
189 | 187 | <p>$choose_pages_label</p> |
190 | 188 | <form id="choose_pages" method="post"> |
191 | 189 | <input type="hidden" name="title" value="$special_namespace:ReplaceText"> |
— | — | @@ -192,32 +190,33 @@ |
193 | 191 | <input type="hidden" name="replacement_str" value="$replacement_str"> |
194 | 192 | |
195 | 193 | END; |
196 | | - foreach ($found_titles as $value_pair) { |
197 | | - list($title, $context_str) = $value_pair; |
198 | | - $text .= "<input type=\"checkbox\" name=\"{$title->getArticleID()}\" checked /> {$skin->makeLinkObj( $title, $title->prefix($title->getText()) )} - <small>$context_str</small><br />\n"; |
199 | | - } |
200 | | - $text .=<<<END |
| 194 | + foreach ($found_titles as $value_pair) { |
| 195 | + list($title, $context_str) = $value_pair; |
| 196 | + $text .= "<input type=\"checkbox\" name=\"{$title->getArticleID()}\" checked /> {$skin->makeLinkObj( $title, $title->prefix($title->getText()) )} - <small>$context_str</small><br />\n"; |
| 197 | + } |
| 198 | + $text .=<<<END |
201 | 199 | <p><input type="Submit" name="replace" value="$replace_label"></p> |
202 | 200 | |
203 | 201 | END; |
204 | | - // only show "invert selections" link if there are more than five pages |
205 | | - if (count($found_titles) > 5) { |
206 | | - $invert_selections_label = wfMsg('replacetext_invertselections'); |
207 | | - $text .=<<<END |
| 202 | + // only show "invert selections" link if there are more than five pages |
| 203 | + if (count($found_titles) > 5) { |
| 204 | + $invert_selections_label = wfMsg('replacetext_invertselections'); |
| 205 | + $text .=<<<END |
208 | 206 | <p><a href="javascript:;" onclick="invertSelections(); return false;">$invert_selections_label</a></p> |
209 | 207 | |
210 | 208 | END; |
211 | | - } |
212 | | - $text .= " </form>\n"; |
213 | | - $wgOut->addHTML($text); |
214 | | - } |
215 | | - } else { |
216 | | - $replacement_label = wfMsg('replacetext_docu'); |
217 | | - $replacement_note = wfMsg('replacetext_note'); |
218 | | - $original_text_label = wfMsg('replacetext_originaltext'); |
219 | | - $replacement_text_label = wfMsg('replacetext_replacementtext'); |
220 | | - $continue_label = wfMsg('replacetext_continue'); |
221 | | - $text =<<<END |
| 209 | + } |
| 210 | + $text .= " </form>\n"; |
| 211 | + $wgOut->addHTML($text); |
| 212 | + } |
| 213 | + } // end elseif |
| 214 | + else { |
| 215 | + $replacement_label = wfMsg('replacetext_docu'); |
| 216 | + $replacement_note = wfMsg('replacetext_note'); |
| 217 | + $original_text_label = wfMsg('replacetext_originaltext'); |
| 218 | + $replacement_text_label = wfMsg('replacetext_replacementtext'); |
| 219 | + $continue_label = wfMsg('replacetext_continue'); |
| 220 | + $text =<<<END |
222 | 221 | <form method="get" action=""> |
223 | 222 | <input type="hidden" name="title" value="$special_namespace:ReplaceText"> |
224 | 223 | <p>$replacement_label</p> |
— | — | @@ -236,7 +235,6 @@ |
237 | 236 | </form> |
238 | 237 | |
239 | 238 | END; |
240 | | - $wgOut->addHTML($text); |
241 | | - } |
242 | | - |
| 239 | + $wgOut->addHTML($text); |
| 240 | + } |
243 | 241 | } |