User talk:Badmadevil: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎Not Implemented Tasks by Language: regexp version, remove unsed http.js)
m (→‎Not Implemented Tasks by Language: php version, hopefully the final version)
Line 9: Line 9:
== Not Implemented Tasks by Language ==
== 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 [[User:IanOsgood|Ian Osgood]]'s [[Rosetta_Code:Wiki_Wishlist|wishlist]]), it seems no such page at Rosetta Code yet. I've coded a dirty hack of such page using client-side javascript. It use xmlhttprequest to get the contents of Solutions_by_Programming_Task & the language specified page that contains tasks such language has been completed, then by using RegExp, extracting links under some specify id/tag, the links of set different of these 2 page are tasks that not implemented.<br /><br />''Sources'':
Hello. IIRC, it has been mentioned an idea to display a page of tasks not implemented by a specific language (It is from [[User:IanOsgood|Ian Osgood]]'s [[Rosetta_Code:Wiki_Wishlist|wishlist]]), it seems no such page at Rosetta Code yet. I've coded a dirty hack of such page using PHP. <br /><br />This version should preserve Rosetta Code's pages look and feel, and may be better integrated into RC site.<br /><br />''Sources'':
{||-
{||-
| style="height:36em;float:left;clear:both;overflow:auto;" |
| style="height:36em;float:left;clear:both;overflow:auto;" |
<php><?php
&lt;html&gt;
&lt;title&gt;Not Implemented Task by Langauge&lt;/title&gt;
&lt;!--script src="/http.js"&gt;&lt;/script--&gt;
&lt;!--script src="http.js"&gt;&lt;/script--&gt;
&lt;script&gt;
<javascript>/**
** C o n f i g
**/
// javascript's XMLHTTPRequest doesn't allow cross site request,
// so this script's location's host has to be same as the Category pages.
// the prefix change to 'http://rosettacode.org/wiki/Category:' should be ok
//var mySitePrefix = 'http://www.lynx.ran/wiki/' ;
var mySitePrefix = 'http://rosettacode.org/wiki/Category:' ;


define('Prefix', 'http://www.lynx.ran/wiki/') ;
// set to true if mySitePrefix is inside rosetta site, should allow reach C++,
//define('Prefix', 'http://rosettacode.org/wiki/Category:') ;
// C# etc. pages.
define('SafeURL', FALSE) ;
//var usesafeurl = false ;
//define('SafeURL', TRUE) ;
var usesafeurl = true ;
define('SafePath', '/w/index.php?title=') ;
define('ROSETTA', 'http://rosettacode.org') ;


define('NoWarning', TRUE) ;
// this is maximum length of '<a .. /a>' plus <li></li>, Capital Heading's HTML
//define('NoWarning', FALSE) ;
// sources in next link capture string in o.getlink function. Currently, it is
// about 340 (found by trail and error).
// Modify this value if longer name of new task is added.
var preFetchLen = 400 ;


define('LANG', 'lang') ; // if use as a query string key-value pair
// To test in a loacl web server (eg. http://localhost):
define('NA', 'Not Avaliable') ;
// 1.mkdir wiki in root dir of localhost,
// save http://rosettacode.org/wiki/Category:<CATEGORYNAME> to
// localhost/wiki/<CATEGORYNAME>/index.htm,
// 2.<CATEGORYNAME> should at leat include Solutions_by_Programming_Task &
// Solutions_by_Programming_Language, then include some other languages
// categories, eg. C, Java, Python, etc. to start testing.
// 3.place http.js where this html can located,
// 4.no query string should be in url of this html,


define('TaskURL', Prefix . 'Solutions_by_Programming_Task') ;
var working = "<none>" ;
define('ListURL', Prefix . 'Solutions_by_Programming_Language') ;


var NA = "N/A" ;
$langID = LangId() ;
$langURL = LangURL($langID) ;
function Page(url, ID) {
var o = new Object ;
o.url = url ;
o.ID = ID ;
o.cnt = 0 ;
o.htm = NA ;
o.lnk = new Array() ;
o.request = function() { HTTP.getText(o.url, o.setHtm)}


// this control how to access this scrtpt
// NOTE:
$scriptURL = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] ;
// GLOBAL RegExp is not use, for I can't handle it right, or the performance is bad.
function ScriptURL($langId = NA){
o.setHtm = function(txt) {
global $langID, $scriptURL ;
var head = 0 ;
var tail = 0 ;
if($langId == NA) $langId = $langID ;
return $scriptURL . ($langId == NA ? "" : '?' . $langId) ;
working = o.url + '<br />';
}
head = txt.search(o.ID + '">') ;

if(head >= 0) {
// use only by the specifuc language page, use SafeURL to reach C++ pages
tail = txt.search('printfooter') ;
if(tail >= 0) {
function LangURL($langId = NA){
global $langID ;
o.htm = txt.slice(head + o.ID.length + 2,tail - 12) ;
o.getlink() ;
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 ;
}
}
}
}
}
}

o.getlink = function() {
if(o.htm != NA) {
function makeDict(&$taskList) {
var re = /href="(.*)"\s*title="(.*)"\s*>.+</ ;
$temp = array() ;
for($i = 0 ; $i < $this->lnk->length ; $i++) {
var start = 0 ;
$alink = $this->lnk->item($i)->getElementsByTagName("a")->item(0) ;
var head = 0 ;
var tail = 0. ;
$href = $alink->getAttribute("href") ;
var htmlen = o.htm.length ;
$temp[$href] = TRUE ;
}
while(start >= 0 && start < htmlen) {
for($i = 0 ; $i < $taskList->length ; $i++) {
var part = o.htm.slice(start, Math.min(start + preFetchLen, htmlen)) ;
$alink = $taskList->item($i)->getElementsByTagName("a")->item(0) ;
head = part.search('<a') ;
if(head >= 0) {
$href = $alink->getAttribute("href") ;
if(array_key_exists($href, $temp) == FALSE)
tail = part.search('a>') ;
$this->dict[$this->notCnt++] = $this->importNode($taskList->item($i),true) ;
if(tail >= 0) {
var mh = part.slice(head, tail).match(re) ;
if(mh) {
o.lnk[mh[1]] = mh[2] ;
o.cnt++ ;
}
}
}
if(head < 0 || tail < 0) start = -1 ; else start += (tail + 1) ;
}
}
}
}
}
return o ;
}
}
var scriptUrl ;
with(window.location)
scriptUrl = protocol + '//' + host + pathname ;
var rosettaUrl = 'http://rosettacode.org'
var langID = getLangID() ;
var langurl = langID != NA && usesafeurl ? safeurl() : mySitePrefix + langID ;
var task = Page(mySitePrefix + 'Solutions_by_Programming_Task', "mw-pages") ;
var lang = Page(langurl, "mw-pages") ;
var list = Page(mySitePrefix + 'Solutions_by_Programming_Language', "mw-subcategories") ;
var tasklist ;
var langlist ;
var timeoutCnt = 5 ;
var count = 0 ;


function getLangID() {
function process_task(){
global $task, $lang ;
var langid = window.location.search.slice(1) ;
if(langid.length == 0) return NA ;


if($lang->notCnt == 0) {
return langid ;
$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
function safeurl(langname) {
global $list ;
if(!langname) langname = langID ;
for($i = 0 ; $i < $list->lnk->length ; $i++) {
return rosettaUrl + '/w/index.php?title=' + langname ;
$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 process() {
function LangId(){
$langId = NA ;
tasklist = document.getElementById("tasklist") ;
if(count($_GET) > 0) {
langlist = document.getElementById("langlist") ;
if(array_key_exists(LANG, $_GET) && strlen($_GET[LANG]) > 0)
tasklist.innerHTML = "...loading..." ;
$langId = $_GET[LANG] ;
langlist.innerHTML = "...loading..." ;
else {
task.request() ;
$keys = array_keys($_GET) ;
list.request() ;
if(count($keys) > 0 && strlen($keys[0]) > 0)
if(langID != NA) {
lang.request() ;
$langId = $keys[0] ;
}
document.title = 'Not Implemented Tasks by Langauge ' + langID ;
}
}
waitLoad() ;
return $langId ;
}
}

function waitLoad() {
/*
timeoutCnt-- ;
* Main Body
// tasklist.innerHTML = makeStatus() + '<br />...loading ' + working + 'timeout in '
*/
// + timeoutCnt + ' sec...' ;

tasklist.innerHTML = '...loading ' + working + 'timeout in ' + timeoutCnt + ' sec...' ;
if(NoWarning) {
if(timeoutCnt > 0 &&
$error_flag = ini_get("error_reporting") ;
((langID != NA && lang.htm == NA) || task.htm == NA || list.htm == NA))
ini_set("error_reporting",$error_flag & ~(E_WARNING|E_NOTICE) ) ;
return setTimeout(waitLoad,1000) ;
if(langID == NA)
return failLoad() ;
if(timeoutCnt <= 0)
return failLoad() ;
if(task.cnt == 0 || list.cnt == 0 || lang.cnt == 0) return failLoad() ;
doParse() ;
}
}
function failLoad() { // this part may be simplified to display just a short err message
var errmsg = "" ;
if(lang.htm == NA) { // may be not known language
if(langID == NA)
errmsg += 'Language ID not known.' ;
else
errmsg += 'Error Page - url : ' +
alink(scriptUrl + '?' + langID,langID,langID, true) ;
}
errmsg += '<br />' ;
with(lang) if(htm == NA)
errmsg += 'fail to load this url : ' + alink(url, url, url) + '<br />' ;
with(list) if(htm == NA)
errmsg += 'fail to load this url : ' + alink(url, url, url) + '<br />' ;
with(task) if(htm == NA)
errmsg += 'fail to load this url : ' + alink(url, url, url) + '<br />' ;
tasklist.innerHTML = errmsg + makeStatus() ;


if($langID != NA) {
var taskdom = ""
$lang = new Page($langURL, 'mw-pages',TRUE) ;
if(task.cnt > 0 && lang.cnt > 0) taskdom = makeTask() ;
try {
if($lang->ok) {
if(taskdom == "") taskdom = task.htm ;
$task = new Page(TaskURL, 'mw-pages') ;
} catch(e) {
if($task->ok)
$lang->makeDict($task->lnk) ;
taskdom = "" ;
}
}
if(taskdom != "")
taskdom = makeMenu() + '<br />' + taskdom ;
else
taskdom = makeMenu() + '<br />Tasks Page not found/load.' ;
var listdom = "" ;
if(list.cnt >0) listdom = makeList();
try {
if(listdom == "") listdom = list.htm ;
} catch(e) {
listdom = "" ;
}
if(listdom != "")
listdom = '<hr />' + makeReturn() + '<hr />' + listdom ;
else
listdom = '<hr />' + makeReturn() + '<hr />Languages List Page not found/load.' ;
langlist.innerHTML = taskdom + listdom ;
}
}
function doParse() {
var taskdom = makeTask() ;
tasklist.innerHTML = makeMenu() + '<h2>Tasks not implemented by ' + alink(rosettaUrl +
'/wiki/' + langID, langID,langID, true) + '</h2><hr /><br />There are <strong>' +
count + '</strong> not implemented tasks out of <strong>' + task.cnt +
'</strong> total tasks. ' + '(' + lang.cnt +
' completed tasks, implementations & puzzles etc.)<br />' + taskdom ;
langlist.innerHTML = makeReturn() + '<hr />' + makeList() ;
}
function alink(url, tip, txt, normalsize, nameanchor) {
var extra = normalsize ? "" : ' style="font-size:smaller;"';
extra += nameanchor ? ' name="' + nameanchor + '"' : "" ;
return '<a href="' + url + '" title="' + tip + '"' + extra + '>' +
unescape(txt) + '</a>' ;
}
function makeMenu() {
var backLink ;
if(langID == NA) {
backLink = alink(list.url, "Solution by Languages",
'Return to Category : Solution by Languages') ;
} else {
backLink = alink(safeurl(), safeurl(), 'Return to Category : ' + langID)
}
var tx = 'otherlanguages' ;
return alink('#' + tx, tx, 'Select other languages', true, 'returntop') +
'&nbsp;&nbsp;|&nbsp;&nbsp;' + backLink + '<hr />' ;
}
function makeReturn() {
var tx = 'returntop' ;
// return alink('#' + tx, tx, 'TOP', true, 'otherlanguages') +
// '&nbsp;&nbsp;|&nbsp;&nbsp;' + makeStatus() ;
return alink('#' + tx, tx, 'TOP', true, 'otherlanguages') ;
}
function makeStatus() {
return '(total&nbsp;/&nbsp;langs&nbsp;/&nbsp;taskss) - pages length : ' +
task.htm.length + '&nbsp;/&nbsp;' + list.htm.length + '&nbsp;/&nbsp;' +
lang.htm.length + '&nbsp;&nbsp;|&nbsp;&nbsp;link count : ' + task.cnt +
'&nbsp;/&nbsp;' + list.cnt + '&nbsp;/&nbsp;' + lang.cnt ;
}
function makeList() {
var backLink ;
if(langID == NA) {
backLink = alink(list.url, "Solution by Languages",
'&#8251') ;
} else {
backLink = alink(safeurl(), langID, '&#8251')
}
if(list.cnt == 0) return "" ;
var html = '<h3>Not Implemented Tasks by Other Languages (<span style=' +
'"font-size:78%;">click ' + backLink + ' to visit Normal Category' +
'</span>) :</h3><table width="100%"><tr>' ;
splitCnt = 0 ;
var cols = 6 ;
var wd = 'width="' + Math.floor(100/cols) + '%"' ;
for(i in list.lnk)
if(list.lnk[i]){
var txt = list.lnk[i] ;
var uri = i ;
var langTag = txt.split(':') ;
var langUri = uri.split(':') ;
if(langTag.length == 2) {
if(splitCnt >= cols) {
splitCnt = 1 ;
html += '</tr><tr>' ;
} else
splitCnt++ ;
var langId = langTag[1] ;
var langUr = langUri[1] ;
html += '<td ' + wd + '>' + alink(safeurl(langUr), txt,"&#8251") + '&nbsp;' +
alink(scriptUrl + '?' + langUr, langId,langId) + '</td>' ;
}
}
html += '</tr></table>' ;
return html ;
}
function makeTask() {
var html = '<table width="100%"><tr><td valign="top" width="33%">' ;
var capital = "?" ;
var heading ;
for(i in task.lnk)
if(!lang.lnk[i]) count++ ;
if(count > 0) {
var splitCnt = 0 ;
for(i in task.lnk)
if(!lang.lnk[i]) {
splitCnt++ ;
var lnk = rosettaUrl + i ;
var txt = task.lnk[i] ;
heading = capital ;
capital = txt.charAt(0) ;
if(splitCnt*3 > count + 2) {
splitCnt = 0 ;
html += '</ul></td><td valign="top" width="33%">'
if(capital == heading)
html += '<h3>' + heading + ' cont.<h3><ul>' ;
}
if(heading.charAt(0) != capital.charAt(0)) {
if(heading != "?")
html += '</ul>'
html += '<h3>' + capital + '<h3><ul>'
}
html += '<li>' + alink(lnk, txt, txt) + '</li>' ;
}
html += '</ul>' ;
} else
return "" ;
html += '</td></tr></table><br />' ;
return html ;
}</javascript>
&lt;/script&gt;
&lt;!-- from http://examples.oreilly.com/jscript5/ , unused parts removed--&gt;
&lt;script&gt;
<javascript>/**
* http.js: utilities for scripted HTTP requests
*
* From the book JavaScript: The Definitive Guide, 5th Edition,
* by David Flanagan. Copyright 2006 O'Reilly Media, Inc. (ISBN: 0596101996)
*/


if($langID != NA && $lang->ok && $task->ok) {
// Make sure we haven't already been loaded
$lang->hdr->nodeValue = 'Not Completed Tasks by ' . $langID ;
var HTTP;
if (HTTP && (typeof HTTP != "object" || HTTP.NAME))
throw new Error("Namespace 'HTTP' already exists");


process_task() ;
// Create our namespace, and specify some meta-information
HTTP = {};
HTTP.NAME = "HTTP"; // The name of this namespace
HTTP.VERSION = 1.0; // The version of this namespace


echo $lang->saveHTML() ;
// This is a list of XMLHttpRequest creation factory functions to try
} else {
HTTP._factories = [
$list = new Page(ListURL, 'mw-subcategories') ;
function() { return new XMLHttpRequest(); },
if($list->ok) {
function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
if($langID == NA)
function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
$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() ;
// When we find a factory that works, store it here
HTTP._factory = null;


echo $list->saveHTML() ;
/**
* Create and return a new XMLHttpRequest object.
*
* The first time we're called, try the list of factory functions until
* we find one that returns a nonnull value and does not throw an
* exception. Once we find a working factory, remember it for later use.
*/
HTTP.newRequest = function() {
if (HTTP._factory != null)
return HTTP._factory();

for(var i = 0; i < HTTP._factories.length; i++) {
try {
var factory = HTTP._factories[i];
var request = factory();
if (request != null) {
HTTP._factory = factory;
return request;
}
}
catch(e) {
continue;
}
}
}
else

echo '
// If we get here, none of the factory candidates succeeded,
<html><body>
// so throw an exception now and for all future calls.
<h3>Unknown Error,
HTTP._factory = function() {
<a href="' . ROSETTA . '">Return Rosetta Code Main Page</a> or
throw new Error("XMLHttpRequest not supported");
<a href="javascript:history.go(-1) ;">Go Back</a>.
}
</h3>
HTTP._factory(); // Throw an error
</body></html>' ;
}
}


if(NoWarning) ini_set("error_reporting",$error_flag) ;
/**
?></php>
* Use XMLHttpRequest to fetch the contents of the specified URL using
* an HTTP GET request. When the response arrives, pass it (as plain
* text) to the specified callback function.
*
* This function does not block and has no return value.
*/
HTTP.getText = function(url, callback) {
var request = HTTP.newRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200)
callback(request.responseText);
}
request.open("GET", url);
request.send(null);
};</javascript>
&lt;/script&gt;
&lt;body onLoad="process();" &gt;
&lt;table width="100%"&gt;
&lt;tr&gt;&lt;td valign=top&gt;&lt;div id="tasklist"&gt;
&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;hr /&gt;&lt;td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td valign=top&gt;&lt;div id="langlist"&gt;
&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
|}
|}
Hope it can be a temporary solution. -- [[User:Badmadevil|badmadevil]] 02:52, 14 June 2008 (MDT)
Hope it can be a temporary solution. -- [[User:Badmadevil|badmadevil]] 02:52, 14 June 2008 (MDT)

Revision as of 17:08, 15 June 2008

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)