r89536 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89535‎ | r89536 | r89537 >
Date:21:33, 5 June 2011
Author:foxtrott
Status:deferred
Tags:
Comment:
try to optimize glossary building
Modified paths:
  • /trunk/extensions/Lingo/LingoBackend.php (modified) (history)
  • /trunk/extensions/Lingo/LingoParser.php (modified) (history)
  • /trunk/extensions/Lingo/LingoTree.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Lingo/LingoBackend.php
@@ -28,7 +28,7 @@
2929
3030 /**
3131 *
32 - * @return Boolean true, if a next element is available
 32+ * @return the next element or null
3333 */
3434 abstract public function next();
3535
Index: trunk/extensions/Lingo/LingoParser.php
@@ -95,16 +95,8 @@
9696 $backend = &$this->mLingoBackend;
9797
9898 // assemble the result array
99 - $this->mLingoArray = array();
10099 while ( $elementData = $backend->next() ) {
101 -
102 - if ( array_key_exists( ( $term = $elementData[LingoElement::ELEMENT_TERM] ), $this->mLingoArray ) ) {
103 - $this->mLingoArray[$term]->addDefinition( $elementData );
104 - } else {
105 - $this->mLingoArray[$term] = new LingoElement( $term, $elementData );
106 - }
107 -
108 - $this->mLingoTree->addTerm( $term, $elementData );
 100+ $this->mLingoTree->addTerm( $elementData[LingoElement::ELEMENT_TERM], $elementData );
109101 }
110102
111103 wfProfileOut( __METHOD__ );
Index: trunk/extensions/Lingo/LingoTree.php
@@ -26,8 +26,8 @@
2727 class LingoTree {
2828
2929 private $mTree = array();
30 - private $mDefinition = null;
31 - private $mMinLength = -1;
 30+ private $mList = array();
 31+ private $mMinLength = 1000;
3232
3333 /**
3434 * Adds a string to the Lingo Tree
@@ -38,49 +38,50 @@
3939 return;
4040 }
4141
42 - $matches;
43 - preg_match_all( '/[[:alpha:]]+|[^[:alpha:]]/u', $term, $matches );
 42+ if ( isset( $this->mList[$term] ) ) { // term exists, store 2nd definition
4443
45 - $this->addElement( $matches[0], $term, $definition );
 44+ $this->mList[$term][-1]->addDefinition( $definition );
4645
47 - if ( $this->mMinLength > -1 ) {
48 - $this->mMinLength = min( array( $this->mMinLength, strlen( $term ) ) );
4946 } else {
50 - $this->mMinLength = strlen( $term );
 47+
 48+ $matches;
 49+ preg_match_all( '/[[:alpha:]]+|[^[:alpha:]]/u', $term, $matches );
 50+
 51+ $this->mList[$term] = $this->addElement( $matches[0], $term, $definition );
 52+
 53+ $this->mMinLength = min( array($this->mMinLength, strlen( $term )) );
5154 }
5255 }
5356
5457 /**
55 - * Recursively adds an element to the Lingo Tree
 58+ * Adds an element to the Lingo Tree
5659 *
5760 * @param array $path
5861 * @param <type> $index
 62+ * @return Array the tree node the element was stored in
5963 */
60 - protected function addElement( Array &$path, &$term, &$definition ) {
 64+ protected function &addElement( Array &$path, &$term, &$definition ) {
 65+
 66+ $tree = &$this->mTree;
 67+
6168 // end of path, store description; end of recursion
62 - if ( $path == null ) {
63 - $this -> addDefinition( $term, $definition );
64 - } else {
65 - $step = array_shift( $path );
 69+ while ( $step = array_shift( $path ) ) {
6670
67 - if ( !array_key_exists( $step, $this->mTree ) ) {
68 - $this->mTree[$step] = new LingoTree();
 71+ if ( !isset( $tree[$step] ) ) {
 72+ $tree[$step] = array();
6973 }
7074
71 - $this->mTree[$step]->addElement( $path, $term, $definition );
 75+ $tree = &$tree[$step];
 76+
7277 }
73 - }
7478
75 - /**
76 - * Adds a defintion to the treenodes list of definitions
77 - * @param <type> $definition
78 - */
79 - protected function addDefinition( &$term, &$definition ) {
80 - if ( $this->mDefinition ) {
81 - $this->mDefinition->addDefinition( $definition );
 79+ if ( isset( $tree[-1] ) ) {
 80+ $tree[-1]->addDefinition( $definition );
8281 } else {
83 - $this->mDefinition = new LingoElement( $term, $definition );
 82+ $tree[-1] = new LingoElement( $term, $definition );
8483 }
 84+
 85+ return $tree;
8586 }
8687
8788 function getMinTermLength() {
@@ -99,7 +100,7 @@
100101
101102 // Did we find the start of a term?
102103 if ( array_key_exists( $currLex, $this->mTree ) ) {
103 - list( $lastindex, $definition ) = $this->mTree[$currLex]->findNextTermNoSkip( $lexemes, $index, $countLexemes );
 104+ list( $lastindex, $definition ) = $this->findNextTermNoSkip( $this->mTree[$currLex], $lexemes, $index, $countLexemes );
104105 }
105106
106107 // this will increase the index even if we found something;
@@ -109,19 +110,19 @@
110111
111112 wfProfileOut( __METHOD__ );
112113 if ( $definition ) {
113 - return array( $index - $start - 1, $lastindex - $index + 2, $definition );
 114+ return array($index - $start - 1, $lastindex - $index + 2, $definition);
114115 } else {
115 - return array( $index - $start, 0, null );
 116+ return array($index - $start, 0, null);
116117 }
117118 }
118119
119 - function findNextTermNoSkip( &$lexemes, $index, $countLexemes ) {
 120+ function findNextTermNoSkip( Array &$tree, &$lexemes, $index, $countLexemes ) {
120121 wfProfileIn( __METHOD__ );
121122
122 - if ( $index + 1 < $countLexemes && array_key_exists( $currLex = $lexemes[$index + 1][0], $this->mTree ) ) {
123 - $ret = $this->mTree[$currLex]->findNextTermNoSkip( $lexemes, $index + 1, $countLexemes );
 123+ if ( $index + 1 < $countLexemes && array_key_exists( $currLex = $lexemes[$index + 1][0], $tree ) ) {
 124+ $ret = $this->findNextTermNoSkip( $tree[$currLex], $lexemes, $index + 1, $countLexemes );
124125 } else {
125 - $ret = array( $index, &$this->mDefinition );
 126+ $ret = array($index, &$tree[-1]);
126127 }
127128 wfProfileOut( __METHOD__ );
128129 return $ret;

Status & tagging log