From: Alexandre Emsenhuber Date: Thu, 29 Dec 2011 15:12:00 +0000 (+0000) Subject: * Added Title::getLinksFrom() and Title::getTemplateLinksFrom() for consistency with... X-Git-Tag: 1.31.0-rc.0~25687 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/supprimer.php?a=commitdiff_plain;h=1b655a802a6d2489853ec54c20f85d355cda2a75;p=lhc%2Fweb%2Fwiklou.git * Added Title::getLinksFrom() and Title::getTemplateLinksFrom() for consistency with Title::getLinksTo() and Title::getTemplateLinksTo() * Deprecated WikiPage::getUsedTemplates() in favour of Title::getTemplateLinksFrom() and updated to it in core --- diff --git a/includes/EditPage.php b/includes/EditPage.php index a443f6dd17..15eb228546 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -2507,7 +2507,7 @@ HTML } return $templates; } else { - return $this->mArticle->getUsedTemplates(); + return $this->mTitle->getTemplateLinksFrom(); } } diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ee118682c9..7b8d4cd9d8 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2256,8 +2256,7 @@ class OutputPage extends ContextSource { $this->addHTML( Html::element( 'textarea', $params, $source ) ); // Show templates used by this article - $page = WikiPage::factory( $this->getTitle() ); - $templates = Linker::formatTemplates( $page->getUsedTemplates() ); + $templates = Linker::formatTemplates( $this->getTitle()->getTemplateLinksFrom() ); $this->addHTML( "
$templates
diff --git a/includes/Title.php b/includes/Title.php index 7d94faa450..b6539eb909 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -3126,8 +3126,6 @@ class Title { * @return Array of Title objects linking here */ public function getLinksTo( $options = array(), $table = 'pagelinks', $prefix = 'pl' ) { - $linkCache = LinkCache::singleton(); - if ( count( $options ) > 0 ) { $db = wfGetDB( DB_MASTER ); } else { @@ -3146,7 +3144,8 @@ class Title { ); $retVal = array(); - if ( $db->numRows( $res ) ) { + if ( $res->numRows() ) { + $linkCache = LinkCache::singleton(); foreach ( $res as $row ) { $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ); if ( $titleObj ) { @@ -3172,6 +3171,76 @@ class Title { return $this->getLinksTo( $options, 'templatelinks', 'tl' ); } + /** + * Get an array of Title objects linked from this Title + * Also stores the IDs in the link cache. + * + * WARNING: do not use this function on arbitrary user-supplied titles! + * On heavily-used templates it will max out the memory. + * + * @param $options Array: may be FOR UPDATE + * @param $table String: table name + * @param $prefix String: fields prefix + * @return Array of Title objects linking here + */ + public function getLinksFrom( $options = array(), $table = 'pagelinks', $prefix = 'pl' ) { + $id = $this->getArticleId(); + + # If the page doesn't exist; there can't be any link from this page + if ( !$id ) { + return array(); + } + + if ( count( $options ) > 0 ) { + $db = wfGetDB( DB_MASTER ); + } else { + $db = wfGetDB( DB_SLAVE ); + } + + $namespaceFiled = "{$prefix}_namespace"; + $titleField = "{$prefix}_title"; + + $res = $db->select( + array( $table, 'page' ), + array( $namespaceFiled, $titleField, 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ), + array( "{$prefix}_from" => $id ), + __METHOD__, + $options, + array( 'page' => array( 'LEFT JOIN', array( "page_namespace=$namespaceFiled", "page_title=$titleField" ) ) ) + ); + + $retVal = array(); + if ( $res->numRows() ) { + $linkCache = LinkCache::singleton(); + foreach ( $res as $row ) { + $titleObj = Title::makeTitle( $row->$namespaceFiled, $row->$titleField ); + if ( $titleObj ) { + if ( $row->page_id ) { + $linkCache->addGoodLinkObjFromRow( $titleObj, $row ); + } else { + $linkCache->addBadLinkObj( $titleObj ); + } + $retVal[] = $titleObj; + } + } + } + return $retVal; + } + + /** + * Get an array of Title objects used on this Title as a template + * Also stores the IDs in the link cache. + * + * WARNING: do not use this function on arbitrary user-supplied titles! + * On heavily-used templates it will max out the memory. + * + * @param $options Array: may be FOR UPDATE + * @return Array of Title the Title objects used here + */ + public function getTemplateLinksFrom( $options = array() ) { + return $this->getLinksFrom( $options, 'templatelinks', 'tl' ); + } + /** * Get an array of Title objects referring to non-existent articles linked from this page * diff --git a/includes/WikiPage.php b/includes/WikiPage.php index ee6713e2cb..c03078df3c 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -2305,35 +2305,6 @@ class WikiPage extends Page { /**#@-*/ - /** - * Return a list of templates used by this article. - * Uses the templatelinks table - * - * @return Array of Title objects - */ - public function getUsedTemplates() { - $result = array(); - $id = $this->mTitle->getArticleID(); - - if ( $id == 0 ) { - return array(); - } - - $dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( array( 'templatelinks' ), - array( 'tl_namespace', 'tl_title' ), - array( 'tl_from' => $id ), - __METHOD__ ); - - if ( $res !== false ) { - foreach ( $res as $row ) { - $result[] = Title::makeTitle( $row->tl_namespace, $row->tl_title ); - } - } - - return $result; - } - /** * Returns a list of hidden categories this page is a member of. * Uses the page_props and categorylinks tables. @@ -2627,6 +2598,17 @@ class WikiPage extends Page { } } + /** + * Return a list of templates used by this article. + * Uses the templatelinks table + * + * @deprecated in 1.19; use Title::getTemplateLinksFrom() + * @return Array of Title objects + */ + public function getUsedTemplates() { + return $this->mTitle->getTemplateLinksFrom(); + } + /** * Perform article updates on a special page creation. *