<?
/*
NameStats v 1.2 © 2007 Nikola Smolenski <smolensk@eunet.yu>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

include("include.php");

head();

?><p>This tool shows frequency of first or last words in article names, or of disambiguation keywords.</p>
<div style="float: right;"><a href="namestats.php?view_source">view source</a> <a href="include.php?view_source">view include.php source</a></div>
<form><table>
<tr>
	<td colspan="2">
	<input type="submit" name="disambig" value="Disambiguation stats"/>
</tr><tr>
	<td align="right">Stats:</td>
	<td>First <input type="radio" name="fl" value="f"<? echo $_GET['fl']=="f"?" checked":""; ?> />
	Last <input type="radio" name="fl" value="l"<? echo $_GET['fl']=="l"?" checked":""; ?> /></td>
</tr><tr>
	<td align="right">Language:</td>
	<td><? printselect("lang",$lang,$_GET['lang']); ?></td>
</tr><tr>
	<td align="right">Wiki:</td>
	<td><? printselect("wiki",$wiki,$_GET['wiki']); ?></td>
</tr><tr>
	<td><input type="submit"/></td>
	<td><input type="reset"/></td>
</tr>
</table></form><?

if(isset($_GET['disambig']) || (
   ($_GET['fl']=="f" || $_GET['fl']=="l") &&
   isset($_GET['lang']) && isset($lang[$_GET['lang']]) && 
   isset($_GET['wiki']) && isset($wiki[$_GET['wiki']]))) {

	$qry="";
	if(isset($_GET['disambig'])) {
		$qry="SELECT CONCAT('(',REPLACE(SUBSTRING_INDEX(page_title,'(',-1),'_',' ')) AS word,COUNT(*) AS cnt FROM page WHERE page_namespace=0 AND page_title LIKE '%)' GROUP BY word ORDER BY cnt DESC";
		$pat="[(]#[)]$";
	} else {
		if($_GET['fl']=="f") {
			$qry="SELECT SUBSTRING_INDEX(page_title,'_',1) AS word,COUNT(*) AS cnt FROM page WHERE page_namespace=0 AND page_title LIKE '%\\_%' GROUP BY word ORDER BY cnt DESC";
			$pat="^#";
		} else {
			$qry="SELECT REVERSE(SUBSTRING_INDEX(REVERSE(page_title),'_',1)) AS word,COUNT(*) AS cnt FROM page WHERE page_namespace=0 AND page_title LIKE '%\\_%' AND page_title NOT LIKE '%)' GROUP BY word ORDER BY cnt DESC";
			$pat="#$";
		}
	}
	mysql_connect("sql","nikola",$mysql_pass);
	mysql_select_db("toolserver");
	$res=mysql_query("SELECT server FROM wiki WHERE lang='$_GET[lang]' AND family='$_GET[wiki]'");
	if(mysql_num_rows($res)) {
		$wiki=($_GET['wiki']=="wikipedia")?"wiki":$_GET['wiki'];
		$r=mysql_fetch_assoc($res);
		mysql_close();
		mysql_connect("sql-s$r[server]","nikola",$mysql_pass);
		mysql_select_db("$_GET[lang]{$wiki}_p");
		$res=mysql_query($qry);
		echo mysql_error();
		echo "<table border=\"1\">\n";
		while($r=mysql_fetch_assoc($res)) {
			echo "<tr><td><a href=\"grep.php?pattern=".rawurlencode(strtr($pat,array("#"=>$r['word'])))."&lang=$_GET[lang]&wiki=$_GET[wiki]\">".htmlspecialchars($r[word])."</a></td><td>$r[cnt]</td></tr>\n";
// http://tools.wikimedia.de/~interiot/cgi-bin/queries/grep?dbname=srwiki_p&namespace=0&type=LIKE&pattern=%25%D1%82%D0%B5%D1%80%D0%B0%D0%BF%D0%B8%D1%98%D0%B0
		}
		echo "</table>\n";
	} else {
		echo "No such project!";
	}
}

foot();
?>