r92506 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92505‎ | r92506 | r92507 >
Date:23:23, 18 July 2011
Author:raindrift
Status:ok (Comments)
Tags:
Comment:
Refactored section assembly code to be more readable
Added a hook that's called for each section. This allows sections to be addressed separately at the document level, for example by wrapping each in a div (as in the reverted r50769). This in-turn enables richer section-specific UI, like highlighting or in-line editing.
Modified paths:
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/Parser.php
@@ -4188,30 +4188,42 @@
41894189 }
41904190
41914191 # split up and insert constructed headlines
4192 -
41934192 $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
41944193 $i = 0;
4195 -
4196 - foreach ( $blocks as $block ) {
4197 - if ( $showEditLink && $headlineCount > 0 && $i == 0 && $block !== "\n" ) {
4198 - # This is the [edit] link that appears for the top block of text when
4199 - # section editing is enabled
4200 -
4201 - # Disabled because it broke block formatting
4202 - # For example, a bullet point in the top line
4203 - # $full .= $sk->editSectionLink(0);
 4194+
 4195+ // build an array of document sections
 4196+ $sections = array();
 4197+ foreach ( $blocks as $block ) {
 4198+ // $head is zero-based, sections aren't.
 4199+ if ( empty( $head[$i - 1] ) ) {
 4200+ $sections[$i] = $block;
 4201+ } else {
 4202+ $sections[$i] = $head[$i - 1] . $block;
42044203 }
4205 - $full .= $block;
4206 - if ( $enoughToc && !$i && $isMain && !$this->mForceTocPosition ) {
4207 - # Top anchor now in skin
4208 - $full = $full.$toc;
4209 - }
42104204
4211 - if ( !empty( $head[$i] ) ) {
4212 - $full .= $head[$i];
4213 - }
 4205+ /**
 4206+ * Send a hook, one per section.
 4207+ * The idea here is to be able to make section-level DIVs, but to do so in a
 4208+ * lower-impact, more correct way than r50769
 4209+ *
 4210+ * $this : caller
 4211+ * $section : the section number
 4212+ * &$sectionContent : ref to the content of the section
 4213+ * $showEditLinks : boolean describing whether this section has an edit link
 4214+ */
 4215+ wfRunHooks( 'ParserSectionCreate', array( $this, $i, &$sections[$i], $showEditLink ) );
 4216+
42144217 $i++;
42154218 }
 4219+
 4220+ if ( $enoughToc && $isMain && !$this->mForceTocPosition ) {
 4221+ // append the TOC at the beginning
 4222+ // Top anchor now in skin
 4223+ $sections[0] = $sections[0] . $toc . "\n";
 4224+ }
 4225+
 4226+ $full .= join( '', $sections );
 4227+
42164228 if ( $this->mForceTocPosition ) {
42174229 return str_replace( '<!--MWTOC-->', $toc, $full );
42184230 } else {

Follow-up revisions

RevisionCommit summaryAuthorDate
r92643Documenting new parser hook, re r92506....raindrift17:08, 20 July 2011
r92919This extension makes article section edit links clearer. It:...raindrift23:45, 22 July 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r50769Reverting the code that encloses article sections in their own div for the ti...aboostani15:12, 19 May 2009

Comments

#Comment by 😂 (talk | contribs)   23:29, 18 July 2011

Setting back to new (not sure why a change to Parser.php was deferred).

Also: when adding a new hook, please update docs/hooks.txt with information about the hook.

#Comment by Brion VIBBER (talk | contribs)   22:56, 19 July 2011

Marking ok, but leaving todo tag for the hooks doc.

#Comment by Raindrift (talk | contribs)   17:09, 20 July 2011

Hook documented, r92643

Status & tagging log