webplatform / mediawiki

WebPlaform Docs’ MediaWiki micro-extensions and skin
10 stars 4 forks source link

Generate Readiness markers from the PHP template instead of inside the page #15

Open renoirb opened 10 years ago

renoirb commented 10 years ago

By doing this, we will be able to generate an easier to style HTML for the readiness markers.

The problem with using readiness markers from inside a wiki page is that we have to style the marker with absolute positioning to make it be visible on the side

Using absolute positioning in our case "works" but has some issues — see #18. A better way to do it would be to get the HTML markup right from the PHP and have the HTML closer to the <body>.

Objective of this task is to improve the HTML render responsible of the Readiness markers by leveraging existing Semantic MediaWiki properties, but from PHP itself.

Path to a solution

Here is a PoC that reads readiness state from an instance of SkinTemplate.

// file: https://github.com/webplatform/mediawiki/blob/master/skin/SkinWebPlatform.php
class SkinWebPlatform extends SkinTemplate {

  const READINESS_TEMPLATE = '<div class="readiness-state STRING_CLASS_NAME"><p>This article is <a href="/WIKIROOT/Property:State">STRING_STATUS</a>.</p></div>';

  /**
   * Get Readiness markers from the PHP template
   *
   * ref:
   *   * https://www.mediawiki.org/wiki/API:Calling_internally
   *
   * Possible states based on return value:
   *  null            = Not Ready
   *  'Not Ready'     = Not Ready
   *  'Unreviewed'    = Unreviewed
   *  'Out of Date'   = Out of Date
   *  'In Progress'   = In Progress
   *  'Almost Ready'  = Almost Ready
   *  'Ready to Use'  = Ready to Use
   *
   * Based on this ASK query:
   *   * http://docs.webplatform.org/w/api.php?format=jsonfm&action=ask&query=%5B%5Bcss%2Fproperties%2Fborder-radius%5D%5D%7C%3FState
   */
  private function getReadinessMarkerState( ) {
    global $wgArticlePath;

    $readynessAttemptWorked = false;
    $data = array();
    $receivedState = null;
    $ask = array();

    try {
      $title = $this->getRequest()->getVal( 'title' );
      $ask['action'] = 'ask';
      $ask['query'] = '[['.$title.']]'.'|?State';
      $params = new DerivativeRequest( $this->getRequest(), $ask, true );

    } catch(Exception $e) {
      $readynessAttemptWorked = false;
      $params = null;
    }

    try {
      $api = new ApiMain( $params );
      $api->execute();
      $result = $api->getResultData();
      //var_dump($result['query']['results']);
      $data = array_shift($result['query']['results']);
      $receivedState = $data['printouts']['State'][0];
    } catch(Exception $e) {
      $readynessAttemptWorked = false;
      $receivedState = false;
    }

    $prepared = array();
    $prepared['WIKIROOT'] = str_replace( array( '$1' , '/' ), '', $wgArticlePath );

    if( empty( $receivedState ) === false ) {
      $prepared['STRING_STATUS'] = $receivedState;
      $prepared['STRING_CLASS_NAME'] = str_replace(' ', '_', $receivedState);
    }

    // TODO: Figure out how to get data when redirects, and spaces are involved

    return $prepared;
  }
}

Then, use the global $wgParser (in a hook?), and mimick/use what’s done in http://docs.webplatform.org/wiki/Property:State and http://docs.webplatform.org/wiki/Template:Flags

Related mailing-list messages

renoirb commented 10 years ago

Changeset pushed to Gerrit https://source.webplatform.org/r/#/c/10/