r90196 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90195‎ | r90196 | r90197 >
Date:08:45, 16 June 2011
Author:ning
Status:deferred
Tags:
Comment:
Wiki Object Model release v1.0
Modified paths:
  • /trunk/extensions/WikiObjectModel/includes/WOM_Processor.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/WOM_Setup.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/apis/WOM_GetObjectModel.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/apis/WOM_SetObjectModel.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOMFactory.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Category.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_HTMLTag.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Link.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_ListItem.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_MagicWord.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Page.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_ParamValue.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Parameter.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_ParserFunction.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Property.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Section.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Sentence.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Table.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_TblCell.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Template.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Text.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/models/WOM_OM_TmplField.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMHTMLTagParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMLinkParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMListItemParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMMagicWordParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMParamValueParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMParameterParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMSectionParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WOMTblCellParser.php (modified) (history)
  • /trunk/extensions/WikiObjectModel/includes/parsers/WikiObjectModelParser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMListItemParser.php
@@ -16,9 +16,9 @@
1717 public function parseNext( $text, WikiObjectModelCollection $parentObj, $offset = 0 ) {
1818 $lastLF = ( $offset == 0 || $text { $offset - 1 } == "\n" );
1919 $text = substr( $text, $offset );
20 - if ( $lastLF ) {
21 - $r = preg_match( '/^([\*#]+)/', $text, $m );
22 - }
 20+ if ( !$lastLF ) return null;
 21+
 22+ $r = preg_match( '/^([\*#]+)/', $text, $m );
2323 if ( $r ) {
2424 $len = strlen( $m[0] );
2525 $obj = new WOMListItemModel( $m[1] );
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMMagicWordParser.php
@@ -13,8 +13,45 @@
1414 $this->m_parserId = WOM_PARSER_ID_MAGICWORD;
1515 }
1616
 17+ static $underscores = array( '0' => '', '1' => '' );
 18+ static $mwa = null;
 19+ static function getDoubleUnderscoreRegex() {
 20+ if ( WOMMagicWordParser::$mwa === null ) {
 21+ WOMMagicWordParser::$mwa = MagicWord::getDoubleUnderscoreArray();
 22+ foreach ( WOMMagicWordParser::$mwa->names as $name ) {
 23+ $magic = MagicWord::get( $name );
 24+ $case = intval( $magic->isCaseSensitive() );
 25+ foreach ( $magic->getSynonyms() as $i => $syn ) {
 26+ $group = '(' . preg_quote( $syn, '/' ) . ')';
 27+ if ( WOMMagicWordParser::$underscores[$case] === '' ) {
 28+ WOMMagicWordParser::$underscores[$case] = $group;
 29+ } else {
 30+ WOMMagicWordParser::$underscores[$case] .= '|' . $group;
 31+ }
 32+ }
 33+ }
 34+ if ( WOMMagicWordParser::$underscores[0] !== '' ) {
 35+ WOMMagicWordParser::$underscores[0] = "/^(" . WOMMagicWordParser::$underscores[0] . ")/i";
 36+ }
 37+ if ( WOMMagicWordParser::$underscores[1] !== '' ) {
 38+ WOMMagicWordParser::$underscores[1] = "/^(" . WOMMagicWordParser::$underscores[1] . ")/";
 39+ }
 40+ }
 41+ return WOMMagicWordParser::$underscores;
 42+ }
1743 public function parseNext( $text, WikiObjectModelCollection $parentObj, $offset = 0 ) {
1844 $text = substr( $text, $offset );
 45+
 46+ $regex = WOMMagicWordParser::getDoubleUnderscoreRegex();
 47+ foreach ( $regex as $reg ) {
 48+ if ( $reg === '' ) continue;
 49+ $r = preg_match( $reg, $text, $m );
 50+ if ( $r ) {
 51+ $len = strlen( $m[0] );
 52+ $magicword = trim( $m[0] );
 53+ return array( 'len' => $len, 'obj' => new WOMMagicWordModel( $magicword, true ) );
 54+ }
 55+ }
1956 $r = preg_match( '/^\{\{([^{|}]+)\}\}/', $text, $m );
2057
2158 if ( $r ) {
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMLinkParser.php
@@ -20,12 +20,12 @@
2121 if ( $r && !preg_match( '/^(?:' . wfUrlProtocols() . ')/', $m[1] ) ) {
2222 return array( 'len' => strlen( $m[0] ), 'obj' => new WOMLinkModel( $m[1], isset( $m[3] ) ? $m[3] : '' ) );
2323 }
24 - $r = preg_match( '/^\[((?:' . wfUrlProtocols() . ')[^ \[\]]+)(\s+([^\]]+))?\]/', $text, $m );
 24+ $r = preg_match( '/^\[((?:' . wfUrlProtocols() . ')[^ \]]+)(\s+([^\]]+))?\]/', $text, $m );
2525 if ( $r ) {
2626 return array( 'len' => strlen( $m[0] ), 'obj' => new WOMLinkModel( $m[1], isset( $m[3] ) ? $m[3] : '' ) );
2727 }
2828 // includes/Parser.php Parser->doMagicLinks
29 - $r = preg_match( '/^(?:' . wfUrlProtocols() . ')[^][<>"\\x00-\\x20\\x7F]+/', $text, $m );
 29+ $r = preg_match( '/^(?:' . wfUrlProtocols() . ')[^][<>"|\\x00-\\x20\\x7F]+/', $text, $m );
3030 if ( $r ) {
3131 return array( 'len' => strlen( $m[0] ), 'obj' => new WOMLinkModel( $m[0] ) );
3232 }
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMParamValueParser.php
@@ -17,7 +17,7 @@
1818 if ( !( $parentObj instanceof WOMParameterModel ) )
1919 return null;
2020
21 - return array( 'len' => $len, 'obj' => new WOMParamValueModel() );
 21+ return array( 'len' => 0, 'obj' => new WOMParamValueModel() );
2222 }
2323
2424 public function isObjectClosed( $obj, $text, $offset ) {
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMHTMLTagParser.php
@@ -24,7 +24,7 @@
2525 $closed = false;
2626 if ( isset( $m[2] ) ) {
2727 $attr = $m[2];
28 - $closed = ( $attr { strlen( $attr ) - 1 } == '/' );
 28+ $closed = ( $attr != '' && $attr { strlen( $attr ) - 1 } == '/' );
2929 if ( $closed ) $attr = substr( $attr, 0, strlen( $attr ) - 1 );
3030 while ( preg_match( '/^\s*([\w]+)\s*=\s*/', $attr, $m1 ) ) {
3131 $attr = substr( $attr, strlen( $m1[0] ) );
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMParameterParser.php
@@ -37,11 +37,11 @@
3838 return array( 'len' => $len, 'obj' => new WOMParameterModel( $key ) );
3939 }
4040 }
41 -
 41+
4242 public function getSubParserID() {
4343 return WOM_PARSER_ID_PARAM_VALUE;
4444 }
45 -
 45+
4646 public function isObjectClosed( $obj, $text, $offset ) {
4747 if ( !( ( $obj instanceof WOMTemplateFieldModel )
4848 || ( $obj instanceof WOMParameterModel ) ) )
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMTblCellParser.php
@@ -13,39 +13,65 @@
1414 $this->m_parserId = WOM_PARSER_ID_TABLECELL;
1515 }
1616
 17+ private function getFirstLineChar( $text, $offset ) {
 18+ for ( $i = $offset; $i >= 0; --$i ) {
 19+ if ( $text { $i } == "\n" ) {
 20+ $s = substr( $text, $i );
 21+ if ( preg_match( '/^\s*([!|])/', $s, $m ) ) {
 22+ return $m[1];
 23+ }
 24+ break;
 25+ }
 26+ }
 27+
 28+ return '';
 29+ }
1730 // FIXME: what if table cell style uses parser function which contains 'return' or '|'
1831 public function parseNext( $text, WikiObjectModelCollection $parentObj, $offset = 0 ) {
1932 if ( !( $parentObj instanceof WOMTableModel ) ) return null;
2033
2134 $lastLF = ( $text { $offset } == "\n" || ( $offset == 0 || $text { $offset - 1 } == "\n" ) );
 35+ // get the first char of current line
 36+ $fch = $this->getFirstLineChar( $text, $offset );
 37+ if ( $fch == '' ) return null;
 38+
2239 $text = substr( $text, $offset );
23 -
24 - $r = preg_match( '/^(!!|\|\||(\s*(\|\+|\|-|[!|])))/', $text, $m );
 40+
 41+ $r = preg_match( '/^(' . ( ( $fch == '!' ) ? '!!|' : '' ) . '\|\||(\s*(\|\+|\|-|[!|])))/', $text, $m );
2542 if ( !$r ) return null;
2643
2744 if ( isset( $m[2] ) && !$lastLF ) return null;
28 -
 45+
2946 $len = strlen( $m[0] );
3047 $text = substr( $text, $len );
31 - $r = preg_match( '/^[^\n|]*\|/', $text, $m1 );
32 -
33 - if ( $r && preg_match( '/\{\{/', $m1[0] ) ) {
 48+ $r = preg_match( '/^([^\n|]*\|)[^|]/', $text, $m1 );
 49+ if ( !$r || preg_match( '/\{\{/', $m1[1] ) ) {
3450 // FIXME: what if matched style contains '{{', just think it is table body
35 - return array( 'len' => strlen( $m[0] ), 'obj' => new WOMTableCellModel( $m[0] ) );
 51+ return array( 'len' => $len, 'obj' => new WOMTableCellModel( $m[0] ) );
3652 }
3753
38 - return array( 'len' => strlen( $m[0] ) + strlen( $m1[0] ), 'obj' => new WOMTableCellModel( $m[0] . $m1[0] ) );
 54+ if ( $fch == '!' ) {
 55+ $pos = strpos( $text, '!!', $len );
 56+ if ( $pos !== false && $pos < strlen( $m1[1] ) ) {
 57+ return array( 'len' => $len, 'obj' => new WOMTableCellModel( $m[0] ) );
 58+ }
 59+ }
 60+
 61+ return array( 'len' => $len + strlen( $m1[1] ), 'obj' => new WOMTableCellModel( $m[0] . $m1[1] ) );
3962 }
4063
4164 public function isObjectClosed( $obj, $text, $offset ) {
4265 if ( !( $obj instanceof WOMTableCellModel ) ) return false;
43 -
 66+
 67+ $fch = $this->getFirstLineChar( $text, $offset );
 68+
4469 $lastLF = ( $text { $offset } == "\n" || ( $offset == 0 || $text { $offset - 1 } == "\n" ) );
45 -
 70+
4671 $text = substr( $text, $offset );
4772 if ( strlen( $text ) == 0 ) return 0;
4873 if ( $lastLF && preg_match( '/^(\s*[!|])/', $text ) ) return 0;
49 - if ( preg_match( '/^(!!)|(\|\|)/', $text ) ) return 0;
 74+ if ( $fch == '' ) return false;
 75+ if ( preg_match( '/^(' . ( ( $fch == '!' ) ? '!!|':'' ) . '\|\|)/', $text ) ) return 0;
5076
5177 return false;
5278 }
Index: trunk/extensions/WikiObjectModel/includes/parsers/WOMSectionParser.php
@@ -18,9 +18,9 @@
1919 public function parseNext( $text, WikiObjectModelCollection $parentObj, $offset = 0 ) {
2020 $lastLF = ( $offset == 0 || $text { $offset - 1 } == "\n" );
2121 $text = substr( $text, $offset );
22 - if ( $lastLF ) {
23 - $r = preg_match( '/^(={1,6})/', $text, $m );
24 - }
 22+ if ( !$lastLF ) return null;
 23+
 24+ $r = preg_match( '/^(={1,6})/', $text, $m );
2525 if ( $r ) {
2626 $text1 = substr( $text, strlen( $m[0] ) );
2727 $s = explode( "\n", $text1, 2 );
@@ -30,9 +30,9 @@
3131 $len = strlen( $m[0] ) + strlen( $s[0] ) + 1/* \n */;
3232 $level = strlen( $m[1] ) < strlen( $m1[1][0] ) ? strlen( $m[1] ) : strlen( $m1[1][0] );
3333
34 - $obj = new WOMSectionModel(
 34+ $obj = new WOMSectionModel( trim(
3535 substr( WOMSectionParser::$heading, 0, strlen( $m[1] ) - $level ) .
36 - substr( $s[0], 0, $m1[1][1] + strlen( $m1[1][0] ) - $level ),
 36+ substr( $s[0], 0, $m1[1][1] + strlen( $m1[1][0] ) - $level ) ),
3737 $level );
3838
3939 while ( $parentObj != null &&
Index: trunk/extensions/WikiObjectModel/includes/parsers/WikiObjectModelParser.php
@@ -1,7 +1,7 @@
22 <?php
33 /**
44 * File holding class WikiObjectModelParser, the base for all object model parser in WOM.
5 - *
 5+ *
66 * Deal plain text only, just get next text token
77 *
88 * @author Ning
@@ -34,7 +34,7 @@
3535 // /// Processing methods /////
3636 public function parseNext( $text, WikiObjectModelCollection $parentObj, $offset = 0 ) {
3737 $text = substr( $text, $offset );
38 - $r = preg_match( '/^\w+/', $text, $m );
 38+ $r = preg_match( '/^[a-zA-Z0-9]+/', $text, $m );
3939 if ( $r ) return array( 'len' => strlen( $m[0] ), 'obj' => new WOMTextModel( $m[0] ) );
4040 // special case, <nowiki>, <noinclude>
4141 $idx = stripos( $text, '<nowiki>' );
@@ -53,7 +53,7 @@
5454 return array( 'len' => 1, 'obj' => new WOMTextModel( $text { 0 } ) );
5555 }
5656
57 - // E.g.,
 57+ // E.g.,
5858 // semantic property is extended from internal links
5959 // parser functions is extended from templates
6060 public function subclassOf( $parserInstance ) {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_MagicWord.php
@@ -5,27 +5,29 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMMagicWordModel extends WikiObjectModel {
1313 protected $m_magicword;
 14+ protected $m_doubleUnderscore;
1415
15 - public function __construct( $magicword ) {
 16+ public function __construct( $magicword, $doubleUnderscore = false ) {
1617 parent::__construct( WOM_TYPE_MAGICWORD );
1718 $this->m_magicword = $magicword;
 19+ $this->m_doubleUnderscore = $doubleUnderscore;
1820 }
1921
2022 public function getMagicWord() {
2123 return $this->m_magicword;
2224 }
23 -
 25+
2426 public function setMagicWord( $magicword ) {
2527 $this->m_magicword = $magicword;
2628 }
2729
2830 public function getWikiText() {
29 - return "{{{$this->m_magicword}}}";
 31+ return $this->m_doubleUnderscore ? $this->m_magicword : "{{{$this->m_magicword}}}";
3032 }
3133
3234 public function setXMLAttribute( $key, $value ) {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Page.php
@@ -66,16 +66,20 @@
6767 }
6868
6969 public function appendChildObject( WikiObjectModel $obj, $id = '' ) {
70 - $p = $this->m_page_objs[$id];
71 - if ( !( $p instanceof WikiObjectModelCollection ) ) {
72 - return;
 70+ if ( $id == '' ) {
 71+ $p = $this;
 72+ } else {
 73+ $p = $this->m_page_objs[$id];
 74+ if ( !( $p instanceof WikiObjectModelCollection ) ) {
 75+ return;
 76+ }
7377 }
7478 $obj->setObjectID( $this->getNextId() );
7579 $p->insertObject( $obj );
7680
7781 $this->addToPageObjectSet( $obj );
7882 }
79 -
 83+
8084 public function removePageObject( $id ) {
8185 $old_obj = $this->m_page_objs[$id];
8286 if ( $old_obj == null ) return;
Index: trunk/extensions/WikiObjectModel/includes/models/WOMFactory.php
@@ -1,9 +1,9 @@
22 <?php
33 /**
44 * This file contains the WikiObjectModelFactory class.
5 - *
 5+ *
66 * @author Ning
7 - *
 7+ *
88 * @file
99 * @ingroup WikiObjectModels
1010 */
@@ -12,14 +12,14 @@
1313
1414 /**
1515 * Array of type labels indexed by type ids. Used for model type resolution.
16 - *
 16+ *
1717 * @var array
1818 */
1919 static private $mTypeLabels;
2020
2121 /**
2222 * Array of class names for creating new WikiObjectModel, indexed by type id.
23 - *
 23+ *
2424 * @var array of WikiObjectModel
2525 */
2626 static private $mTypeClasses;
@@ -27,9 +27,9 @@
2828 /**
2929 * Create a value from a type id. If no $value is given, an empty container
3030 * is created, the value of which can be set later on.
31 - *
 31+ *
3232 * @param $typeid id string for the given type
33 - *
 33+ *
3434 * @return WikiObjectModel
3535 */
3636 static public function newTypeIDValue( $typeid ) {
@@ -78,7 +78,7 @@
7979 /**
8080 * A function for registering/overwriting pomtypes for WOM. Should be
8181 * called from within the hook 'mwInitWOMTypes'.
82 - *
 82+ *
8383 * @param string $id
8484 * @param string $className
8585 * @param mixed $label
@@ -100,7 +100,7 @@
101101 *
102102 * This method may or may not take aliases into account. For unknown
103103 * labels, the normalised (DB-version) label is used as an ID.
104 - *
 104+ *
105105 * @param string $label
106106 */
107107 static public function findTypeID( $label ) {
@@ -118,7 +118,7 @@
119119 * Get the translated user label for a given internal ID. If the ID does
120120 * not have a label associated with it in the current language, the ID
121121 * itself is transformed into a label (appropriate for user defined types).
122 - *
 122+ *
123123 * @param string $id
124124 */
125125 static public function findTypeLabel( $id ) {
@@ -142,7 +142,7 @@
143143 * a property, and that are internal (i.e. not user defined). No labels are
144144 * returned for internal types without user labels (e.g. the special types
145145 * for wome special properties), and for user defined types.
146 - *
 146+ *
147147 * @return array
148148 */
149149 static public function getKnownTypeLabels() {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Category.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMCategoryModel extends WikiObjectModel {
@@ -13,13 +13,19 @@
1414
1515 public function __construct( $name ) {
1616 parent::__construct( WOM_TYPE_CATEGORY );
17 - $this->m_name = $name;
 17+ $title = Title::newFromText( $name, NS_CATEGORY );
 18+ if ( $title == null ) {
 19+ // no idea why, just leave it
 20+ $this->m_name = $name;
 21+ } else {
 22+ $this->m_name = $title->getText();
 23+ }
1824 }
1925
2026 public function getName() {
2127 return $this->m_name;
2228 }
23 -
 29+
2430 public function setName( $name ) {
2531 $this->m_name = $name;
2632 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_TmplField.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMTemplateFieldModel extends WOMParameterModel {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Parameter.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMParameterModel extends WikiObjectModelCollection {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_TblCell.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMTableCellModel extends WikiObjectModelCollection {
@@ -21,7 +21,7 @@
2222 public function getPrefix() {
2323 return $this->m_prefix;
2424 }
25 -
 25+
2626 public function setPrefix( $prefix ) {
2727 $this->m_prefix = $prefix;
2828 }
@@ -29,4 +29,20 @@
3030 public function getWikiText() {
3131 return "{$this->m_prefix}" . parent::getWikiText();
3232 }
 33+
 34+ public function setXMLAttribute( $key, $value ) {
 35+ if ( $value == '' ) throw new MWException( __METHOD__ . ": value cannot be empty" );
 36+
 37+ $value = str_replace( '\n', "\n", $value );
 38+ if ( $key == 'prefix' ) {
 39+ $this->m_prefix = $value;
 40+ return;
 41+ }
 42+ throw new MWException( __METHOD__ . ": invalid key/value pair: prefix=table_cell_prefix (! / | / ||)" );
 43+ }
 44+
 45+ protected function getXMLAttributes() {
 46+ $prefix = str_replace( "\n", '\n', $this->m_prefix );
 47+ return "prefix=\"{$prefix}\"";
 48+ }
3349 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Text.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMTextModel extends WikiObjectModel {
@@ -29,6 +29,6 @@
3030 }
3131
3232 public function toXML() {
33 - return htmlentities( $this->m_text );
 33+ return htmlspecialchars( $this->m_text );
3434 }
3535 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Property.php
@@ -5,12 +5,13 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMPropertyModel extends WikiObjectModel {
1313 protected $m_property; // name
1414 protected $m_smwdatavalue; // value, caption, type
 15+ protected $m_visible;
1516
1617 public function __construct( $property, $value, $caption = '' ) {
1718 parent::__construct( WOM_TYPE_PROPERTY );
@@ -25,6 +26,7 @@
2627
2728 $this->m_property = $property;
2829 $this->m_smwdatavalue = $smwdatavalue;
 30+ $this->m_visible = !preg_match( '/\s+/', $caption );
2931 }
3032
3133 public function getProperty() {
@@ -34,7 +36,7 @@
3537 public function setProperty( $property ) {
3638 $this->m_property = $property;
3739 }
38 -
 40+
3941 public function getSMWDataValue() {
4042 return $this->m_smwdatavalue;
4143 }
@@ -42,12 +44,14 @@
4345 public function setSMWDataValue( $smwdatavalue ) {
4446 $this->m_smwdatavalue = $smwdatavalue;
4547 }
46 -
 48+
4749 public function getWikiText() {
4850 $res = "[[{$this->getPropertyName()}::{$this->getPropertyValue()}";
4951 if ( $this->getPropertyValue() != $this->getCaption()
5052 && $this->getCaption() != '' ) {
5153 $res .= "|{$this->getCaption()}";
 54+ } else if ( !$this->m_visible ) {
 55+ $res .= "| ";
5256 }
5357 $res .= "]]";
5458
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Table.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMTableModel extends WikiObjectModelCollection {
@@ -24,8 +24,20 @@
2525 public function setStyle( $style ) {
2626 $this->m_style = $style;
2727 }
28 -
 28+
2929 public function getWikiText() {
3030 return "{| {$this->m_style}" . parent::getWikiText() . "\n|}";
3131 }
 32+
 33+ public function setXMLAttribute( $key, $value ) {
 34+ if ( $key == 'style' ) {
 35+ $this->m_style = $value;
 36+ return;
 37+ }
 38+ throw new MWException( __METHOD__ . ": invalid key/value pair: style=table_style" );
 39+ }
 40+
 41+ protected function getXMLAttributes() {
 42+ return "style=\"{$this->m_style}\"";
 43+ }
3244 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_ListItem.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMListItemModel extends WikiObjectModelCollection {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Link.php
@@ -5,14 +5,14 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMLinkModel extends WikiObjectModel {
1313 protected $m_link;
1414 protected $m_caption;
1515
16 - public function __construct( $link, $caption = '' ) {
 16+ public function __construct( $link, $caption = null ) {
1717 parent::__construct( WOM_TYPE_LINK );
1818 $this->m_link = $link;
1919 $this->m_caption = $caption;
@@ -31,11 +31,11 @@
3232 public function setLink( $link ) {
3333 $this->m_link = $link;
3434 }
35 -
 35+
3636 public function getCaption() {
3737 return $this->m_caption;
3838 }
39 -
 39+
4040 public function setCaption( $caption ) {
4141 $this->m_caption = $caption;
4242 }
@@ -44,14 +44,18 @@
4545 if ( $this->isInline() ) {
4646 return "[[{$this->m_link}" . ( $this->m_caption ? "|{$this->m_caption}" : "" ) . "]]";
4747 } else {
48 - return "[{$this->m_link}" . ( $this->m_caption ? " {$this->m_caption}" : "" ) . "]";
 48+ if ( $this->m_caption === null ) {
 49+ return $this->m_link;
 50+ } else {
 51+ return "[{$this->m_link}" . ( $this->m_caption ? " {$this->m_caption}" : "" ) . "]";
 52+ }
4953 }
5054 }
5155
5256 protected function getXMLContent() {
5357 return "
54 -<url>{$this->m_link}</url>
55 -<caption>{$this->m_caption}</caption>
 58+<url><![CDATA[{$this->m_link}]]></url>
 59+<caption><![CDATA[{$this->m_caption}]]></caption>
5660 ";
5761 }
5862 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_ParamValue.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMParamValueModel extends WikiObjectModelCollection {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_HTMLTag.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMHTMLTagModel extends WikiObjectModelCollection {
@@ -21,7 +21,7 @@
2222 public function getName() {
2323 return $this->m_name;
2424 }
25 -
 25+
2626 public function setName( $name ) {
2727 $this->m_name = $name;
2828 }
@@ -29,11 +29,11 @@
3030 public function getAttributes() {
3131 return $this->m_attributes;
3232 }
33 -
 33+
3434 public function setAttributes( $attrs ) {
3535 $this->m_attributes = $attrs;
3636 }
37 -
 37+
3838 public function getWikiText() {
3939 return "<{$this->m_name}>" . parent::getWikiText() . "</{$this->m_name}>";
4040 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_ParserFunction.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111 class WOMParserFunctionModel extends WikiObjectModelCollection {
1212 protected $m_function_key;
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Sentence.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMSentenceModel extends WikiObjectModelCollection {
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Section.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMSectionModel extends WikiObjectModelCollection {
@@ -27,7 +27,7 @@
2828 public function setName( $name ) {
2929 $this->m_name = $name;
3030 }
31 -
 31+
3232 public function getLevel() {
3333 return $this->m_level;
3434 }
@@ -35,9 +35,9 @@
3636 public function setLevel( $level ) {
3737 $this->m_level = $level;
3838 }
39 -
 39+
4040 public function getHeaderText() {
41 -// return "\n" .
 41+// return "\n" .
4242 return substr( WOMSectionModel::$heading, 0, $this->m_level ) .
4343 $this->m_name .
4444 substr( WOMSectionModel::$heading, 0, $this->m_level ) .
@@ -47,7 +47,7 @@
4848 public function getWikiText() {
4949 return $this->getHeaderText() . parent::getWikiText();
5050 }
51 -
 51+
5252 public function getContent() {
5353 return parent::getWikiText();
5454 }
Index: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_Template.php
@@ -5,7 +5,7 @@
66 * @author Ning
77 * @file
88 * @ingroup WikiObjectModels
9 - *
 9+ *
1010 */
1111
1212 class WOMTemplateModel extends WikiObjectModelCollection {
@@ -20,10 +20,10 @@
2121
2222 if ( $this->m_title->getNamespace() == NS_MAIN ) {
2323 // http://www.mediawiki.org/wiki/Help:Transclusion
24 - // If the source is in the Main article namespace (e.g., "Cat"),
 24+ // If the source is in the Main article namespace (e.g., "Cat"),
2525 // you must put a colon (:) in front of the name, thus: {{:Cat}}
2626
27 - // If the source is in the Template namespace (e.g., "Template:Villagepumppages"),
 27+ // If the source is in the Template namespace (e.g., "Template:Villagepumppages"),
2828 // just use the name itself, alone, thus: {{Villagepumppages}}
2929 if ( $this->m_name { 0 } != ':' ) {
3030 $this->m_title = Title::makeTitleSafe( NS_TEMPLATE, $this->m_name );
@@ -34,7 +34,7 @@
3535 public function getName() {
3636 return $this->m_name;
3737 }
38 -
 38+
3939 public function setName( $name ) {
4040 $this->m_name = $name;
4141 }
Index: trunk/extensions/WikiObjectModel/includes/WOM_Processor.php
@@ -36,15 +36,16 @@
3737 WOMProcessor::setupParsers();
3838 }
3939 global $wgOMModelParserMapping;
40 - $id = $wgOMModelParserMapping[$obj->getTypeID()];
41 - if ( isset( WOMProcessor::$parsers[$id] ) ) {
42 - $result = WOMProcessor::$parsers[$id];
43 - } else {
44 - $result = WOMProcessor::$base_parser;
 40+ if ( isset( $wgOMModelParserMapping[$obj->getTypeID()] ) ) {
 41+ $id = $wgOMModelParserMapping[$obj->getTypeID()];
 42+ if ( isset( WOMProcessor::$parsers[$id] ) ) {
 43+ wfProfileOut( $fname );
 44+ return WOMProcessor::$parsers[$id];
 45+ }
4546 }
4647 wfProfileOut( $fname );
4748
48 - return $result;
 49+ return WOMProcessor::$base_parser;
4950 }
5051
5152 private static function applyObjID( WikiObjectModel $wom, WOMPageModel $root ) {
@@ -67,7 +68,7 @@
6869
6970 $root = new WOMPageModel();
7071 WOMProcessor::parseNext( $text, $root, $root );
71 - WOMProcessor::parseSentences($root);
 72+ WOMProcessor::parseSentences( $root );
7273 WOMProcessor::applyObjID( $root, $root );
7374 wfProfileOut( $fname );
7475 return $root;
@@ -187,6 +188,11 @@
188189 } else {
189190 $parserInstance2 = $parserInstance;
190191 $result = $parserInstance->parseNext( $text, $parentObj, $offset );
 192+ if ( $result == null ) {
 193+ // FIXME: just omit current char, this will not fit for Wiki parser
 194+ ++ $offset;
 195+ continue;
 196+ }
191197 }
192198
193199 $next_obj = $result['obj'];
@@ -204,10 +210,12 @@
205211
206212 WOMProcessor::assemble( $next_obj );
207213
208 - if ( $next_obj->isCollection() && !$result['closed'] ) {
 214+ if ( $next_obj->isCollection() && !( isset( $result['closed'] ) && $result['closed'] ) ) {
209215 $collection_start = $offset;
210 - $d = WOMProcessor::parseNext( $text, $next_obj, $rootObj, $offset, ( $parserInstance2 == null ? null :
211 - WOMProcessor::$parsers[$parserInstance2->getSubParserID()] ) );
 216+ $d = WOMProcessor::parseNext( $text, $next_obj, $rootObj, $offset,
 217+ ( ( $parserInstance2 != null && isset( WOMProcessor::$parsers[$parserInstance2->getSubParserID()] ) ) ?
 218+ WOMProcessor::$parsers[$parserInstance2->getSubParserID()] :
 219+ null ) );
212220 if ( $d == 100 && $parserInstance2->isObjectClosed( $next_obj, $text, $offset ) === false ) {
213221 // rollback
214222 $p = WOMProcessor::getObjectParser( $parentObj );
@@ -491,7 +499,7 @@
492500
493501 wfProfileOut( $fname );
494502 }
495 -
 503+
496504 public static function appendPageObject( $object, $title, $obj_id = '', $summary = '', $revision_id = 0, $force_update = true ) {
497505 $fname = 'WikiObjectModel::appendPageObject (WOM)';
498506 wfProfileIn( $fname );
@@ -627,7 +635,7 @@
628636
629637 wfProfileOut( $fname );
630638 }
631 -
 639+
632640 public static function updatePageText( $text, $title, $obj_id, $summary = '', $revision_id = 0, $force_update = true ) {
633641 $fname = 'WikiObjectModel::updatePageText (WOM)';
634642 wfProfileIn( $fname );
Index: trunk/extensions/WikiObjectModel/includes/WOM_Setup.php
@@ -130,9 +130,3 @@
131131 $wgAutoloadClasses['ApiWOMGetObjectModel'] = $wgOMIP . '/includes/apis/WOM_GetObjectModel.php';
132132
133133
134 -
135 -
136 -// constants for special properties
137 -define( 'WOM_WF_SP_HAS_TEMPLATE', 1 );
138 -define( 'WOM_WF_SP_HAS_MULTIPLE_TEMPLATE', 2 );
139 -define( 'WOM_WF_SP_HAS_TEMPLATEFIELD_CONNECTOR', 3 );
Index: trunk/extensions/WikiObjectModel/includes/apis/WOM_GetObjectModel.php
@@ -50,24 +50,38 @@
5151 $result['result'] = 'Success';
5252
5353 // pay attention to special xml tag, e.g., <property><value>...</value></property>
 54+ $result['return'] = array();
5455 if ( $type == 'count' ) {
5556 $count = 0;
5657 foreach ( $objs as $id ) {
5758 if ( $id == '' ) continue;
5859 ++ $count;
5960 }
60 - $result['return'] = $count;
 61+ $this->getResult()->setContent( $result['return'], $count );
6162 } else {
62 - $result['return'] = array();
 63+ $xml = '';
 64+ $page_obj = WOMProcessor::getPageObject( $articleTitle, $rid );
6365 foreach ( $objs as $id ) {
6466 if ( $id == '' ) continue;
 67+ $wobj = $page_obj->getObject( $id );
6568 $result['return'][$id] = array();
6669 if ( $type == 'xml' ) {
67 - $this->getResult()->setContent( $result['return'][$id], WOMProcessor::getPageObject( $articleTitle, $rid )->getObject( $id )->toXML() );
 70+ $xml .= "<{$id} xml:space=\"preserve\">{$wobj->toXML()}</{$id}>";
 71+// $this->getResult()->setContent( $result['return'][$id], $wobj->toXML() );
6872 } else {
69 - $this->getResult()->setContent( $result['return'][$id], WOMProcessor::getPageObject( $articleTitle, $rid )->getObject( $id )->getWikiText() );
 73+ $this->getResult()->setContent( $result['return'][$id], $wobj->getWikiText() );
7074 }
7175 }
 76+ if ( $type == 'xml' ) {
 77+ header ( "Content-Type: application/rdf+xml" );
 78+ echo <<<OUTPUT
 79+<?xml version="1.0" encoding="UTF-8" ?>
 80+<api><womget result="Success"><return>
 81+{$xml}
 82+</return></womget></api>
 83+OUTPUT;
 84+ exit( 1 );
 85+ }
7286 }
7387 }
7488 $this->getResult()->addValue( null, $this->getModuleName(), $result );
@@ -78,9 +92,9 @@
7993 'page' => null,
8094 'xpath' => null,
8195 'type' => array(
82 - ApiBase :: PARAM_DFLT => 'get',
 96+ ApiBase :: PARAM_DFLT => 'wiki',
8397 ApiBase :: PARAM_TYPE => array(
84 - 'get',
 98+ 'wiki',
8599 'count',
86100 'xml',
87101 ),
@@ -99,9 +113,9 @@
100114 'xpath' => 'DOM-like xpath to locate WOM object instances (http://www.w3schools.com/xpath/xpath_syntax.asp)',
101115 'type' => array (
102116 'Type to fetch useful wiki object data',
103 - 'type = get, get specified object',
 117+ 'type = wiki, get wiki text of specified object',
104118 'type = count, get objects count with specified xpath',
105 - 'type = xml, view objects\' xml format with specified xpath, usually use with format=xml',
 119+ 'type = xml, view "encoded objects\' xml" with specified xpath, usually use with format=xml',
106120 ),
107121 'rid' => 'Revision id of specified page - by dafault latest updated revision (0) is used',
108122 );
Index: trunk/extensions/WikiObjectModel/includes/apis/WOM_SetObjectModel.php
@@ -19,12 +19,12 @@
2020 $this->dieUsage( 'Must specify xpath', 1 );
2121
2222 $page = $params['page'];
23 - $type = $params['type'];
 23+ $verb = $params['verb'];
2424 $xpath = $params['xpath'];
2525 $value = $params['value'];
2626 $summary = $params['summary'];
2727 $rid = $params['rid'];
28 - $force_update = $params['force_update'];
 28+ $force_update = ( intval( $params['force_update'] ) == 1 );
2929
3030 $articleTitle = Title::newFromText( $page );
3131 if ( !$articleTitle )
@@ -52,9 +52,9 @@
5353 throw new MWException( __METHOD__ . ": object does not found, xpath: {$xpath}" );
5454 }
5555
56 - if ( $type == 'remove' ) {
 56+ if ( $verb == 'remove' ) {
5757 WOMProcessor::removePageObject( $articleTitle, $oid, $summary, $rid, $force_update );
58 - } else if ( $type == 'removeall' ) {
 58+ } else if ( $verb == 'removeall' ) {
5959 $wom = WOMProcessor::getPageObject( $articleTitle, $rid );
6060 foreach ( $objs as $id ) {
6161 if ( $id == '' ) continue;
@@ -76,13 +76,13 @@
7777 if ( is_null( $params['value'] ) )
7878 $this->dieUsage( 'Must specify value', 2 );
7979
80 - if ( $type == 'insert' ) {
 80+ if ( $verb == 'insert' ) {
8181 WOMProcessor::insertPageText( $value, $articleTitle, $oid, $summary, $rid, $force_update );
82 - } else if ( $type == 'update' ) {
 82+ } else if ( $verb == 'update' ) {
8383 WOMProcessor::updatePageText( $value, $articleTitle, $oid, $summary, $rid, $force_update );
84 - } else if ( $type == 'append' ) {
 84+ } else if ( $verb == 'append' ) {
8585 WOMProcessor::appendPageText( $value, $articleTitle, $oid, $summary, $rid, $force_update );
86 - } else if ( $type == 'attribute' ) {
 86+ } else if ( $verb == 'attribute' ) {
8787 $wom = WOMProcessor::getPageObject( $articleTitle, $rid );
8888 $obj = $wom->getObject( $oid );
8989 $kv = explode( '=', $value, 2 );
@@ -119,7 +119,7 @@
120120 protected function getAllowedParams() {
121121 return array (
122122 'page' => null,
123 - 'type' => array(
 123+ 'verb' => array(
124124 ApiBase :: PARAM_DFLT => 'update',
125125 ApiBase :: PARAM_TYPE => array(
126126 'update',
@@ -141,10 +141,10 @@
142142 ApiBase :: PARAM_MIN => 0
143143 ),
144144 'force_update' => array(
145 - ApiBase :: PARAM_DFLT => 'true',
 145+ ApiBase :: PARAM_DFLT => '1',
146146 ApiBase :: PARAM_TYPE => array(
147 - 'true',
148 - 'false',
 147+ '1',
 148+ '0',
149149 ),
150150 ),
151151 );
@@ -153,23 +153,27 @@
154154 protected function getParamDescription() {
155155 return array (
156156 'page' => 'Title of the page to modify',
157 - 'type' => 'Type to set to change wiki object instances',
 157+ 'verb' => 'Action verb to set to change wiki object instances',
158158 'xpath' => array(
159159 'DOM-like xpath to locate WOM object instances (http://www.w3schools.com/xpath/xpath_syntax.asp)',
160 - 'type = update, xpath to elements to be updated',
161 - 'type = attribute, xpath to elements, the attribute will be updated',
162 - 'type = insert, the element will be inserted right before the element specified by xpath',
163 - 'type = append, the element will be appended right to the element children elements specified by xpath',
164 - 'type = remove, xpath to element to be removed',
165 - 'type = removeall, xpath to elements to be removed',
 160+ 'verb = update, xpath to elements to be updated',
 161+ 'verb = attribute, xpath to elements, the attribute will be updated',
 162+ 'verb = insert, the element will be inserted right before the element specified by xpath',
 163+ 'verb = append, the element will be appended right to the element children elements specified by xpath',
 164+ 'verb = remove, xpath to element to be removed',
 165+ 'verb = removeall, xpath to elements to be removed',
166166 ),
167167 'value' => array(
168168 'Value to set',
169 - 'type = attribute, attribute_name=attribute_value',
 169+ 'verb = attribute, attribute_name=attribute_value',
170170 ),
171171 'summary' => 'Edit summary',
172172 'rid' => 'Revision id of specified page - by dafault latest updated revision (0) is used',
173 - 'force_update' => 'Force to update even if the revision id does not match the latest edition',
 173+ 'force_update' => array(
 174+ 'Force to update even if the revision id does not match the latest edition',
 175+ 'force_update = 0, return "revision not match" exception if rid is not the latest one',
 176+ 'force_update = 1, update anyway',
 177+ ),
174178 );
175179 }
176180

Status & tagging log