Skip to content

Commit 10c4df1

Browse files
committed
Working wikitext->raw parser
1 parent f4cff6b commit 10c4df1

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

BricksetSnippet.api.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@ public function execute() {
66
$id = $this->getMain()->getVal('id');
77
$name = $this->getMain()->getVal('name');
88

9-
$result = array(
10-
'url' => '',
11-
'snippet' => '',
12-
);
9+
$title = BricksetSnippet::getTitle( $id, $name );
10+
11+
$title = Title::newFromText( '7965 Millennium Falcon' );
12+
13+
if ( $title ) {
14+
15+
$page = new WikiPage( $title );
16+
$content = $page->getContent()->getNativeData();
17+
18+
$snippet = BricksetSnippet::parseText( $content );
1319

14-
$this->getResult()->addValue( null, $this->getModuleName(), $result );
20+
$result = array(
21+
'url' => $title->getFullURL(),
22+
'snippet' => $snippet,
23+
);
24+
25+
$this->getResult()->addValue( null, $this->getModuleName(), $result );
26+
}
1527

1628
return true;
1729
}
@@ -21,7 +33,7 @@ public function getDescription() {
2133
}
2234

2335
public function getAllowedParams() {
24-
return array_merge( parent::getAllowedParams(), array(
36+
return array(
2537
'id' => array (
2638
ApiBase::PARAM_TYPE => 'string',
2739
ApiBase::PARAM_REQUIRED => true
@@ -30,14 +42,14 @@ public function getAllowedParams() {
3042
ApiBase::PARAM_TYPE => 'string',
3143
ApiBase::PARAM_REQUIRED => true
3244
),
33-
) );
45+
);
3446
}
3547

3648
public function getParamDescription() {
37-
return array_merge( parent::getParamDescription(), array(
49+
return array(
3850
'id' => 'The ID of the set to return',
3951
'name' => 'The name of the set to return'
40-
) );
52+
);
4153
}
4254

4355
public function getExamples() {

BricksetSnippet.body.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Class for static functions
5+
*/
6+
class BricksetSnippet {
7+
8+
/**
9+
* Parse wikitext into raw paragraph
10+
*
11+
* @param String $text: text to proccess
12+
* @return String: processed text
13+
*/
14+
static function parseText( $text ) {
15+
$parts = explode( '==', $text );
16+
$text = $parts[0]; // remove anything after the first heading
17+
18+
$text = preg_replace( "/'''/", "", $text ); // remove bold
19+
$text = preg_replace( "/''/", "", $text ); // remove italics
20+
21+
$text = preg_replace( '/\[\[([^|]+?)\]\]/', '$1', $text ); // remove non-piped links
22+
$text = preg_replace( '/\[\[[^|\n]+?\|([^|\n]+?)\]\]/', '$1', $text ); // remove piped links
23+
24+
$text = preg_replace( '/<.+?>/', '', $text ); // remove html elements
25+
26+
$text = preg_replace( '/{{[^{]+?}}/', '', $text ); // remove 3rd layer templates
27+
$text = preg_replace( '/{{[^{]+?}}/', '', $text ); // remove 2nd layer templates
28+
$text = preg_replace( '/{{[^{]+?}}/', '', $text ); // remove 1st layer templates
29+
30+
$text = trim( $text );
31+
32+
return $text;
33+
}
34+
35+
/**
36+
* Get the title object for the given search data, or false if none found
37+
*
38+
* @param String $id: id of article to find
39+
* @param String $name: name of article to find
40+
* @return Title|boolean: title if found OR false if none found
41+
*/
42+
static function getTitle( $id, $name ) {
43+
44+
}
45+
}

BricksetSnippet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
);
1111

1212
$wgAutoloadClasses['BricksetSnippetAPI'] = __DIR__ . '/BricksetSnippet.api.php';
13+
$wgAutoloadClasses['BricksetSnippet'] = __DIR__ . '/BricksetSnippet.body.php';
1314

1415
$wgAPIModules['bricksetsnippet'] = 'BricksetSnippetAPI';
1516

16-
$wgExtensionMessagesFiles['myextension'] = __DIR__ . '/BricksetSnippet.i18n.php';
17+
$wgExtensionMessagesFiles['BricksetSnippet'] = __DIR__ . '/BricksetSnippet.i18n.php';

0 commit comments

Comments
 (0)