Index: trunk/extensions/PatchOutputMobile/PatchOutputMobile.php |
— | — | @@ -1,247 +1,261 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | # Needs to be called within MediaWiki; not standalone |
5 | | -if ( !defined('MEDIAWIKI') ) { |
6 | | - echo("This is an extension to the MediaWiki package and cannot be run standalone.\n" ); |
7 | | - die(-1); |
| 5 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 6 | + echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); |
| 7 | + die( -1 ); |
8 | 8 | } |
9 | 9 | |
10 | 10 | # Define the extension; allows us make sure the extension is used correctly |
11 | | -DEFINE( 'PATCHOUTPUTMOBILE', 'PatchOutputMobile' ); |
| 11 | +define( 'PATCHOUTPUTMOBILE', 'PatchOutputMobile' ); |
12 | 12 | |
| 13 | +// Extension credits that will show up on Special:Version |
13 | 14 | $wgExtensionCredits['other'][] = array( |
14 | | - 'name' => 'PatchOutputMobile', |
15 | | - 'version' => ExtPatchOutputMobile::VERSION, |
16 | | - 'author' => '[http://www.mediawiki.org/wiki/User:Preilly Preilly]', |
17 | | - 'url' => 'http://www.mediawiki.org/wiki/Extension:PatchOutputMobile', |
18 | | - 'description' => 'patch html output for mobile' |
| 15 | + 'name' => 'PatchOutputMobile', |
| 16 | + 'version' => ExtPatchOutputMobile::VERSION, |
| 17 | + 'author' => '[http://www.mediawiki.org/wiki/User:Preilly Preilly]', |
| 18 | + 'description' => 'Patch HTML output for mobile', |
| 19 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:PatchOutputMobile', |
19 | 20 | ); |
20 | | - |
| 21 | + |
21 | 22 | $wgExtPatchOutputMobile = new ExtPatchOutputMobile(); |
22 | | - |
23 | | -$wgHooks['OutputPageBeforeHTML'][] = array(&$wgExtPatchOutputMobile, |
24 | | - 'onOutputPageBeforeHTML'); |
25 | | - |
| 23 | + |
| 24 | +$wgHooks['OutputPageBeforeHTML'][] = array( &$wgExtPatchOutputMobile, |
| 25 | + 'onOutputPageBeforeHTML' ); |
| 26 | + |
26 | 27 | class ExtPatchOutputMobile { |
27 | | - const VERSION = '0.2'; |
| 28 | + const VERSION = '0.2.1'; |
28 | 29 | |
29 | 30 | private $doc; |
30 | 31 | |
31 | | - public $items_to_remove = array("#contentSub", #redirection notice |
32 | | - "div.messagebox", #cleanup data |
33 | | - "#siteNotice", #site notice |
34 | | - "#siteSub", #"From Wikipedia..." |
35 | | - "#jump-to-nav", #jump-to-nav |
36 | | - "div.editsection", #edit blocks |
37 | | - "div.infobox", # Infoboxes in the article |
38 | | - "table.toc", #table of contents |
39 | | - "#catlinks", #category links |
40 | | - "div.stub", #stub warnings |
41 | | - "table.metadata", #ugly metadata |
42 | | - "form", |
43 | | - "div.sister-project", |
44 | | - "script", |
45 | | - "div.magnify", #stupid magnify thing |
46 | | - ".editsection", |
47 | | - "span.t", |
48 | | - 'sup[style*="help"]', |
49 | | - ".portal", |
50 | | - "#protected-icon", |
51 | | - ".printfooter", |
52 | | - ".boilerplate", |
53 | | - "#id-articulo-destacado", |
54 | | - "#coordinates", |
55 | | - "#top", |
56 | | - ".hiddenStructure", |
57 | | - ".noprint", |
58 | | - ".medialist", |
59 | | - ".mw-search-createlink"); |
60 | | - |
61 | | - public function onOutputPageBeforeHTML(&$out, &$text) { |
62 | | - ob_start(array(&$this, 'parse')); |
| 32 | + public $items_to_remove = array( |
| 33 | + '#contentSub', # redirection notice |
| 34 | + 'div.messagebox', # cleanup data |
| 35 | + '#siteNotice', # site notice |
| 36 | + '#siteSub', # "From Wikipedia..." |
| 37 | + '#jump-to-nav', # jump-to-nav |
| 38 | + 'div.editsection', # edit blocks |
| 39 | + 'div.infobox', # Infoboxes in the article |
| 40 | + 'table.toc', # table of contents |
| 41 | + '#catlinks', # category links |
| 42 | + 'div.stub', # stub warnings |
| 43 | + 'table.metadata', # ugly metadata |
| 44 | + 'form', |
| 45 | + 'div.sister-project', |
| 46 | + 'script', |
| 47 | + 'div.magnify', # stupid magnify thing |
| 48 | + '.editsection', |
| 49 | + 'span.t', |
| 50 | + 'sup[style*="help"]', |
| 51 | + '.portal', |
| 52 | + '#protected-icon', |
| 53 | + '.printfooter', |
| 54 | + '.boilerplate', |
| 55 | + '#id-articulo-destacado', |
| 56 | + '#coordinates', |
| 57 | + '#top', |
| 58 | + '.hiddenStructure', |
| 59 | + '.noprint', |
| 60 | + '.medialist', |
| 61 | + '.mw-search-createlink' |
| 62 | + ); |
| 63 | + |
| 64 | + public function onOutputPageBeforeHTML( &$out, &$text ) { |
| 65 | + ob_start( array( &$this, 'parse' ) ); |
63 | 66 | return true; |
64 | 67 | } |
65 | | - |
66 | | - private function _show_hide_callback($matches) { |
| 68 | + |
| 69 | + private function _show_hide_callback( $matches ) { |
67 | 70 | static $headings = 0; |
68 | | - $show = "Show"; |
69 | | - $hide = "Hide"; |
70 | | - $back_to_top = "Jump Back A Section"; |
| 71 | + $show = 'Show'; |
| 72 | + $hide = 'Hide'; |
| 73 | + $back_to_top = 'Jump Back A Section'; |
71 | 74 | ++$headings; |
72 | 75 | // Back to top link |
73 | | - $base = "<div class='section_anchors' id='anchor_" . intval($headings - 1) . "'><a href='#section_" . intval($headings - 1) . "' class='back_to_top'>↑ {$back_to_top}</a></div>"; |
| 76 | + $base = "<div class='section_anchors' id='anchor_" . intval( $headings - 1 ) . |
| 77 | + "'><a href='#section_" . intval( $headings - 1 ) . |
| 78 | + "' class='back_to_top'>↑ {$back_to_top}</a></div>"; |
74 | 79 | // generate the HTML we are going to inject |
75 | 80 | $buttons = "<button class='section_heading show' section_id='{$headings}'>{$show}</button><button class='section_heading hide' section_id='{$headings}'>{$hide}</button>"; |
76 | 81 | $base .= "<h2 class='section_heading' id='section_{$headings}'{$matches[1]}{$buttons} <span>{$matches[2]}</span></h2><div class='content_block' id='content_{$headings}'>"; |
77 | 82 | |
78 | | - if ($headings > 1) { |
| 83 | + if ( $headings > 1 ) { |
79 | 84 | // Close it up here |
80 | | - $base = "</div>" . $base; |
| 85 | + $base = '</div>' . $base; |
81 | 86 | } |
82 | | - |
| 87 | + |
83 | 88 | $GLOBALS['headings'] = $headings; |
84 | 89 | |
85 | | - return $base; |
| 90 | + return $base; |
86 | 91 | } |
87 | | - |
88 | | - public function javascriptize($s) { |
89 | | - //Closures are a PHP 5.3 feature. MediaWiki currently requires PHP 5.2.3 or higher. So, using old style for now. |
90 | | - $s = preg_replace_callback( '/<h2(.*)<span class="mw-headline" [^>]*>(.+)<\/span>\w*<\/h2>/', array(&$this, '_show_hide_callback'), $s ); |
91 | | - |
| 92 | + |
| 93 | + public function javascriptize( $s ) { |
| 94 | + // Closures are a PHP 5.3 feature. |
| 95 | + // MediaWiki currently requires PHP 5.2.3 or higher. |
| 96 | + // So, using old style for now. |
| 97 | + $s = preg_replace_callback( |
| 98 | + '/<h2(.*)<span class="mw-headline" [^>]*>(.+)<\/span>\w*<\/h2>/', |
| 99 | + array( &$this, '_show_hide_callback' ), |
| 100 | + $s |
| 101 | + ); |
| 102 | + |
92 | 103 | // if we had any, make sure to close the whole thing! |
93 | | - if (isset($GLOBALS['headings']) && $GLOBALS['headings'] > 0) { |
94 | | - $s = str_replace('<div class="visualClear">', '</div><div class="visualClear">', $s); |
| 104 | + if ( isset( $GLOBALS['headings'] ) && $GLOBALS['headings'] > 0 ) { |
| 105 | + $s = str_replace( |
| 106 | + '<div class="visualClear">', |
| 107 | + '</div><div class="visualClear">', |
| 108 | + $s |
| 109 | + ); |
95 | 110 | } |
96 | 111 | |
97 | 112 | return $s; |
98 | 113 | } |
99 | | - |
100 | | - public function parse($s) { |
101 | | - // foreach(self::$mTable as $from => $to) { |
102 | | - // $s =& str_replace( $from, $to, $s ); |
| 114 | + |
| 115 | + public function parse( $s ) { |
| 116 | + // foreach( self::$mTable as $from => $to ) { |
| 117 | + // $s =& str_replace( $from, $to, $s ); |
103 | 118 | // } |
104 | | - |
105 | | - return $this->DOMParse($s); |
| 119 | + |
| 120 | + return $this->DOMParse( $s ); |
106 | 121 | } |
107 | | - |
| 122 | + |
108 | 123 | private function parse_items_to_remove() { |
109 | 124 | $item_to_remove_records = array(); |
110 | | - |
111 | | - foreach ($this->items_to_remove as $item_to_remove) { |
| 125 | + |
| 126 | + foreach ( $this->items_to_remove as $item_to_remove ) { |
112 | 127 | $type = ''; |
113 | 128 | $raw_name = ''; |
114 | | - CSS_detection::detect_id_css_or_tag($item_to_remove, $type, $raw_name); |
| 129 | + CSS_detection::detect_id_css_or_tag( $item_to_remove, $type, $raw_name ); |
115 | 130 | $item_to_remove_records[$type][] = $raw_name; |
116 | 131 | } |
117 | | - |
| 132 | + |
118 | 133 | return $item_to_remove_records; |
119 | 134 | } |
120 | | - |
121 | | - public function DOMParse($html) { |
122 | | - |
123 | | - libxml_use_internal_errors(true); |
124 | | - $this->doc = DOMDocument::loadHTML($html); |
125 | | - libxml_use_internal_errors(false); |
| 135 | + |
| 136 | + public function DOMParse( $html ) { |
| 137 | + libxml_use_internal_errors( true ); |
| 138 | + $this->doc = DOMDocument::loadHTML( $html ); |
| 139 | + libxml_use_internal_errors( false ); |
126 | 140 | $this->doc->preserveWhiteSpace = false; |
127 | 141 | $this->doc->strictErrorChecking = false; |
128 | | - |
| 142 | + |
129 | 143 | $item_to_remove_records = $this->parse_items_to_remove(); |
130 | | - |
131 | | - //Tags |
132 | | - |
133 | | - //You can't remove DOMNodes from a DOMNodeList as you're iterating over them |
134 | | - // in a foreach loop. It will seemingly leave the internal iterator on the foreach out of wack |
135 | | - // and results will be quite strange. Though, making a queue of items to remove |
136 | | - // seems to work. For example: |
137 | | - |
138 | | - $title_node = $this->doc->getElementsByTagName('title'); |
139 | | - |
140 | | - if ($title_node->length > 0) { |
141 | | - $title = $title_node->item(0)->nodeValue; |
| 144 | + |
| 145 | + // Tags |
| 146 | + |
| 147 | + // You can't remove DOMNodes from a DOMNodeList as you're iterating |
| 148 | + // over them in a foreach loop. It will seemingly leave the internal |
| 149 | + // iterator on the foreach out of wack and results will be quite |
| 150 | + // strange. Though, making a queue of items to remove seems to work. |
| 151 | + // For example: |
| 152 | + $title_node = $this->doc->getElementsByTagName( 'title' ); |
| 153 | + |
| 154 | + if ( $title_node->length > 0 ) { |
| 155 | + $title = $title_node->item( 0 )->nodeValue; |
142 | 156 | } |
143 | | - |
| 157 | + |
144 | 158 | $domElemsToRemove = array(); |
145 | | - foreach ($item_to_remove_records['TAG'] as $tag_to_remove) { |
146 | | - $tag_to_remove_nodes = $this->doc->getElementsByTagName($tag_to_remove); |
147 | | - |
148 | | - foreach($tag_to_remove_nodes as $tag_to_remove_node) { |
149 | | - if ($tag_to_remove_node) { |
| 159 | + foreach ( $item_to_remove_records['TAG'] as $tag_to_remove ) { |
| 160 | + $tag_to_remove_nodes = $this->doc->getElementsByTagName( $tag_to_remove ); |
| 161 | + |
| 162 | + foreach( $tag_to_remove_nodes as $tag_to_remove_node ) { |
| 163 | + if ( $tag_to_remove_node ) { |
150 | 164 | $domElemsToRemove[] = $tag_to_remove_node; |
151 | 165 | } |
152 | 166 | } |
153 | 167 | } |
154 | | - |
155 | | - foreach($domElemsToRemove as $domElement){ |
156 | | - $domElement->parentNode->removeChild($domElement); |
| 168 | + |
| 169 | + foreach( $domElemsToRemove as $domElement ) { |
| 170 | + $domElement->parentNode->removeChild( $domElement ); |
157 | 171 | } |
158 | | - |
159 | | - //Elements with named Ids |
160 | | - foreach ($item_to_remove_records['ID'] as $item_to_remove) { |
161 | | - $item_to_remove_node = $this->doc->getElementById($item_to_remove); |
162 | | - if ($item_to_remove_node) { |
163 | | - $removed_item_to_remove = $item_to_remove_node->parentNode->removeChild($item_to_remove_node); |
| 172 | + |
| 173 | + // Elements with named IDs |
| 174 | + foreach ( $item_to_remove_records['ID'] as $item_to_remove ) { |
| 175 | + $item_to_remove_node = $this->doc->getElementById( $item_to_remove ); |
| 176 | + if ( $item_to_remove_node ) { |
| 177 | + $removed_item_to_remove = $item_to_remove_node->parentNode->removeChild( $item_to_remove_node ); |
164 | 178 | } |
165 | 179 | } |
166 | | - |
| 180 | + |
167 | 181 | // CSS Classes |
168 | | - $xpath = new DOMXpath($this->doc); |
169 | | - foreach ($item_to_remove_records['CLASS'] as $class_to_remove) { |
170 | | - |
171 | | - $elements = $xpath->query('//*[@class="'.$class_to_remove.'"]'); |
172 | | - |
173 | | - foreach($elements as $element) { |
174 | | - $removed_element = $element->parentNode->removeChild($element); |
| 182 | + $xpath = new DOMXpath( $this->doc ); |
| 183 | + foreach ( $item_to_remove_records['CLASS'] as $class_to_remove ) { |
| 184 | + $elements = $xpath->query( '//*[@class="' . $class_to_remove . '"]' ); |
| 185 | + |
| 186 | + foreach( $elements as $element ) { |
| 187 | + $removed_element = $element->parentNode->removeChild( $element ); |
175 | 188 | } |
176 | 189 | } |
177 | | - |
178 | | - //Tags with CSS Classes |
179 | | - foreach ($item_to_remove_records['TAG_CLASS'] as $class_to_remove) { |
180 | | - |
181 | | - $parts = explode(".", $class_to_remove); |
182 | | - |
183 | | - $elements = $xpath->query('//'.$parts[0].'[@class="'.$parts[1].'"]'); |
184 | | - |
185 | | - foreach($elements as $element) { |
186 | | - $removed_element = $element->parentNode->removeChild($element); |
| 190 | + |
| 191 | + // Tags with CSS Classes |
| 192 | + foreach ( $item_to_remove_records['TAG_CLASS'] as $class_to_remove ) { |
| 193 | + $parts = explode( '.', $class_to_remove ); |
| 194 | + |
| 195 | + $elements = $xpath->query( |
| 196 | + '//' . $parts[0] . '[@class="' . $parts[1] . '"]' |
| 197 | + ); |
| 198 | + |
| 199 | + foreach( $elements as $element ) { |
| 200 | + $removed_element = $element->parentNode->removeChild( $element ); |
187 | 201 | } |
188 | 202 | } |
189 | | - |
190 | | - $content = $this->doc->getElementById('content'); |
191 | | - |
192 | | - $content_html = $this->doc->saveXML($content, LIBXML_NOEMPTYTAG); |
193 | | - |
194 | | - if (empty($title)) { |
| 203 | + |
| 204 | + $content = $this->doc->getElementById( 'content' ); |
| 205 | + |
| 206 | + $content_html = $this->doc->saveXML( $content, LIBXML_NOEMPTYTAG ); |
| 207 | + |
| 208 | + if ( empty( $title ) ) { |
195 | 209 | $title = 'Wikipedia'; |
196 | 210 | } |
197 | | - |
198 | | - require('views/notices/_donate.html.php'); |
199 | | - require('views/layout/_search_webkit.html.php'); |
200 | | - require('views/layout/_footmenu_default.html.php'); |
201 | | - require('views/layout/application.html.php'); |
202 | | - |
203 | | - return (strlen($content_html) > 4000) ? $this->javascriptize($application_html) : $application_html; //$content_html; |
| 211 | + |
| 212 | + require( 'views/notices/_donate.html.php' ); |
| 213 | + require( 'views/layout/_search_webkit.html.php' ); |
| 214 | + require( 'views/layout/_footmenu_default.html.php' ); |
| 215 | + require( 'views/layout/application.html.php' ); |
| 216 | + |
| 217 | + return ( strlen( $content_html ) > 4000 ) ? $this->javascriptize( $application_html ) : $application_html; //$content_html; |
204 | 218 | } |
205 | 219 | } |
206 | 220 | |
207 | 221 | class CSS_detection { |
208 | 222 | |
209 | | - public static function detect_id_css_or_tag($snippet, &$type, &$raw_name) { |
| 223 | + public static function detect_id_css_or_tag( $snippet, &$type, &$raw_name ) { |
210 | 224 | $output = ''; |
211 | | - |
212 | | - if (strpos($snippet, '.') === 0) { |
| 225 | + |
| 226 | + if ( strpos( $snippet, '.' ) === 0 ) { |
213 | 227 | $output = 'Class found: '; |
214 | 228 | $type = 'CLASS'; |
215 | | - $raw_name = substr($snippet, 1); |
| 229 | + $raw_name = substr( $snippet, 1 ); |
216 | 230 | } |
217 | | - |
218 | | - if (strpos($snippet, '#') === 0) { |
| 231 | + |
| 232 | + if ( strpos( $snippet, '#' ) === 0 ) { |
219 | 233 | $output = 'ID found: '; |
220 | 234 | $type = 'ID'; |
221 | | - $raw_name = substr($snippet, 1); |
| 235 | + $raw_name = substr( $snippet, 1 ); |
222 | 236 | } |
223 | | - |
224 | | - if (strpos($snippet, '.') !== 0 && |
225 | | - strpos($snippet, '.') !== false) { |
| 237 | + |
| 238 | + if ( strpos( $snippet, '.' ) !== 0 && |
| 239 | + strpos( $snippet, '.' ) !== false ) { |
226 | 240 | $output = 'Tag with Class found: '; |
227 | 241 | $type = 'TAG_CLASS'; |
228 | 242 | $raw_name = $snippet; |
229 | 243 | } |
230 | | - |
231 | | - if (strpos($snippet, '.') === false && |
232 | | - strpos($snippet, '#') === false && |
233 | | - strpos($snippet, '[') === false && |
234 | | - strpos($snippet, ']') === false) { |
| 244 | + |
| 245 | + if ( strpos( $snippet, '.' ) === false && |
| 246 | + strpos( $snippet, '#' ) === false && |
| 247 | + strpos( $snippet, '[' ) === false && |
| 248 | + strpos( $snippet, ']' ) === false ) { |
235 | 249 | $output = 'Tag found: '; |
236 | 250 | $type = 'TAG'; |
237 | 251 | $raw_name = $snippet; |
238 | 252 | } |
239 | | - |
240 | | - if (empty($output)) { |
| 253 | + |
| 254 | + if ( empty( $output ) ) { |
241 | 255 | $output = 'Unknown HTML snippet found: '; |
242 | 256 | $type = 'UNKNOWN'; |
243 | 257 | $raw_name = $snippet; |
244 | 258 | } |
245 | | - |
| 259 | + |
246 | 260 | return $output; |
247 | 261 | } |
248 | 262 | } |
\ No newline at end of file |