User talk:Badmadevil

From Rosetta Code
Revision as of 17:08, 15 June 2008 by rosettacode>Badmadevil (→‎Not Implemented Tasks by Language: php version, hopefully the final version)

Welcome to Rosetta Code! I founded RC around a year ago, and still do most of the administrative work. Thanks for your code contributions. I'm curious, though, how did you hear about RC? --Short Circuit 10:44, 23 February 2008 (MST)

Hi, thanks. I heard from a forward message posted at Digital Mars D newsgroup about a month ago. -badmadevil 03:03, 24 February 2008 (MST)

Mylang

Good job with the mylang templates. I had done as well as I could before, but these are better. --Mwn3d 13:52, 22 March 2008 (MDT)

thank for appreciation :) --badmadevil 07:54, 23 March 2008 (MDT)

Not Implemented Tasks by Language

Hello. IIRC, it has been mentioned an idea to display a page of tasks not implemented by a specific language (It is from Ian Osgood's wishlist), it seems no such page at Rosetta Code yet. I've coded a dirty hack of such page using PHP.

This version should preserve Rosetta Code's pages look and feel, and may be better integrated into RC site.

Sources:

<php><?php

define('Prefix', 'http://www.lynx.ran/wiki/') ; //define('Prefix', 'http://rosettacode.org/wiki/Category:') ; define('SafeURL', FALSE) ; //define('SafeURL', TRUE) ; define('SafePath', '/w/index.php?title=') ; define('ROSETTA', 'http://rosettacode.org') ;

define('NoWarning', TRUE) ; //define('NoWarning', FALSE) ;

define('LANG', 'lang') ; // if use as a query string key-value pair define('NA', 'Not Avaliable') ;

define('TaskURL', Prefix . 'Solutions_by_Programming_Task') ; define('ListURL', Prefix . 'Solutions_by_Programming_Language') ;

$langID = LangId() ; $langURL = LangURL($langID) ;

// this control how to access this scrtpt $scriptURL = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] ; function ScriptURL($langId = NA){

 global $langID, $scriptURL ;
 if($langId == NA) $langId = $langID ;
 return $scriptURL . ($langId == NA ? "" : '?' . $langId) ;

}

// use only by the specifuc language page, use SafeURL to reach C++ pages function LangURL($langId = NA){

 global $langID ;
 if($langId == NA) $langId = $langID ;
 if($langId == NA) 
   return "http://" ; // if $langID is NA, this url will not be used anyway.
 return SafeURL ? ROSETTA . SafePath . $langId : Prefix . $langId ;

}

class Page extends DOMDocument {

 public $lnk = NULL ;
 public $hdr, $msg , $tds ;
 public $dict = array() ;
 public $ok = FALSE ;
 public $cnt , $notCnt = 0 ; // $notCnt = count of not implemented tasks
 function __construct($url,$id,$isLangPage = FALSE) {
   if($this->loadHTMLFile($url)) {
     $context = $this->getElementById($id) ;
     $xpath = new DOMXPath($this) ;
     $this->lnk = $xpath->query('table/tr/td/ul/li', $context) ;
     if($this->lnk && $this->lnk->length > 0) {
       $this->hdr = $xpath->query('h2', $context)->item(0) ;
       $this->msg = $xpath->query('p', $context)->item(0) ;
       if($isLangPage)
         $this->tds = $xpath->query('table/tr/td', $context) ;
       $this->cnt = $this->lnk->length ;
       $this->ok = TRUE ;
     }
   }
 }
 function makeDict(&$taskList) {
   $temp = array() ;
   for($i = 0 ; $i < $this->lnk->length ; $i++) {
     $alink = $this->lnk->item($i)->getElementsByTagName("a")->item(0) ;
     $href = $alink->getAttribute("href") ;
     $temp[$href] = TRUE ;
   }
   for($i = 0 ; $i < $taskList->length ; $i++) {
     $alink = $taskList->item($i)->getElementsByTagName("a")->item(0) ;
     $href = $alink->getAttribute("href") ;
     if(array_key_exists($href, $temp) == FALSE) 
       $this->dict[$this->notCnt++] = $this->importNode($taskList->item($i),true) ;
   }
 }

}

function process_task(){

 global $task, $lang ;
 if($lang->notCnt == 0) {
   $lang->msg->nodeValue =
     "All {$task->cnt} simpler tasks are completed ( {$lang->cnt} " .
     "total completed tasks, some implementations and puzzles may be " .
     "not yet completed )." ;
 } else {
   $lang->msg->nodeValue =
     "There are {$lang->notCnt} not completed tasks " .
     "out of {$task->cnt} total tasks ( {$lang->cnt} " .
     "completed tasks, implementations and puzzles etc.)." ;
   
   $curr = 0 ;
   $splitMax = intval(($lang->notCnt + 2) / 3) ;
   if($splitMax < 5) $splitMax = 5 ;
   
   $capital = '?' ;  
   $heading =  ;
   
   $lang->lnk = null ; // release node ref. to be remove
   for($i = 0 ; $i < $lang->tds->length ; $i++) {
     $td = $lang->tds->item($i) ;      
     // clear old content
     while ($td->childNodes->length)
       $td->removeChild($td->childNodes->item(0));
       
     $td->setAttribute("width", "33%") ;
     $ul = NULL ;        
     $splitCnt = 0 ;
     while($curr < $lang->notCnt && $splitCnt < $splitMax) {
       $li = $lang->dict[$curr] ;
       $heading = $capital ;
       $title = $li->getElementsByTagName("a")->item(0)->getAttribute("title") ;       
       $capital = strtoupper(substr($title, 0, 1)) ;
       if($splitCnt == 0) {
         if($capital == $heading) 
           $h3 = $lang->createElement("h3", $capital . " cont.") ;
         else
           $h3 = $lang->createElement("h3", $capital) ;
         $td->appendChild($h3) ;
         $ul = $lang->createElement("ul") ;
       } else if($capital != $heading) {
         $td->appendChild($ul) ;
         $h3 = $lang->createElement("h3", $capital) ;
         $td->appendChild($h3) ;           
         $ul = $lang->createElement("ul") ;
       }
       $ul->appendChild($li) ;
       $curr++ ;
       $splitCnt++ ;
     }     
     if($ul) $td->appendChild($ul) ;                       
   } 
 }

}

function process_list(){ // replace languages link with corresponding scriptURL

 global $list ;
 for($i = 0 ; $i < $list->lnk->length ; $i++) {
   $alink = $list->lnk->item($i)->getElementsByTagName("a")->item(0) ;
   $href = $alink->getAttribute("href") ;
   $idLang = substr($href, 1 + strrpos($href,":")) ;
   $alink->setAttribute("href", ScriptURL($idLang)) ;
 }

}

function LangId(){

 $langId = NA ;
 if(count($_GET) > 0) {
   if(array_key_exists(LANG, $_GET) && strlen($_GET[LANG]) > 0)
     $langId = $_GET[LANG] ;
   else {
     $keys = array_keys($_GET) ;
     if(count($keys) > 0 && strlen($keys[0]) > 0)
       $langId = $keys[0] ;
   }
 }
 return $langId ;

}

/*

*   Main Body
*/

if(NoWarning) {

 $error_flag = ini_get("error_reporting") ;
 ini_set("error_reporting",$error_flag & ~(E_WARNING|E_NOTICE) ) ;

}

if($langID != NA) {

 $lang = new Page($langURL, 'mw-pages',TRUE) ;
 if($lang->ok) {
   $task = new Page(TaskURL, 'mw-pages') ;
   if($task->ok)
     $lang->makeDict($task->lnk) ;
 }

}

if($langID != NA && $lang->ok && $task->ok) {

 $lang->hdr->nodeValue = 'Not Completed Tasks by ' . $langID ;
 process_task() ;
 echo $lang->saveHTML() ;

} else {

 $list = new Page(ListURL, 'mw-subcategories') ;
 if($list->ok) {
   if($langID == NA)
     $list->hdr->nodeValue = 'Not Completed Tasks by Other Languages' ;
   else
     $list->hdr->nodeValue = 'Not Completed Tasks by Other Languages ( "'
       . $langID . '" page not found )' ;
   process_list() ;
   echo $list->saveHTML() ;
 }
 else 
   echo '
   <html><body>

Unknown Error, <a href="' . ROSETTA . '">Return Rosetta Code Main Page</a> or <a href="javascript:history.go(-1) ;">Go Back</a>.

   </body></html>' ;

}

if(NoWarning) ini_set("error_reporting",$error_flag) ; ?></php>

Hope it can be a temporary solution. -- badmadevil 02:52, 14 June 2008 (MDT)