Merge pull request #6017 from MrPetovan/task/move-pager-out-of-app

Road to Router Part 5: Move pager out of App
This commit is contained in:
Michael Vogel 2018-10-25 21:21:23 +02:00 committed by GitHub
commit dbfe5c9d2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 576 additions and 426 deletions

View file

@ -1069,45 +1069,3 @@ function validate_include(&$file)
// Simply return flag
return $valid;
}
/**
* @brief Get the data which is needed for infinite scroll
*
* For invinite scroll we need the page number of the actual page
* and the the URI where the content of the next page comes from.
* This data is needed for the js part in main.js.
* Note: infinite scroll does only work for the network page (module)
*
* @param string $module The name of the module (e.g. "network")
* @return array Of infinite scroll data
* 'pageno' => $pageno The number of the actual page
* 'reload_uri' => $reload_uri The URI of the content we have to load
*/
function infinite_scroll_data($module)
{
if (PConfig::get(local_user(), 'system', 'infinite_scroll')
&& $module == 'network'
&& defaults($_GET, 'mode', '') != 'minimal'
) {
// get the page number
$pageno = defaults($_GET, 'page', 1);
$reload_uri = "";
// try to get the uri from which we load the content
foreach ($_GET as $param => $value) {
if (($param != "page") && ($param != "q")) {
$reload_uri .= "&" . $param . "=" . urlencode($value);
}
}
$a = get_app();
if ($a->page_offset != "" && !strstr($reload_uri, "&offset=")) {
$reload_uri .= "&offset=" . urlencode($a->page_offset);
}
$arr = ["pageno" => $pageno, "reload_uri" => $reload_uri];
return $arr;
}
}

View file

@ -6,6 +6,7 @@
use Friendica\App;
use Friendica\Content\ContactSelector;
use Friendica\Content\Feature;
use Friendica\Content\Pager;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Addon;
use Friendica\Core\Config;
@ -433,8 +434,8 @@ function conv_get_blocklist()
* that are based on unique features of the calling module.
*
*/
function conversation(App $a, array $items, $mode, $update, $preview = false, $order = 'commented', $uid = 0) {
function conversation(App $a, array $items, Pager $pager, $mode, $update, $preview = false, $order = 'commented', $uid = 0)
{
$ssl_state = (local_user() ? true : false);
$profile_owner = 0;
@ -469,7 +470,7 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
. ((x($_GET, 'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
. ((x($_GET, 'file')) ? '&file=' . $_GET['file'] : '')
. "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
. "'; var profile_page = " . $pager->getPage() . "; </script>\r\n";
}
} elseif ($mode === 'profile') {
$items = conversation_add_children($items, false, $order, $uid);
@ -488,7 +489,7 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
$live_update_div = '<div id="live-profile"></div>' . "\r\n"
. "<script> var profile_uid = " . $a->profile['profile_uid']
. "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
. "; var netargs = '?f='; var profile_page = " . $pager->getPage() . "; </script>\r\n";
}
}
} elseif ($mode === 'notes') {
@ -498,7 +499,7 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
if (!$update) {
$live_update_div = '<div id="live-notes"></div>' . "\r\n"
. "<script> var profile_uid = " . local_user()
. "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
. "; var netargs = '/?f='; var profile_page = " . $pager->getPage() . "; </script>\r\n";
}
} elseif ($mode === 'display') {
$items = conversation_add_children($items, false, $order, $uid);
@ -516,7 +517,7 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
if (!$update) {
$live_update_div = '<div id="live-community"></div>' . "\r\n"
. "<script> var profile_uid = -1; var netargs = '" . substr($a->cmd, 10)
."/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
."/?f='; var profile_page = " . $pager->getPage() . "; </script>\r\n";
}
} elseif ($mode === 'contacts') {
$items = conversation_add_children($items, true, $order, $uid);
@ -525,7 +526,7 @@ function conversation(App $a, array $items, $mode, $update, $preview = false, $o
if (!$update) {
$live_update_div = '<div id="live-contacts"></div>' . "\r\n"
. "<script> var profile_uid = -1; var netargs = '" . substr($a->cmd, 9)
."/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
."/?f='; var profile_page = " . $pager->getPage() . "; </script>\r\n";
}
} elseif ($mode === 'search') {
$live_update_div = '<div id="live-search"></div>' . "\r\n";

View file

@ -262,132 +262,6 @@ function unxmlify($s) {
return $ret;
}
/**
* @brief Paginator function. Pushes relevant links in a pager array structure.
*
* Links are generated depending on the current page and the total number of items.
* Inactive links (like "first" and "prev" on page 1) are given the "disabled" class.
* Current page link is given the "active" CSS class
*
* @param App $a App instance
* @param int $count [optional] item count (used with minimal pager)
* @return Array data for pagination template
*/
function paginate_data(App $a, $count = null) {
$stripped = preg_replace('/([&?]page=[0-9]*)/', '', $a->query_string);
$stripped = str_replace('q=', '', $stripped);
$stripped = trim($stripped, '/');
$pagenum = $a->pager['page'];
if (($a->page_offset != '') && !preg_match('/[?&].offset=/', $stripped)) {
$stripped .= '&offset=' . urlencode($a->page_offset);
}
$url = $stripped;
$data = [];
function _l(&$d, $name, $url, $text, $class = '') {
if (strpos($url, '?') === false && ($pos = strpos($url, '&')) !== false) {
$url = substr($url, 0, $pos) . '?' . substr($url, $pos + 1);
}
$d[$name] = ['url' => $url, 'text' => $text, 'class' => $class];
}
if (!is_null($count)) {
// minimal pager (newer / older)
$data['class'] = 'pager';
_l($data, 'prev', $url . '&page=' . ($a->pager['page'] - 1), L10n::t('newer'), 'previous' . ($a->pager['page'] == 1 ? ' disabled' : ''));
_l($data, 'next', $url . '&page=' . ($a->pager['page'] + 1), L10n::t('older'), 'next' . ($count <= 0 ? ' disabled' : ''));
} else {
// full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
$data['class'] = 'pagination';
if ($a->pager['total'] > $a->pager['itemspage']) {
_l($data, 'first', $url . '&page=1', L10n::t('first'), $a->pager['page'] == 1 ? 'disabled' : '');
_l($data, 'prev', $url . '&page=' . ($a->pager['page'] - 1), L10n::t('prev'), $a->pager['page'] == 1 ? 'disabled' : '');
$numpages = $a->pager['total'] / $a->pager['itemspage'];
$numstart = 1;
$numstop = $numpages;
// Limit the number of displayed page number buttons.
if ($numpages > 8) {
$numstart = (($pagenum > 4) ? ($pagenum - 4) : 1);
$numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 8));
}
$pages = [];
for ($i = $numstart; $i <= $numstop; $i++) {
if ($i == $a->pager['page']) {
_l($pages, $i, '#', $i, 'current active');
} else {
_l($pages, $i, $url . '&page='. $i, $i, 'n');
}
}
if (($a->pager['total'] % $a->pager['itemspage']) != 0) {
if ($i == $a->pager['page']) {
_l($pages, $i, '#', $i, 'current active');
} else {
_l($pages, $i, $url . '&page=' . $i, $i, 'n');
}
}
$data['pages'] = $pages;
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
_l($data, 'next', $url . '&page=' . ($a->pager['page'] + 1), L10n::t('next'), $a->pager['page'] == $lastpage ? 'disabled' : '');
_l($data, 'last', $url . '&page=' . $lastpage, L10n::t('last'), $a->pager['page'] == $lastpage ? 'disabled' : '');
}
}
return $data;
}
/**
* Automatic pagination.
*
* To use, get the count of total items.
* Then call $a->set_pager_total($number_items);
* Optionally call $a->set_pager_itemspage($n) to the number of items to display on each page
* Then call paginate($a) after the end of the display loop to insert the pager block on the page
* (assuming there are enough items to paginate).
* When using with SQL, the setting LIMIT %d, %d => $a->pager['start'],$a->pager['itemspage']
* will limit the results to the correct items for the current page.
* The actual page handling is then accomplished at the application layer.
*
* @param App $a App instance
* @return string html for pagination #FIXME remove html
*/
function paginate(App $a) {
$data = paginate_data($a);
$tpl = get_markup_template("paginate.tpl");
return replace_macros($tpl, ["pager" => $data]);
}
/**
* Alternative pager
* @param App $a App instance
* @param int $i
* @return string html for pagination #FIXME remove html
*/
function alt_pager(App $a, $i) {
$data = paginate_data($a, $i);
$tpl = get_markup_template("paginate.tpl");
return replace_macros($tpl, ['pager' => $data]);
}
/**
* Loader for infinite scrolling
* @return string html for loader

View file

@ -8,6 +8,7 @@
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Content\Feature;
use Friendica\Content\Pager;
use Friendica\Content\Text\Markdown;
use Friendica\Core\Addon;
use Friendica\Core\Config;
@ -482,10 +483,9 @@ function admin_page_contactblock(App $a)
$total = DBA::count('contact', $condition);
$a->setPagerTotal($total);
$a->setPagerItemsPage(30);
$pager = new Pager($a->query_string, 30);
$statement = DBA::select('contact', [], $condition, ['limit' => [$a->pager['start'], $a->pager['itemspage']]]);
$statement = DBA::select('contact', [], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]);
$contacts = DBA::toArray($statement);
@ -513,7 +513,7 @@ function admin_page_contactblock(App $a)
'$contacts' => $contacts,
'$total_contacts' => L10n::tt('%s total blocked contact', '%s total blocked contacts', $total),
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
'$contacturl' => ['contact_url', L10n::t("Profile URL"), '', L10n::t("URL of the remote contact to block.")],
]);
return $o;
@ -1812,12 +1812,7 @@ function admin_page_users(App $a)
/* get pending */
$pending = Register::getPending();
/* get users */
$total = q("SELECT COUNT(*) AS `total` FROM `user` WHERE 1");
if (count($total)) {
$a->setPagerTotal($total[0]['total']);
$a->setPagerItemsPage(100);
}
$pager = new Pager($a->query_string, 100);
/* ordering */
$valid_orders = [
@ -1849,7 +1844,7 @@ function admin_page_users(App $a)
FROM `user`
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
WHERE `user`.`verified`
ORDER BY $sql_order $sql_order_direction LIMIT %d, %d", intval($a->pager['start']), intval($a->pager['itemspage'])
ORDER BY $sql_order $sql_order_direction LIMIT %d, %d", $pager->getStart(), $pager->getItemsPerPage()
);
$adminlist = explode(",", str_replace(" ", "", Config::get('config', 'admin_email')));
@ -1947,7 +1942,7 @@ function admin_page_users(App $a)
'$form_security_token' => BaseModule::getFormSecurityToken("admin_users"),
// values //
'$baseurl' => System::baseUrl(true),
'$baseurl' => $a->getBaseURL(true),
'$pending' => $pending,
'deleted' => $deleted,
@ -1956,7 +1951,7 @@ function admin_page_users(App $a)
'$newusernickname' => ['new_user_nickname', L10n::t("Nickname"), '', L10n::t("Nickname of the new user.")],
'$newuseremail' => ['new_user_email', L10n::t("Email"), '', L10n::t("Email address of the new user."), '', '', 'email'],
]);
$o .= paginate($a);
$o .= $pager->renderFull(DBA::count('user'));
return $o;
}

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\ContactSelector;
use Friendica\Content\Pager;
use Friendica\Core\L10n;
use Friendica\Core\System;
use Friendica\Database\DBA;
@ -45,9 +46,9 @@ function allfriends_content(App $a)
$total = Model\GContact::countAllFriends(local_user(), $cid);
$a->setPagerTotal($total);
$pager = new Pager($a->query_string);
$r = Model\GContact::allFriends(local_user(), $cid, $a->pager['start'], $a->pager['itemspage']);
$r = Model\GContact::allFriends(local_user(), $cid, $pager->getStart(), $pager->getItemsPerPage());
if (!DBA::isResult($r)) {
$o .= L10n::t('No friends to display.');
return $o;
@ -103,7 +104,7 @@ function allfriends_content(App $a)
//'$title' => L10n::t('Friends of %s', htmlentities($c[0]['name'])),
'$tab_str' => $tab_str,
'$contacts' => $entries,
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
return $o;

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\ContactSelector;
use Friendica\Content\Pager;
use Friendica\Core\L10n;
use Friendica\Database\DBA;
use Friendica\Model;
@ -81,44 +82,44 @@ function common_content(App $a)
}
if ($cid) {
$t = Model\GContact::countCommonFriends($uid, $cid);
$total = Model\GContact::countCommonFriends($uid, $cid);
} else {
$t = Model\GContact::countCommonFriendsZcid($uid, $zcid);
$total = Model\GContact::countCommonFriendsZcid($uid, $zcid);
}
if ($t > 0) {
$a->setPagerTotal($t);
} else {
if ($total < 1) {
notice(L10n::t('No contacts in common.') . EOL);
return $o;
}
$pager = new Pager($a->query_string);
if ($cid) {
$r = Model\GContact::commonFriends($uid, $cid, $a->pager['start'], $a->pager['itemspage']);
$common_friends = Model\GContact::commonFriends($uid, $cid, $pager->getStart(), $pager->getItemsPerPage());
} else {
$r = Model\GContact::commonFriendsZcid($uid, $zcid, $a->pager['start'], $a->pager['itemspage']);
$common_friends = Model\GContact::commonFriendsZcid($uid, $zcid, $pager->getStart(), $pager->getItemsPerPage());
}
if (!DBA::isResult($r)) {
if (!DBA::isResult($common_friends)) {
return $o;
}
$id = 0;
$entries = [];
foreach ($r as $rr) {
foreach ($common_friends as $common_friend) {
//get further details of the contact
$contact_details = Model\Contact::getDetailsByURL($rr['url'], $uid);
$contact_details = Model\Contact::getDetailsByURL($common_friend['url'], $uid);
// $rr['id'] is needed to use contact_photo_menu()
/// @TODO Adding '/" here avoids E_NOTICE on missing constants
$rr['id'] = $rr['cid'];
$common_friend['id'] = $common_friend['cid'];
$photo_menu = Model\Contact::photoMenu($rr);
$photo_menu = Model\Contact::photoMenu($common_friend);
$entry = [
'url' => $rr['url'],
'itemurl' => defaults($contact_details, 'addr', $rr['url']),
'url' => $common_friend['url'],
'itemurl' => defaults($contact_details, 'addr', $common_friend['url']),
'name' => $contact_details['name'],
'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
'img_hover' => htmlentities($contact_details['name']),
@ -147,7 +148,7 @@ function common_content(App $a)
'$title' => $title,
'$tab_str' => $tab_str,
'$contacts' => $entries,
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
return $o;

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Core\ACL;
use Friendica\Core\Config;
use Friendica\Core\L10n;
@ -153,9 +154,9 @@ function community_content(App $a, $update = 0)
$itemspage_network = $a->force_max_items;
}
$a->setPagerItemsPage($itemspage_network);
$pager = new Pager($a->query_string, $itemspage_network);
$r = community_getitems($a->pager['start'], $a->pager['itemspage'], $content, $accounttype);
$r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
if (!DBA::isResult($r)) {
info(L10n::t('No results.') . EOL);
@ -179,22 +180,22 @@ function community_content(App $a, $update = 0)
}
$previousauthor = $item["author-link"];
if (($numposts < $maxpostperauthor) && (count($s) < $a->pager['itemspage'])) {
if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
$s[] = $item;
}
}
if (count($s) < $a->pager['itemspage']) {
$r = community_getitems($a->pager['start'] + ($count * $a->pager['itemspage']), $a->pager['itemspage'], $content, $accounttype);
if (count($s) < $pager->getItemsPerPage()) {
$r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
}
} while ((count($s) < $a->pager['itemspage']) && ( ++$count < 50) && (count($r) > 0));
} while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
} else {
$s = $r;
}
$o .= conversation($a, $s, 'community', $update, false, 'commented', local_user());
$o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
if (!$update) {
$o .= alt_pager($a, count($r));
$o .= $pager->renderMinimal(count($r));
}
$t = get_markup_template("community.tpl");

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\Addon;
use Friendica\Core\Config;
@ -16,8 +17,6 @@ use Friendica\Util\Proxy as ProxyUtils;
function directory_init(App $a)
{
$a->setPagerItemsPage(60);
if (local_user()) {
$a->page['aside'] .= Widget::findPeople();
$a->page['aside'] .= Widget::follow();
@ -83,16 +82,18 @@ function directory_content(App $a)
$publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 " );
$total = 0;
$cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` FROM `profile`
LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` $sql_extra");
if (DBA::isResult($cnt)) {
$a->setPagerTotal($cnt['total']);
$total = $cnt['total'];
}
$pager = new Pager($a->query_string, 60);
$order = " ORDER BY `name` ASC ";
$limit = intval($a->pager['start'])."," . intval($a->pager['itemspage']);
$limit = $pager->getStart()."," . $pager->getItemsPerPage();
$r = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
`contact`.`addr`, `contact`.`url` AS profile_url FROM `profile`
@ -212,7 +213,7 @@ function directory_content(App $a)
'$findterm' => (strlen($search) ? $search : ""),
'$title' => L10n::t('Site Directory'),
'$submit' => L10n::t('Find'),
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
} else {
info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL);

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\ContactSelector;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\Config;
use Friendica\Core\L10n;
@ -66,12 +67,13 @@ function dirfind_content(App $a, $prefix = "") {
$o = '';
if ($search) {
$pager = new Pager($a->query_string);
if ($discover_user) {
$j = new stdClass();
$j->total = 1;
$j->items_page = 1;
$j->page = $a->pager['page'];
$j->page = $pager->getPage();
$objresult = new stdClass();
$objresult->cid = 0;
@ -93,14 +95,13 @@ function dirfind_content(App $a, $prefix = "") {
Model\GContact::update($user_data);
}
} elseif ($local) {
if ($community)
if ($community) {
$extra_sql = " AND `community`";
else
} else {
$extra_sql = "";
}
$perpage = 80;
$startrec = (($a->pager['page']) * $perpage) - $perpage;
$pager->setItemsPerPage(80);
if (Config::get('system','diaspora_enabled')) {
$diaspora = Protocol::DIASPORA;
@ -137,11 +138,11 @@ function dirfind_content(App $a, $prefix = "") {
DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
intval($startrec), intval($perpage));
$pager->getStart(), $pager->getItemsPerPage());
$j = new stdClass();
$j->total = $count[0]["total"];
$j->items_page = $perpage;
$j->page = $a->pager['page'];
$j->items_page = $pager->getItemsPerPage();
$j->page = $pager->getPage();
foreach ($results AS $result) {
if (PortableContact::alternateOStatusUrl($result["nurl"])) {
continue;
@ -177,22 +178,18 @@ function dirfind_content(App $a, $prefix = "") {
// Add found profiles from the global directory to the local directory
Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
} else {
$p = (($pager->getPage() != 1) ? '&p=' . $pager->getPage() : '');
$p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
if(strlen(Config::get('system','directory')))
if (strlen(Config::get('system','directory'))) {
$x = Network::fetchUrl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
$j = json_decode($x);
}
if ($j->total) {
$a->setPagerTotal($j->total);
$a->setPagerItemsPage($j->items_page);
$j = json_decode($x);
$pager->setItemsPerPage($j->items_page);
}
if (!empty($j->results)) {
$id = 0;
foreach ($j->results as $jj) {
@ -253,13 +250,11 @@ function dirfind_content(App $a, $prefix = "") {
}
$tpl = get_markup_template('viewcontact_template.tpl');
$o .= replace_macros($tpl,[
'title' => $header,
'$contacts' => $entries,
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($j->total),
]);
} else {
info(L10n::t('No matches') . EOL);
}

View file

@ -4,6 +4,7 @@
*/
use Friendica\App;
use Friendica\Content\Pager;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\ACL;
@ -16,9 +17,8 @@ use Friendica\Model\Contact;
use Friendica\Model\Group;
use Friendica\Model\Item;
use Friendica\Model\Profile;
use Friendica\Protocol\DFRN;
use Friendica\Protocol\ActivityPub;
use Friendica\Util\Security;
use Friendica\Protocol\DFRN;
function display_init(App $a)
{
@ -358,7 +358,7 @@ function display_content(App $a, $update = false, $update_uid = 0)
$o .= "<script> var netargs = '?f=&item_id=" . $item_id . "'; </script>";
}
$o .= conversation($a, [$item], 'display', $update_uid, false, 'commented', local_user());
$o .= conversation($a, [$item], new Pager($a->query_string), 'display', $update_uid, false, 'commented', local_user());
// Preparing the meta header
$description = trim(HTML::toPlaintext(BBCode::convert($item["body"], false), 0, true));

View file

@ -16,6 +16,7 @@
*/
use Friendica\App;
use Friendica\Content\Pager;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Addon;
@ -33,6 +34,7 @@ use Friendica\Protocol\Email;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Emailer;
use Friendica\Util\Security;
use function Friendica\Core\function_exists;
require_once 'include/enotify.php';
require_once 'include/text.php';
@ -667,10 +669,10 @@ function item_post(App $a) {
$datarray["item_id"] = -1;
$datarray["author-network"] = Protocol::DFRN;
$o = conversation($a,[array_merge($contact_record,$datarray)],'search', false, true);
$o = conversation($a, [array_merge($contact_record, $datarray)], new Pager($a->query_string), 'search', false, true);
logger('preview: ' . $o);
echo json_encode(['preview' => $o]);
killme();
exit();
}
Addon::callHooks('post_local',$datarray);

View file

@ -4,6 +4,7 @@
*/
use Friendica\App;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\Config;
use Friendica\Core\L10n;
@ -53,9 +54,11 @@ function match_content(App $a)
$tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']);
if ($tags) {
$pager = new Pager($a->query_string);
$params['s'] = $tags;
if ($a->pager['page'] != 1) {
$params['p'] = $a->pager['page'];
if ($pager->getPage() != 1) {
$params['p'] = $pager->getPage();
}
if (strlen(Config::get('system', 'directory'))) {
@ -66,12 +69,9 @@ function match_content(App $a)
$j = json_decode($x);
if ($j->total) {
$a->setPagerTotal($j->total);
$a->setPagerItemsPage($j->items_page);
}
if (count($j->results)) {
$pager->setItemsPerPage($j->items_page);
$id = 0;
foreach ($j->results as $jj) {
@ -114,13 +114,11 @@ function match_content(App $a)
$tpl = get_markup_template('viewcontact_template.tpl');
$o .= replace_macros(
$tpl,
[
$o .= replace_macros($tpl, [
'$title' => L10n::t('Profile Match'),
'$contacts' => $entries,
'$paginate' => paginate($a)]
);
'$paginate' => $pager->renderFull($j->total)
]);
} else {
info(L10n::t('No matches') . EOL);
}

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Smilies;
use Friendica\Content\Text\BBCode;
use Friendica\Core\ACL;
@ -13,10 +14,10 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Mail;
use Friendica\Module\Login;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Util\Temporal;
use Friendica\Module\Login;
require_once 'include/conversation.php';
@ -273,16 +274,18 @@ function message_content(App $a)
$o .= $header;
$total = 0;
$r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
intval(local_user())
);
if (DBA::isResult($r)) {
$a->setPagerTotal($r[0]['total']);
$total = $r[0]['total'];
}
$r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
$pager = new Pager($a->query_string);
$r = get_messages(local_user(), $pager->getStart(), $pager->getItemsPerPage());
if (!DBA::isResult($r)) {
info(L10n::t('No messages.') . EOL);
@ -291,7 +294,7 @@ function message_content(App $a)
$o .= render_messages($r, 'mail_list.tpl');
$o .= paginate($a);
$o .= $pager->renderFull($total);
return $o;
}

View file

@ -8,14 +8,15 @@ use Friendica\App;
use Friendica\Content\Feature;
use Friendica\Content\ForumManager;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\ACL;
use Friendica\Core\Addon;
use Friendica\Core\Config;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Group;
@ -35,6 +36,8 @@ function network_init(App $a)
return;
}
Hook::add('head', __FILE__, 'network_infinite_scroll_head');
$search = (x($_GET, 'search') ? escape_tags($_GET['search']) : '');
if (($search != '') && !empty($_GET['submit'])) {
@ -279,7 +282,7 @@ function network_query_get_sel_group(App $a)
* @param integer $update Used for the automatic reloading
* @return string SQL with the appropriate LIMIT clause
*/
function networkPager($a, $update)
function networkPager(App $a, Pager $pager, $update)
{
if ($update) {
// only setup pagination on initial page view
@ -302,9 +305,9 @@ function networkPager($a, $update)
$itemspage_network = $a->force_max_items;
}
$a->setPagerItemsPage($itemspage_network);
$pager->setItemsPerPage($itemspage_network);
return sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
return sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
}
/**
@ -334,18 +337,18 @@ function networkSetSeen($condition)
* @param integer $update Used for the automatic reloading
* @return string HTML of the conversation
*/
function networkConversation($a, $items, $mode, $update, $ordering = '')
function networkConversation(App $a, $items, Pager $pager, $mode, $update, $ordering = '')
{
// Set this so that the conversation function can find out contact info for our wall-wall items
$a->page_contact = $a->contact;
$o = conversation($a, $items, $mode, $update, false, $ordering, local_user());
$o = conversation($a, $items, $pager, $mode, $update, false, $ordering, local_user());
if (!$update) {
if (PConfig::get(local_user(), 'system', 'infinite_scroll')) {
$o .= scroll_loader();
} else {
$o .= alt_pager($a, count($items));
$o .= $pager->renderMinimal(count($items));
}
}
@ -372,7 +375,7 @@ function network_content(App $a, $update = 0, $parent = 0)
}
}
if (x($_GET, 'file')) {
if (!empty($_GET['file'])) {
$flat_mode = true;
}
@ -388,12 +391,14 @@ function network_content(App $a, $update = 0, $parent = 0)
/**
* @brief Get the network content in flat view
*
* @param Pager $pager
* @param App $a The global App
* @param integer $update Used for the automatic reloading
* @return string HTML of the network content in flat view
*/
function networkFlatView(App $a, $update = 0)
{
global $pager;
// Rawmode is used for fetching new content at the end of the page
$rawmode = (isset($_GET['mode']) && ($_GET['mode'] == 'raw'));
@ -405,7 +410,7 @@ function networkFlatView(App $a, $update = 0)
$o = '';
$file = ((x($_GET, 'file')) ? $_GET['file'] : '');
$file = defaults($_GET, 'file', '');
if (!$update && !$rawmode) {
$tabs = network_tabs($a);
@ -437,12 +442,15 @@ function networkFlatView(App $a, $update = 0)
}
}
$pager_sql = networkPager($a, $update);
$pager = new Pager($a->query_string);
/// @TODO Figure out why this variable is unused
$pager_sql = networkPager($a, $pager, $update);
if (strlen($file)) {
$condition = ["`term` = ? AND `otype` = ? AND `type` = ? AND `uid` = ?",
$file, TERM_OBJ_POST, TERM_FILE, local_user()];
$params = ['order' => ['tid' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
$params = ['order' => ['tid' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$result = DBA::select('term', ['oid'], $condition);
$posts = [];
@ -456,14 +464,14 @@ function networkFlatView(App $a, $update = 0)
$condition = ['uid' => local_user()];
}
$params = ['order' => ['id' => true], 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
$params = ['order' => ['id' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$result = Item::selectForUser(local_user(), [], $condition, $params);
$items = Item::inArray($result);
$condition = ['unseen' => true, 'uid' => local_user()];
networkSetSeen($condition);
$o .= networkConversation($a, $items, 'network-new', $update);
$o .= networkConversation($a, $items, $pager, 'network-new', $update);
return $o;
}
@ -471,12 +479,17 @@ function networkFlatView(App $a, $update = 0)
/**
* @brief Get the network content in threaded view
*
* @global Pager $pager
* @param App $a The global App
* @param integer $update Used for the automatic reloading
* @param integer $parent
* @return string HTML of the network content in flat view
*/
function networkThreadedView(App $a, $update, $parent)
{
/// @TODO this will have to be converted to a static property of the converted Module\Network class
global $pager;
// Rawmode is used for fetching new content at the end of the page
$rawmode = (isset($_GET['mode']) AND ( $_GET['mode'] == 'raw'));
@ -543,14 +556,12 @@ function networkThreadedView(App $a, $update, $parent)
$tabs = network_tabs($a);
$o .= $tabs;
if ($gid) {
if (($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
if ($gid && ($t = Contact::getOStatusCountByGroupId($gid)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) {
notice(L10n::tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
"Warning: This group contains %s members from a network that doesn't allow non public messages.",
$t) . EOL);
notice(L10n::t("Messages in this group won't be send to these receivers.").EOL);
}
}
Nav::setSelected('network');
@ -706,13 +717,15 @@ function networkThreadedView(App $a, $update, $parent)
$sql_order = "$sql_table.$ordering";
if (x($_GET, 'offset')) {
if (!empty($_GET['offset'])) {
$sql_range = sprintf(" AND $sql_order <= '%s'", DBA::escape($_GET['offset']));
} else {
$sql_range = '';
}
$pager_sql = networkPager($a, $update);
$pager = new Pager($a->query_string);
$pager_sql = networkPager($a, $pager, $update);
$last_date = '';
@ -721,31 +734,31 @@ function networkThreadedView(App $a, $update, $parent)
if ($last_received != '') {
$last_date = $last_received;
$sql_range .= sprintf(" AND $sql_table.`received` < '%s'", DBA::escape($last_received));
$a->setPagerPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
$pager->setPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
}
break;
case 'commented':
if ($last_commented != '') {
$last_date = $last_commented;
$sql_range .= sprintf(" AND $sql_table.`commented` < '%s'", DBA::escape($last_commented));
$a->setPagerPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
$pager->setPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
}
break;
case 'created':
if ($last_created != '') {
$last_date = $last_created;
$sql_range .= sprintf(" AND $sql_table.`created` < '%s'", DBA::escape($last_created));
$a->setPagerPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
$pager->setPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
}
break;
case 'id':
if (($last_id > 0) && ($sql_table == '`thread`')) {
$sql_range .= sprintf(" AND $sql_table.`iid` < '%s'", DBA::escape($last_id));
$a->setPagerPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
$pager->setPage(1);
$pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
}
break;
}
@ -835,7 +848,7 @@ function networkThreadedView(App $a, $update, $parent)
if ($last_date > $top_limit) {
$top_limit = $last_date;
} elseif ($a->pager['page'] == 1) {
} elseif ($pager->getPage() == 1) {
// Highest possible top limit when we are on the first page
$top_limit = DateTimeFormat::utcNow();
}
@ -898,7 +911,12 @@ function networkThreadedView(App $a, $update, $parent)
$date_offset = $_GET['offset'];
}
$a->page_offset = $date_offset;
$query_string = $a->query_string;
if ($date_offset && !preg_match('/[?&].offset=/', $query_string)) {
$query_string .= '&offset=' . urlencode($date_offset);
}
$pager->setQueryString($query_string);
// We aren't going to try and figure out at the item, group, and page
// level which items you've seen and which you haven't. If you're looking
@ -914,7 +932,7 @@ function networkThreadedView(App $a, $update, $parent)
$mode = 'network';
$o .= networkConversation($a, $items, $mode, $update, $ordering);
$o .= networkConversation($a, $items, $pager, $mode, $update, $ordering);
return $o;
}
@ -1019,3 +1037,30 @@ function network_tabs(App $a)
// --- end item filter tabs
}
/**
* Network hook into the HTML head to enable infinite scroll.
*
* Since the HTML head is built after the module content has been generated, we need to retrieve the base query string
* of the page to make the correct asynchronous call. This is obtained through the Pager that was instantiated in
* networkThreadedView or networkFlatView.
*
* @global Pager $pager
* @param App $a
* @param string $htmlhead The head tag HTML string
*/
function network_infinite_scroll_head(App $a, &$htmlhead)
{
/// @TODO this will have to be converted to a static property of the converted Module\Network class
global $pager;
if (PConfig::get(local_user(), 'system', 'infinite_scroll')
&& defaults($_GET, 'mode', '') != 'minimal'
) {
$tpl = get_markup_template('infinite_scroll_head.tpl');
$htmlhead .= replace_macros($tpl, [
'$pageno' => $pager->getPage(),
'$reload_uri' => $pager->getBaseQueryString()
]);
}
}

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Core\L10n;
use Friendica\Database\DBA;
use Friendica\Model\Item;
@ -60,10 +61,10 @@ function notes_content(App $a, $update = false)
$condition = ['uid' => local_user(), 'post-type' => Item::PT_PERSONAL_NOTE, 'gravity' => GRAVITY_PARENT,
'wall' => false, 'contact-id'=> $a->contact['id']];
$a->setPagerItemsPage(40);
$pager = new Pager($a->query_string, 40);
$params = ['order' => ['created' => true],
'limit' => [$a->pager['start'], $a->pager['itemspage']]];
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$r = Item::selectThreadForUser(local_user(), ['uri'], $condition, $params);
$count = 0;
@ -73,10 +74,10 @@ function notes_content(App $a, $update = false)
$count = count($notes);
$o .= conversation($a, $notes, 'notes', $update);
$o .= conversation($a, $notes, $pager, 'notes', $update);
}
$o .= alt_pager($a, $count);
$o .= $pager->renderMinimal($count);
return $o;
}

View file

@ -7,6 +7,7 @@
use Friendica\App;
use Friendica\Content\ContactSelector;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Core\L10n;
use Friendica\Core\NotificationsManager;
use Friendica\Core\Protocol;
@ -120,11 +121,11 @@ function notifications_content(App $a)
}
// Set the pager
$a->setPagerItemsPage($perpage);
$pager = new Pager($a->query_string, $perpage);
// Add additional informations (needed for json output)
$notifs['items_page'] = $a->pager['itemspage'];
$notifs['page'] = $a->pager['page'];
$notifs['items_page'] = $pager->getItemsPerPage();
$notifs['page'] = $pager->getPage();
// Json output
if (intval($json) === 1) {
@ -315,7 +316,7 @@ function notifications_content(App $a)
'$notif_content' => $notif_content,
'$notif_nocontent' => $notif_nocontent,
'$notif_show_lnk' => $notif_show_lnk,
'$notif_paginate' => alt_pager($a, count($notif_content))
'$notif_paginate' => $pager->renderMinimal(count($notif_content))
]);
return $o;

View file

@ -6,6 +6,7 @@
use Friendica\App;
use Friendica\Content\Feature;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Text\BBCode;
use Friendica\Core\ACL;
use Friendica\Core\Addon;
@ -25,8 +26,8 @@ use Friendica\Object\Image;
use Friendica\Protocol\DFRN;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Map;
use Friendica\Util\Temporal;
use Friendica\Util\Security;
use Friendica\Util\Temporal;
require_once 'include/items.php';
@ -1135,16 +1136,18 @@ function photos_content(App $a)
if ($datatype === 'album') {
$album = hex2bin($datum);
$total = 0;
$r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
intval($owner_uid),
DBA::escape($album)
);
if (DBA::isResult($r)) {
$a->setPagerTotal(count($r));
$a->setPagerItemsPage(20);
$total = count($r);
}
$pager = new Pager($a->query_string, 20);
/// @TODO I have seen this many times, maybe generalize it script-wide and encapsulate it?
$order_field = defaults($_GET, 'order', '');
if ($order_field === 'posted') {
@ -1160,8 +1163,8 @@ function photos_content(App $a)
AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT %d , %d",
intval($owner_uid),
DBA::escape($album),
intval($a->pager['start']),
intval($a->pager['itemspage'])
$pager->getStart(),
$pager->getItemsPerPage()
);
// edit album name
@ -1230,7 +1233,7 @@ function photos_content(App $a)
'$upload' => [L10n::t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album)],
'$order' => $order,
'$edit' => $edit,
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
return $o;
@ -1385,15 +1388,18 @@ function photos_content(App $a)
$map = null;
$link_item = [];
$total = 0;
if (DBA::isResult($linked_items)) {
// This is a workaround to not being forced to rewrite the while $sql_extra handling
$link_item = Item::selectFirst([], ['id' => $linked_items[0]['id']]);
$condition = ["`parent` = ? AND `parent` != `id`", $link_item['parent']];
$a->setPagerTotal(DBA::count('item', $condition));
$total = DBA::count('item', $condition);
$params = ['order' => ['id'], 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
$pager = new Pager($a->query_string);
$params = ['order' => ['id'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$result = Item::selectForUser($link_item['uid'], Item::ITEM_FIELDLIST, $condition, $params);
$items = Item::inArray($result);
@ -1608,7 +1614,7 @@ function photos_content(App $a)
}
$responses = get_responses($conv_responses, $response_verbs, '', $link_item);
$paginate = paginate($a);
$paginate = $pager->renderFull($total);
}
$photo_tpl = get_markup_template('photo_view.tpl');
@ -1644,19 +1650,19 @@ function photos_content(App $a)
// Default - show recent photos with upload link (if applicable)
//$o = '';
$total = 0;
$r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
$sql_extra GROUP BY `resource-id`",
intval($a->data['user']['uid']),
DBA::escape('Contact Photos'),
DBA::escape(L10n::t('Contact Photos'))
);
if (DBA::isResult($r)) {
$a->setPagerTotal(count($r));
$a->setPagerItemsPage(20);
$total = count($r);
}
$pager = new Pager($a->query_string, 20);
$r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`,
ANY_VALUE(`created`) AS `created` FROM `photo`
@ -1665,8 +1671,8 @@ function photos_content(App $a)
intval($a->data['user']['uid']),
DBA::escape('Contact Photos'),
DBA::escape(L10n::t('Contact Photos')),
intval($a->pager['start']),
intval($a->pager['itemspage'])
$pager->getStart(),
$pager->getItemsPerPage()
);
$photos = [];
@ -1709,7 +1715,7 @@ function photos_content(App $a)
'$can_post' => $can_post,
'$upload' => [L10n::t('Upload New Photos'), 'photos/'.$a->data['user']['nickname'].'/upload'],
'$photos' => $photos,
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
return $o;

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Widget;
use Friendica\Core\ACL;
use Friendica\Core\Addon;
@ -18,10 +19,10 @@ use Friendica\Model\Group;
use Friendica\Model\Item;
use Friendica\Model\Profile;
use Friendica\Module\Login;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\DFRN;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Security;
use Friendica\Protocol\ActivityPub;
function profile_init(App $a)
{
@ -307,9 +308,9 @@ function profile_content(App $a, $update = 0)
$itemspage_network = $a->force_max_items;
}
$a->setPagerItemsPage($itemspage_network);
$pager = new Pager($a->query_string, $itemspage_network);
$pager_sql = sprintf(" LIMIT %d, %d ", intval($a->pager['start']), intval($a->pager['itemspage']));
$pager_sql = sprintf(" LIMIT %d, %d ", $pager->getStart(), $pager->getItemsPerPage());
$items = q("SELECT `item`.`uri`
FROM `thread`
@ -344,10 +345,10 @@ function profile_content(App $a, $update = 0)
}
}
$o .= conversation($a, $items, 'profile', $update, false, 'created', local_user());
$o .= conversation($a, $items, $pager, 'profile', $update, false, 'created', local_user());
if (!$update) {
$o .= alt_pager($a, count($items));
$o .= $pager->renderMinimal(count($items));
}
return $o;

View file

@ -6,6 +6,7 @@
use Friendica\App;
use Friendica\Content\Feature;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\L10n;
@ -200,6 +201,8 @@ function search_content(App $a) {
// OR your own posts if you are a logged in member
// No items will be shown if the member has a blocked profile wall.
$pager = new Pager($a->query_string);
if ($tag) {
logger("Start tag search for '".$search."'", LOGGER_DEBUG);
@ -207,7 +210,7 @@ function search_content(App $a) {
AND `otype` = ? AND `type` = ? AND `term` = ?",
local_user(), TERM_OBJ_POST, TERM_HASHTAG, $search];
$params = ['order' => ['created' => true],
'limit' => [$a->pager['start'], $a->pager['itemspage']]];
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$terms = DBA::select('term', ['oid'], $condition, $params);
$itemids = [];
@ -230,7 +233,7 @@ function search_content(App $a) {
AND `body` LIKE CONCAT('%',?,'%')",
local_user(), $search];
$params = ['order' => ['id' => true],
'limit' => [$a->pager['start'], $a->pager['itemspage']]];
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$items = Item::selectForUser(local_user(), [], $condition, $params);
$r = Item::inArray($items);
}
@ -252,9 +255,9 @@ function search_content(App $a) {
]);
logger("Start Conversation for '".$search."'", LOGGER_DEBUG);
$o .= conversation($a, $r, 'search', false, false, 'commented', local_user());
$o .= conversation($a, $r, $pager, 'search', false, false, 'commented', local_user());
$o .= alt_pager($a,count($r));
$o .= $pager->renderMinimal(count($r));
logger("Done '".$search."'", LOGGER_DEBUG);

View file

@ -5,6 +5,7 @@
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\System;
@ -334,24 +335,25 @@ function videos_content(App $a)
// Default - show recent videos (no upload link for now)
//$o = '';
$total = 0;
$r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
$sql_extra GROUP BY hash",
intval($a->data['user']['uid'])
);
if (DBA::isResult($r)) {
$a->setPagerTotal(count($r));
$a->setPagerItemsPage(20);
$total = count($r);
}
$pager = new Pager($a->query_string, 20);
$r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
FROM `attach`
WHERE `uid` = %d AND filetype LIKE '%%video%%'
$sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
intval($a->data['user']['uid']),
intval($a->pager['start']),
intval($a->pager['itemspage'])
$pager->getStart(),
$pager->getItemsPerPage()
);
$videos = [];
@ -388,7 +390,7 @@ function videos_content(App $a)
'$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
]);
$o .= paginate($a);
$o .= $pager->renderFull($total);
return $o;
}

View file

@ -2,17 +2,19 @@
/**
* @file mod/viewcontacts.php
*/
use Friendica\App;
use Friendica\Content\ContactSelector;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\Profile;
use Friendica\Util\Proxy as ProxyUtils;
use Friendica\Core\System;
function viewcontacts_init(App $a)
{
@ -61,6 +63,7 @@ function viewcontacts_content(App $a)
return $o;
}
$total = 0;
$r = q("SELECT COUNT(*) AS `total` FROM `contact`
WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
AND NOT `hidden` AND NOT `archive`
@ -71,8 +74,9 @@ function viewcontacts_content(App $a)
DBA::escape(Protocol::OSTATUS)
);
if (DBA::isResult($r)) {
$a->setPagerTotal($r[0]['total']);
$total = $r[0]['total'];
}
$pager = new Pager($a->query_string);
$r = q("SELECT * FROM `contact`
WHERE `uid` = %d AND NOT `blocked` AND NOT `pending`
@ -83,8 +87,8 @@ function viewcontacts_content(App $a)
DBA::escape(Protocol::DFRN),
DBA::escape(Protocol::DIASPORA),
DBA::escape(Protocol::OSTATUS),
intval($a->pager['start']),
intval($a->pager['itemspage'])
$pager->getStart(),
$pager->getItemsPerPage()
);
if (!DBA::isResult($r)) {
info(L10n::t('No contacts.').EOL);
@ -124,7 +128,7 @@ function viewcontacts_content(App $a)
$o .= replace_macros($tpl, [
'$title' => L10n::t('Contacts'),
'$contacts' => $contacts,
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
return $o;

View file

@ -34,8 +34,6 @@ class App
public $query_string = '';
public $config = [];
public $page = [];
public $pager = [];
public $page_offset;
public $profile;
public $profile_uid;
public $user;
@ -303,16 +301,6 @@ class App
$this->module = 'home';
}
// See if there is any page number information, and initialise pagination
$this->pager['page'] = !empty($_GET['page']) && intval($_GET['page']) > 0 ? intval($_GET['page']) : 1;
$this->pager['itemspage'] = 50;
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
if ($this->pager['start'] < 0) {
$this->pager['start'] = 0;
}
$this->pager['total'] = 0;
// Detect mobile devices
$mobile_detect = new MobileDetect();
$this->is_mobile = $mobile_detect->isMobile();
@ -732,23 +720,6 @@ class App
return $this->urlPath;
}
public function setPagerTotal($n)
{
$this->pager['total'] = intval($n);
}
public function setPagerItemsPage($n)
{
$this->pager['itemspage'] = ((intval($n) > 0) ? intval($n) : 0);
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
}
public function setPagerPage($n)
{
$this->pager['page'] = $n;
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
}
/**
* Initializes App->page['htmlhead'].
*
@ -798,9 +769,6 @@ class App
$touch_icon = 'images/friendica-128.png';
}
// get data wich is needed for infinite scroll on the network page
$infinite_scroll = infinite_scroll_data($this->module);
Core\Addon::callHooks('head', $this->page['htmlhead']);
$tpl = get_markup_template('head.tpl');
@ -818,7 +786,6 @@ class App
'$update_interval' => $interval,
'$shortcut_icon' => $shortcut_icon,
'$touch_icon' => $touch_icon,
'$infinite_scroll' => $infinite_scroll,
'$block_public' => intval(Core\Config::get('system', 'block_public')),
'$stylesheets' => $this->stylesheets,
]) . $this->page['htmlhead'];

282
src/Content/Pager.php Normal file
View file

@ -0,0 +1,282 @@
<?php
namespace Friendica\Content;
use Friendica\Core\L10n;
/**
* The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class Pager
{
/**
* @var integer
*/
private $page = 1;
/**
* @var integer
*/
private $itemsPerPage = 50;
/**
* @var string
*/
private $baseQueryString = '';
/**
* Instantiates a new Pager with the base parameters.
*
* Guesses the page number from the GET parameter 'page'.
*
* @param string $queryString The query string of the current page
* @param integer $itemsPerPage An optional number of items per page to override the default value
*/
public function __construct($queryString, $itemsPerPage = 50)
{
$this->setQueryString($queryString);
$this->setItemsPerPage($itemsPerPage);
$this->setPage(defaults($_GET, 'page', 1));
}
/**
* Returns the start offset for a LIMIT clause. Starts at 0.
*
* @return integer
*/
public function getStart()
{
return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
}
/**
* Returns the number of items per page
*
* @return integer
*/
public function getItemsPerPage()
{
return $this->itemsPerPage;
}
/**
* Returns the current page number
*
* @return type
*/
public function getPage()
{
return $this->page;
}
/**
* Returns the base query string.
*
* Warning: this isn't the same value as passed to the constructor.
* See setQueryString() for the inventory of transformations
*
* @see setBaseQuery()
* @return string
*/
public function getBaseQueryString()
{
return $this->baseQueryString;
}
/**
* Sets the number of items per page, 1 minimum.
*
* @param integer $itemsPerPage
*/
public function setItemsPerPage($itemsPerPage)
{
$this->itemsPerPage = max(1, intval($itemsPerPage));
}
/**
* Sets the current page number. Starts at 1.
*
* @param integer $page
*/
public function setPage($page)
{
$this->page = max(1, intval($page));
}
/**
* Sets the base query string from a full query string.
*
* Strips the 'page' parameter, and remove the 'q=' string for some reason.
*
* @param string $queryString
*/
public function setQueryString($queryString)
{
$stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
$stripped = str_replace('q=', '', $stripped);
$stripped = trim($stripped, '/');
$this->baseQueryString = $stripped;
}
/**
* Ensures the provided URI has its query string punctuation in order.
*
* @param string $uri
* @return string
*/
private function ensureQueryParameter($uri)
{
if (strpos($uri, '?') === false && ($pos = strpos($uri, '&')) !== false) {
$uri = substr($uri, 0, $pos) . '?' . substr($uri, $pos + 1);
}
return $uri;
}
/**
* @brief Minimal pager (newer/older)
*
* This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
* The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
*
* Example usage:
*
* $pager = new Pager($a->query_string);
*
* $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
* $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
*
* $html = $pager->renderMinimal(count($items));
*
* @param integer $itemCount The number of displayed items on the page
* @return string HTML string of the pager
*/
public function renderMinimal($itemCount)
{
$displayedItemCount = max(0, intval($itemCount));
$data = [
'class' => 'pager',
'prev' => [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
'text' => L10n::t('newer'),
'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
],
'next' => [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
'text' => L10n::t('older'),
'class' => 'next' . ($displayedItemCount <= 0 ? ' disabled' : '')
]
];
$tpl = get_markup_template('paginate.tpl');
return replace_macros($tpl, ['pager' => $data]);
}
/**
* @brief Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
*
* This mode presents page numbers as well as first, previous, next and last links.
* The itemCount is the total number of items including those not displayed.
*
* Example usage:
*
* $total = DBA::count($table, $condition);
*
* $pager = new Pager($a->query_string, $total);
*
* $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
* $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
*
* $html = $pager->renderFull();
*
* @param integer $itemCount The total number of items including those note displayed on the page
* @return string HTML string of the pager
*/
public function renderFull($itemCount)
{
$totalItemCount = max(0, intval($itemCount));
$data = [];
$data['class'] = 'pagination';
if ($totalItemCount > $this->getItemsPerPage()) {
$data['first'] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'),
'text' => L10n::t('first'),
'class' => $this->getPage() == 1 ? 'disabled' : ''
];
$data['prev'] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
'text' => L10n::t('prev'),
'class' => $this->getPage() == 1 ? 'disabled' : ''
];
$numpages = $totalItemCount / $this->getItemsPerPage();
$numstart = 1;
$numstop = $numpages;
// Limit the number of displayed page number buttons.
if ($numpages > 8) {
$numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
$numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
}
$pages = [];
for ($i = $numstart; $i <= $numstop; $i++) {
if ($i == $this->getPage()) {
$pages[$i] = [
'url' => '#',
'text' => $i,
'class' => 'current active'
];
} else {
$pages[$i] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i),
'text' => $i,
'class' => 'n'
];
}
}
if (($totalItemCount % $this->getItemsPerPage()) != 0) {
if ($i == $this->getPage()) {
$pages[$i] = [
'url' => '#',
'text' => $i,
'class' => 'current active'
];
} else {
$pages[$i] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i),
'text' => $i,
'class' => 'n'
];
}
}
$data['pages'] = $pages;
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
$data['next'] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
'text' => L10n::t('next'),
'class' => $this->getPage() == $lastpage ? 'disabled' : ''
];
$data['last'] = [
'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
'text' => L10n::t('last'),
'class' => $this->getPage() == $lastpage ? 'disabled' : ''
];
}
$tpl = get_markup_template('paginate.tpl');
return replace_macros($tpl, ['pager' => $data]);
}
}

View file

@ -341,8 +341,7 @@ class ACL extends BaseObject
if (Config::get('system', 'poco_local_search')) {
$return = GContact::searchByName($search, $mode);
} else {
$a = self::getApp();
$p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
$p = defaults($_GET, 'page', 1) != 1 ? '&p=' . defaults($_GET, 'page', 1) : '';
$curlResult = Network::curl(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
if ($curlResult->isSuccess()) {

View file

@ -38,16 +38,32 @@ class Hook extends BaseObject
$stmt = DBA::select('hook', ['hook', 'file', 'function'], [], ['order' => ['priority' => 'desc', 'file']]);
while ($hook = DBA::fetch($stmt)) {
if (!array_key_exists($hook['hook'], self::$hooks)) {
self::$hooks[$hook['hook']] = [];
}
self::$hooks[$hook['hook']][] = [$hook['file'], $hook['function']];
self::add($hook['hook'], $hook['file'], $hook['function']);
}
DBA::close($stmt);
}
/**
* Registers a hook.
* @brief Adds a new hook to the hooks array.
*
* This function is meant to be called by modules on each page load as it works after loadHooks has been called.
*
* @param type $hook
* @param type $file
* @param type $function
*/
public static function add($hook, $file, $function)
{
if (!array_key_exists($hook, self::$hooks)) {
self::$hooks[$hook] = [];
}
self::$hooks[$hook][] = [$file, $function];
}
/**
* @brief Registers a hook.
*
* This function is meant to be called once when an addon is enabled for example as it doesn't add to the current hooks.
*
* @param string $hook the name of the hook
* @param string $file the name of the file that hooks into

View file

@ -5,6 +5,7 @@
namespace Friendica\Model;
use Friendica\BaseObject;
use Friendica\Content\Pager;
use Friendica\Core\Addon;
use Friendica\Core\Config;
use Friendica\Core\L10n;
@ -1345,25 +1346,27 @@ class Contact extends BaseObject
$cid, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
}
$pager = new Pager($a->query_string);
$params = ['order' => ['created' => true],
'limit' => [$a->pager['start'], $a->pager['itemspage']]];
'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
if ($thread_mode) {
$r = Item::selectThreadForUser(local_user(), ['uri'], $condition, $params);
$items = Item::inArray($r);
$o = conversation($a, $items, 'contacts', $update);
$o = conversation($a, $items, $pager, 'contacts', $update);
} else {
$r = Item::selectForUser(local_user(), [], $condition, $params);
$items = Item::inArray($r);
$o = conversation($a, $items, 'contact-posts', false);
$o = conversation($a, $items, $pager, 'contact-posts', false);
}
if (!$update) {
$o .= alt_pager($a, count($items));
$o .= $pager->renderMinimal(count($items));
}
return $o;

View file

@ -6,6 +6,7 @@ use Friendica\App;
use Friendica\BaseModule;
use Friendica\Content\ContactSelector;
use Friendica\Content\Nav;
use Friendica\Content\Pager;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Widget;
use Friendica\Core\ACL;
@ -778,9 +779,9 @@ class Contact extends BaseModule
intval($_SESSION['uid'])
);
if (DBA::isResult($r)) {
$a->setPagerTotal($r[0]['total']);
$total = $r[0]['total'];
}
$pager = new Pager($a->query_string);
$sql_extra3 = Widget::unavailableNetworks();
@ -788,8 +789,8 @@ class Contact extends BaseModule
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 $sql_extra3 ORDER BY `name` ASC LIMIT %d , %d ",
intval($_SESSION['uid']),
intval($a->pager['start']),
intval($a->pager['itemspage'])
$pager->getStart(),
$pager->getItemsPerPage()
);
if (DBA::isResult($r)) {
foreach ($r as $rr) {
@ -821,7 +822,7 @@ class Contact extends BaseModule
'contacts_batch_drop' => L10n::t('Delete'),
],
'$h_batch_actions' => L10n::t('Batch Actions'),
'$paginate' => paginate($a),
'$paginate' => $pager->renderFull($total),
]);
return $o;

View file

@ -733,7 +733,7 @@ function loadScrollContent() {
// get the raw content from the next page and insert this content
// right before "#conversation-end"
$.get('network?mode=raw' + infinite_scroll.reload_uri + '&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
$.get(infinite_scroll.reload_uri + '&mode=raw&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
$("#scroll-loader").hide();
if ($(data).length > 0) {
$(data).insertBefore('#conversation-end');

View file

@ -48,15 +48,6 @@
var updateInterval = {{$update_interval}};
var localUser = {{if $local_user}}{{$local_user}}{{else}}false{{/if}};
{{* Create an object with the data which is needed for infinite scroll.
For the relevant js part look at function loadContent() in main.js. *}}
{{if $infinite_scroll}}
var infinite_scroll = {
"pageno" : {{$infinite_scroll.pageno}},
"reload_uri" : "{{$infinite_scroll.reload_uri}}"
}
{{/if}}
function confirmDelete() { return confirm("{{$delitem}}"); }
function commentExpand(id) {
$("#comment-edit-text-" + id).value = "";

View file

@ -0,0 +1,8 @@
<script>
{{* Create an object with the data which is needed for infinite scroll.
For the relevant js part look at function loadContent() in main.js. *}}
var infinite_scroll = {
"pageno" : {{$pageno}},
"reload_uri": "{{$reload_uri}}"
}
</script>

View file

@ -1,5 +1,4 @@
{{* Strings which are needed for some js functions (e.g. translation or the interval for page update)
They are loaded into the html <head> so that js functions can use them *}}
<script type="text/javascript">
@ -9,13 +8,4 @@ They are loaded into the html <head> so that js functions can use them *}}
var aStr = {
'delitem' : "{{$delitem}}",
};
{{* Create an object with the data which is needed for infinite scroll.
For the relevant js part look at function loadContent() in main.js. *}}
{{if $infinite_scroll}}
var infinite_scroll = {
'pageno' : {{$infinite_scroll.pageno}},
'reload_uri' : "{{$infinite_scroll.reload_uri}}"
}
{{/if}}
</script>