r89174 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89173‎ | r89174 | r89175 >
Date:15:57, 30 May 2011
Author:ashley
Status:deferred
Tags:
Comment:
Lingo: cleanup funky spacing, add some braces, etc.
Modified paths:
  • /trunk/extensions/Lingo/Lingo.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Lingo/Lingo.php
@@ -1,140 +1,158 @@
22 <?php
3 -
43 /**
5 - * Provides hover-over tool tips on articles from words defined on the Terminology page.
6 - * For more info see http://mediawiki.org/wiki/Extension:Lingo
 4+ * Provides hover-over tool tips on articles from words defined on the
 5+ * Terminology page.
76 *
 7+ * @file
88 * @defgroup Lingo
99 * @author Barry Coughlan
1010 * @copyright 2010 Barry Coughlan
1111 * @version 0.1
1212 * @licence GNU General Public Licence 2.0 or later
 13+ * @see http://www.mediawiki.org/wiki/Extension:Lingo Documentation
1314 */
1415 if ( !defined( 'MEDIAWIKI' ) ) {
1516 die( 'This file is part of a MediaWiki extension, it is not a valid entry point.' );
1617 }
1718
18 -define( 'LINGO_VERSION', '0.1' );
19 -
20 -$wgExtensionCredits[ 'parserhook' ][ ] = array(
 19+// Extension credits that will show up on Special:Version
 20+$wgExtensionCredits['parserhook'][] = array(
2121 'path' => __FILE__,
2222 'name' => 'Lingo',
 23+ 'version' => '0.1',
2324 'author' => 'Barry Coughlan',
2425 'url' => 'http://www.mediawiki.org/wiki/Extension:Lingo',
2526 'description' => 'Provides hover-over tool tips on articles from words defined on the [[Terminology]] page',
26 - 'version' => '0.1'
2727 );
2828
29 -$wgHooks[ 'OutputPageBeforeHTML' ][ ] = 'lingoHook';
 29+$wgHooks['OutputPageBeforeHTML'][] = 'lingoHook';
3030
31 -function lingoHook ( &$out, &$text ) {
 31+function lingoHook( &$out, &$text ) {
3232 global $wgOut, $wgScriptPath;
33 - $out -> includeJQuery();
34 - $out -> addHeadItem( 'tooltip.css', '<link rel="stylesheet" type="text/css" href="' . $wgScriptPath . '/extensions/Lingo/tooltip.css"/>' );
35 - $out -> addHeadItem( 'tooltip.js', '<script type="text/javascript" src="' . $wgScriptPath . '/extensions/Lingo/tooltip.min.js"></script>' );
 33+ $out->includeJQuery();
 34+ $out->addHeadItem( 'tooltip.css', '<link rel="stylesheet" type="text/css" href="' . $wgScriptPath . '/extensions/Lingo/tooltip.css"/>' );
 35+ $out->addHeadItem( 'tooltip.js', '<script type="text/javascript" src="' . $wgScriptPath . '/extensions/Lingo/tooltip.min.js"></script>' );
3636 return $out;
3737 }
3838
39 -function getLingoArray ( &$content ) {
40 - $term = array( );
 39+function getLingoArray( &$content ) {
 40+ $term = array();
4141 $c = explode( "\n", $content );
4242
4343 foreach ( $c as $entry ) {
44 - if ( empty( $entry ) || $entry[ 0 ] !== ';' )
 44+ if ( empty( $entry ) || $entry[ 0 ] !== ';' ) {
4545 continue;
 46+ }
4647
4748 $terms = explode( ':', $entry, 2 );
48 - if ( count( $terms ) < 2 )
49 - continue; //Invalid syntax
50 - //Add to array
51 - $term[ trim( substr( $terms[ 0 ], 1 ) ) ] = trim( $terms[ 1 ] );
 49+ if ( count( $terms ) < 2 ) {
 50+ continue; // Invalid syntax
 51+ }
 52+ // Add to array
 53+ $term[trim( substr( $terms[0], 1 ) )] = trim( $terms[1] );
5254 }
5355 return $term;
5456 }
5557
56 -$wgHooks[ 'ParserAfterTidy' ][ ] = 'lingoParser';
 58+$wgHooks['ParserAfterTidy'][] = 'lingoParser';
5759
58 -function lingoParser ( &$parser, &$text ) {
 60+function lingoParser( &$parser, &$text ) {
5961 global $wgRequest;
60 - $action = $wgRequest -> getVal( 'action', 'view' );
61 - if ( $action == "edit" || $action == "ajax" || isset( $_POST[ 'wpPreview' ] ) )
 62+
 63+ $action = $wgRequest->getVal( 'action', 'view' );
 64+ if ( $action == 'edit' || $action == 'ajax' || isset( $_POST['wpPreview'] ) ) {
6265 return false;
 66+ }
6367
64 - //Get Terminology page
 68+ // Get Terminology page
6569 $rev = Revision::newFromTitle( Title::makeTitle( null, 'Terminology' ) );
66 - if ( !$rev )
 70+ if ( !$rev ) {
6771 return false;
68 - $content = &$rev -> getText();
69 - if ( empty( $content ) )
 72+ }
 73+ $content = &$rev->getText();
 74+ if ( empty( $content ) ) {
7075 return false;
 76+ }
7177
72 - //Get array of terms
 78+ // Get array of terms
7379 $terms = getLingoArray( $content );
74 - //Get the minimum length abbreviation so we don't bother checking against words shorter than that
 80+ // Get the minimum length abbreviation so we don't bother checking against words shorter than that
7581 $min = min( array_map( 'strlen', array_keys( $terms ) ) );
7682
77 - //Parse HTML from page
78 - // FIXME: this works in PHP 5.3.3. What about 5.1?
 83+ // Parse HTML from page
 84+ // @todo FIXME: this works in PHP 5.3.3. What about 5.1?
7985 wfSuppressWarnings();
8086 $doc = DOMDocument::loadHTML( '<html><meta http-equiv="content-type" content="charset=utf-8"/>' . $text . '</html>' );
8187 wfRestoreWarnings();
8288
83 - //Find all text in HTML.
 89+ // Find all text in HTML.
8490 $xpath = new DOMXpath( $doc );
85 - $elements = $xpath -> query( "//*[text()!=' ']/text()" );
 91+ $elements = $xpath->query( "//*[text()!=' ']/text()" );
8692
87 - //Iterate all HTML text matches
88 - $nb = $elements -> length;
 93+ // Iterate all HTML text matches
 94+ $nb = $elements->length;
8995 $changed = false;
9096 for ( $pos = 0; $pos < $nb; $pos++ ) {
91 - $el = &$elements -> item( $pos );
92 - if ( strlen( $el -> nodeValue ) < $min )
 97+ $el = &$elements->item( $pos );
 98+ if ( strlen( $el->nodeValue ) < $min ) {
9399 continue;
 100+ }
94101
95 - //Split node text into words, putting offset and text into $offsets[0] array
96 - preg_match_all( "/[^\s\.,;:]+/", $el -> nodeValue, $offsets, PREG_OFFSET_CAPTURE );
 102+ // Split node text into words, putting offset and text into $offsets[0] array
 103+ preg_match_all(
 104+ '/[^\s\.,;:]+/',
 105+ $el->nodeValue,
 106+ $offsets,
 107+ PREG_OFFSET_CAPTURE
 108+ );
97109
98 - //Search and replace words in reverse order (from end of string backwards),
99 - //This way we don't mess up the offsets of the words as we iterate
100 - $len = count( $offsets[ 0 ] );
 110+ // Search and replace words in reverse order (from end of string backwards),
 111+ // This way we don't mess up the offsets of the words as we iterate
 112+ $len = count( $offsets[0] );
101113 for ( $i = $len - 1; $i >= 0; $i-- ) {
102 - $offset = $offsets[ 0 ][ $i ];
103 - //Check if word is an abbreviation from the terminologies
104 - if ( !is_numeric( $offset[ 0 ] ) && isset( $terms[ $offset[ 0 ] ] ) ) { //Word matches, replace with appropriate span tag
 114+ $offset = $offsets[0][$i];
 115+ // Check if word is an abbreviation from the terminologies
 116+ // Word matches, replace with appropriate span tag
 117+ if ( !is_numeric( $offset[0] ) && isset( $terms[$offset[0]] ) ) {
105118 $changed = true;
106119
107 - $tip = htmlentities( $terms[ $offset[ 0 ] ], ENT_COMPAT, 'UTF-8' );
 120+ $tip = htmlentities( $terms[$offset[0]], ENT_COMPAT, 'UTF-8' );
108121
109 - $beforeMatchNode = $doc -> createTextNode( substr( $el -> nodeValue, 0, $offset[ 1 ] ) );
110 - $afterMatchNode = $doc -> createTextNode( substr( $el -> nodeValue, $offset[ 1 ] + strlen( $offset[ 0 ] ), strlen( $el -> nodeValue ) - 1 ) );
 122+ $beforeMatchNode = $doc->createTextNode( substr( $el->nodeValue, 0, $offset[1] ) );
 123+ $afterMatchNode = $doc->createTextNode(
 124+ substr(
 125+ $el->nodeValue,
 126+ $offset[1] + strlen( $offset[0] ),
 127+ strlen( $el->nodeValue ) - 1
 128+ )
 129+ );
111130
112 - //Wrap abbreviation in <span> tags
113 - $span = $doc -> createElement( 'span', $offset[ 0 ] );
114 - $span -> setAttribute( 'class', "tooltip_abbr" );
 131+ // Wrap abbreviation in <span> tags
 132+ $span = $doc->createElement( 'span', $offset[0] );
 133+ $span->setAttribute( 'class', 'tooltip_abbr' );
115134
116 - //Wrap definition in <span> tags, hidden
117 - $spanTip = $doc -> createElement( 'span', $tip );
118 - $spanTip -> setAttribute( 'class', "tooltip_hide" );
 135+ // Wrap definition in <span> tags, hidden
 136+ $spanTip = $doc->createElement( 'span', $tip );
 137+ $spanTip->setAttribute( 'class', 'tooltip_hide' );
119138
120 - $el -> parentNode -> insertBefore( $beforeMatchNode, $el );
121 - $el -> parentNode -> insertBefore( $span, $el );
122 - $span -> appendChild( $spanTip );
123 - $el -> parentNode -> insertBefore( $afterMatchNode, $el );
124 - $el -> parentNode -> removeChild( $el );
125 - $el = $beforeMatchNode; //Set new element to the text before the match for next iteration
 139+ $el->parentNode->insertBefore( $beforeMatchNode, $el );
 140+ $el->parentNode->insertBefore( $span, $el );
 141+ $span->appendChild( $spanTip );
 142+ $el->parentNode->insertBefore( $afterMatchNode, $el );
 143+ $el->parentNode->removeChild( $el );
 144+ // Set new element to the text before the match for next iteration
 145+ $el = $beforeMatchNode;
126146 }
127147 }
128148 }
129149
130150 if ( $changed ) {
 151+ $body = $xpath->query( '/html/body' );
131152
132 - $body = $xpath -> query( '/html/body' );
133 -
134153 $text = '';
135 - foreach ( $body -> item( 0 ) -> childNodes as $child ) {
136 - $text .= $doc -> saveXML( $child );
 154+ foreach ( $body->item( 0 )->childNodes as $child ) {
 155+ $text .= $doc->saveXML( $child );
137156 }
138 -
139157 }
140158
141159 return true;

Status & tagging log