r35904 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r35903‎ | r35904 | r35905 >
Date:01:16, 5 June 2008
Author:nad
Status:old
Tags:
Comment:
1.0.3 - fixed another multiple-trees bug
Modified paths:
  • /trunk/extensions/TreeAndMenu/TreeAndMenu.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TreeAndMenu/TreeAndMenu.php
@@ -14,7 +14,7 @@
1515
1616 if (!defined('MEDIAWIKI')) die('Not an entry point.');
1717
18 -define('TREEANDMENU_VERSION','1.0.2, 2008-06-05');
 18+define('TREEANDMENU_VERSION','1.0.3, 2008-06-05');
1919
2020 # Set any unset images to default titles
2121 if (!isset($wgTreeViewImages) || !is_array($wgTreeViewImages)) $wgTreeViewImages = array();
@@ -52,8 +52,8 @@
5353 * Constructor
5454 */
5555 function __construct() {
56 - global $wgOut,$wgHooks,$wgParser,$wgScriptPath,$wgJsMimeType,
57 - $wgTreeMagic,$wgMenuMagic,$wgTreeViewImages,$wgTreeViewShowLines;
 56+ global $wgOut, $wgHooks, $wgParser, $wgScriptPath, $wgJsMimeType,
 57+ $wgTreeMagic, $wgMenuMagic, $wgTreeViewImages, $wgTreeViewShowLines;
5858
5959 # Add hooks
6060 $wgParser->setFunctionHook($wgTreeMagic,array($this,'expandTree'));
@@ -62,7 +62,7 @@
6363
6464 # Update general tree paths and properties
6565 $this->baseDir = dirname(__FILE__);
66 - $this->baseUrl = preg_replace('|^.+(?=[/\\\\]extensions)|',$wgScriptPath,$this->baseDir);
 66+ $this->baseUrl = preg_replace('|^.+(?=[/\\\\]extensions)|', $wgScriptPath, $this->baseDir);
6767 $this->useLines = $wgTreeViewShowLines ? 'true' : 'false';
6868 $this->uniq = uniqid($this->uniqname);
6969
@@ -84,7 +84,7 @@
8585 */
8686 public function expandTree() {
8787 $args = func_get_args();
88 - return $this->expandTreeAndMenu('tree',$args);
 88+ return $this->expandTreeAndMenu('tree', $args);
8989 }
9090
9191 /**
@@ -92,17 +92,17 @@
9393 */
9494 public function expandMenu() {
9595 $args = func_get_args();
96 - return $this->expandTreeAndMenu('menu',$args);
 96+ return $this->expandTreeAndMenu('menu', $args);
9797 }
9898
9999 /**
100100 * Expand either kind of parser-function (reformats tree rows for matching later) and store args
101101 */
102 - private function expandTreeAndMenu($magic,$args) {
 102+ private function expandTreeAndMenu($magic, $args) {
103103 $parser = array_shift($args);
104104
105105 # Store args for this tree for later use
106 - foreach ($args as $arg) if (preg_match('/^(\\w+?)\\s*=\\s*(.+)$/s',$arg,$m)) $args[$m[1]] = $m[2]; else $text = $arg;
 106+ foreach ($args as $arg) if (preg_match('/^(\\w+?)\\s*=\\s*(.+)$/s', $arg, $m)) $args[$m[1]] = $m[2]; else $text = $arg;
107107
108108 # If root, parse as wikitext
109109 if (isset($args['root'])) {
@@ -117,8 +117,8 @@
118118 $this->args[$this->id] = $args;
119119
120120 # Reformat tree rows for matching in ParserAfterStrip
121 - $text = preg_replace('/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/',"{$this->uniq}3$1{$this->uniq}4",$text);
122 - $text = preg_replace_callback('/^(\\*+)(.*?)$/m',array($this,'formatRow'),$text);
 121+ $text = preg_replace('/(?<=\\*)\\s*\\[\\[Image:(.+?)\\]\\]/', "{$this->uniq}3$1{$this->uniq}4", $text);
 122+ $text = preg_replace_callback('/^(\\*+)(.*?)$/m', array($this, 'formatRow'), $text);
123123
124124 return $text;
125125 }
@@ -139,17 +139,17 @@
140140 /**
141141 * Called after parser has finished (ParserAfterTidy) so all transcluded parts can be assembled into final trees
142142 */
143 - public function renderTreeAndMenu(&$parser,&$text) {
 143+ public function renderTreeAndMenu(&$parser, &$text) {
144144 global $wgJsMimeType;
145145 $u = $this->uniq;
146146
147147 # Determine which trees are sub trees
148148 # - there should be a more robust way to do this,
149149 # it's just based on the fact that all sub-tree's have a minus preceding their row data
150 - if (!preg_match_all("/\x7f\x7f1$u\x7f(.+?)\x7f/",$text,$subs)) $subs = array(1 => array());
 150+ if (!preg_match_all("/\x7f\x7f1$u\x7f(.+?)\x7f/", $text, $subs)) $subs = array(1 => array());
151151
152152 # Extract all the formatted tree rows in the page and if any, replace with dTree JavaScript
153 - if (preg_match_all("/\x7f1$u\x7f(.+?)\x7f([0-9]+)\x7f({$u}3(.+?){$u}4)?(.*?)(?=\x7f[12]$u)/",$text,$matches,PREG_SET_ORDER)) {
 153+ if (preg_match_all("/\x7f1$u\x7f(.+?)\x7f([0-9]+)\x7f({$u}3(.+?){$u}4)?(.*?)(?=\x7f[12]$u)/", $text, $matches, PREG_SET_ORDER)) {
154154
155155 # PASS-1: build $rows array containing depth, and tree start/end information
156156 $rows = array();
@@ -158,13 +158,13 @@
159159 $lastId = '';
160160 $lastDepth = 0;
161161 foreach ($matches as $match) {
162 - list(,$id,$depth,,$icon,$item) = $match;
 162+ list(, $id, $depth,, $icon, $item) = $match;
163163 $start = false;
164164 if ($id != $lastId) {
165165 if (!isset($depths[$id])) $depths[$id] = $depths[$lastId]+$lastDepth;
166 - if ($start = $rootId != $id && !in_array($id,$subs[1])) $depths[$rootId = $id] = 0;
 166+ if ($start = $rootId != $id && !in_array($id, $subs[1])) $depths[$rootId = $id] = 0;
167167 }
168 - if ($item) $rows[] = array($rootId,$depth+$depths[$id],$icon,$item,$start);
 168+ if ($item) $rows[] = array($rootId, $depth+$depths[$id], $icon, addslashes($item), $start);
169169 $lastId = $id;
170170 $lastDepth = $depth;
171171 }
@@ -175,22 +175,22 @@
176176 $node = 0;
177177 $last = -1;
178178 $nodes = '';
179 - foreach ($rows as $info) {
 179+ foreach ($rows as $i => $info) {
180180 $node++;
181 - list($id,$depth,$icon,$item,$start) = $info;
 181+ list($id, $depth, $icon, $item, $start) = $info;
182182 $args = $this->args[$id];
183183 $type = $args['type'];
184 - $end = $node == count($rows) || $rows[$node][4];
 184+ $end = $i == count($rows)-1 || $rows[$i+1][4];
185185 if (!isset($args['root'])) $args['root'] = ''; # tmp - need to handle rootless trees
186186 if ($start) $node = 1;
187187
188188 # Append node script for this row
189189 if ($depth > $last) $parents[$depth] = $node-1;
190190 $parent = $parents[$depth];
191 - if ($type == 'tree') $nodes .= "{$this->uniqname}$id.add($node,$parent,'".addslashes($item)."');\n";
 191+ if ($type == 'tree') $nodes .= "{$this->uniqname}$id.add($node, $parent, '$item ($start)');\n";
192192 else {
193193 if (!$start) {
194 - if ($depth < $last) $nodes .= str_repeat('</ul></li>',$last-$depth);
 194+ if ($depth < $last) $nodes .= str_repeat('</ul></li>', $last-$depth);
195195 elseif ($depth > $last) $nodes .= "\n<ul>";
196196 }
197197 $parity[$depth] = isset($parity[$depth]) ? $parity[$depth]^1 : 0;
@@ -229,8 +229,8 @@
230230 else {
231231
232232 # Finalise a menu
233 - if ($depth > 0) $nodes .= str_repeat('</ul></li>',$depth);
234 - $nodes = preg_replace("/<(a.*? )title=\".+?\".*?>/","<$1>",$nodes); # IE has problems with title attribute in suckerfish menus
 233+ if ($depth > 0) $nodes .= str_repeat('</ul></li>', $depth);
 234+ $nodes = preg_replace("/<(a.*? )title=\".+?\".*?>/", "<$1>", $nodes); # IE has problems with title attribute in suckerfish menus
235235 $html = "
236236 <ul class='$class' id='$id'>\n$nodes</ul>
237237 <script type=\"$wgJsMimeType\">
@@ -245,14 +245,14 @@
246246 ";
247247 }
248248
249 - $text = preg_replace("/\x7f1$u\x7f$id\x7f.+?$/m",$html,$text,1); # replace first occurence of this trees root-id
 249+ $text = preg_replace("/\x7f1$u\x7f$id\x7f.+?$/m", $html, $text, 1); # replace first occurence of this trees root-id
250250 $nodes = '';
251251 $last = -1;
252252 }
253253 }
254254 }
255255
256 - $text = preg_replace("/\x7f1$u\x7f.+?[\\r\\n]+/m",'',$text); # Remove all unreplaced row information
 256+ $text = preg_replace("/\x7f1$u\x7f.+?[\\r\\n]+/m", '', $text); # Remove all unreplaced row information
257257 return true;
258258 }
259259
@@ -271,9 +271,9 @@
272272 /**
273273 * Needed in MediaWiki >1.8.0 for magic word hooks to work properly
274274 */
275 -function wfTreeAndMenuLanguageGetMagic(&$magicWords,$langCode = 0) {
276 - global $wgTreeMagic,$wgMenuMagic;
277 - $magicWords[$wgTreeMagic] = array($langCode,$wgTreeMagic);
278 - $magicWords[$wgMenuMagic] = array($langCode,$wgMenuMagic);
 275+function wfTreeAndMenuLanguageGetMagic(&$magicWords, $langCode = 0) {
 276+ global $wgTreeMagic, $wgMenuMagic;
 277+ $magicWords[$wgTreeMagic] = array($langCode, $wgTreeMagic);
 278+ $magicWords[$wgMenuMagic] = array($langCode, $wgMenuMagic);
279279 return true;
280280 }

Status & tagging log