/gforge/plugins/wiki/www/lib/PageList.php
PHP | 1734 lines | 1100 code | 115 blank | 519 comment | 223 complexity | b2c7fa863c1e18c54d082b991150a8c1 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
Large files files are truncated, but you can click here to view the full file
- <?php rcs_id('$Id: PageList.php,v 1.135 2005/09/14 05:59:03 rurban Exp $');
- /**
- * List a number of pagenames, optionally as table with various columns.
- * This library relieves some work for these plugins:
- *
- * AllPages, BackLinks, LikePages, MostPopular, TitleSearch, WikiAdmin* and more
- *
- * It also allows dynamic expansion of those plugins to include more
- * columns in their output.
- *
- * Column 'info=' arguments:
- *
- * 'pagename' _("Page Name")
- * 'mtime' _("Last Modified")
- * 'hits' _("Hits")
- * 'summary' _("Last Summary")
- * 'version' _("Version")),
- * 'author' _("Last Author")),
- * 'locked' _("Locked"), _("locked")
- * 'minor' _("Minor Edit"), _("minor")
- * 'markup' _("Markup")
- * 'size' _("Size")
- * 'creator' _("Creator")
- * 'owner' _("Owner")
- * 'checkbox' selectable checkbox at the left.
- * 'content'
- *
- * Special, custom columns: Either theme or plugin (WikiAdmin*) specific.
- * 'remove' _("Remove")
- * 'perm' _("Permission Mask")
- * 'acl' _("ACL")
- * 'renamed_pagename' _("Rename to")
- * 'ratingwidget', ... wikilens theme specific.
- * 'custom' See plugin/_WikiTranslation
- *
- * Symbolic 'info=' arguments:
- * 'all' All columns except the special columns
- * 'most' pagename, mtime, author, size, hits, ...
- * 'some' pagename, mtime, author
- *
- * FIXME: In this refactoring I (Jeff) have un-implemented _ctime, _cauthor, and
- * number-of-revision. Note the _ctime and _cauthor as they were implemented
- * were somewhat flawed: revision 1 of a page doesn't have to exist in the
- * database. If lots of revisions have been made to a page, it's more than likely
- * that some older revisions (include revision 1) have been cleaned (deleted).
- *
- * DONE:
- * paging support: limit, offset args
- * check PagePerm "list" access-type,
- * all columns are sortable (Thanks to the wikilens team).
- * cols > 1, comma, azhead, ordered (OL lists)
- * ->supportedArgs() which arguments are supported, so that the plugin
- * doesn't explictly need to declare it
- *
- * FIXED:
- * fix memory exhaustion on large pagelists with old --memory-limit php's only.
- * Status: improved 2004-06-25 16:19:36 rurban
- * but needs further testing.
- */
- class _PageList_Column_base {
- var $_tdattr = array();
- function _PageList_Column_base ($default_heading, $align = false) {
- $this->_heading = $default_heading;
- if ($align) {
- // align="char" isn't supported by any browsers yet :(
- //if (is_array($align))
- // $this->_tdattr = $align;
- //else
- $this->_tdattr['align'] = $align;
- }
- }
- function format ($pagelist, $page_handle, &$revision_handle) {
- return HTML::td($this->_tdattr,
- HTML::raw(' '),
- $this->_getValue($page_handle, $revision_handle),
- HTML::raw(' '));
- }
- function getHeading () {
- return $this->_heading;
- }
- function setHeading ($heading) {
- $this->_heading = $heading;
- }
- // old-style heading
- function heading () {
- // allow sorting?
- if (1 /* or in_array($this->_field, PageList::sortable_columns())*/) {
- // multiple comma-delimited sortby args: "+hits,+pagename"
- // asc or desc: +pagename, -pagename
- $sortby = PageList::sortby($this->_field, 'flip_order');
- //Fixme: pass all also other GET args along. (limit, p[])
- //TODO: support GET and POST
- $s = HTML::a(array('href' =>
- $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby,
- 'nocache' => '1')),
- 'class' => 'pagetitle',
- 'title' => sprintf(_("Sort by %s"), $this->_field)),
- HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
- } else {
- $s = HTML(HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
- }
- return HTML::th(array('align' => 'center'),$s);
- }
- // new grid-style sortable heading
- // see activeui.js
- function button_heading ($pagelist, $colNum) {
- global $WikiTheme, $request;
- // allow sorting?
- if (1 /* or in_array($this->_field, PageList::sortable_columns()) */) {
- // multiple comma-delimited sortby args: "+hits,+pagename"
- $src = false;
- $noimg_src = $WikiTheme->getButtonURL('no_order');
- if ($noimg_src)
- $noimg = HTML::img(array('src' => $noimg_src,
- 'width' => '7',
- 'height' => '7',
- 'border' => 0,
- 'alt' => '.'));
- else
- $noimg = HTML::raw(' ');
- if ($request->getArg('sortby')) {
- if ($pagelist->sortby($colNum, 'check')) { // show icon?
- $sortby = $pagelist->sortby($request->getArg('sortby'), 'flip_order');
- //$request->setArg('sortby', $sortby);
- $desc = (substr($sortby,0,1) == '-'); // asc or desc? (+pagename, -pagename)
- $src = $WikiTheme->getButtonURL($desc ? 'asc_order' : 'desc_order');
- } else {
- $sortby = $pagelist->sortby($colNum, 'init');
- }
- } else {
- $sortby = $pagelist->sortby($colNum, 'init');
- }
- if (!$src) {
- $img = $noimg;
- //$img->setAttr('alt', _("Click to sort"));
- } else {
- $img = HTML::img(array('src' => $src,
- 'width' => '7',
- 'height' => '7',
- 'border' => 0,
- 'alt' => _("Click to reverse sort order")));
- }
- $s = HTML::a(array('href' =>
- //Fixme: pass all also other GET args along. (limit is ok, p[])
- //Fixme: convert to POST submit[sortby]
- $request->GetURLtoSelf(array('sortby' => $sortby,
- /*'nocache' => '1'*/)),
- 'class' => 'gridbutton',
- 'title' => sprintf(_("Click to sort by %s"), $this->_field)),
- HTML::raw(' '),
- $noimg,
- HTML::raw(' '),
- $this->_heading,
- HTML::raw(' '),
- $img,
- HTML::raw(' '));
- } else {
- $s = HTML(HTML::raw(' '), $this->_heading, HTML::raw(' '));
- }
- return HTML::th(array('align' => 'center', 'valign' => 'middle',
- 'class' => 'gridbutton'), $s);
- }
- /**
- * Take two columns of this type and compare them.
- * An undefined value is defined to be < than the smallest defined value.
- * This base class _compare only works if the value is simple (e.g., a number).
- *
- * @param $colvala $this->_getValue() of column a
- * @param $colvalb $this->_getValue() of column b
- *
- * @return -1 if $a < $b, 1 if $a > $b, 0 otherwise.
- */
- function _compare($colvala, $colvalb) {
- if (is_string($colvala))
- return strcmp($colvala,$colvalb);
- $ret = 0;
- if (($colvala === $colvalb) || (!isset($colvala) && !isset($colvalb))) {
- ;
- } else {
- $ret = (!isset($colvala) || ($colvala < $colvalb)) ? -1 : 1;
- }
- return $ret;
- }
- };
- class _PageList_Column extends _PageList_Column_base {
- function _PageList_Column ($field, $default_heading, $align = false) {
- $this->_PageList_Column_base($default_heading, $align);
- $this->_need_rev = substr($field, 0, 4) == 'rev:';
- $this->_iscustom = substr($field, 0, 7) == 'custom:';
- if ($this->_iscustom) {
- $this->_field = substr($field, 7);
- }
- elseif ($this->_need_rev)
- $this->_field = substr($field, 4);
- else
- $this->_field = $field;
- }
- function _getValue ($page_handle, &$revision_handle) {
- if ($this->_need_rev) {
- if (!$revision_handle)
- // columns which need the %content should override this. (size, hi_content)
- $revision_handle = $page_handle->getCurrentRevision(false);
- return $revision_handle->get($this->_field);
- }
- else {
- return $page_handle->get($this->_field);
- }
- }
-
- function _getSortableValue ($page_handle, &$revision_handle) {
- $val = $this->_getValue($page_handle, $revision_handle);
- if ($this->_field == 'hits')
- return (int) $val;
- elseif (is_object($val))
- return $val->asString();
- else
- return (string) $val;
- }
- };
- /* overcome a call_user_func limitation by not being able to do:
- * call_user_func_array(array(&$class, $class_name), $params);
- * So we need $class = new $classname($params);
- * And we add a 4th param to get at the parent $pagelist object
- */
- class _PageList_Column_custom extends _PageList_Column {
- function _PageList_Column_custom($params) {
- $this->_pagelist =& $params[3];
- $this->_PageList_Column($params[0], $params[1], $params[2]);
- }
- }
- class _PageList_Column_size extends _PageList_Column {
- function format (&$pagelist, $page_handle, &$revision_handle) {
- return HTML::td($this->_tdattr,
- HTML::raw(' '),
- $this->_getValue($pagelist, $page_handle, $revision_handle),
- HTML::raw(' '));
- }
-
- function _getValue (&$pagelist, $page_handle, &$revision_handle) {
- if (!$revision_handle or (!$revision_handle->_data['%content']
- or $revision_handle->_data['%content'] === true)) {
- $revision_handle = $page_handle->getCurrentRevision(true);
- unset($revision_handle->_data['%pagedata']['_cached_html']);
- }
- $size = $this->_getSize($revision_handle);
- // we can safely purge the content when it is not sortable
- if (empty($pagelist->_sortby[$this->_field]))
- unset($revision_handle->_data['%content']);
- return $size;
- }
-
- function _getSortableValue ($page_handle, &$revision_handle) {
- if (!$revision_handle)
- $revision_handle = $page_handle->getCurrentRevision(true);
- return (empty($revision_handle->_data['%content']))
- ? 0 : strlen($revision_handle->_data['%content']);
- }
- function _getSize($revision_handle) {
- $bytes = @strlen($revision_handle->_data['%content']);
- return ByteFormatter($bytes);
- }
- }
- class _PageList_Column_bool extends _PageList_Column {
- function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
- $this->_PageList_Column($field, $default_heading, 'center');
- $this->_textIfTrue = $text;
- $this->_textIfFalse = new RawXml('—'); //mdash
- }
- function _getValue ($page_handle, &$revision_handle) {
- //FIXME: check if $this is available in the parent (->need_rev)
- $val = _PageList_Column::_getValue($page_handle, $revision_handle);
- return $val ? $this->_textIfTrue : $this->_textIfFalse;
- }
- };
- class _PageList_Column_checkbox extends _PageList_Column {
- function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
- $this->_name = $name;
- $heading = HTML::input(array('type' => 'button',
- 'title' => _("Click to de-/select all pages"),
- //'width' => '100%',
- 'name' => $default_heading,
- 'value' => $default_heading,
- 'onclick' => "flipAll(this.form)"
- ));
- $this->_PageList_Column($field, $heading, 'center');
- }
- function _getValue ($pagelist, $page_handle, &$revision_handle) {
- $pagename = $page_handle->getName();
- $selected = !empty($pagelist->_selected[$pagename]);
- if (strstr($pagename,'[') or strstr($pagename,']')) {
- $pagename = str_replace(array('[',']'),array('%5B','%5D'),$pagename);
- }
- if ($selected) {
- return HTML::input(array('type' => 'checkbox',
- 'name' => $this->_name . "[$pagename]",
- 'value' => 1,
- 'checked' => 'checked'));
- } else {
- return HTML::input(array('type' => 'checkbox',
- 'name' => $this->_name . "[$pagename]",
- 'value' => 1));
- }
- }
- function format ($pagelist, $page_handle, &$revision_handle) {
- return HTML::td($this->_tdattr,
- HTML::raw(' '),
- $this->_getValue($pagelist, $page_handle, $revision_handle),
- HTML::raw(' '));
- }
- // don't sort this javascript button
- function button_heading ($pagelist, $colNum) {
- $s = HTML(HTML::raw(' '), $this->_heading, HTML::raw(' '));
- return HTML::th(array('align' => 'center', 'valign' => 'middle',
- 'class' => 'gridbutton'), $s);
- }
- };
- class _PageList_Column_time extends _PageList_Column {
- function _PageList_Column_time ($field, $default_heading) {
- $this->_PageList_Column($field, $default_heading, 'right');
- global $WikiTheme;
- $this->Theme = &$WikiTheme;
- }
- function _getValue ($page_handle, &$revision_handle) {
- $time = _PageList_Column::_getValue($page_handle, $revision_handle);
- return $this->Theme->formatDateTime($time);
- }
- };
- class _PageList_Column_version extends _PageList_Column {
- function _getValue ($page_handle, &$revision_handle) {
- if (!$revision_handle)
- $revision_handle = $page_handle->getCurrentRevision();
- return $revision_handle->getVersion();
- }
- };
- // Output is hardcoded to limit of first 50 bytes. Otherwise
- // on very large Wikis this will fail if used with AllPages
- // (PHP memory limit exceeded)
- class _PageList_Column_content extends _PageList_Column {
- function _PageList_Column_content ($field, $default_heading, $align = false) {
- $this->_PageList_Column($field, $default_heading, $align);
- $this->bytes = 50;
- if ($field == 'content') {
- $this->_heading .= sprintf(_(" ... first %d bytes"),
- $this->bytes);
- } elseif ($field == 'hi_content') {
- global $HTTP_POST_VARS;
- if (!empty($HTTP_POST_VARS['admin_replace'])) {
- $search = $HTTP_POST_VARS['admin_replace']['from'];
- $this->_heading .= sprintf(_(" ... around %s"),
- '»'.$search.'«');
- }
- }
- }
-
- function _getValue ($page_handle, &$revision_handle) {
- if (!$revision_handle or (!$revision_handle->_data['%content']
- or $revision_handle->_data['%content'] === true)) {
- $revision_handle = $page_handle->getCurrentRevision(true);
- }
- // Not sure why implode is needed here, I thought
- // getContent() already did this, but it seems necessary.
- $c = implode("\n", $revision_handle->getContent());
- if (empty($pagelist->_sortby[$this->_field]))
- unset($revision_handle->_data['%content']);
- if ($this->_field == 'hi_content') {
- global $HTTP_POST_VARS;
- unset($revision_handle->_data['%pagedata']['_cached_html']);
- $search = $HTTP_POST_VARS['admin_replace']['from'];
- if ($search and ($i = strpos($c,$search))) {
- $l = strlen($search);
- $j = max(0,$i - ($this->bytes / 2));
- return HTML::div(array('style' => 'font-size:x-small'),
- HTML::div(array('class' => 'transclusion'),
- HTML::span(substr($c, $j, ($this->bytes / 2))),
- HTML::span(array("style"=>"background:yellow"),$search),
- HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
- );
- } else {
- $c = sprintf(_("%s not found"),
- '»'.$search.'«');
- return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
- $c);
- }
- } elseif (($len = strlen($c)) > $this->bytes) {
- $c = substr($c, 0, $this->bytes);
- }
- include_once('lib/BlockParser.php');
- // false --> don't bother processing hrefs for embedded WikiLinks
- $ct = TransformText($c, $revision_handle->get('markup'), false);
- if (empty($pagelist->_sortby[$this->_field]))
- unset($revision_handle->_data['%pagedata']['_cached_html']);
- return HTML::div(array('style' => 'font-size:x-small'),
- HTML::div(array('class' => 'transclusion'), $ct),
- // Don't show bytes here if size column present too
- ($this->parent->_columns_seen['size'] or !$len) ? "" :
- ByteFormatter($len, /*$longformat = */true));
- }
-
- function _getSortableValue ($page_handle, &$revision_handle) {
- return substr(_PageList_Column::_getValue($page_handle, $revision_handle),0,50);
- }
- };
- class _PageList_Column_author extends _PageList_Column {
- function _PageList_Column_author ($field, $default_heading, $align = false) {
- _PageList_Column::_PageList_Column($field, $default_heading, $align);
- $this->dbi =& $GLOBALS['request']->getDbh();
- }
- function _getValue ($page_handle, &$revision_handle) {
- $author = _PageList_Column::_getValue($page_handle, $revision_handle);
- if (isWikiWord($author) && $this->dbi->isWikiPage($author))
- return WikiLink($author);
- else
- return $author;
- }
- };
- class _PageList_Column_owner extends _PageList_Column_author {
- function _getValue ($page_handle, &$revision_handle) {
- $author = $page_handle->getOwner();
- if (isWikiWord($author) && $this->dbi->isWikiPage($author))
- return WikiLink($author);
- else
- return $author;
- }
- };
- class _PageList_Column_creator extends _PageList_Column_author {
- function _getValue ($page_handle, &$revision_handle) {
- $author = $page_handle->getCreator();
- if (isWikiWord($author) && $this->dbi->isWikiPage($author))
- return WikiLink($author);
- else
- return $author;
- }
- };
- class _PageList_Column_pagename extends _PageList_Column_base {
- var $_field = 'pagename';
- function _PageList_Column_pagename () {
- $this->_PageList_Column_base(_("Page Name"));
- global $request;
- $this->dbi = &$request->getDbh();
- }
- function _getValue ($page_handle, &$revision_handle) {
- if ($this->dbi->isWikiPage($page_handle->getName()))
- return WikiLink($page_handle, 'known');
- else
- return WikiLink($page_handle, 'unknown');
- }
- function _getSortableValue ($page_handle, &$revision_handle) {
- return $page_handle->getName();
- }
- /**
- * Compare two pagenames for sorting. See _PageList_Column::_compare.
- **/
- function _compare($colvala, $colvalb) {
- return strcmp($colvala, $colvalb);
- }
- };
- class PageList {
- var $_group_rows = 3;
- var $_columns = array();
- var $_columnsMap = array(); // Maps column name to column number.
- var $_excluded_pages = array();
- var $_pages = array();
- var $_caption = "";
- var $_pagename_seen = false;
- var $_types = array();
- var $_options = array();
- var $_selected = array();
- var $_sortby = array();
- var $_maxlen = 0;
- function PageList ($columns = false, $exclude = false, $options = false) {
- if ($options)
- $this->_options = $options;
- // let plugins predefine only certain objects, such its own custom pagelist columns
- if (!empty($this->_options['types'])) {
- $this->_types = $this->_options['types'];
- unset($this->_options['types']);
- }
- $this->_initAvailableColumns();
- $symbolic_columns =
- array(
- 'all' => array_diff(array_keys($this->_types), // all but...
- array('checkbox','remove','renamed_pagename',
- 'content','hi_content','perm','acl')),
- 'most' => array('pagename','mtime','author','hits'),
- 'some' => array('pagename','mtime','author')
- );
- if ($columns) {
- if (!is_array($columns))
- $columns = explode(',', $columns);
- // expand symbolic columns:
- foreach ($symbolic_columns as $symbol => $cols) {
- if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
- $columns = array_diff(array_merge($columns,$cols),array($symbol));
- }
- }
- if (!in_array('pagename',$columns))
- $this->_addColumn('pagename');
- foreach ($columns as $col) {
- $this->_addColumn($col);
- }
- }
- // If 'pagename' is already present, _addColumn() will not add it again
- $this->_addColumn('pagename');
- foreach (array('sortby','limit','paging','count','dosort') as $key) {
- if (!empty($options) and !empty($options[$key])) {
- $this->_options[$key] = $options[$key];
- } else {
- $this->_options[$key] = $GLOBALS['request']->getArg($key);
- }
- }
- $this->_options['sortby'] = $this->sortby($this->_options['sortby'], 'init');
- if ($exclude) {
- if (is_string($exclude) and !is_array($exclude))
- $exclude = $this->explodePageList($exclude, false,
- $this->_options['sortby'],
- $this->_options['limit']);
- $this->_excluded_pages = $exclude;
- }
- $this->_messageIfEmpty = _("<no matches>");
- }
- // Currently PageList takes these arguments:
- // 1: info, 2: exclude, 3: hash of options
- // Here we declare which options are supported, so that
- // the calling plugin may simply merge this with its own default arguments
- function supportedArgs () {
- return array(// Currently supported options:
- /* what columns, what pages */
- 'info' => 'pagename',
- 'exclude' => '', // also wildcards, comma-seperated lists
- // and <!plugin-list !> arrays
- /* select pages by meta-data: */
- 'author' => false, // current user by []
- 'owner' => false, // current user by []
- 'creator' => false, // current user by []
- /* for the sort buttons in <th> */
- 'sortby' => '', // same as for WikiDB::getAllPages
- // (unsorted is faster)
- /* PageList pager options:
- * These options may also be given to _generate(List|Table) later
- * But limit and offset might help the query WikiDB::getAllPages()
- */
- 'limit' => 0, // number of rows (pagesize)
- 'paging' => 'auto', // 'auto' top + bottom rows if applicable
- // // 'top' top only if applicable
- // // 'bottom' bottom only if applicable
- // // 'none' don't page at all
- // (TODO: clarify what if $paging==false ?)
- /* list-style options (with single pagename column only so far) */
- 'cols' => 1, // side-by-side display of list (1-3)
- 'azhead' => 0, // 1: group by initials
- // 2: provide shortcut links to initials also
- 'comma' => 0, // condensed comma-seperated list,
- // 1 if without links, 2 if with
- 'commasep' => false, // Default: ', '
- 'ordered' => false, // OL or just UL lists (ignored for comma)
- );
- }
- function setCaption ($caption_string) {
- $this->_caption = $caption_string;
- }
- function getCaption () {
- // put the total into the caption if needed
- if (is_string($this->_caption) && strstr($this->_caption, '%d'))
- return sprintf($this->_caption, $this->getTotal());
- return $this->_caption;
- }
- function setMessageIfEmpty ($msg) {
- $this->_messageIfEmpty = $msg;
- }
- function getTotal () {
- return !empty($this->_options['count'])
- ? (integer) $this->_options['count'] : count($this->_pages);
- }
- function isEmpty () {
- return empty($this->_pages);
- }
- function addPage($page_handle) {
- if (!empty($this->_excluded_pages)) {
- if (!in_array((is_string($page_handle) ? $page_handle : $page_handle->getName()),
- $this->_excluded_pages))
- $this->_pages[] = $page_handle;
- } else {
- $this->_pages[] = $page_handle;
- }
- }
- function pageNames() {
- $pages = array();
- foreach ($this->_pages as $page_handle) {
- $pages[] = $page_handle->getName();
- }
- return $pages;
- }
- function _getPageFromHandle($page_handle) {
- if (is_string($page_handle)) {
- if (empty($page_handle)) return $page_handle;
- //$dbi = $GLOBALS['request']->getDbh(); // no, safe memory!
- $page_handle = $GLOBALS['request']->_dbi->getPage($page_handle);
- }
- return $page_handle;
- }
- /**
- * Take a PageList_Page object, and return an HTML object to display
- * it in a table or list row.
- */
- function _renderPageRow (&$page_handle, $i = 0) {
- $page_handle = $this->_getPageFromHandle($page_handle);
- //FIXME. only on sf.net
- if (!is_object($page_handle)) {
- trigger_error("PageList: Invalid page_handle $page_handle", E_USER_WARNING);
- return;
- }
- if (!isset($page_handle)
- or empty($page_handle)
- or (!empty($this->_excluded_pages)
- and in_array($page_handle->getName(), $this->_excluded_pages)))
- return; // exclude page.
-
- // enforce view permission
- if (!mayAccessPage('view', $page_handle->getName()))
- return;
- $group = (int)($i / $this->_group_rows);
- $class = ($group % 2) ? 'oddrow' : 'evenrow';
- $revision_handle = false;
- $this->_maxlen = max($this->_maxlen, strlen($page_handle->getName()));
- if (count($this->_columns) > 1) {
- $row = HTML::tr(array('class' => $class));
- foreach ($this->_columns as $col)
- $row->pushContent($col->format($this, $page_handle, $revision_handle));
- } else {
- $col = $this->_columns[0];
- $row = $col->_getValue($page_handle, $revision_handle);
- }
- return $row;
- }
- function addPages ($page_iter) {
- //Todo: if limit check max(strlen(pagename))
- while ($page = $page_iter->next()) {
- $this->addPage($page);
- }
- }
- function addPageList (&$list) {
- if (empty($list)) return; // Protect reset from a null arg
- foreach ($list as $page) {
- if (is_object($page))
- $page = $page->_pagename;
- $this->addPage((string)$page);
- }
- }
- function maxLen() {
- global $request;
- $dbi =& $request->getDbh();
- if (isa($dbi,'WikiDB_SQL')) {
- extract($dbi->_backend->_table_names);
- $res = $dbi->_backend->_dbh->getOne("SELECT max(length(pagename)) FROM $page_tbl");
- if (DB::isError($res) || empty($res)) return false;
- else return $res;
- } elseif (isa($dbi,'WikiDB_ADODB')) {
- extract($dbi->_backend->_table_names);
- $row = $dbi->_backend->_dbh->getRow("SELECT max(length(pagename)) FROM $page_tbl");
- return $row ? $row[0] : false;
- } else
- return false;
- }
- function getContent() {
- // Note that the <caption> element wants inline content.
- $caption = $this->getCaption();
- if ($this->isEmpty())
- return $this->_emptyList($caption);
- elseif (count($this->_columns) == 1)
- return $this->_generateList($caption);
- else
- return $this->_generateTable($caption);
- }
- function printXML() {
- PrintXML($this->getContent());
- }
- function asXML() {
- return AsXML($this->getContent());
- }
-
- /**
- * Handle sortby requests for the DB iterator and table header links.
- * Prefix the column with + or - like "+pagename","-mtime", ...
- *
- * Supported actions:
- * 'init' : unify with predefined order. "pagename" => "+pagename"
- * 'flip_order' : "mtime" => "+mtime" => "-mtime" ...
- * 'db' : "-pagename" => "pagename DESC"
- * 'check' :
- *
- * Now all columns are sortable. (patch by DanFr)
- * Some columns have native DB backend methods, some not.
- */
- function sortby ($column, $action, $valid_fields=false) {
- global $request;
- if (empty($column)) return '';
- if (is_int($column)) {
- $column = $this->_columns[$column - 1]->_field;
- //$column = $col->_field;
- }
- //if (!is_string($column)) return '';
- // support multiple comma-delimited sortby args: "+hits,+pagename"
- // recursive concat
- if (strstr($column, ',')) {
- $result = ($action == 'check') ? true : array();
- foreach (explode(',', $column) as $col) {
- if ($action == 'check')
- $result = $result && $this->sortby($col, $action, $valid_fields);
- else
- $result[] = $this->sortby($col, $action, $valid_fields);
- }
- // 'check' returns true/false for every col. return true if all are true.
- // i.e. the unsupported 'every' operator in functional languages.
- if ($action == 'check')
- return $result;
- else
- return join(",", $result);
- }
- if (substr($column,0,1) == '+') {
- $order = '+'; $column = substr($column,1);
- } elseif (substr($column,0,1) == '-') {
- $order = '-'; $column = substr($column,1);
- }
- // default initial order: +pagename, -mtime, -hits
- if (empty($order))
- if (in_array($column, array('mtime','hits')))
- $order = '-';
- else
- $order = '+';
- if ($action == 'flip_order') {
- return ($order == '+' ? '-' : '+') . $column;
- } elseif ($action == 'init') {
- $this->_sortby[$column] = $order;
- return $order . $column;
- } elseif ($action == 'check') {
- return (!empty($this->_sortby[$column])
- or ($request->getArg('sortby')
- and strstr($request->getArg('sortby'),$column)));
- } elseif ($action == 'db') {
- // Performance enhancement: use native DB sort if possible.
- if (($valid_fields and in_array($column, $valid_fields))
- or (method_exists($request->_dbi->_backend, 'sortable_columns')
- and (in_array($column, $request->_dbi->_backend->sortable_columns())))) {
- // omit this sort method from the _sortPages call at rendering
- // asc or desc: +pagename, -pagename
- return $column . ($order == '+' ? ' ASC' : ' DESC');
- } else {
- return '';
- }
- }
- return '';
- }
- // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
- function explodePageList($input, $include_empty=false, $sortby=false,
- $limit=false, $exclude=false)
- {
- if (empty($input)) return array();
- // expand wildcards from list of all pages
- if (preg_match('/[\?\*]/', $input)) {
- include_once("lib/TextSearchQuery.php");
- $search = new TextSearchQuery(str_replace(",", " ", $input), true, 'glob');
- $dbi = $GLOBALS['request']->getDbh();
- $iter = $dbi->titleSearch($search, $sortby, $limit, $exclude);
- $pages = array();
- while ($pagehandle = $iter->next()) {
- $pages[] = $pagehandle->getName();
- }
- return $pages;
- /*
- //TODO: need an SQL optimization here
- $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit,
- $exclude);
- while ($pagehandle = $allPagehandles->next()) {
- $allPages[] = $pagehandle->getName();
- }
- return explodeList($input, $allPages);
- */
- } else {
- //TODO: do the sorting, normally not needed if used for exclude only
- return explode(',', $input);
- }
- }
- function allPagesByAuthor($wildcard, $include_empty=false, $sortby=false,
- $limit=false, $exclude=false) {
- $dbi = $GLOBALS['request']->getDbh();
- $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
- $allPages = array();
- if ($wildcard === '[]') {
- $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
- if (!$wildcard) return $allPages;
- }
- $do_glob = preg_match('/[\?\*]/', $wildcard);
- while ($pagehandle = $allPagehandles->next()) {
- $name = $pagehandle->getName();
- $author = $pagehandle->getAuthor();
- if ($author) {
- if ($do_glob) {
- if (glob_match($wildcard, $author))
- $allPages[] = $name;
- } elseif ($wildcard == $author) {
- $allPages[] = $name;
- }
- }
- // TODO: purge versiondata_cache
- }
- return $allPages;
- }
- function allPagesByOwner($wildcard, $include_empty=false, $sortby=false,
- $limit=false, $exclude=false) {
- $dbi = $GLOBALS['request']->getDbh();
- $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
- $allPages = array();
- if ($wildcard === '[]') {
- $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
- if (!$wildcard) return $allPages;
- }
- $do_glob = preg_match('/[\?\*]/', $wildcard);
- while ($pagehandle = $allPagehandles->next()) {
- $name = $pagehandle->getName();
- $owner = $pagehandle->getOwner();
- if ($owner) {
- if ($do_glob) {
- if (glob_match($wildcard, $owner))
- $allPages[] = $name;
- } elseif ($wildcard == $owner) {
- $allPages[] = $name;
- }
- }
- }
- return $allPages;
- }
- function allPagesByCreator($wildcard, $include_empty=false, $sortby=false,
- $limit=false, $exclude=false) {
- $dbi = $GLOBALS['request']->getDbh();
- $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
- $allPages = array();
- if ($wildcard === '[]') {
- $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
- if (!$wildcard) return $allPages;
- }
- $do_glob = preg_match('/[\?\*]/', $wildcard);
- while ($pagehandle = $allPagehandles->next()) {
- $name = $pagehandle->getName();
- $creator = $pagehandle->getCreator();
- if ($creator) {
- if ($do_glob) {
- if (glob_match($wildcard, $creator))
- $allPages[] = $name;
- } elseif ($wildcard == $creator) {
- $allPages[] = $name;
- }
- }
- }
- return $allPages;
- }
- ////////////////////
- // private
- ////////////////////
- /** Plugin and theme hooks:
- * If the pageList is initialized with $options['types'] these types are also initialized,
- * overriding the standard types.
- */
- function _initAvailableColumns() {
- global $customPageListColumns;
- $standard_types =
- array(
- 'content'
- => new _PageList_Column_content('rev:content', _("Content")),
- // new: plugin specific column types initialised by the relevant plugins
- /*
- 'hi_content' // with highlighted search for SearchReplace
- => new _PageList_Column_content('rev:hi_content', _("Content")),
- 'remove'
- => new _PageList_Column_remove('remove', _("Remove")),
- // initialised by the plugin
- 'renamed_pagename'
- => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
- 'perm'
- => new _PageList_Column_perm('perm', _("Permission")),
- 'acl'
- => new _PageList_Column_acl('acl', _("ACL")),
- */
- 'checkbox'
- => new _PageList_Column_checkbox('p', _("Select")),
- 'pagename'
- => new _PageList_Column_pagename,
- 'mtime'
- => new _PageList_Column_time('rev:mtime', _("Last Modified")),
- 'hits'
- => new _PageList_Column('hits', _("Hits"), 'right'),
- 'size'
- => new _PageList_Column_size('rev:size', _("Size"), 'right'),
- /*array('align' => 'char', 'char' => ' ')*/
- 'summary'
- => new _PageList_Column('rev:summary', _("Last Summary")),
- 'version'
- => new _PageList_Column_version('rev:version', _("Version"),
- 'right'),
- 'author'
- => new _PageList_Column_author('rev:author', _("Last Author")),
- 'owner'
- => new _PageList_Column_owner('author_id', _("Owner")),
- 'creator'
- => new _PageList_Column_creator('author_id', _("Creator")),
- /*
- 'group'
- => new _PageList_Column_author('group', _("Group")),
- */
- 'locked'
- => new _PageList_Column_bool('locked', _("Locked"),
- _("locked")),
- 'minor'
- => new _PageList_Column_bool('rev:is_minor_edit',
- _("Minor Edit"), _("minor")),
- 'markup'
- => new _PageList_Column('rev:markup', _("Markup")),
- // 'rating' initialised by the wikilens theme hook: addPageListColumn
- /*
- 'rating'
- => new _PageList_Column_rating('rating', _("Rate")),
- */
- );
- if (empty($this->_types))
- $this->_types = array();
- // add plugin specific pageList columns, initialized by $options['types']
- $this->_types = array_merge($standard_types, $this->_types);
- // add theme custom specific pageList columns:
- // set the 4th param as the current pagelist object.
- if (!empty($customPageListColumns)) {
- foreach ($customPageListColumns as $column => $params) {
- $class_name = array_shift($params);
- $params[3] =& $this;
- $class = new $class_name($params);
- $this->_types[$column] =& $class;
- }
- }
- }
- function getOption($option) {
- if (array_key_exists($option, $this->_options)) {
- return $this->_options[$option];
- }
- else {
- return null;
- }
- }
- /**
- * Add a column to this PageList, given a column name.
- * The name is a type, and optionally has a : and a label. Examples:
- *
- * pagename
- * pagename:This page
- * mtime
- * mtime:Last modified
- *
- * If this function is called multiple times for the same type, the
- * column will only be added the first time, and ignored the succeeding times.
- * If you wish to add multiple columns of the same type, use addColumnObject().
- *
- * @param column name
- * @return true if column is added, false otherwise
- */
- function _addColumn ($column) {
-
- if (isset($this->_columns_seen[$column]))
- return false; // Already have this one.
- if (!isset($this->_types[$column]))
- $this->_initAvailableColumns();
- $this->_columns_seen[$column] = true;
- if (strstr($column, ':'))
- list ($column, $heading) = explode(':', $column, 2);
- // FIXME: these column types have hooks (objects) elsewhere
- // Omitting this warning should be overridable by the extension
- if (!isset($this->_types[$column])) {
- $silently_ignore = array('numbacklinks',
- 'rating',/*'ratingwidget',*/
- 'coagreement', 'minmisery',
- /*'prediction',*/
- 'averagerating', 'top3recs');
- if (!in_array($column, $silently_ignore))
- trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
- return false;
- }
- // FIXME: anon users might rate and see ratings also.
- // Defer this logic to the plugin.
- if ($column == 'rating' and !$GLOBALS['request']->_user->isSignedIn())
- return false;
- $this->addColumnObject($this->_types[$column]);
- return true;
- }
- /**
- * Add a column to this PageList, given a column object.
- *
- * @param $col object An object derived from _PageList_Column.
- **/
- function addColumnObject($col) {
- if (is_array($col)) {// custom column object
- $params =& $col;
- $class_name = array_shift($params);
- $params[3] =& $this;
- $col = new $class_name($params);
- }
- $heading = $col->getHeading();
- if (!empty($heading))
- $col->setHeading($heading);
- $this->_columns[] = $col;
- $this->_columnsMap[$col->_field] = count($this->_columns); // start with 1
- }
- /**
- * Compare _PageList_Page objects.
- **/
- function _pageCompare(&$a, &$b) {
- if (empty($this->_sortby) or count($this->_sortby) == 0) {
- // No columns to sort by
- return 0;
- }
- else {
- $pagea = $this->_getPageFromHandle($a); // If a string, convert to page
- assert(isa($pagea, 'WikiDB_Page'));
- $pageb = $this->_getPageFromHandle($b); // If a string, convert to page
- assert(isa($pageb, 'WikiDB_Page'));
- foreach ($this->_sortby as $colNum => $direction) {
- if (!is_int($colNum)) // or column fieldname
- $colNum = $this->_columnsMap[$colNum];
- $col = $this->_columns[$colNum - 1];
- assert(isset($col));
- $revision_handle = false;
- $aval = $col->_getSortableValue($pagea, $revision_handle);
- $bval = $col->_getSortableValue($pageb, $revision_handle);
- $cmp = $col->_compare($aval, $bval);
- if ($direction === "-") // Reverse the sense of the comparison
- $cmp *= -1;
- if ($cmp !== 0)
- // This is the first comparison that is not equal-- go with it
- return $cmp;
- }
- return 0;
- }
- }
- /**
- * Put pages in order according to the sortby arg, if given
- * If the sortby cols are already sorted by the DB call, don't do usort.
- * TODO: optimize for multiple sortable cols
- */
- function _sortPages() {
- if (count($this->_sortby) > 0) {
- $need_sort = $this->_options['dosort'];
- foreach ($this->_sortby as $col => $dir) {
- if (! $this->sortby($col, 'db'))
- $need_sort = true;
- }
- if ($need_sort) { // There are some columns to sort by
- usort($this->_pages, array($this, '_pageCompare'));
- }
- }
- //unset($GLOBALS['PhpWiki_pagelist']);
- }
- function limit($limit) {
- if (is_array($limit)) return $limit;
- if (strstr($limit, ','))
- return split(',', $limit);
- else
- return array(0, $limit);
- }
- function pagingTokens($numrows = false, $ncolumns = false, $limit = false) {
- if ($numrows === false)
- $numrows = $this->getTotal();
- if ($limit === false)
- $limit = $this->_options['limit'];
- if ($ncolumns === false)
- $ncolumns = count($this->_columns);
- list($offset, $pagesize) = $this->limit($limit);
- if (!$pagesize or
- (!$offset and $numrows <= $pagesize) or
- ($offset + $pagesize < 0))
- return false;
- $request = &$GLOBALS['request'];
- $pagename = $request->getArg('pagename');
- $defargs = $request->args;
- if (USE_PATH_INFO) unset($defargs['pagename']);
- if ($defargs['action'] == 'browse') unset($defargs['action']);
- $prev = $defargs;
- $tokens = array();
- $tokens['PREV'] = false; $tokens['PREV_LINK'] = "";
- $tokens['COLS'] = count($this->_columns);
- $tokens['COUNT'] = $numrows;
- $tokens['OFFSET'] = $offset;
- $tokens['SIZE'] = $pagesize;
- $tokens['NUMPAGES'] = (int)($numrows / $pagesize)+1;
- $tokens['ACTPAGE'] = (int) (($offset+1) / $pagesize)+1;
- if ($offset > 0) {
- $prev['limit'] = max(0, $offset - $pagesize) . ",$pagesize";
- $prev['count'] = $numrows;
- $tokens['LIMIT'] = $prev['limit'];
- $tokens['PREV'] = true;
- $tokens['PREV_LINK'] = WikiURL($pagename, $prev);
- $prev['limit'] = "0,$pagesize";
- $tokens['FIRST_LINK'] = WikiURL($pagename, $prev);
- }
- $next = $defargs;
- $tokens['NEXT'] = false; $tokens['NEXT_LINK'] = "";
- if ($offset + $pagesize < $numrows) {
- $next['limit'] = min($offset + $pagesize, $numrows - $pagesize) . ",$pagesize";
- $next['count'] = $numrows;
- $tokens['LIMIT'] = $next['limit'];
- $tokens['NEXT'] = true;
- $tokens['NEXT_LINK'] = WikiURL($pagename, $next);
- $next['limit'] = $numrows - $pagesize . ",$pagesize";
- $tokens['LAST_LINK'] = WikiURL($pagename, $next);
- }
- return $tokens;
- }
-
- // make a table given the caption
- function _generateTable($caption) {
- if (count($this->_sortby) > 0) $this->_sortPages();
- $rows = array(); $i = 0;
- foreach ($this->_pages as $pagenum => $page) {
- $rows[] = $this->_renderPageRow($page, $i++);
- }
- $table = HTML::table(array('cellpadding' => 0,
- 'cellspacing' => 1,
- 'border' => 0,
- 'class' => 'pagelist'));
- if ($caption)
- $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
- //Warning: This is quite fragile. It depends solely on a private variable
- // in ->_addColumn()
- if (!empty($this->_columns_seen['checkbox'])) {
- $table->pushContent($this->_jsFlipAll());
- }
- $do_paging = ( isset($this->_options['paging'])
- and !empty($this->_options['limit'])
- and $this->getTotal()
- and $this->_options['paging'] != 'none' );
- $row = HTML::tr();
- $table_summary = array();
- $i = 1; // start with 1!
- foreach ($this->_columns as $col) {
- $heading = $col->button_heading($this, $i);
- if ( $do_paging
- and isset($col->_field)
- and $col->_field == 'pagename'
- and ($maxlen = $this->maxLe…
Large files files are truncated, but you can click here to view the full file