* Added Title::getLinksFrom() and Title::getTemplateLinksFrom() for consistency with...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Thu, 29 Dec 2011 15:12:00 +0000 (15:12 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Thu, 29 Dec 2011 15:12:00 +0000 (15:12 +0000)
* Deprecated WikiPage::getUsedTemplates() in favour of Title::getTemplateLinksFrom() and updated to it in core

includes/EditPage.php
includes/OutputPage.php
includes/Title.php
includes/WikiPage.php

index a443f6d..15eb228 100644 (file)
@@ -2507,7 +2507,7 @@ HTML
                        }
                        return $templates;
                } else {
-                       return $this->mArticle->getUsedTemplates();
+                       return $this->mTitle->getTemplateLinksFrom();
                }
        }
 
index ee11868..7b8d4cd 100644 (file)
@@ -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( "<div class='templatesUsed'>
 $templates
 </div>
index 7d94faa..b6539eb 100644 (file)
@@ -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
         *
index ee6713e..c03078d 100644 (file)
@@ -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.
         *