r57099 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57098‎ | r57099 | r57100 >
Date:11:14, 30 September 2009
Author:werdna
Status:deferred
Tags:
Comment:
LiquidThreads subject handling:
* Fix display of subject editing field for editing existing top-level threads.
* Try to normalise thread titles instead of dying when a thread subject has nasty chars in it.
* Display an error message when invalid titles are specified.
* General code cleanup.
Modified paths:
  • /trunk/extensions/LiquidThreads/classes/Threads.php (modified) (history)
  • /trunk/extensions/LiquidThreads/classes/View.php (modified) (history)
  • /trunk/extensions/LiquidThreads/i18n/Lqt.i18n.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LiquidThreads/i18n/Lqt.i18n.php
@@ -33,9 +33,9 @@
3434 'lqt_new_thread' => 'Start a new discussion',
3535 'lqt_invalid_subject' => 'The subject you entered is invalid.
3636 It may:
37 -* Contain invalid characters, such as []{}<>,
3837 * Be too long, or
3938 * Conflict with interwiki prefixes or namespace names.',
 39+ 'lqt_empty_subject' => 'You must enter a subject.',
4040 'lqt_subject_change_forbidden' => 'You cannot change the subject for this thread because you are not allowed to move pages.',
4141 'lqt_in_response_to' => 'In reply to $1 by $2, above:',
4242 'lqt_edited_notice' => 'Edited',
Index: trunk/extensions/LiquidThreads/classes/Threads.php
@@ -201,16 +201,47 @@
202202 return self::incrementedTitle( $base, NS_LQT_THREAD );
203203 }
204204
 205+ // This will attempt to replace invalid characters and sequences in a title with
 206+ // a safe replacement (_, currently).
 207+ public static function makeTitleValid( $text ) {
 208+ static $rxTc;
 209+
 210+ if ( is_callable( array( 'Title', 'getTitleInvalidRegex' ) ) ) {
 211+ $rxTc = Title::getTitleInvalidRegex();
 212+ } elseif (!$rxTc) { // Back-compat
 213+ $rxTc = '/' .
 214+ # Any character not allowed is forbidden...
 215+ '[^' . Title::legalChars() . ']' .
 216+ # URL percent encoding sequences interfere with the ability
 217+ # to round-trip titles -- you can't link to them consistently.
 218+ '|%[0-9A-Fa-f]{2}' .
 219+ # XML/HTML character references produce similar issues.
 220+ '|&[A-Za-z0-9\x80-\xff]+;' .
 221+ '|&#[0-9]+;' .
 222+ '|&#x[0-9A-Fa-f]+;' .
 223+ '/S';
 224+ }
 225+
 226+ $text = preg_replace( $rxTc, '_', $text );
 227+
 228+ return $text;
 229+ }
 230+
205231 /** Keep trying titles starting with $basename until one is unoccupied. */
206232 public static function incrementedTitle( $basename, $namespace ) {
207233 $i = 2;
208234
209 - $replacements = array_fill_keys( array( '[', ']', '{', '}', '|' ), '_' );
210 - $basename = strtr( $basename, $replacements );
 235+ // Try to make the title valid.
 236+ $basename = Threads::makeTitleValid( $basename );
211237
212238 $t = Title::makeTitleSafe( $namespace, $basename );
213239 while ( !$t || $t->exists() ||
214240 in_array( $t->getPrefixedDBkey(), self::$occupied_titles ) ) {
 241+
 242+ if (!$t) {
 243+ throw new MWException( "Error in creating title for basename $basename" );
 244+ }
 245+
215246 $t = Title::makeTitleSafe( $namespace, $basename . ' (' . $i . ')' );
216247 $i++;
217248 }
Index: trunk/extensions/LiquidThreads/classes/View.php
@@ -273,27 +273,31 @@
274274 throughout the edit cycle, since the article doesn't exist yet anyways.
275275 */
276276
277 - // Stuff that might break the save
 277+ // Check if we actually want a subject, pull the submitted subject, and validate it.
 278+ $subject_expected = ( $edit_type == 'new' || $thread && $thread->isTopmostThread() );
 279+ $subject = $this->request->getVal( 'lqt_subject_field', '' );
278280 $valid_subject = true;
279 - $failed_rename = false;
280281
281 - $subject = $this->request->getVal( 'lqt_subject_field', '' );
282 -
283282 if ( $edit_type == 'summarize' && $edit_applies_to->summary() ) {
284283 $article = $edit_applies_to->summary();
285284 } elseif ( $edit_type == 'summarize' ) {
286285 $t = $this->newSummaryTitle( $edit_applies_to );
287286 $article = new Article( $t );
288 - } elseif ( $thread == null ) {
289 - if ( $subject && is_null( Title::makeTitleSafe( NS_LQT_THREAD, $subject ) ) ) {
 287+ } elseif ( !$thread ) {
 288+ if ( !$subject ) {
290289 // Dodgy title
 290+ $t = $this->scratchTitle();
291291 $valid_subject = false;
292 - $t = $this->scratchTitle();
293 - } else {
294 - if ( $edit_type == 'new' ) {
295 - $t = $this->newScratchTitle( $subject );
296 - } elseif ( $edit_type == 'reply' ) {
297 - $t = $this->newReplyTitle( $subject, $edit_applies_to );
 292+ } else {
 293+ try {
 294+ if ( $edit_type == 'new' ) {
 295+ $t = $this->newScratchTitle( $subject );
 296+ } elseif ( $edit_type == 'reply' ) {
 297+ $t = $this->newReplyTitle( $subject, $edit_applies_to );
 298+ }
 299+ } catch( MWException $excep ) {
 300+ $t = $this->scratchTitle();
 301+ $valid_subject = false;
298302 }
299303 }
300304 $article = new Article( $t );
@@ -303,15 +307,21 @@
304308
305309 $e = new EditPage( $article );
306310
307 -
308 - // Find errors.
309 - if (!$valid_subject && $subject) {
 311+ // Display an error if a subject is specified but it's invalid
 312+ if ( $subject_expected && $this->request->wasPosted() && !$valid_subject ) {
 313+ if ( !$subject ) {
 314+ $msg = 'lqt_empty_subject';
 315+ } else {
 316+ $msg = 'lqt_invalid_subject';
 317+ }
 318+
310319 $e->editFormPageTop .=
311320 Xml::tags( 'div', array( 'class' => 'error' ),
312 - wfMsgExt( 'lqt_invalid_subject', 'parse' ) );
 321+ wfMsgExt( $msg, 'parse' ) );
313322 }
314323
315 - if ( (!$valid_subject && $subject) || $failed_rename ) {
 324+ // Quietly force a preview if no subject has been specified.
 325+ if ( (!$valid_subject && $subject) || ($subject_expected && !$subject) ) {
316326 // Dirty hack to prevent saving from going ahead
317327 global $wgRequest;
318328 $wgRequest->setVal( 'wpPreview', true );
@@ -339,7 +349,7 @@
340350 $wgMemc->set( $key, 1, 3600 );
341351 }
342352
343 - if ( $edit_type == 'new' ) {
 353+ if ( $subject_expected ) {
344354 wfLoadExtensionMessages( 'LiquidThreads' );
345355 // This is a top-level post; show the subject line.
346356 $db_subject = $thread ? $thread->subjectWithoutIncrement() : '';

Status & tagging log