Skip to content

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

Open
@renoirb

Description

@renoirb

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

  • Getting the "Readiness state" of the current page

  • Call the Api internally, without relying on cURL (!!) see Calling internally the MediaWiki API

  • Look which hooks to use, OutputPageParserOutput, ArticleAfterFetchContent?

  • Figure out most efficient place in MW execution flow to generate the full HTML. Found: In an instance of SkinTemplate

  • Quick and dirty way would be to str_replace($readiness_state_string, ' ', '_'); as is. (/)

  • Expected output:

    <div class="readiness-state Ready_to_Use"> 
        <p>This article is <a href="/wiki/Property:State">Ready to Use.</a></p>
    </div>

Notes

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

Reference

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions