Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 165 lines (143 sloc) 5.586 kb
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
1 <?php
2 class BookDesignerParser {
3 function __construct($designer, $options) {
4 $this->designer = $designer;
5 $this->options = $options;
6 }
7
8 protected $designer;
9 protected $options;
10 protected $pagestack = array();
11 protected $pagelist = array();
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
12 protected $titlepage = null;
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
13
14 function getPages() {
15 return $this->pagelist;
16 }
17
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
18 function titlePage() {
19 return $this->titlepage;
20 }
21
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
22 function getHeaderTemplateTag() {
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
23 $bookname = $this->titlepage->name();
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
24 return $this->options->UseHeader() ? "{{" . $bookname . "}}" : "";
25 }
26
27 function getFooterTemplateTag() {
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
28 $bookname = $this->titlepage->name();
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
29 return $this->options->UseFooter() ? "{{" . $bookname . "/Footer}}" : "";
30 }
31
32 function getPageLinkWikiText($path, $name) {
33 $template = $this->options->PageLinkTemplate();
34 return str_replace(array('$1', '$2'), array($path, $name), $template);
35 }
36
37 function getSectionHeadWikiText($name) {
38 $template = $this->options->SectionHeaderTemplate();
39 return str_replace('$1', $name, $template);
40 }
41
42 function getPageHeadText($isroot) {
43 $text = $this->getHeaderTemplateTag() . "\n\n";
44 return $text;
45 }
46
47 function getPageFootText($isroot) {
48 $text = "\n\n";
49 return $text . $this->getFooterTemplateTag();
50 }
51
52 function addPageToList($page) {
53 $this->pagelist[] = $page;
54 }
55
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
56 function getHeaderTemplateText() {
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
57 $header = wfMsg('bookdesigner-defaultheader', $this->titlepage->name(),
58 $this->titlepage->fullname());
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
59 return $header;
60 }
61
62 function getFooterTemplateText() {
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
63 $header = wfMsg('bookdesigner-defaultfooter', $this->titlepage->name(),
64 $this->titlepage->fullname());
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
65 return $header;
66 }
67
68 function maybeAddTemplates() {
69 if ($this->options->useHeader()) {
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
70 $head = new BookDesignerPage("Template:" . $this->titlepage->name(),
71 "Template:" . $this->titlepage->name());
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
72 $head->text($this->getHeaderTemplateText());
2b94901 @Whiteknight redo the way we determine whether a page gets created or not.
authored
73 $head->forceCreate(true);
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
74 $this->addPageToList($head);
75 }
76 if ($this->options->useFooter()) {
ab68396 @Whiteknight remove code about chapter link formatting. non-starter. Add messages for...
authored
77 $name = "Template:" . $this->titlepage->name() . "/Footer";
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
78 $foot = new BookDesignerPage($name, $name);
79 $foot->text($this->getFooterTemplateText());
2b94901 @Whiteknight redo the way we determine whether a page gets created or not.
authored
80 $foot->forceCreate(true);
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
81 $this->addPageToList($foot);
82 }
83 }
84
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
85 # push a new page onto the stack, and set it as the current. Return the
86 # object with information about the page
87 function startPage($name, $fullname, $children) {
88 $num = sizeof($this->pagestack);
89 $isroot = true;
90 if ($num > 0)
91 $isroot = false;
92 $page = new BookDesignerPage($name, $fullname);
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
93 if ($this->titlepage == null)
94 $this->titlepage = $page;
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
95 $page->children($children);
96 $page->text($this->getPageHeadText($isroot));
97 array_push($this->pagestack, $page);
98 $this->currentpage = $page;
99 return $page;
100 }
101
102 # pop the last page off the stack, update the current page to be the previous
103 # one, and return the page that was popped
104 function endPage() {
105 $old = array_pop($this->pagestack);
106 $num = sizeof($this->pagestack);
107 $isroot = false;
108 if ($num > 0) {
109 $lastpage = $this->pagestack[sizeof($this->pagestack) - 1];
110 $this->currentpage = $lastpage;
111 }
112 else {
113 $isroot = true;
114 $this->currentpage = null;
115 }
116 $old->addText($this->getPageFootText($isroot));
1904f19 @Whiteknight fix some bugs with the new architecture
authored
117
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
118 return $old;
119 }
120
121
122 # Home-brewed XML parser. I'm not familiar with any PHP XML libraries or
123 # utilities, and I don't know what features the MediaWiki server is going
124 # to have available anyway, so I'm just writing my own quickly.
125 # TODO: Reimplement 'NumberPages' option, to prepend a page number to each page.
126 function parse($text) {
127 global $wgOut, $wgScriptPath;
128 $matches = array();
129 $lines = explode("\n", $text);
130
131 $bookline = $lines[0];
132 if (!preg_match("/<page name='([^']+)' children='(\d+)'>/", $bookline, $matches)) {
133 $wgOut->addHTML("XML ERROR");
134 return;
135 }
136
137 # dummy, just to get the loop started
1904f19 @Whiteknight fix some bugs with the new architecture
authored
138 $this->currentpage = new BookDesignerPage("", "");
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
139
140 for ($i = 0; $i < sizeof($lines); $i++) {
141 $line = $lines[$i];
142 if (preg_match("/<page name='([^']+)' children='(\d+)'>/", $line, $matches)) {
143 $name = $matches[1];
144 if ($i == 0)
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
145 $fullname = $this->options->bookNamespace() . $name;
1904f19 @Whiteknight fix some bugs with the new architecture
authored
146 else
147 $fullname = $this->currentpage->fullname() . "/" . $name;
148 $this->currentpage->addText($this->getPageLinkWikiText($fullname, $name) . "\n");
149 $this->startPage($name, $fullname, $matches[2]);
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
150 }
151 else if (preg_match("/<heading name='([^']+)' children='(\d+)'>/", $line, $matches)) {
152 $name = $matches[1];
1904f19 @Whiteknight fix some bugs with the new architecture
authored
153 $this->currentpage->addText($this->getSectionHeadWikiText($name) . "\n\n");
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
154 }
155 else if($line == "</page>") {
156 $page = $this->endPage();
157 $this->addPageToList($page);
158 }
159 else if($line == "</heading>") {
160 }
161 }
8e5f3fb @Whiteknight Add header and footer templates again. The default text of which is spec...
authored
162 $this->maybeAddTemplates();
bd33ef7 @Whiteknight begin another refactor, break the code inot smaller classes and files
authored
163 }
8d22c5b @Whiteknight implement basic namespacing. The parser now saves the first page as the ...
authored
164 }
Something went wrong with that request. Please try again.