[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Representation of a page title within %MediaWiki. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 * http://www.gnu.org/copyleft/gpl.html 19 * 20 * @file 21 * @license GPL 2+ 22 * @author Daniel Kinzler 23 */ 24 25 /** 26 * Represents a page (or page fragment) title within %MediaWiki. 27 * 28 * @note In contrast to Title, this is designed to be a plain value object. That is, 29 * it is immutable, does not use global state, and causes no side effects. 30 * 31 * @note TitleValue represents the title of a local page (or fragment of a page). 32 * It does not represent a link, and does not support interwiki prefixes etc. 33 * 34 * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue 35 */ 36 class TitleValue { 37 /** 38 * @var int 39 */ 40 protected $namespace; 41 42 /** 43 * @var string 44 */ 45 protected $dbkey; 46 47 /** 48 * @var string 49 */ 50 protected $fragment; 51 52 /** 53 * Constructs a TitleValue. 54 * 55 * @note TitleValue expects a valid DB key; typically, a TitleValue is constructed either 56 * from a database entry, or by a TitleParser. We could apply "some" normalization here, 57 * such as substituting spaces by underscores, but that would encourage the use of 58 * un-normalized text when constructing TitleValues. For constructing a TitleValue from 59 * user input or external sources, use a TitleParser. 60 * 61 * @param int $namespace The namespace ID. This is not validated. 62 * @param string $dbkey The page title in valid DBkey form. No normalization is applied. 63 * @param string $fragment The fragment title. Use '' to represent the whole page. 64 * No validation or normalization is applied. 65 * 66 * @throws InvalidArgumentException 67 */ 68 public function __construct( $namespace, $dbkey, $fragment = '' ) { 69 if ( !is_int( $namespace ) ) { 70 throw new InvalidArgumentException( '$namespace must be an integer' ); 71 } 72 73 if ( !is_string( $dbkey ) ) { 74 throw new InvalidArgumentException( '$dbkey must be a string' ); 75 } 76 77 // Sanity check, no full validation or normalization applied here! 78 if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) { 79 throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey ); 80 } 81 82 if ( !is_string( $fragment ) ) { 83 throw new InvalidArgumentException( '$fragment must be a string' ); 84 } 85 86 if ( $dbkey === '' ) { 87 throw new InvalidArgumentException( '$dbkey must not be empty' ); 88 } 89 90 $this->namespace = $namespace; 91 $this->dbkey = $dbkey; 92 $this->fragment = $fragment; 93 } 94 95 /** 96 * @return int 97 */ 98 public function getNamespace() { 99 return $this->namespace; 100 } 101 102 /** 103 * @return string 104 */ 105 public function getFragment() { 106 return $this->fragment; 107 } 108 109 /** 110 * Returns the title's DB key, as supplied to the constructor, 111 * without namespace prefix or fragment. 112 * 113 * @return string 114 */ 115 public function getDBkey() { 116 return $this->dbkey; 117 } 118 119 /** 120 * Returns the title in text form, 121 * without namespace prefix or fragment. 122 * 123 * This is computed from the DB key by replacing any underscores with spaces. 124 * 125 * @note To get a title string that includes the namespace and/or fragment, 126 * use a TitleFormatter. 127 * 128 * @return string 129 */ 130 public function getText() { 131 return str_replace( '_', ' ', $this->getDBkey() ); 132 } 133 134 /** 135 * Creates a new TitleValue for a different fragment of the same page. 136 * 137 * @param string $fragment The fragment name, or "" for the entire page. 138 * 139 * @return TitleValue 140 */ 141 public function createFragmentTitle( $fragment ) { 142 return new TitleValue( $this->namespace, $this->dbkey, $fragment ); 143 } 144 145 /** 146 * Returns a string representation of the title, for logging. This is purely informative 147 * and must not be used programmatically. Use the appropriate TitleFormatter to generate 148 * the correct string representation for a given use. 149 * 150 * @return string 151 */ 152 public function __toString() { 153 $name = $this->namespace . ':' . $this->dbkey; 154 155 if ( $this->fragment !== '' ) { 156 $name .= '#' . $this->fragment; 157 } 158 159 return $name; 160 } 161 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |