r52394 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52393‎ | r52394 | r52395 >
Date:11:07, 25 June 2009
Author:catrope
Status:deferred
Tags:
Comment:
NavigableTOC:
* Use mergeSectionTrees() to improve performance for section previews: they no longer need to parse the result of replaceSection()
* Instead of unlinking all section except the edited section and its descendants, unlink all sections without an offset. This fixes a bug where sibling sections added in preview would not be linked, and as a side effect also unlinks sections included from templates.
Modified paths:
  • /trunk/extensions/UsabilityInitiative/NavigableTOC/ISSUES (modified) (history)
  • /trunk/extensions/UsabilityInitiative/NavigableTOC/NavigableTOC.hooks.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/NavigableTOC/NavigableTOC.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/NavigableTOC/NavigableTOC.hooks.php
@@ -11,7 +11,7 @@
1212 /* Static Functions */
1313
1414 /**
15 - * EditPage::showEditForm::initial hook
 15+ * EditPage::showEditForm:initial hook
1616 * Adds the TOC to the edit form
1717 */
1818 public static function addTOC( &$ep ) {
@@ -29,10 +29,18 @@
3030 $popts->setTidy( true );
3131 $popts->enableLimitReport();
3232 $articleObj = new Article( $ep->mTitle );
33 - $p_result = false;
34 - if ( $ep->preview && $ep->section == '' )
 33+ $p_result = $p_result2 = false;
 34+ if ( $ep->preview ) {
3535 $p_result = $ep->mParserOutput;
36 - else if ( $wgEnableParserCache && !$ep->preview ) {
 36+ if ( $ep->section != '' ) {
 37+ // Store this result and make sure the
 38+ // ParserOutput for the entire page is
 39+ // grabbed as well
 40+ $p_result2 = $p_result;
 41+ $p_result = false;
 42+ }
 43+ }
 44+ else if ( $wgEnableParserCache ) {
3745 $p_result = $pcache->get( $articleObj, $popts );
3846 // The ParserOutput in cache could be too old to have
3947 // byte offsets. In that case, reparse
@@ -48,35 +56,50 @@
4957 }
5058 if ( !$p_result ) {
5159 $popts->setIsPreview( $ep->preview || $ep->section != '' );
52 - $text = $articleObj->getContent();
53 - if ( $ep->preview ) {
54 - if( $ep->section == 'new' )
55 - $text .= $ep->textbox1;
56 - else if ( $ep->section != '' )
57 - $text = $wgParser->replaceSection( $text,
58 - $ep->section, $ep->textbox1 );
59 - else
60 - $text = $ep->textbox1;
61 - }
62 - $p_result = $wgParser->parse( $text, $ep->mTitle,
63 - $popts );
 60+ $p_result = $wgParser->parse( $articleObj->getContent(),
 61+ $ep->mTitle, $popts );
6462 if ( $wgEnableParserCache )
65 - $pcache->save( $p_result, $articleObj, $popts );
 63+ $pcache->save( $p_result, $articleObj,
 64+ $popts );
6665 }
 66+
 67+ if( $p_result2 ) {
 68+ // Merge the section trees of the original article and
 69+ // the edited text; this saves us from parsing the
 70+ // entire page with the edited section replaced
 71+ $sectionTree = Parser::mergeSectionTrees(
 72+ $p_result->getSections(),
 73+ $p_result2->getSections(),
 74+ $ep->section, $ep->mTitle, strlen( $ep->textbox1 ) );
 75+ $toc = $wgUser->getSkin()->generateTOC( $sectionTree );
 76+ } else {
 77+ $sectionTree = $p_result->getSections();
 78+ $toc = $p_result->getTOCHTML();
 79+ }
6780
6881 $js = "\$.section = '" . Xml::escapeJsString( $ep->section ) . "';";
6982 $js .= "\$.sectionOffsets = [";
7083 $targetLevel = false;
71 - foreach ( $p_result->getSections() as $section )
 84+ $targetSection = false;
 85+ foreach ( $sectionTree as $section )
7286 if ( !is_null( $section['byteoffset'] ) ) {
7387 if ( $ep->section != '' ) {
7488 // Only get offsets for the section
75 - // being edited and its descendants
 89+ // being edited and its descendants.
 90+ // In preview mode, sibling sections
 91+ // may have been added, so use the
 92+ // number of sections in $p_result2
7693 if ( $section['index'] < $ep->section )
7794 continue;
78 - else if ( $section['index'] == $ep->section )
79 - $targetLevel = $section['level'];
80 - else if ( $section['level'] <= $targetLevel )
 95+ else if ( $section['index'] == $ep->section ) {
 96+ if ( $ep->preview )
 97+ $targetSection = $ep->section +
 98+ count( $p_result2->getSections() );
 99+ else
 100+ $targetLevel = $section['level'];
 101+ }
 102+ else if ( ( $ep->preview && $section['index'] >= $targetSection ) ||
 103+ ( !$ep->preview && $section['level'] <= $targetLevel ) )
81104 break;
82105 }
83106 $js .= intval( $section['byteoffset'] ) . ',';
@@ -87,8 +110,8 @@
88111 // Terrible hack to prevent two TOCs with the same ID
89112 // from being displayed
90113 $toc = str_replace( '<table id="toc"',
91 - '<table id="navigableTOC"', $p_result->getTOCHTML() );
 114+ '<table id="navigableTOC"', $toc );
92115 $ep->editFormTextTop .= $toc . $jsTag;
93116 return true;
94 - }
 117+ }
95118 }
Index: trunk/extensions/UsabilityInitiative/NavigableTOC/NavigableTOC.js
@@ -112,33 +112,23 @@
113113 .data( 'offset', $.sectionOffsets[i] );
114114 } else if ( $.section != 'new' && $.section != 0 ) {
115115 // Existing section edit
116 - // Unlink all irrelevant section links
117 - $.section = parseInt( $.section );
118 - $( '.toc:last * li' ).not( '.tocsection-' + $.section + ' * li')
119 - .not( '.tocsection-' + $.section ).each( function() {
120 - link = $(this).children( 'a' );
121 - if ( link.is( ':visible' ) ) {
122 - link.hide();
123 - $(this).prepend( link.html() );
124 - }
125 - });
126 -
127116 // Set adjusted offsets on the usable links
 117+ $.section = parseInt( $.section );
128118 for ( i = 0; i < $.sectionOffsets.length; i++ )
129119 $( '.tocsection-' + ( i + $.section ) ).children( 'a' )
130120 .data( 'offset', $.sectionOffsets[i] -
131121 $.sectionOffsets[0] );
132 - } else {
133 - // New section or section 0
134 - // Unlink everything
135 - $( '.toc:last * li' ).each( function() {
136 - link = $(this).children( 'a' );
137 - if ( link.visible() ) {
138 - link.hide();
139 - $(this).prepend( link.html() );
140 - }
141 - });
142122 }
 123+ // Unlink all section links that didn't get an offset
 124+ $( '.toc:last * li' ).each( function() {
 125+ link = $(this).children( 'a' );
 126+ if ( typeof link.data( 'offset') == 'undefined' &&
 127+ link.is( ':visible' ) ) {
 128+ link.hide();
 129+ $(this).prepend( link.html() );
 130+ }
 131+ });
 132+
143133 $( '.toc:last * li a' ).click( function(e) {
144134 if( typeof jQuery(this).data( 'offset' ) != 'undefined' )
145135 jQuery( '#wpTextbox1' ).scrollToPosition( jQuery(this).data( 'offset' ) );
Index: trunk/extensions/UsabilityInitiative/NavigableTOC/ISSUES
@@ -6,13 +6,3 @@
77 7x15 (Linux), but these values should really be determined dynamically.
88 ** Both of these can be avoided by determining the caret position properly,
99 I'll ask Michael Dale about this.
10 -* When previewing, two TOCs may show up (one in the preview, one navigable),
11 - in which case the first TOC gets two [hide] links and the second gets none
12 -* When previewing a section, both the section content and the entire page are
13 - parsed, which is slow.
14 -** We're parsing the entire page solely for its TOC, maybe we can take the
15 - old TOC from the parser cache and blend in the edited section?
16 -** Is it even desirable to show full context here? Showing the edited section
17 - as top-level is less parser-intensive for section previews
18 -* TOC is only shown when it'd normally appear on the page (4 sections or
19 - __TOC__ and no __NOTOC__), is this desirable?

Status & tagging log