https://www.mediawiki.org/wiki/Special:Code/MediaWiki/115120

Revision: 115120
Author:   ning
Date:     2012-05-03 11:05:25 +0000 (Thu, 03 May 2012)
Log Message:
-----------
add new api, womapi, to enable mw api upon womget results (before rendering)

Modified Paths:
--------------
    trunk/extensions/WikiObjectModel/WikiObjectModel.php
    trunk/extensions/WikiObjectModel/includes/WOM_Setup.php
    trunk/extensions/WikiObjectModel/includes/models/WOM_OM_HTMLTag.php
    
trunk/extensions/WikiObjectModel/includes/models/WikiObjectModelCollection.php
    trunk/extensions/WikiObjectModel/includes/parsers/WOMListItemParser.php

Added Paths:
-----------
    trunk/extensions/WikiObjectModel/includes/apis/WOM_WikiObjectApi.php

Modified: trunk/extensions/WikiObjectModel/WikiObjectModel.php
===================================================================
--- trunk/extensions/WikiObjectModel/WikiObjectModel.php        2012-05-03 
07:34:04 UTC (rev 115119)
+++ trunk/extensions/WikiObjectModel/WikiObjectModel.php        2012-05-03 
11:05:25 UTC (rev 115120)
@@ -57,6 +57,23 @@
 
        $wgHooks['womRegisterParserFunctionParsers'][] = 
'wfWOMRegisterParserFunctionParsers';
 
+       // APIs
+       global $wgAPIModules, $wgAutoloadClasses, $wgOMIP;
+       $wgAPIModules['womset'] = 'ApiWOMSetObjectModel';
+       $wgAutoloadClasses['ApiWOMSetObjectModel'] = $wgOMIP . 
'/includes/apis/WOM_SetObjectModel.php';
+       $wgAPIModules['womget'] = 'ApiWOMGetObjectModel';
+       $wgAutoloadClasses['ApiWOMGetObjectModel'] = $wgOMIP . 
'/includes/apis/WOM_GetObjectModel.php';
+       $wgAPIModules['womapi'] = 'ApiWOMWikiObjectApi';
+       $wgAutoloadClasses['ApiWOMWikiObjectApi'] = $wgOMIP . 
'/includes/apis/WOM_WikiObjectApi.php';
+
+       $wgAPIModules['womoutput'] = 'ApiWOMOutputObjectModel';
+       $wgAutoloadClasses['ApiWOMOutputObjectModel'] = $wgOMIP . 
'/includes/apis/WOM_OutputObjectModel.php';
+       $wgAPIModules['womwiki'] = 'ApiWOMGetWikiResult';
+       $wgAutoloadClasses['ApiWOMGetWikiResult'] = $wgOMIP . 
'/includes/apis/WOM_GetWikiResult.php';
+
+       $wgAPIModules['womquery'] = 'ApiWOMQuery';
+       $wgAutoloadClasses['ApiWOMQuery'] = $wgOMIP . 
'/includes/apis/WOM_Query.php';
+
        $wgExtensionCredits['parserhook'][] = array(
                'path' => __FILE__,
                'name' => 'Wiki ObjectModel Extension',

Modified: trunk/extensions/WikiObjectModel/includes/WOM_Setup.php
===================================================================
--- trunk/extensions/WikiObjectModel/includes/WOM_Setup.php     2012-05-03 
07:34:04 UTC (rev 115119)
+++ trunk/extensions/WikiObjectModel/includes/WOM_Setup.php     2012-05-03 
11:05:25 UTC (rev 115120)
@@ -145,16 +145,3 @@
 $wgOMParagraphObjectTypes[] = WOM_TYPE_LISTITEM;
 $wgOMParagraphObjectTypes[] = WOM_TYPE_PARSERFUNCTION;
 $wgOMParagraphObjectTypes[] = WOM_TYPE_TEMPLATE;
-
-// APIs
-global $wgAPIModules;
-$wgAPIModules['womset'] = 'ApiWOMSetObjectModel';
-$wgAutoloadClasses['ApiWOMSetObjectModel'] = $wgOMIP . 
'/includes/apis/WOM_SetObjectModel.php';
-$wgAPIModules['womget'] = 'ApiWOMGetObjectModel';
-$wgAutoloadClasses['ApiWOMGetObjectModel'] = $wgOMIP . 
'/includes/apis/WOM_GetObjectModel.php';
-$wgAPIModules['womquery'] = 'ApiWOMQuery';
-$wgAutoloadClasses['ApiWOMQuery'] = $wgOMIP . '/includes/apis/WOM_Query.php';
-$wgAPIModules['womoutput'] = 'ApiWOMOutputObjectModel';
-$wgAutoloadClasses['ApiWOMOutputObjectModel'] = $wgOMIP . 
'/includes/apis/WOM_OutputObjectModel.php';
-$wgAPIModules['womwiki'] = 'ApiWOMGetWikiResult';
-$wgAutoloadClasses['ApiWOMGetWikiResult'] = $wgOMIP . 
'/includes/apis/WOM_GetWikiResult.php';

Added: trunk/extensions/WikiObjectModel/includes/apis/WOM_WikiObjectApi.php
===================================================================
--- trunk/extensions/WikiObjectModel/includes/apis/WOM_WikiObjectApi.php        
                        (rev 0)
+++ trunk/extensions/WikiObjectModel/includes/apis/WOM_WikiObjectApi.php        
2012-05-03 11:05:25 UTC (rev 115120)
@@ -0,0 +1,193 @@
+<?php
+
+/**
+ * @addtogroup API
+ */
+class ApiWOMWikiObjectApi extends ApiBase {
+       private $m_apiInst = null;
+
+       public function __construct( $main, $action ) {
+               parent :: __construct( $main, $action );
+       }
+
+       public function execute() {
+               global $wgUser;
+
+               $params = $this->extractRequestParams();
+               if ( is_null( $params['title'] ) )
+                       $this->dieUsage( 'Must specify page title', 0 );
+               if ( is_null( $params['xpath'] ) )
+                       $this->dieUsage( 'Must specify xpath', 1 );
+               if ( is_null( $params['api'] ) )
+                       $this->dieUsage( 'Must specify api action name', 2 );
+               if ( is_null( $params['wommap'] ) )
+                       $this->dieUsage( 'Must specify wom => api mapping 
info', 3 );
+
+               $page_name = $params['title'];
+               $xpath = $params['xpath'];
+               $api = $params['api'];
+               $rid = $params['rid'];
+
+               $wommaps = $this->parseMapInfo( $params['wommap'] );
+
+               $articleTitle = Title::newFromText( $page_name );
+               if ( !$articleTitle )
+                       $this->dieUsage( "Can't create title object 
($page_name)", 2 );
+
+               $article = new Article( $articleTitle );
+               if ( !$article->exists() )
+                       $this->dieUsage( "Article doesn't exist ($page_name)", 
3 );
+
+               try {
+                       $page_obj = WOMProcessor::getPageObject( $articleTitle, 
$rid );
+                       $objs = WOMProcessor::getObjIdByXPath2( $page_obj, 
$xpath );
+
+                       $vals = array();
+                       $first = true;
+                       foreach ( $objs as $id ) {
+                               if ( $id == '' ) continue;
+                               $wobj = $page_obj->getObject( $id );
+
+                               foreach ( $wommaps as $idx => $map ) {
+                                       if ( !$map['setting']['multiple'] && 
!$first ) continue;
+                                       $val = '';
+                                       if ( strtolower( $map['xpath'] ) == 
'innerwiki' ) {
+                                               $val = ( $wobj instanceof 
WikiObjectModelCollection ) ? $wobj->getInnerWikiText() : $wobj->getWikiText();
+                                       } else {
+                                               $xObj = simplexml_load_string( 
$wobj->toXML() );
+                                               $os = $xObj->xpath( 
$map['xpath'] );
+                                               if ( count( $os ) > 0 ) {
+                                                       foreach ( $os as $o ) 
$val .= strval( $o );
+                                               }
+                                       }
+                                       $wommaps[$idx]['value'] .= $val . ( 
$map['setting']['delimiter'] ? $map['setting']['delimiter']:'' );
+                               }
+                               if ( $first ) $first = false;
+                       }
+               } catch ( Exception $e ) {
+                       $err = $e->getMessage();
+               }
+
+               $result = array();
+               if ( isset( $err ) ) {
+                       $result = array(
+                               'result' => 'Failure',
+                               'message' => array(),
+                       );
+                       $this->getResult()->setContent( $result['message'], 
$err );
+               } else {
+                       $result['result'] = 'Success';
+                       $result['revisionID'] = $page_obj->getRevisionID();
+                       foreach ( $wommaps as $map ) {
+                               $this->getMain()->getRequest()->setVal( 
$map['param'], $map['value'] );
+                       }
+                       $this->m_apiInst->profileIn();
+                       $this->m_apiInst->execute();
+                       $this->m_apiInst->profileOut();
+               }
+
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$result );
+       }
+
+       private function parseMapInfo( $mapinfo ) {
+               $wommaps = array();
+               foreach ( explode( '|', str_replace( '||', 
'___PIPEPLACEHOLDER__', $mapinfo ) ) as $map ) {
+                       $s = explode( '=', str_replace( '___PIPEPLACEHOLDER__', 
'|', $map ), 2 );
+                       $set = explode( ',', $s[1], 2 );
+                       $len = strlen( $set[1] );
+                       $settings = array();
+                       for ( $i = 0; $i < $len; ++$i ) {
+                               if ( strtolower( $set[1] { $i } ) == 'm' ) {
+                                       $settings['multiple'] = true;
+                                       ++$i;
+                                       $settings['delimiter'] = $set[1] { $i } 
;
+                               }
+                       }
+                       $wommaps[] = array(
+                               'xpath' => $s[0],
+                               'param' => $set[0],
+                               'setting' => $settings,
+                               'value' => ''
+                       );
+               }
+               return $wommaps;
+       }
+
+       protected function getAllowedParams() {
+               // tricky here
+               $mainInst = $this->getMain();
+               $tmp = array_keys( $mainInst->getModules() );
+               $ids = array(
+                       array_search( 'womset', $tmp ),
+                       array_search( 'womget', $tmp ),
+                       array_search( 'womapi', $tmp ),
+                       array_search( 'womoutput', $tmp ),
+                       array_search( 'womwiki', $tmp ),
+                       array_search( 'womquery', $tmp )
+               );
+               foreach ( $ids as $id ) unset( $tmp[$id] );
+
+               $params = array (
+                       'title' => null,
+                       'xpath' => null,
+                       'rid' => array (
+                               ApiBase :: PARAM_TYPE => 'integer',
+                               ApiBase :: PARAM_DFLT => 0,
+                               ApiBase :: PARAM_MIN => 0
+                       ),
+                       'wommap' => null,
+                       'api' => array(
+                               ApiBase :: PARAM_DFLT => 'help',
+                               ApiBase :: PARAM_TYPE => $tmp
+                       ),
+               );
+
+               $api = $mainInst->getRequest()->getVal( $this->encodeParamName( 
'api' ), 'help' );
+               if ( $api == 'help' ) {
+                       // apply description
+                       $params['...'] = null;
+               } else {
+//                     $mainInst = new ApiMain($mainInst->getRequest());
+                       $modules = $mainInst->getModules();
+                       $this->m_apiInst = new $modules[$api] ( $mainInst, $api 
);
+                       $params = $params + $this->m_apiInst->getFinalParams();
+               }
+
+               return $params;
+       }
+
+       protected function getParamDescription() {
+               return array (
+                       'title' => 'Title of the page to modify',
+                       'xpath' => 'DOM-like xpath to locate WOM object 
instances (http://www.w3schools.com/xpath/xpath_syntax.asp)',
+                       'wommap' => array (
+                               'Settings to map WOM result on parameter(s) of 
api actions',
+                               '"|" as delimiter, to separate map items',
+                               'format: xpath of values=api parameter 
name,other settings',
+                               '    for "|" inside map item, use double "||"',
+                               '  xpath:',
+                               '    "@property" as property name,',
+                               '    "innerwiki" as wiki text inside xml 
object',
+                               '  settings:',
+                               '    "m<delimiter>", multiple field, if 
multiple flag is not set, always use the first result',
+                       ),
+                       'api' => 'Api (action) name of common Wiki',
+                       'rid' => 'Revision id of specified page - by dafault 
latest updated revision (0) is used',
+                       '...' => 'standard api parameters',
+               );
+       }
+
+       protected function getDescription() {
+               return 'Call to execute MW apis upon Wiki objects inside title';
+       }
+
+       protected function getExamples() {
+               return array (
+                       
'api.php?action=womapi&title=Somepage&xpath=//template&wommap=@name=titles,m||&api=query&prop=info'
+               );
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+}

Modified: trunk/extensions/WikiObjectModel/includes/models/WOM_OM_HTMLTag.php
===================================================================
--- trunk/extensions/WikiObjectModel/includes/models/WOM_OM_HTMLTag.php 
2012-05-03 07:34:04 UTC (rev 115119)
+++ trunk/extensions/WikiObjectModel/includes/models/WOM_OM_HTMLTag.php 
2012-05-03 11:05:25 UTC (rev 115120)
@@ -52,10 +52,6 @@
                return "<{$this->m_name}{$attr}>" . parent::getWikiText() . 
"</{$this->m_name}>";
        }
 
-       public function getInnerWikiText() {
-               return parent::getWikiText();
-       }
-
        public function updateOnNodeClosed() {
                // use SemanticForms to bind properties to fields
        }

Modified: 
trunk/extensions/WikiObjectModel/includes/models/WikiObjectModelCollection.php
===================================================================
--- 
trunk/extensions/WikiObjectModel/includes/models/WikiObjectModelCollection.php  
    2012-05-03 07:34:04 UTC (rev 115119)
+++ 
trunk/extensions/WikiObjectModel/includes/models/WikiObjectModelCollection.php  
    2012-05-03 11:05:25 UTC (rev 115120)
@@ -31,7 +31,7 @@
                return null;
        }
 
-       public function getWikiText() {
+       public function getInnerWikiText() {
                $text = '';
                foreach ( $this->m_objects as $obj ) {
                        $text .= $obj->getWikiText();
@@ -39,6 +39,10 @@
                return $text;
        }
 
+       public function getWikiText() {
+               return $this->getInnerWikiText();
+       }
+
        /**
         * Hook here, value updates on collection complete
         */

Modified: 
trunk/extensions/WikiObjectModel/includes/parsers/WOMListItemParser.php
===================================================================
--- trunk/extensions/WikiObjectModel/includes/parsers/WOMListItemParser.php     
2012-05-03 07:34:04 UTC (rev 115119)
+++ trunk/extensions/WikiObjectModel/includes/parsers/WOMListItemParser.php     
2012-05-03 11:05:25 UTC (rev 115120)
@@ -44,7 +44,7 @@
                $parentClose = WOMProcessor::getObjectParser( $obj->getParent() 
)
                        ->isObjectClosed( $obj->getParent(), $text, $offset );
                if ( $parentClose !== false ) return 0;
-               
+
                return false;
        }
 }


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to