Difference between revisions of "Extension talk:Treeview5.php"
(Add bug for tracking down) |
(new voodooless way) |
||
Line 1: | Line 1: | ||
{{ext-talk-msg|Tree view}} | {{ext-talk-msg|Tree view}} | ||
− | + | Treeview5.1 is a new approach to the core recursive bullet-list code which doesn't require any voodoo, and should be compatible with all MediaWiki versions including 1.12. | |
− | + | == How it works == | |
− | + | The initial problem was how to write a parser-function which could generate a tree which could be composed of other transcluded tree's (i.e. parser-functions which are recursively executed forming a larger whole structure together). This seemed impossible since there are no MediaWiki properties available in the environment which reveal the current depth that the parser-function expansion is operating at. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | The | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | But after thinking about the problem more closely, it turns out that the tree can be constructed without depth information by converting each tree row into a new format containing depth and tree id information which is protected from wiki-parsing. This information can then be converted into tree JavaScript after the parser has finished in the ''ParserAfterTidy'' hook. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Here is the wikitext of an example tree which includes two transcluded tree's | |
− | |||
− | |||
− | |||
<pre> | <pre> | ||
− | + | *[[toplevl]] | |
+ | **bar | ||
+ | ***baz | ||
+ | *foo | ||
+ | **[[Bar]] | ||
+ | ***{{:Foo}} | ||
+ | **[[Baz]] | ||
+ | ***{{:Bar}} | ||
</pre> | </pre> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | The ''expandTree'' method (the ''#tree'' parser-function hook) then converts all the matched rows into the following general structure: | |
+ | 1{uniq}-{id}-{depth}-{item}2{uniq} | ||
+ | Where ''uniq'' is a unique string representing treeview's in general, ''id'' is a unique string representing the current tree, ''depth'' is the depth of the current row with respect to the current tree root, and ''item'' is the wikitext within the current row. The information is "protected" from the parser merely by the fact that it contains no markup characters, but this could be made more robust later. | ||
− | + | After the resulting structure has been parsed, the content portions will have been converted into HTML and the information portions remain unchanged. Below is the HTML source of the above example after parsing and printed at the start of the ''renderTree'' method (the ''ParserAfterTidy'' hook). | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<pre> | <pre> | ||
− | + | 1tv47c8d2444a04d-47c8d2447a3e7-1-<a href="/wiki/index.php?title=Toplevl&action=edit" class="new" title="Toplevl">toplevl</a>2tv47c8d2444a04d | |
− | + | 1tv47c8d2444a04d-47c8d2447a3e7-2-bar2tv47c8d2444a04d | |
+ | 1tv47c8d2444a04d-47c8d2447a3e7-3-baz2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a3e7-1-foo2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a3e7-2-<a href="/Bar" title="Bar">Bar</a>2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a3e7-3-1tv47c8d2444a04d-47c8d244781d7-1-<a href="/Foo" title="Foo">Foo</a>2tv47c8d2444a04d2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d244781d7-2-bar2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d244781d7-3-baz2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d244781d7-1-foo2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d244781d7-2-<a href="/Bar" title="Bar">Bar</a>2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d244781d7-2-<a href="/wiki/index.php?title=Baz&action=edit" class="new" title="Baz">Baz</a>2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a3e7-2-<a href="/wiki/index.php?title=Baz&action=edit" class="new" title="Baz">Baz</a>2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a3e7-3-1tv47c8d2444a04d-47c8d2447a020-1-<a href="/Bar" title="Bar">bar</a>2tv47c8d2444a04d2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a020-2-baz2tv47c8d2444a04d | ||
+ | 1tv47c8d2444a04d-47c8d2447a020-3-biz2tv47c8d2444a04d | ||
</pre> | </pre> | ||
− | |||
− | + | The following match and loop will then loop through all the rows no matter what level and extract the values back out | |
{{code|<php> | {{code|<php> | ||
− | + | if (preg_match_all("/1$uniq-(.+?)-([0-9]+)-({$uniq}3(.+?){$uniq}4)?(.*?)(?=[12]$uniq)/",$text,$matches,PREG_SET_ORDER)) { | |
− | + | foreach ($matches as $n => $row) { | |
− | $ | + | list(,$id,$depth,,$icon,$item) = $row; |
− | $ | ||
− | |||
− | |||
</php>}} | </php>}} | ||
− | + | Here is a bullet list of the ''id'', ''depth'' and ''item'' information from the rows when running the above code over the HTML source shown above: | |
− | + | * 47c8d0860fe80:1,toplevl | |
− | + | * 47c8d0860fe80:2,bar | |
− | + | * 47c8d0860fe80:3,baz | |
− | + | * 47c8d0860fe80:1,foo | |
− | + | * 47c8d0860fe80:2,Bar | |
− | + | * 47c8d0860fe80:3, | |
+ | * 47c8d08609a27:1,Foo | ||
+ | * 47c8d08609a27:2,bar | ||
+ | * 47c8d08609a27:3,baz | ||
+ | * 47c8d08609a27:1,foo | ||
+ | * 47c8d08609a27:2,Bar | ||
+ | * 47c8d08609a27:2,Baz | ||
+ | * 47c8d0860fe80:2,Baz | ||
+ | * 47c8d0860fe80:3, | ||
+ | * 47c8d0860fa81:1,bar | ||
+ | * 47c8d0860fa81:2,baz | ||
+ | * 47c8d0860fa81:3,biz | ||
− | + | The content of the loop then converts the list into JavaScript statements which result in a ''dTree''. The correct recursive depths are calculated by adding the last depth to the current if its ''id'' is a new one. | |
− | |||
== See also == | == See also == | ||
*[http://www.destroydrop.com/javascripts/tree/ dTree] (javascript based) | *[http://www.destroydrop.com/javascripts/tree/ dTree] (javascript based) | ||
*[http://www.silpstream.com/blog/wp-dtree/ Slipstream wp-Dtree] (php based) | *[http://www.silpstream.com/blog/wp-dtree/ Slipstream wp-Dtree] (php based) |
Revision as of 04:09, 1 March 2008
Treeview5.1 is a new approach to the core recursive bullet-list code which doesn't require any voodoo, and should be compatible with all MediaWiki versions including 1.12.
How it works
The initial problem was how to write a parser-function which could generate a tree which could be composed of other transcluded tree's (i.e. parser-functions which are recursively executed forming a larger whole structure together). This seemed impossible since there are no MediaWiki properties available in the environment which reveal the current depth that the parser-function expansion is operating at.
But after thinking about the problem more closely, it turns out that the tree can be constructed without depth information by converting each tree row into a new format containing depth and tree id information which is protected from wiki-parsing. This information can then be converted into tree JavaScript after the parser has finished in the ParserAfterTidy hook.
Here is the wikitext of an example tree which includes two transcluded tree's
*[[toplevl]] **bar ***baz *foo **[[Bar]] ***{{:Foo}} **[[Baz]] ***{{:Bar}}
The expandTree method (the #tree parser-function hook) then converts all the matched rows into the following general structure:
1{uniq}-{id}-{depth}-{item}2{uniq}
Where uniq is a unique string representing treeview's in general, id is a unique string representing the current tree, depth is the depth of the current row with respect to the current tree root, and item is the wikitext within the current row. The information is "protected" from the parser merely by the fact that it contains no markup characters, but this could be made more robust later.
After the resulting structure has been parsed, the content portions will have been converted into HTML and the information portions remain unchanged. Below is the HTML source of the above example after parsing and printed at the start of the renderTree method (the ParserAfterTidy hook).
1tv47c8d2444a04d-47c8d2447a3e7-1-<a href="/wiki/index.php?title=Toplevl&action=edit" class="new" title="Toplevl">toplevl</a>2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-2-bar2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-3-baz2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-1-foo2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-2-<a href="/Bar" title="Bar">Bar</a>2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-3-1tv47c8d2444a04d-47c8d244781d7-1-<a href="/Foo" title="Foo">Foo</a>2tv47c8d2444a04d2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d244781d7-2-bar2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d244781d7-3-baz2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d244781d7-1-foo2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d244781d7-2-<a href="/Bar" title="Bar">Bar</a>2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d244781d7-2-<a href="/wiki/index.php?title=Baz&action=edit" class="new" title="Baz">Baz</a>2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-2-<a href="/wiki/index.php?title=Baz&action=edit" class="new" title="Baz">Baz</a>2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a3e7-3-1tv47c8d2444a04d-47c8d2447a020-1-<a href="/Bar" title="Bar">bar</a>2tv47c8d2444a04d2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a020-2-baz2tv47c8d2444a04d 1tv47c8d2444a04d-47c8d2447a020-3-biz2tv47c8d2444a04d
The following match and loop will then loop through all the rows no matter what level and extract the values back out
Here is a bullet list of the id, depth and item information from the rows when running the above code over the HTML source shown above:
- 47c8d0860fe80:1,toplevl
- 47c8d0860fe80:2,bar
- 47c8d0860fe80:3,baz
- 47c8d0860fe80:1,foo
- 47c8d0860fe80:2,Bar
- 47c8d0860fe80:3,
- 47c8d08609a27:1,Foo
- 47c8d08609a27:2,bar
- 47c8d08609a27:3,baz
- 47c8d08609a27:1,foo
- 47c8d08609a27:2,Bar
- 47c8d08609a27:2,Baz
- 47c8d0860fe80:2,Baz
- 47c8d0860fe80:3,
- 47c8d0860fa81:1,bar
- 47c8d0860fa81:2,baz
- 47c8d0860fa81:3,biz
The content of the loop then converts the list into JavaScript statements which result in a dTree. The correct recursive depths are calculated by adding the last depth to the current if its id is a new one.
See also
- dTree (javascript based)
- Slipstream wp-Dtree (php based)