1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14/**
15 *
16 */
17class RankLib extends TikiLib
18{
19	/**
20	 * @param $limit
21	 * @param array $categ
22	 * @param null $lang
23	 * @return mixed
24	 */
25	function wiki_ranking_top_pages($limit, $categ = [], $lang = null)
26	{
27		global $user, $prefs;
28		$pagesAdded = [];
29
30		$bindvals = [];
31		$mid = '';
32		if ($categ) {
33			$mid .= " INNER JOIN (`tiki_objects` as tob, `tiki_category_objects` as tco) ON (tp.`pageName` = tob.`itemId` and tob.`objectId` = tco.`catObjectId`) WHERE tob.`type` = 'wiki page' AND (tco.`categId` = ?";
34			$bindvals[] = $categ[0];
35			//FIXME
36			for ($i = 1, $icount_categ = count($categ); $i < $icount_categ; $i++) {
37				$mid .= " OR tco.`categId` = " . $categ[$i];
38			}
39			$mid .= ")";
40		}
41
42		$query = "select distinct tp.`pageName`, tp.`hits`, tp.`lang`, tp.`page_id` from `tiki_pages` tp $mid order by `hits` desc";
43
44		$result = $this->query($query, $bindvals);
45		$ret = [];
46		$count = 0;
47		while (($res = $result->fetchRow()) && $count < $limit) {
48			$perms = Perms::get(['type' => 'wiki page', 'object' => $res['pageName']]);
49			if ($perms->view) {
50				global $disableBestLang;
51				$disableBestLang = false;
52				if ($res['lang'] > '' && $prefs['feature_best_language'] == 'y') {
53					// find best language equivalent
54					$multilinguallib = TikiLib::lib('multilingual');
55					if ($multilinguallib->useBestLanguage()) {
56						$bestLangPageId = $multilinguallib->selectLangObj('wiki page', $res['page_id'], null, 'tiki_p_view');
57						if ($res['page_id'] != $bestLangPageId) {
58							$res['pageName'] = $this->get_page_name_from_id($bestLangPageId);
59						}
60					}
61				}
62				if ($prefs['feature_best_language'] != 'y' || ! $res['lang'] || empty($pagesAdded) || ! in_array($res['pageName'], $pagesAdded)) {
63					$aux['name'] = $res['pageName'];
64					$aux['hits'] = $res['hits'];
65					$aux['href'] = 'tiki-index.php?page=' . urlencode($res['pageName']);
66					if ($disableBestLang == true) {
67						$aux['href'] .= '&amp;bl=n';
68					}
69					$ret[] = $aux;
70					$pagesAdded[] = $res['pageName'];
71					++$count;
72				}
73			}
74		}
75
76		$retval["data"] = $ret;
77		$retval["title"] = tra("Wiki top pages");
78		$retval["y"] = tra("Hits");
79		$retval["type"] = "nb";
80		return $retval;
81	}
82
83	/**
84	 * @param $limit
85	 * @param array $categ
86	 * @return mixed
87	 */
88	function wiki_ranking_top_pagerank($limit, $categ = [])
89	{
90		global $user, $prefs;
91
92		$roll = mt_rand(1, (int) $prefs['wiki_ranking_reload_probability']);
93		if ($roll == 1) {
94			$this->pageRank();
95		}
96
97		$bindvals = [];
98		$mid = '';
99		if ($categ) {
100			$mid .= " INNER JOIN (`tiki_objects` as tob, `tiki_category_objects` as tco) ON (tp.`pageName` = tob.`itemId` and tob.`objectId` = tco.`catObjectId`) WHERE tob.`type` = 'wiki page' AND (tco.`categId` = ?";
101		//FIXME
102			$bindvals[] = $categ[0];
103			for ($i = 1, $icount_categ = count($categ); $i < $icount_categ; $i++) {
104				$mid .= " OR tco.`categId` = " . $categ[$i];
105			}
106			$mid .= ")";
107		}
108
109		$query = "select tp.`pageName`, tp.`pageRank` from `tiki_pages` tp $mid order by `pageRank` desc";
110
111		$result = $this->query($query, $bindvals);
112		$ret = [];
113		$count = 0;
114		while (($res = $result->fetchRow()) && $count < $limit) {
115			if ($this->user_has_perm_on_object($user, $res['pageName'], 'wiki page', 'tiki_p_view')) {
116				$aux['name'] = $res['pageName'];
117				$aux['hits'] = $res['pageRank'];
118				$aux['href'] = 'tiki-index.php?page=' . urlencode($res['pageName']);
119				$ret[] = $aux;
120				++$count;
121			}
122		}
123
124		$retval["data"] = $ret;
125		$retval["title"] = tra("Most-relevant pages");
126		$retval["y"] = tra("Relevance");
127		$retval["type"] = "nb";
128		return $retval;
129	}
130
131	/**
132	 * @param $limit
133	 * @param array $categ
134	 * @return mixed
135	 */
136	function wiki_ranking_last_pages($limit, $categ = [])
137	{
138		global $user, $prefs;
139
140		$bindvals = [];
141		$mid = '';
142		if ($categ) {
143			$mid .= " INNER JOIN (`tiki_objects` as tob, `tiki_category_objects` as tco) ON (tp.`pageName` = tob.`itemId` and tob.`objectId` = tco.`catObjectId`) WHERE tob.`type` = 'wiki page' AND (tco.`categId` = ?";
144			//FIXME
145			$bindvals[] = $categ[0];
146			for ($i = 1, $icount_categ = count($categ); $i < $icount_categ; $i++) {
147				$mid .= " OR tco.`categId` = " . $categ[$i];
148			}
149			$mid .= ")";
150		}
151
152		$query = "select tp.`pageName`, tp.`lastModif`, tp.`hits` from `tiki_pages` tp $mid order by `lastModif` desc";
153
154		$result = $this->query($query, $bindvals);
155		$ret = [];
156		$count = 0;
157		while (($res = $result->fetchRow()) && $count < $limit) {
158			if ($this->user_has_perm_on_object($user, $res['pageName'], 'wiki page', 'tiki_p_view')) {
159				$aux['name'] = $res['pageName'];
160				$aux['hits'] = $res['lastModif'];
161				$aux['href'] = 'tiki-index.php?page=' . urlencode($res['pageName']);
162				$ret[] = $aux;
163				++$count;
164			}
165		}
166
167		$retval["data"] = $ret;
168		$retval["title"] = tra("Wiki last pages");
169		$retval["y"] = tra("Modified");
170		$retval["type"] = "date";
171		return $retval;
172	}
173
174	/**
175	 * @param $limit
176	 * @param string $forumId
177	 * @param bool $last_replied
178	 * @return mixed
179	 */
180	function forums_ranking_last_replied_topics($limit, $forumId = '', $last_replied = true)
181	{
182		$retval = $this->forums_ranking_last_topics($limit, $forumId, $last_replied);
183		return $retval;
184	}
185
186	function forums_ranking_last_topics($limit, $forumId = '', $last_replied = false)
187	{
188		// $last_replied == true, means that topics shown will be based on last replied, not last created.
189		global $user;
190		if (is_array($forumId)) {
191			$bindvars = $forumId;
192			$mid = ' and a.`object` in (' . implode(',', array_fill(0, count($forumId), '?')) . ')';
193		} elseif (! empty($forumId)) {
194			$bindvars = [(int) $forumId];
195			$mid = ' and a.`object`=?';
196		} else {
197			$bindvars = [];
198			$mid = '';
199		}
200/*if ($last_replied == false)
201{	*/
202		$query = "select * from
203			`tiki_comments` a,`tiki_forums` tf where
204			`objectType` = 'forum' and
205			`parentId`=0 $mid order by `commentDate` desc";
206/*} else {
207$query = "select a.*, tf.*, max(b.`commentDate`) as `lastPost` from
208`tiki_comments` a left join `tiki_comments` b on b.`parentId`=a.`threadId` right join `tiki_forums` tf on "
209.$this->cast("tf.`forumId`","string")." = a.`object`".
210" where a.`objectType` = 'forum' and a.`parentId`=0 $mid group by a.`threadId` order by `lastPost` desc";
211}*/
212		$result = $this->query($query, $bindvars);
213		$ret = [];
214		$count = 0;
215		while (($res = $result->fetchRow()) && $count < $limit) {
216			if ($this->user_has_perm_on_object($user, $res['threadId'], 'thread', 'tiki_p_forum_read')) {
217				if ($mid == '') { // no forumId selected
218					$aux['name'] = $res['name'] . ': ' . $res['title']; //forum name plus topic
219				} else { // forumId selected
220					$aux['name'] = $res['title']; // omit forum name
221				}
222				$aux['title'] = $res['title'];
223				$aux['href'] = 'tiki-view_forum_thread.php?comments_parentId=' . $res['threadId'];
224				if ($last_replied == false) {
225					$aux['date'] = $res['commentDate'];
226					// the following line is correct, the second column named hits shows date
227					$aux['hits'] = $res['commentDate'];
228				} else {
229					$aux['date'] = $res['lastPost'];
230					$aux['hits'] = $res['lastPost'];
231				}
232				$aux['user'] = $res['userName'];
233				$ret[] = $aux;
234				++$count;
235			}
236		}
237		$retval["data"] = $ret;
238		$retval["title"] = tra("Forums last topics");
239		$retval["y"] = tra("Topic date");
240		$retval["type"] = "date";
241		return $retval;
242	}
243
244	/**
245	 * @param $limit
246	 * @param bool $toponly
247	 * @param string $forumId
248	 * @return mixed
249	 */
250	function forums_ranking_last_posts($limit, $toponly = false, $forumId = '')
251	{
252		global $user;
253		$offset = 0;
254		$count = 0;
255		$ret = [];
256		$result = TikiLib::lib('comments')->get_all_comments('forum', 0, $limit, 'commentDate_desc', '', '', '', $toponly, $forumId);
257		$result['data'] = Perms::filter(['type' => 'forum'], 'object', $result['data'], ['object' => 'object'], 'forum_read');
258		foreach ($result['data'] as $res) {
259			$aux['name'] = $res['title'];
260			$aux['title'] = $res['parentTitle'];
261			$tmp = $res['parentId'];
262			if ($tmp == 0) {
263				$tmp = $res['threadId'];
264			}
265			$aux['href'] = $res['href'];
266			$aux['hits'] = $this->get_long_datetime($res['commentDate']);
267			$tmp = $res['parentId'];
268			if ($tmp == 0) {
269				$tmp = $res['threadId'];
270			}
271			$aux['date'] = $res['commentDate'];
272			$aux['user'] = $res['userName'];
273			$ret[] = $aux;
274		}
275		$retval["data"] = $ret;
276		$retval["title"] = tra("Forums last posts");
277		$retval["y"] = tra("Topic date");
278		$retval["type"] = "date";
279		return $retval;
280	}
281
282	/**
283	 * @param $limit
284	 * @param string $forumId
285	 * @return mixed
286	 */
287	function forums_ranking_most_read_topics($limit, $forumId = '')
288	{
289		$result = TikiLib::lib('comments')->get_all_comments('forum', 0, $limit, 'hits_desc', '', '', '', true, $forumId);
290
291		$ret = [];
292		foreach ($result['data'] as $res) {
293			$aux['name'] = $forumId ? $res['title'] : $res['parentTitle'] . ': ' . $res['title'];
294				$aux['title'] = $res['title'];
295				$aux['hits'] = $res['hits'];
296				$aux['href'] = 'tiki-view_forum_thread.php?comments_parentId=' . $res['threadId'];
297				$ret[] = $aux;
298		}
299
300		$retval["data"] = $ret;
301		$retval["title"] = tra("Forums most-read topics");
302		$retval["y"] = tra("Reads");
303		$retval["type"] = "nb";
304		return $retval;
305	}
306
307	/**
308	 * @param $qty
309	 * @return mixed
310	 */
311	function forums_top_posters($qty)
312	{
313		$query = "select `user`, `posts` from `tiki_user_postings` order by " . $this->convertSortMode("posts_desc");
314		$result = $this->query($query, [], $qty);
315		$ret = [];
316
317		while ($res = $result->fetchRow()) {
318			$aux["name"] = $res["user"];
319			$aux["posts"] = $res["posts"];
320			$ret[] = $aux;
321		}
322		$retval["data"] = $ret;
323
324		return $retval;
325	}
326
327	/**
328	 * @param $limit
329	 * @return mixed
330	 */
331	function forums_ranking_top_topics($limit)
332	{
333		$ret = [];
334		$comments = TikiLib::lib('comments')->get_forum_topics(null, 0, $limit, 'average_desc');
335		foreach ($comments as $res) {
336			$aux = [];
337			$aux['name'] = $res['name'] . ': ' . $res['title'];
338			$aux['title'] = $res['title'];
339			$aux['hits'] = $res['average'];
340			$aux['href'] = 'tiki-view_forum_thread.php?comments_parentId=' . $res['threadId'];
341			$ret[] = $aux;
342		}
343
344		$retval["data"] = $ret;
345		$retval["title"] = tra("Forums best topics");
346		$retval["y"] = tra("Score");
347		$retval["type"] = "nb";
348		return $retval;
349	}
350
351	/**
352	 * @param $limit
353	 * @return mixed
354	 */
355	function forums_ranking_most_visited_forums($limit)
356	{
357		$result = TikiLib::lib('comments')->list_forums(0, $limit, 'hits_desc');
358		$ret = [];
359		$count = 0;
360		foreach ($result['data'] as $res) {
361			$aux['name'] = $res['name'];
362			$aux['hits'] = $res['hits'];
363			$aux['href'] = 'tiki-view_forum.php?forumId=' . $res['forumId'];
364			$ret[] = $aux;
365		}
366
367		$retval["data"] = $ret;
368		$retval["title"] = tra("Most-visited forums");
369		$retval["y"] = tra("Visits");
370		$retval["type"] = "nb";
371		return $retval;
372	}
373
374	/**
375	 * @param $limit
376	 * @return mixed
377	 */
378	function forums_ranking_most_commented_forum($limit)
379	{
380		$result = TikiLib::lib('comments')->list_forums(0, $limit, 'comments_desc');
381		$ret = [];
382		$count = 0;
383		foreach ($result['data'] as $res) {
384			$aux['name'] = $res['name'];
385			$aux['hits'] = $res['hits'];
386			$aux['href'] = 'tiki-view_forum.php?forumId=' . $res['forumId'];
387			$ret[] = $aux;
388		}
389
390		$retval["data"] = $ret;
391		$retval["title"] = tra("Forums with most posts");
392		$retval["y"] = tra("Posts");
393		$retval["type"] = "nb";
394		return $retval;
395	}
396
397	/**
398	 * @param $limit
399	 * @return mixed
400	 */
401	function gal_ranking_top_galleries($limit)
402	{
403		global $user;
404		$query = "select * from `tiki_galleries` where `visible`=? order by `hits` desc";
405
406		$result = $this->query($query, ['y']);
407		$ret = [];
408		$count = 0;
409		while (($res = $result->fetchRow()) && $count < $limit) {
410			if ($this->user_has_perm_on_object($user, $res['galleryId'], 'image gallery', 'tiki_p_view_image_gallery')) {
411				$aux['name'] = $res['name'];
412				$aux['hits'] = $res['hits'];
413				$aux['href'] = 'tiki-browse_gallery.php?galleryId=' . $res['galleryId'];
414				$ret[] = $aux;
415				++$count;
416			}
417		}
418
419		$retval["data"] = $ret;
420		$retval["title"] = tra("Wiki top galleries");
421		$retval["y"] = tra("Visits");
422		$retval["type"] = "nb";
423		return $retval;
424	}
425
426	/**
427	 * @param $limit
428	 * @return mixed
429	 */
430	function filegal_ranking_top_galleries($limit)
431	{
432		global $user;
433		$query = "select * from `tiki_file_galleries` where `visible`=? order by `hits` desc";
434
435		$result = $this->query($query, ['y'], $limit, 0);
436		$ret = [];
437		$count = 0;
438		while (($res = $result->fetchRow()) && $count < $limit) {
439			if ($this->user_has_perm_on_object($user, $res['galleryId'], 'file gallery', 'tiki_p_view_file_gallery')) {
440				$aux['name'] = $res['name'];
441				$aux['hits'] = $res['hits'];
442				$aux['href'] = 'tiki-list_file_gallery.php?galleryId=' . $res['galleryId'];
443				$ret[] = $aux;
444				++$count;
445			}
446		}
447
448		$retval["data"] = $ret;
449		$retval["title"] = tra("Wiki top file galleries");
450		$retval["y"] = tra("Visits");
451		$retval["type"] = "nb";
452		return $retval;
453	}
454
455	/**
456	 * @param $limit
457	 * @return mixed
458	 */
459	function gal_ranking_top_images($limit)
460	{
461		global $user;
462		$query = "select `imageId`, `name`, `hits`, `galleryId` from `tiki_images` order by `hits` desc";
463
464		$result = $this->query($query, [], $limit, 0);
465		$ret = [];
466
467		while ($res = $result->fetchRow()) {
468			if ($this->user_has_perm_on_object($user, $res['galleryId'], 'image gallery', 'tiki_p_view_image_gallery')) {
469				$aux["name"] = $res["name"];
470				$aux["hits"] = $res["hits"];
471				$aux["href"] = 'tiki-browse_image.php?imageId=' . $res["imageId"];
472				$ret[] = $aux;
473			}
474		}
475
476		$retval["data"] = $ret;
477		$retval["title"] = tra("Wiki top images");
478		$retval["y"] = tra("Hits");
479		$retval["type"] = "nb";
480		return $retval;
481	}
482
483	/**
484	 * @param $limit
485	 * @return mixed
486	 */
487	function filegal_ranking_top_files($limit)
488	{
489		global $user;
490		$query = "select `fileId`,`filename`,`hits`, `galleryId` from `tiki_files` order by `hits` desc";
491
492		$result = $this->query($query, [], $limit, 0);
493		$ret = [];
494
495		while ($res = $result->fetchRow()) {
496			if ($this->user_has_perm_on_object($user, $res['fileId'], 'file', 'tiki_p_view_file_gallery')) {
497				$aux["name"] = $res["filename"];
498				$aux["hits"] = $res["hits"];
499				$aux["href"] = 'tiki-download_file.php?fileId=' . $res["fileId"];
500				$ret[] = $aux;
501			}
502		}
503
504		$retval["data"] = $ret;
505		$retval["title"] = tra("Wiki top files");
506		$retval["y"] = tra("Downloads");
507		$retval["type"] = "nb";
508		return $retval;
509	}
510
511	/**
512	 * @param $limit
513	 * @return mixed
514	 */
515	function gal_ranking_last_images($limit)
516	{
517		global $user;
518		$query = "select `imageId`,`name`,`created`, `galleryId` from `tiki_images` order by `created` desc";
519
520		$result = $this->query($query, [], $limit, 0);
521		$ret = [];
522
523		while ($res = $result->fetchRow()) {
524			if ($this->user_has_perm_on_object($user, $res['galleryId'], 'image gallery', 'tiki_p_view_image_gallery')) {
525				$aux["name"] = $res["name"];
526				$aux["hits"] = $res["created"];
527				$aux["href"] = 'tiki-browse_image.php?imageId=' . $res["imageId"];
528				$ret[] = $aux;
529			}
530		}
531
532		$retval["data"] = $ret;
533		$retval["title"] = tra("Wiki most-recent images");
534		$retval["y"] = tra("Upload date");
535		$retval["type"] = "date";
536		return $retval;
537	}
538
539	/**
540	 * @param $limit
541	 * @return mixed
542	 */
543	function filegal_ranking_last_files($limit)
544	{
545		global $user;
546		$query = "select `fileId`,`filename`,`created`, `galleryId` from `tiki_files` order by `created` desc";
547
548		$result = $this->query($query, [], $limit, 0);
549		$ret = [];
550
551		while ($res = $result->fetchRow()) {
552			if ($this->user_has_perm_on_object($user, $res['fileId'], 'file', 'tiki_p_view_file_gallery')) {
553				$aux["name"] = $res["filename"];
554				$aux["hits"] = $res["created"];
555				$aux["href"] = 'tiki-download_file.php?fileId=' . $res["fileId"];
556				$ret[] = $aux;
557			}
558		}
559
560		$retval["data"] = $ret;
561		$retval["title"] = tra("Wiki most-recent files");
562		$retval["y"] = tra("Upload date");
563		$retval["type"] = "date";
564		return $retval;
565	}
566
567	/**
568	 * @param $limit
569	 * @return mixed
570	 */
571	function cms_ranking_top_articles($limit)
572	{
573		global $user;
574		$query = "select `tiki_articles`.*, `tiki_article_types`.`show_pre_publ` from `tiki_articles` inner join `tiki_article_types` on `tiki_articles`.`type` = `tiki_article_types`.`type` order by `nbreads` desc";
575
576		$result = $this->query($query, [], $limit, 0);
577		$ret = [];
578
579		while ($res = $result->fetchRow()) {
580			if ($this->user_has_perm_on_object($user, $res['articleId'], 'article', 'tiki_p_read_article') && ($res["show_pre_publ"] == 'y' or $this->now > $res["publishDate"])) {
581				$aux["name"] = $res["title"];
582				$aux["hits"] = $res["nbreads"];
583				$aux["href"] = 'tiki-read_article.php?articleId=' . $res["articleId"];
584				$ret[] = $aux;
585			}
586		}
587
588		$retval["data"] = $ret;
589		$retval["title"] = tra("Top Articles");
590		$retval["y"] = tra("Reads");
591		$retval["type"] = "nb";
592		return $retval;
593	}
594
595	/**
596	 * @param $limit
597	 * @return mixed
598	 */
599	function blog_ranking_top_blogs($limit)
600	{
601		global $user;
602		$query = "select * from `tiki_blogs` order by `hits` desc";
603
604		$result = $this->query($query, [], $limit, 0);
605		$ret = [];
606
607		while ($res = $result->fetchRow()) {
608			if ($this->user_has_perm_on_object($user, $res['blogId'], 'blog', 'tiki_p_read_blog')) {
609				$aux["name"] = $res["title"];
610				$aux["hits"] = $res["hits"];
611				$aux["href"] = 'tiki-view_blog.php?blogId=' . $res["blogId"];
612				$ret[] = $aux;
613			}
614		}
615
616		$retval["data"] = $ret;
617		$retval["title"] = tra("Most-visited blogs");
618		$retval["y"] = tra("Visits");
619		$retval["type"] = "nb";
620		return $retval;
621	}
622
623	/**
624	 * @param $limit
625	 * @return mixed
626	 */
627	function blog_ranking_top_active_blogs($limit)
628	{
629		global $user;
630		$query = "select * from `tiki_blogs` order by `activity` desc";
631
632		$result = $this->query($query, [], $limit, 0);
633		$ret = [];
634
635		while ($res = $result->fetchRow()) {
636			if ($this->user_has_perm_on_object($user, $res['blogId'], 'blog', 'tiki_p_read_blog')) {
637				$aux["name"] = $res["title"];
638				$aux["hits"] = $res["activity"];
639				$aux["href"] = 'tiki-view_blog.php?blogId=' . $res["blogId"];
640				$ret[] = $aux;
641			}
642		}
643
644		$retval["data"] = $ret;
645		$retval["title"] = tra("Most-active blogs");
646		$retval["y"] = tra("Activity");
647		$retval["type"] = "nb";
648		return $retval;
649	}
650
651	/**
652	 * @param $limit
653	 * @return mixed
654	 */
655	function blog_ranking_last_posts($limit)
656	{
657		global $user;
658		$query = "select * from `tiki_blog_posts` order by `created` desc";
659
660		$result = $this->query($query, [], $limit, 0);
661		$ret = [];
662
663		while ($res = $result->fetchRow()) {
664			if ($this->user_has_perm_on_object($user, $res['postId'], 'blog post', 'tiki_p_read_blog')) {
665				$q = "select `title` from `tiki_blogs` where `blogId`=?";
666
667				$name = $this->getOne($q, [$res["blogId"]]);
668				$aux["name"] = $name;
669				$aux["hits"] = $res["created"];
670				$aux["href"] = 'tiki-view_blog.php?blogId=' . $res["blogId"];
671				$ret[] = $aux;
672			}
673		}
674
675		$retval["data"] = $ret;
676		$retval["title"] = tra("Blogs last posts");
677		$retval["y"] = tra("Post date");
678		$retval["type"] = "date";
679		return $retval;
680	}
681
682	/**
683	 * @param $limit
684	 * @param array $categ
685	 * @return mixed
686	 */
687	function wiki_ranking_top_authors($limit, $categ = [])
688	{
689		global $user;
690
691		$bindvals = [];
692		$mid = '';
693		if ($categ) {
694			$mid .= " INNER JOIN (`tiki_objects` as tob, `tiki_category_objects` as tco) ON (tp.`pageName` = tob.`itemId` and tob.`objectId` = tco.`catObjectId`)
695				WHERE tob.`type` = 'wiki page'
696				AND (tco.`categId` = ?"
697			;
698
699			//FIXME
700			$bindvals[] = $categ[0];
701			for ($i = 1, $icount_categ = count($categ); $i < $icount_categ; $i++) {
702				$mid .= " OR tco.`categId` = " . $categ[$i];
703			}
704			$mid .= ")";
705		}
706		$query = "select distinct tp.`user`, count(*) as `numb` from `tiki_pages` tp $mid group by `user` order by " . $this->convertSortMode("numb_desc");
707
708		$result = $this->query($query, $bindvals, $limit, 0);
709		$ret = [];
710		$retu = [];
711
712		while ($res = $result->fetchRow()) {
713			$ret["name"] = $res["user"];
714			$ret["hits"] = $res["numb"];
715			$ret["href"] = "tiki-user_information.php?view_user=" . urlencode($res["user"]);
716			$retu[] = $ret;
717		}
718		$retval["data"] = $retu;
719		$retval["title"] = tra("Wiki top authors");
720		$retval["y"] = tra("Pages");
721		$retval["type"] = "nb";
722		return $retval;
723	}
724
725	/**
726	 * @param $limit
727	 * @return mixed
728	 */
729	function cms_ranking_top_authors($limit)
730	{
731		$query = "select distinct `author`, count(*) as `numb` from `tiki_articles` group by `author` order by " . $this->convertSortMode("numb_desc");
732
733		$result = $this->query($query, [], $limit, 0);
734		$ret = [];
735		$retu = [];
736
737		while ($res = $result->fetchRow()) {
738			$ret["name"] = $res["author"];
739			$ret["hits"] = $res["numb"];
740			$ret["href"] = "tiki-user_information.php?view_user=" . urlencode($res["author"]);
741			$retu[] = $ret;
742		}
743		$retval["data"] = $retu;
744		$retval["title"] = tra("Top article authors");
745		$retval["y"] = tra("Articles");
746		$retval["type"] = "nb";
747		return $retval;
748	}
749}
750$ranklib = new RankLib;
751