DynamicPageList3/includes/Article.php

402 lines
11 KiB
PHP
Raw Normal View History

2020-11-22 20:00:48 +00:00
<?php
/**
* DynamicPageList3
* DPL Article Class
*
2020-11-24 00:40:27 +00:00
* @author IlyaHaykinson, Unendlich, Dangerville, Algorithmix, Theaitetos, Alexia E. Smith, Universal Omega
2020-11-22 20:00:48 +00:00
* @license GPL-2.0-or-later
* @package DynamicPageList3
*
2021-02-22 23:48:01 +00:00
*/
2021-05-30 18:33:21 +00:00
2020-11-22 20:00:48 +00:00
namespace DPL;
2021-05-30 18:33:21 +00:00
use MediaWiki\MediaWikiServices;
2020-11-22 20:00:48 +00:00
use User;
class Article {
/**
* Title
*
2021-02-22 23:48:01 +00:00
* @var object
2020-11-22 20:00:48 +00:00
*/
public $mTitle = null;
/**
* Namespace ID
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mNamespace = -1;
/**
* Page ID
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mID = 0;
/**
* Selected title of initial page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mSelTitle = null;
/**
* Selected namespace ID of initial page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mSelNamespace = -1;
/**
* Selected title of image.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mImageSelTitle = null;
/**
* HTML link to page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mLink = '';
/**
* External link on the page.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mExternalLink = null;
/**
* First character of the page title.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mStartChar = null;
/**
* Heading (link to the associated page) that page belongs to in the list (default '' means no heading)
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mParentHLink = ''; // heading (link to the associated page) that page belongs to in the list (default '' means no heading)
/**
* Category links on the page.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
public $mCategoryLinks = [];
/**
* Category names (without link) in the page.
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
public $mCategoryTexts = [];
/**
* Number of times this page has been viewed.
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mCounter = null;
/**
* Article length in bytes of wiki text
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mSize = null;
/**
* Timestamp depending on the user's request (can be first/last edit, page_touched, ...)
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mDate = null;
/**
* Timestamp depending on the user's request, based on user format definition.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $myDate = null;
/**
* Revision ID
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mRevision = null;
/**
* Link to editor (first/last, depending on user's request) 's page or contributions if not registered.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mUserLink = null;
/**
* Name of editor (first/last, depending on user's request) or contributions if not registered.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mUser = null;
/**
* Edit Summary(Revision Comment)
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mComment = null;
/**
* Number of bytes changed.
*
2021-02-22 23:48:01 +00:00
* @var int
2020-11-22 20:00:48 +00:00
*/
public $mContribution = 0;
/**
* Short string indicating the size of a contribution.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mContrib = '';
/**
* User text of who made the changes.
*
2021-02-22 23:48:01 +00:00
* @var string
2020-11-22 20:00:48 +00:00
*/
public $mContributor = null;
/**
* Article Headings - Maps heading to count (# of pages under each heading).
*
2021-02-22 23:48:01 +00:00
* @var array
2020-11-22 20:00:48 +00:00
*/
2021-02-22 23:48:01 +00:00
private static $headings = [];
2020-11-22 20:00:48 +00:00
/**
* Main Constructor
*
* @access public
* @param string Title
* @param integer Namespace
* @return void
*/
2021-02-22 23:48:01 +00:00
public function __construct( $title, $namespace ) {
2020-11-22 20:00:48 +00:00
$this->mTitle = $title;
$this->mNamespace = $namespace;
}
/**
* Initialize a new instance from a database row.
*
* @access public
* @param array Database Row
* @param object \DPL\Parameters Object
* @param object Mediawiki Title Object
* @param integer Page Namespace ID
* @param string Page Title as Selected from Query
* @return object \DPL\Article Object
*/
2021-02-22 23:48:01 +00:00
public static function newFromRow( $row, Parameters $parameters, \Title $title, $pageNamespace, $pageTitle ) {
2021-05-30 18:33:21 +00:00
global $wgLang;
2020-11-22 20:00:48 +00:00
2021-02-22 23:48:01 +00:00
$article = new Article( $title, $pageNamespace );
2020-11-25 15:53:02 +00:00
$revActorName = null;
2021-02-22 23:48:01 +00:00
if ( isset( $row['revactor_actor'] ) ) {
2020-11-25 15:53:02 +00:00
$revActorName = User::newFromActorId( $row['revactor_actor'] )->getName();
}
2020-11-22 20:00:48 +00:00
$titleText = $title->getText();
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'shownamespace' ) === true ) {
2020-11-22 20:00:48 +00:00
$titleText = $title->getPrefixedText();
}
2021-02-22 23:48:01 +00:00
$replaceInTitle = $parameters->getParameter( 'replaceintitle' );
if ( is_array( $replaceInTitle ) && count( $replaceInTitle ) === 2 ) {
$titleText = preg_replace( $replaceInTitle[0], $replaceInTitle[1], $titleText );
2020-11-22 20:00:48 +00:00
}
//Chop off title if longer than the 'titlemaxlen' parameter.
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'titlemaxlen' ) !== null && strlen( $titleText ) > $parameters->getParameter( 'titlemaxlen' ) ) {
$titleText = substr( $titleText, 0, $parameters->getParameter( 'titlemaxlen' ) ) . '...';
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'showcurid' ) === true && isset( $row['page_id'] ) ) {
$articleLink = '[' . $title->getLinkURL( [ 'curid' => $row['page_id'] ] ) . ' ' . htmlspecialchars( $titleText ) . ']';
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$articleLink = '[[' . ( $parameters->getParameter( 'escapelinks' ) && ( $pageNamespace == NS_CATEGORY || $pageNamespace == NS_FILE ) ? ':' : '' ) . $title->getFullText() . '|' . htmlspecialchars( $titleText ) . ']]';
2020-11-22 20:00:48 +00:00
}
$article->mLink = $articleLink;
2021-05-30 18:33:21 +00:00
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
2020-11-22 20:00:48 +00:00
//get first char used for category-style output
2021-02-22 23:48:01 +00:00
if ( isset( $row['sortkey'] ) ) {
2021-05-30 18:33:21 +00:00
$article->mStartChar = $contLang->convert( $contLang->firstChar( $row['sortkey'] ) );
2020-11-22 20:00:48 +00:00
} else {
2021-05-30 18:33:21 +00:00
$article->mStartChar = $contLang->convert( $contLang->firstChar( $pageTitle ) );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
$article->mID = intval( $row['page_id'] );
2020-11-22 20:00:48 +00:00
//External link
2021-02-22 23:48:01 +00:00
if ( isset( $row['el_to'] ) ) {
2020-11-22 20:00:48 +00:00
$article->mExternalLink = $row['el_to'];
}
//SHOW PAGE_COUNTER
2021-02-22 23:48:01 +00:00
if ( isset( $row['page_counter'] ) ) {
$article->mCounter = intval( $row['page_counter'] );
2020-11-22 20:00:48 +00:00
}
//SHOW PAGE_SIZE
2021-02-22 23:48:01 +00:00
if ( isset( $row['page_len'] ) ) {
$article->mSize = intval( $row['page_len'] );
2020-11-22 20:00:48 +00:00
}
//STORE initially selected PAGE
2021-02-22 23:48:01 +00:00
if ( is_array( $parameters->getParameter( 'linksto' ) ) && ( count( $parameters->getParameter( 'linksto' ) ) || count( $parameters->getParameter( 'linksfrom' ) ) ) ) {
if ( !isset( $row['sel_title'] ) ) {
2020-11-22 20:00:48 +00:00
$article->mSelTitle = 'unknown page';
$article->mSelNamespace = 0;
} else {
$article->mSelTitle = $row['sel_title'];
$article->mSelNamespace = $row['sel_ns'];
}
}
//STORE selected image
2021-02-22 23:48:01 +00:00
if ( is_array( $parameters->getParameter( 'imageused' ) ) && count( $parameters->getParameter( 'imageused' ) ) > 0 ) {
if ( !isset( $row['image_sel_title'] ) ) {
2020-11-22 20:00:48 +00:00
$article->mImageSelTitle = 'unknown image';
} else {
$article->mImageSelTitle = $row['image_sel_title'];
}
}
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'goal' ) != 'categories' ) {
2020-11-22 20:00:48 +00:00
//REVISION SPECIFIED
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'lastrevisionbefore' ) || $parameters->getParameter( 'allrevisionsbefore' ) || $parameters->getParameter( 'firstrevisionsince' ) || $parameters->getParameter( 'allrevisionssince' ) ) {
2020-11-23 04:33:40 +00:00
$article->mRevision = $row['revactor_rev'];
2020-11-22 20:00:48 +00:00
$article->mUser = $revActorName;
2020-11-23 04:33:40 +00:00
$article->mDate = $row['revactor_timestamp'];
2020-11-22 20:00:48 +00:00
}
//SHOW "PAGE_TOUCHED" DATE, "FIRSTCATEGORYDATE" OR (FIRST/LAST) EDIT DATE
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'addpagetoucheddate' ) ) {
2020-11-22 20:00:48 +00:00
$article->mDate = $row['page_touched'];
2021-02-22 23:48:01 +00:00
} elseif ( $parameters->getParameter( 'addfirstcategorydate' ) ) {
2020-11-22 20:00:48 +00:00
$article->mDate = $row['cl_timestamp'];
2021-02-22 23:48:01 +00:00
} elseif ( $parameters->getParameter( 'addeditdate' ) && isset( $row['revactor_timestamp'] ) ) {
2020-11-23 04:33:40 +00:00
$article->mDate = $row['revactor_timestamp'];
2021-02-22 23:48:01 +00:00
} elseif ( $parameters->getParameter( 'addeditdate' ) && isset( $row['page_touched'] ) ) {
2020-11-22 20:00:48 +00:00
$article->mDate = $row['page_touched'];
}
//Time zone adjustment
2021-02-22 23:48:01 +00:00
if ( $article->mDate ) {
$article->mDate = $wgLang->userAdjust( $article->mDate );
2020-11-22 20:00:48 +00:00
}
2021-02-22 23:48:01 +00:00
if ( $article->mDate && $parameters->getParameter( 'userdateformat' ) ) {
2020-11-22 20:00:48 +00:00
//Apply the userdateformat
2021-02-22 23:48:01 +00:00
$article->myDate = gmdate( $parameters->getParameter( 'userdateformat' ), wfTimeStamp( TS_UNIX, $article->mDate ) );
2020-11-22 20:00:48 +00:00
}
// CONTRIBUTION, CONTRIBUTOR
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'addcontribution' ) ) {
2020-11-22 20:00:48 +00:00
$article->mContribution = $row['contribution'];
$article->mContributor = User::newFromActorId( $row['contributor'] )->getName();
2021-02-22 23:48:01 +00:00
$article->mContrib = substr( '*****************', 0, (int)round( log( $row['contribution'] ) ) );
2020-11-22 20:00:48 +00:00
}
//USER/AUTHOR(S)
// because we are going to do a recursive parse at the end of the output phase
// we have to generate wiki syntax for linking to a user´s homepage
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'adduser' ) || $parameters->getParameter( 'addauthor' ) || $parameters->getParameter( 'addlasteditor' ) ) {
2020-11-22 20:00:48 +00:00
$article->mUserLink = '[[User:' . $revActorName . '|' . $revActorName . ']]';
$article->mUser = $revActorName;
}
//CATEGORY LINKS FROM CURRENT PAGE
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'addcategories' ) && ( $row['cats'] ) ) {
$artCatNames = explode( ' | ', $row['cats'] );
foreach ( $artCatNames as $artCatName ) {
$article->mCategoryLinks[] = '[[:Category:' . $artCatName . '|' . str_replace( '_', ' ', $artCatName ) . ']]';
$article->mCategoryTexts[] = str_replace( '_', ' ', $artCatName );
2020-11-22 20:00:48 +00:00
}
}
// PARENT HEADING (category of the page, editor (user) of the page, etc. Depends on ordermethod param)
2021-02-22 23:48:01 +00:00
if ( $parameters->getParameter( 'headingmode' ) != 'none' ) {
switch ( $parameters->getParameter( 'ordermethod' )[0] ) {
2020-11-22 20:00:48 +00:00
case 'category':
//Count one more page in this heading
2021-02-22 23:48:01 +00:00
self::$headings[$row['cl_to']] = ( isset( self::$headings[$row['cl_to']] ) ? self::$headings[$row['cl_to']] + 1 : 1 );
if ( $row['cl_to'] == '' ) {
2020-11-22 20:00:48 +00:00
//uncategorized page (used if ordermethod=category,...)
2021-02-22 23:48:01 +00:00
$article->mParentHLink = '[[:Special:Uncategorizedpages|' . wfMessage( 'uncategorizedpages' ) . ']]';
2020-11-22 20:00:48 +00:00
} else {
2021-02-22 23:48:01 +00:00
$article->mParentHLink = '[[:Category:' . $row['cl_to'] . '|' . str_replace( '_', ' ', $row['cl_to'] ) . ']]';
2020-11-22 20:00:48 +00:00
}
break;
case 'user':
2021-02-22 23:48:01 +00:00
self::$headings[$revActorName] = ( isset( self::$headings[$revActorName] ) ? self::$headings[$revActorName] + 1 : 1 );
if ( $row['revactor_actor'] == 0 ) { //anonymous user
2020-11-22 20:00:48 +00:00
$article->mParentHLink = '[[User:' . $revActorName . '|' . $revActorName . ']]';
} else {
$article->mParentHLink = '[[User:' . $revActorName . '|' . $revActorName . ']]';
}
break;
}
}
}
return $article;
}
/**
* Returns all heading information processed from all newly instantiated article objects.
*
* @access public
* @return array Headings
*/
public static function getHeadings() {
return self::$headings;
}
/**
* Reset the headings to their initial state.
* Ideally this Article class should not exist and be handled by the built in MediaWiki class.
* Bug: https://jira/browse/HYD-913
*
* @access public
* @return void
*/
public static function resetHeadings() {
self::$headings = [];
}
/**
* Get the formatted date for this article if available.
*
* @access public
* @return mixed Formatted string or null for none set.
*/
public function getDate() {
global $wgLang;
2021-02-22 23:48:01 +00:00
if ( $this->myDate !== null ) {
2020-11-22 20:00:48 +00:00
return $this->myDate;
2021-02-22 23:48:01 +00:00
} elseif ( $this->mDate !== null ) {
return $wgLang->timeanddate( $article->mDate, true );
2020-11-22 20:00:48 +00:00
}
return null;
}
2020-11-23 04:33:40 +00:00
}