Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
renoirb opened this issue Sep 11, 2014 · 1 comment

Comments

@renoirb
Copy link
Member

renoirb commented Sep 11, 2014

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

@renoirb
Copy link
Member Author

renoirb commented Sep 12, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant